home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / libnov.c < prev    next >
C/C++ Source or Header  |  1996-08-25  |  13KB  |  578 lines

  1. /*******************WARNING*********************
  2.  
  3. This is a *MODIFIED* version of Geoff Coller's proof-of-concept NOV
  4. implementation.
  5.  
  6. It has been modified to support threading directly from a file handle
  7. to a NNTP server without a temporary file.
  8.  
  9. This is not a complete distribution.  We have only distributed enough
  10. to support NN's needs.
  11.  
  12. The original version came from world.std.com:/src/news/nov.dist.tar.Z
  13. and was dated 11 Aug 1993.
  14.  
  15. In any case, bugs you find here are probably my fault, as I've trimmed
  16. a fair bit of unused code.
  17.  
  18. -Peter Wemm  <peter@DIALix.oz.au>
  19. */
  20.  
  21. /*
  22.  * Copyright (c) Geoffrey Collyer 1992, 1993.
  23.  * All rights reserved.
  24.  * Written by Geoffrey Collyer.
  25.  * Thanks to UUNET Communications Services Inc for financial support.
  26.  *
  27.  * This software is not subject to any license of the American Telephone
  28.  * and Telegraph Company, the Regents of the University of California, or
  29.  * the Free Software Foundation.
  30.  *
  31.  * Permission is granted to anyone to use this software for any purpose on
  32.  * any computer system, and to alter it and redistribute it freely, subject
  33.  * to the following restrictions:
  34.  *
  35.  * 1. The authors are not responsible for the consequences of use of this
  36.  *    software, no matter how awful, even if they arise from flaws in it.
  37.  *
  38.  * 2. The origin of this software must not be misrepresented, either by
  39.  *    explicit claim or by omission.  Since few users ever read sources,
  40.  *    credits must appear in the documentation.
  41.  *
  42.  * 3. Altered versions must be plainly marked as such, and must not be
  43.  *    misrepresented as being the original software.  Since few users
  44.  *    ever read sources, credits must appear in the documentation.
  45.  *
  46.  * 4. This notice may not be removed or altered.
  47.  */
  48.  
  49.  
  50. /*
  51.  * library to access news history adjunct data
  52.  */
  53.  
  54. #include "config.h"
  55. #include "hash.h"
  56. #include "hdbm.h"
  57. #include "newsoverview.h"
  58.  
  59. #define NEWSARTS "/usr/spool/news"    /* default news spool */
  60.  
  61. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  62.  
  63. /* imports */
  64. extern char *progname;
  65.  
  66. static char *newsarts = NEWSARTS;    /* news spool */
  67. static int prsoverview();
  68.  
  69. #ifdef DO_NOV_DIGEST
  70. static void de_digest();
  71. #endif
  72.  
  73. novartdir(dir)
  74. char *dir;
  75. {
  76.     newsarts = (dir == NULL? NEWSARTS: dir);
  77. }
  78.  
  79. static struct novgroup *            /* malloced */
  80. novnew()
  81. {
  82.     register struct novgroup *gp = (struct novgroup *)malloc(sizeof *gp);
  83.  
  84.     if (gp != NULL) {
  85.         gp->g_first = gp->g_curr = NULL;
  86.         gp->g_msgids = gp->g_roots = NULL;
  87.         gp->g_dir = NULL;
  88.         gp->g_stream = NULL;
  89.     }
  90.     return gp;
  91. }
  92.  
  93. struct novgroup *                /* malloced cookie */
  94. novopen(grp)                    /* change to group grp */
  95. char *grp;
  96. {
  97.     register struct novgroup *gp = novnew();
  98.     register char *sgrp;
  99.     register char *s;
  100.  
  101.     if (gp == NULL)
  102.         return NULL;
  103.     sgrp = strsave(grp);
  104.     if (sgrp == NULL) {
  105.         free((char *)gp);
  106.         return NULL;
  107.     }
  108.     for (s = sgrp; *s != '\0'; s++)
  109.         if (*s == '.')
  110.             *s = '/';
  111.     gp->g_dir = str3save(newsarts, "/", sgrp);
  112.     free(sgrp);
  113.     return gp;
  114. }
  115.  
  116. struct novgroup *
  117. novstream(fp)
  118. register FILE *fp;
  119. {
  120.     register struct novgroup *gp = novnew();
  121.  
  122.     if (gp != NULL)
  123.         gp->g_stream = fp;
  124.     return gp;
  125. }
  126.  
  127. /*
  128.  * novseek()
  129.  *    For local overview file, use binary search to find first line
  130.  *    which is at artnum or before.
  131.  *    Ripped off from inn1.4/nnrpd/newnews.c
  132.  */
  133. novseek(fp, artnum)
  134. register FILE *fp;
  135. register int artnum;
  136. {
  137.     char    *line;
  138.     long    upper;
  139.     long    lower;
  140.     long    middle;
  141.  
  142.     /* Read first line -- is it in our range? */
  143.     (void)fseek(fp, 0L, 0);
  144.     if ((line = fgetstr(fp)) == NULL)
  145.     return 0;
  146.     if (atol(line) >= artnum) {
  147.     (void)fseek(fp, 0L, 0);
  148.     return 1;
  149.     }
  150.  
  151.     /* Set search ranges and go. */
  152.     lower = 0;
  153.     (void)fseek(fp, 0L, 2);
  154.     upper = ftell(fp);
  155.     for ( ; ; ) {
  156.     /* Seek to middle line. */
  157.     middle = (upper + lower) / 2;
  158.     (void)fseek(fp, middle, 0);
  159.     while (++middle <= upper && getc(fp) != '\n')
  160.         continue;
  161.  
  162.     if (middle >= upper)
  163.         break;
  164.  
  165.     if ((line = fgetstr(fp)) != NULL && atol(line) > artnum)
  166.         upper = middle;
  167.     else if (lower == middle)
  168.         break;
  169.     else
  170.         lower = middle;
  171.     }
  172.  
  173.     /* Move to lower bound; we know this will always be the start of a line. */
  174.     (void)fseek(fp, lower, 0);
  175.     while ((line = fgetstr(fp)) != NULL)
  176.     if (atol(line) >= artnum) {
  177.         (void)fseek(fp, lower, 0);
  178.         return 1;
  179.     }
  180.  
  181.     return 0;
  182. }
  183.  
  184.  
  185. struct novart *
  186. novall(gp, first, last)
  187. register struct novgroup *gp;
  188. register int first, last;
  189. {
  190.     if (gp->g_first == NULL)    /* new group? */
  191.         (void) prsoverview(gp, first, last);
  192.     return gp->g_first;
  193. }
  194.  
  195. struct novart *
  196. novnext(gp)
  197. register struct novgroup *gp;            /* cookie from novopen */
  198. {
  199.     register struct novart *thisart;
  200.  
  201.     if (gp->g_first == NULL)    /* new group? */
  202.         (void) prsoverview(gp);
  203.     thisart = gp->g_curr;
  204.     if (thisart != NULL)
  205.         gp->g_curr = thisart->a_nxtnum;
  206.     return thisart;
  207. }
  208.  
  209. static
  210. freeart(art)
  211. register struct novart *art;
  212. {
  213.     if (art->a_refs != NULL)
  214.         free(art->a_refs);
  215.     if (art->a_parent != NULL)
  216.         free(art->a_parent);
  217.     if (art->a_num != NULL)
  218.         free(art->a_num);    /* the original input line, chopped */
  219.     free((char *)art);
  220. }
  221.  
  222. #define MAXFIELDS 9        /* last field is "other" fields */
  223. #define DEFREFS 20
  224.  
  225. #define PRSFAIL 0        /* disaster (out of memory, etc.) */
  226. #define PRSOKAY 1
  227. #define PRSBAD  2        /* bad syntax */
  228.  
  229. static int
  230. prsovline(line, gp, art, prevart)
  231. register char *line;        /* malloced; will be chopped up */
  232. register struct novgroup *gp;
  233. register struct novart *art, *prevart;
  234. {
  235.     register int nf, nrefs, len;
  236.     char *fields[MAXFIELDS], *refs[DEFREFS];
  237.     char **refsp = refs;
  238.     static struct novart zart;
  239.  
  240.     *art = zart;        /* make freeart safe if we bail out early */
  241.     len = strlen(line);
  242.     if (len > 0 && line[len-1] == '\n')
  243.         line[len-1] = '\0';    /* make field count straightforward */
  244.     nf = split(line, fields, MAXFIELDS, "\t");
  245.     if (nf < MAXFIELDS - 1)    /* only "others" fields are optional */
  246.         return PRSBAD;    /* skip this line */
  247.     while (nf < MAXFIELDS)
  248.         fields[nf++] = "";    /* fake missing fields */
  249.  
  250.     /*
  251.      * duplicate message-ids would confuse the threading code and anyway
  252.      * should not happen (now that relaynews suppresses multiple links
  253.      * within a group for the same article), so ignore any entries for
  254.      * duplicate message-ids.
  255.      */
  256.     if (hashfetch(gp->g_msgids, fields[4]) != NULL)
  257.         return PRSBAD;
  258.  
  259.     art->a_parent = NULL;
  260.     art->a_refs = strsave(fields[5]); /* fields[5] will be split below */
  261.     if (art->a_refs == NULL)
  262.         return PRSFAIL;
  263.     if (art->a_refs[0] != '\0') {    /* at least one ref? */
  264.         nrefs = awksplit(fields[5], &refsp, DEFREFS, "");
  265.         if (refsp == NULL)
  266.             return PRSFAIL;
  267.         if (nrefs > 0) {    /* last ref is parent */
  268.             if (refsp[nrefs - 1] == NULL)
  269.                 return PRSFAIL;
  270.             art->a_parent = strsave(refsp[nrefs - 1]);
  271.             if (art->a_parent == NULL)
  272.                 return PRSFAIL;
  273.             if (refsp != refs)
  274.                 free((char *)refsp);
  275.         }
  276.     }
  277.     art->a_num = fields[0];        /* line */
  278.     art->a_subj = fields[1];
  279.     art->a_from = fields[2];
  280.     art->a_date = fields[3];
  281.     art->a_msgid = fields[4];
  282.     /* see above for fields[5] */
  283.     art->a_bytes = fields[6];
  284.     art->a_lines = fields[7];
  285.     art->a_others = fields[8];
  286.     art->a_nxtnum = NULL;
  287.  
  288.     if (!hashstore(gp->g_msgids, art->a_msgid, (char *)art))
  289.         return PRSFAIL;
  290.     if (gp->g_first == NULL)
  291.         gp->g_first = art;
  292.     if (prevart != NULL)
  293.         prevart->a_nxtnum = art;
  294.     return PRSOKAY;
  295. }
  296.  
  297. static int
  298. prsoverview(gp, first, last)
  299. register struct novgroup *gp;            /* cookie from novopen */
  300. register int first, last;
  301. {
  302.     register struct novart *art, *prevart = NULL;
  303.     register int prssts;
  304.     int hsize;
  305.     char *line;
  306.  
  307.     gp->g_curr = gp->g_first = NULL;
  308.     if (gp->g_dir == NULL && gp->g_stream == NULL)
  309.         return 0;
  310.     if (gp->g_stream == NULL) {
  311.         line = str3save(gp->g_dir, "/", ".overview");
  312.         if (line == NULL)
  313.             return 0;
  314.         gp->g_stream = fopen(line, "r");
  315.         free(line);
  316.         if (gp->g_stream == NULL)
  317.             return 0;
  318.     }
  319.  
  320.     /* parse input and store in gp->g_msgids for later traversal */
  321.     hsize = (last - first) | 0x7f;
  322.     gp->g_msgids = hashcreate(hsize, (unsigned (*)())NULL);
  323.     if (gp->g_msgids == NULL) {
  324.         if (gp->g_dir != NULL)        /* we opened the stream? */
  325.             (void) fclose(gp->g_stream);
  326.         return 0;
  327.     }
  328.  
  329.     if (!use_nntp) {
  330.         if (!novseek(gp->g_stream, first))
  331.             goto done;
  332.     }
  333.     while ((line = fgetstr(gp->g_stream)) != NULL) {
  334.         if (strcmp(line, ".") == 0) /* EOF on a NNTP stream */
  335.             break;
  336.         art = (struct novart *)malloc(sizeof *art);
  337.         if (art == NULL || (prssts = prsovline(strsave(line), gp, art, prevart)) == PRSFAIL) {
  338.             if (gp->g_dir != NULL)    /* we opened the stream? */
  339.                 (void) fclose(gp->g_stream);
  340.             if (art != NULL)
  341.                 freeart(art);
  342.             return 0;
  343.         }
  344.         if (prssts == PRSOKAY)
  345.             prevart = art;
  346.         else
  347.             freeart(art);
  348.     }
  349. done:
  350.     if (gp->g_dir != NULL)        /* we opened the stream? */
  351.         (void) fclose(gp->g_stream);
  352.     gp->g_curr = gp->g_first;
  353.  
  354. #ifdef DO_NOV_DIGEST
  355.     /*
  356.      * This is really horrible.  NOV doesn't break down digests
  357.      * (I don't think it should), but NN wants all the
  358.      * information up front. We have to find any digest
  359.      * and break it apart.
  360.      */
  361.     for(art = gp->g_first; art; art=art->a_nxtnum) {
  362.         if (is_digest(art->a_subj))
  363.             de_digest(gp, art);
  364.     }
  365. #endif
  366.     return 1;
  367. }
  368.  
  369. #ifdef DO_NOV_DIGEST
  370. #include "news.h"
  371.  
  372. static char *build_nov_line();
  373. static char *detab_cp();
  374.  
  375. static void
  376. de_digest(gp, ap)
  377. struct novgroup *gp;            /* cookie from novopen */
  378. struct novart *ap;
  379. {
  380.     register struct novart *art, *prevart;
  381.     news_header_buffer dgbuf;
  382.     int cont, seq, prsrc;
  383.     FILE *fp;
  384.     char *line;
  385.  
  386. #ifdef NNTP
  387.     if (use_nntp)
  388.         fp = nntp_get_article(atol(ap->a_num), 0);
  389.     else
  390. #endif    /* NNTP */
  391.         fp = open_file(ap->a_num, OPEN_READ);
  392.  
  393.     if (fp == NULL)
  394.         return;
  395.  
  396.     cont = 1;
  397.     prevart = ap;
  398.     seq = 0;
  399.  
  400.     skip_digest_body(fp);
  401.     while (cont && (cont = get_digest_article(fp, dgbuf)) >= 0) {
  402.         if (seq == 0) {
  403. #ifndef NO_MEMMOVE
  404.             memmove(ap->a_num + 1, ap->a_num,
  405.                 ap->a_bytes - ap->a_num);
  406. #else
  407.             bcopy(ap->a_num, ap->a_num + 1,
  408.                 ap->a_bytes - ap->a_num);
  409. #endif    /* NO_MEMMOVE */
  410.             ap->a_num[0] = '-';
  411.             ap->a_subj++;
  412.             ap->a_from++;
  413.             ap->a_date++;
  414.             ap->a_msgid++;
  415.         }
  416.         else {
  417.             if ((art=(struct novart *)malloc(sizeof *art)) == NULL)
  418.                 break;
  419.             if ((line = build_nov_line(ap, &digest, seq)) == NULL)
  420.             {
  421.                 free(art);
  422.                 break;
  423.             }
  424.             if (prsovline(line, gp, art, NULL) != PRSOKAY)
  425.             {
  426.                 if (art->a_num != line)
  427.                     free(line);
  428.                 freeart(art);
  429.                 continue;
  430.             }
  431.             art->a_nxtnum = prevart->a_nxtnum;
  432.             prevart->a_nxtnum = art;
  433.             prevart = art;
  434.         }
  435.         seq++;
  436.     }
  437.     fclose(fp);
  438. }
  439.  
  440. static char *
  441. build_nov_line(ap, dp, seq)
  442. struct novart *ap;
  443. struct digest_header *dp;
  444. int seq;
  445. {
  446.     char *cp, *bp;
  447.     int len, i;
  448.     char *flds[10];
  449.  
  450.     flds[0] = dp->dg_subj;
  451.     flds[1] = dp->dg_from;
  452.  
  453.     if (dp->dg_date)
  454.         flds[2] = dp->dg_date;
  455.     else
  456.         flds[2] = ap->a_date;
  457.  
  458.     flds[3] = ap->a_msgid;
  459.     flds[4] = ap->a_refs;
  460.     flds[5] = ap->a_bytes;
  461.     flds[6] = ap->a_others;
  462.  
  463.     len = 64;
  464.     for (i = 0; i <= 6; i++) {
  465.         if (flds[i])
  466.             len += strlen(flds[i]);
  467.         else
  468.             flds[i] = "";
  469.     }
  470.     if ((bp = malloc(len)) == NULL)
  471.         return(bp);
  472.  
  473.     cp = bp;
  474.     *cp++ = '0';
  475.     *cp++ = '\t';
  476.     cp = detab_cp(cp, flds[0]);
  477.     *cp++ = '\t';
  478.     cp = detab_cp(cp, flds[1]);
  479.     *cp++ = '\t';
  480.     cp = detab_cp(cp, flds[2]);
  481.     *cp++ = '\t';
  482.  
  483.     cp = detab_cp(cp, flds[3]);    /* need unique msgid */
  484.     sprintf(cp, ".%d\t", seq);
  485.     cp += strlen(cp);
  486.  
  487.     cp = detab_cp(cp, flds[4]);
  488.     *cp++ = '\t';
  489.  
  490.     cp = detab_cp(cp, flds[5]);    /* add position data to byte count */
  491.     sprintf(cp, ":%d:%d:%d\t", dp->dg_hpos, dp->dg_fpos - dp->dg_hpos,
  492.         dp->dg_lpos);
  493.     cp += strlen(cp);
  494.  
  495.     sprintf(cp, "%d\t", --dp->dg_lines);
  496.     cp += strlen(cp);
  497.  
  498.     detab_cp(cp, flds[6]);
  499.  
  500.     return(bp);
  501. }
  502.  
  503. static char *
  504. detab_cp(dst, src)
  505. register char *dst, *src;
  506. {
  507.     while(*dst = *src++) {
  508.         if (*dst == '\t')
  509.             *dst = ' ';
  510.         dst++;
  511.     }
  512.     return(dst);
  513. }
  514. #endif /* DO_NOV_DIGEST */
  515.  
  516. /*
  517.  * if this article has no parent, enter it in the roots hash table.
  518.  * if it has a parent, make this article the parent's first child,
  519.  * even it means making the existing first child our first sibling.
  520.  */
  521. /* ARGSUSED */
  522. static
  523. numvisit(key, data, hook)
  524. char *key, *data, *hook;
  525. {
  526.     register struct novart *art = (struct novart *)data, *parent = NULL;
  527.     register char *msgid;
  528.     register struct novgroup *gp = (struct novgroup *)hook;
  529.  
  530.     if (gp->g_roots == NULL) {
  531.         gp->g_roots = hashcreate(500, (unsigned (*)())NULL);
  532.         if (gp->g_roots == NULL)    /* better not happen */
  533.             return;
  534.     }
  535.  
  536.     msgid = art->a_msgid;
  537.     if (art->a_parent != NULL)
  538.         parent = (struct novart *)hashfetch(gp->g_msgids, art->a_parent);
  539.     if (parent != NULL) {
  540.         if (parent->a_child1 != NULL) {
  541.             if (art->a_sibling != NULL)
  542.                 return;    /* sibling in use; better not happen */
  543.             art->a_sibling = parent->a_child1;
  544.         }
  545.         parent->a_child1 = msgid;
  546.     } else {                /* no parent - must be a root */
  547.         art->a_parent = NULL;
  548.         if (!hashstore(gp->g_roots, msgid, (char *)art))
  549.             return;        /* better not happen */
  550.     }
  551. }
  552.  
  553.  
  554. novthread(gp)
  555. register struct novgroup *gp;
  556. {
  557.     if (gp->g_first == NULL)    /* new group? */
  558.         (void) prsoverview(gp);
  559.     /* build trees */
  560.     if (gp->g_first != NULL)
  561.         hashwalk(gp->g_msgids, numvisit, (char *)gp);
  562. }
  563.  
  564. novclose(gp)
  565. register struct novgroup *gp;
  566. {
  567.     register struct novart *art, *next;
  568.  
  569.     hashdestroy(gp->g_msgids);
  570.     hashdestroy(gp->g_roots);
  571.     if (gp->g_dir != NULL)
  572.         free(gp->g_dir);
  573.     for (art = gp->g_first; art != NULL; art = next) {
  574.         next = art->a_nxtnum;
  575.         freeart(art);
  576.     }
  577. }
  578.