home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / c-client / news.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  40.5 KB  |  1,425 lines

  1. /*
  2.  * Program:    Netnews mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    4 September 1991
  13.  * Last Edited:    30 May 1992
  14.  *
  15.  * Copyright 1992 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <pwd.h>
  39. #include <netdb.h>
  40. #include <errno.h>
  41. extern int errno;        /* just in case */
  42. #include <sys/types.h>
  43. #include <sys/dir.h>
  44. #include <sys/file.h>
  45. #include <sys/stat.h>
  46. #include <sys/time.h>
  47. #include <sys/uio.h>
  48. #include "osdep.h"
  49. #include "mail.h"
  50. #include "news.h"
  51. #include "rfc822.h"
  52. #include "misc.h"
  53.  
  54. /* Netnews mail routines */
  55.  
  56.  
  57. /* Driver dispatch used by MAIL */
  58.  
  59. DRIVER newsdriver = {
  60.   (DRIVER *) NIL,        /* next driver */
  61.   news_valid,            /* mailbox is valid for us */
  62.   news_find,            /* find mailboxes */
  63.   news_find_bboards,        /* find bboards */
  64.   news_open,            /* open mailbox */
  65.   news_close,            /* close mailbox */
  66.   news_fetchfast,        /* fetch message "fast" attributes */
  67.   news_fetchflags,        /* fetch message flags */
  68.   news_fetchenvelope,        /* fetch message envelopes */
  69.   news_fetchheader,        /* fetch message header only */
  70.   news_fetchtext,        /* fetch message body only */
  71.   news_fetchbody,        /* fetch message body section */
  72.   news_setflag,            /* set message flag */
  73.   news_clearflag,        /* clear message flag */
  74.   news_search,            /* search for message based on criteria */
  75.   news_ping,            /* ping mailbox to see if still alive */
  76.   news_check,            /* check for new messages */
  77.   news_expunge,            /* expunge deleted messages */
  78.   news_copy,            /* copy messages to another mailbox */
  79.   news_move,            /* move messages to another mailbox */
  80.   news_gc            /* garbage collect stream */
  81. };
  82.  
  83. /* Netnews mail validate mailbox
  84.  * Accepts: mailbox name
  85.  * Returns: our driver if name is valid, otherwise calls valid in next driver
  86.  */
  87.  
  88. DRIVER *news_valid (name)
  89.     char *name;
  90. {
  91.   int fd;
  92.   char *s,*t,*u;
  93.   char tmp[MAILTMPLEN];
  94.   struct stat sbuf;
  95.   DRIVER *ret = newsdriver.next ? (*newsdriver.next->valid) (name) : NIL;
  96.                 /* looks plausible and news installed? */
  97.   if (name && (*name == '*') && !(strchr (name,'/')) &&
  98.       ((fd = open (ACTIVEFILE,O_RDONLY,NIL)) >= 0)) {
  99.     fstat (fd,&sbuf);        /* get size of active file */
  100.                 /* slurp in active file */
  101.     read (fd,s = (char *) fs_get (sbuf.st_size+1),sbuf.st_size);
  102.     s[sbuf.st_size] = '\0';    /* tie off file */
  103.     close (fd);            /* flush file */
  104.     lcase (strcpy (tmp,name+1));/* make sure compare with lower case */
  105.     if (t = strtok (s,"\n"))    /* get first name */
  106.       do if (u = strchr (t,' ')) {
  107.     *u = '\0';        /* tie off at end of name */
  108.     if (!strcmp (tmp,t)) {    /* name matches? */
  109.       ret = &newsdriver;    /* seems to be a valid name */
  110.       break;
  111.     }
  112.       } while (t = strtok (NIL,"\n"));
  113.     fs_give ((void **) &s);    /* flush data */
  114.  }
  115.   return ret;            /* return status */
  116. }
  117.  
  118. /* Netnews mail find list of mailboxes
  119.  * Accepts: mail stream
  120.  *        pattern to search
  121.  */
  122.  
  123. void news_find (stream,pat)
  124.     MAILSTREAM *stream;
  125.     char *pat;
  126. {
  127.   /* Always a no-op */
  128. }
  129.  
  130.  
  131. /* Netnews mail find list of bboards
  132.  * Accepts: mail stream
  133.  *        pattern to search
  134.  */
  135.  
  136. void news_find_bboards (stream,pat)
  137.     MAILSTREAM *stream;
  138.     char *pat;
  139. {
  140.   int fd;
  141.   char tmp[MAILTMPLEN];
  142.   char patx[MAILTMPLEN];
  143.   char *s,*t,*u;
  144.   struct stat sbuf;
  145.   lcase (strcpy (patx,pat));    /* make sure compare with lower case */
  146.                 /* make sure news exists on this system */
  147.   if (!(stat (NEWSSPOOL,&sbuf) || stat (ACTIVEFILE,&sbuf))) {
  148.                 /* do .newsrc only if not anonymous */
  149.     if (!(stream && stream->anonymous) &&
  150.     ((fd = open (NEWSRC,O_RDONLY,NIL)) >= 0)) {
  151.       fstat (fd,&sbuf);        /* get file size and read data */
  152.       read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  153.       close (fd);        /* close file */
  154.       s[sbuf.st_size] = '\0';    /* tie off string */
  155.       if (t = strtok (s,"\n")) do if (u = strchr (t,':')) {
  156.     *u = '\0';        /* tie off at end of name */
  157.     if (pmatch (lcase (t),patx)) mm_bboard (t);
  158.       } while (t = strtok (NIL,"\n"));
  159.       fs_give ((void **) &s);
  160.     }
  161.   }
  162.                 /* try local list */
  163.   if (!(stream && stream->anonymous) &&
  164.       (fd = open (strcat (strcpy (tmp,getpwuid (geteuid ())->pw_dir),
  165.               "/.mailboxlist"),O_RDONLY,NIL)) >= 0) {
  166.     fstat (fd,&sbuf);        /* get file size and read data */
  167.     read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  168.     close (fd);            /* close file */
  169.     s[sbuf.st_size] = '\0';    /* tie off string */
  170.     if (t = strtok (s,"\n"))    /* if have a first line */
  171.       do if ((*t++ == '*') && pmatch (lcase (t),patx)) mm_bboard (t);
  172.     while (t = strtok (NIL,"\n"));
  173.     fs_give ((void **) &s);
  174.   }
  175. }
  176.  
  177. /* Netnews mail open
  178.  * Accepts: stream to open
  179.  * Returns: stream on success, NIL on failure
  180.  */
  181.  
  182. MAILSTREAM *news_open (stream)
  183.     MAILSTREAM *stream;
  184. {
  185.   int fd;
  186.   long i,nmsgs,j,k;
  187.   long recent = 0;
  188.   char *s;
  189.   char tmp[MAILTMPLEN];
  190.   struct hostent *host_name;
  191.   struct direct **names;
  192.   struct stat sbuf;
  193.   if (LOCAL) {            /* close old file if stream being recycled */
  194.     news_close (stream);    /* dump and save the changes */
  195.     stream->dtb = &newsdriver;    /* reattach this driver */
  196.   }
  197.   lcase (stream->mailbox);    /* build news directory name */
  198.   sprintf (s = tmp,"%s/%s",NEWSSPOOL,stream->mailbox + 1);
  199.   while (s = strchr (s,'.')) *s = '/';
  200.                 /* scan directory */
  201.   if ((nmsgs = scandir (tmp,&names,news_select,news_numsort)) >= 0) {
  202.     stream->local = fs_get (sizeof (NEWSLOCAL));
  203.     LOCAL->dirty = NIL;        /* no update to .newsrc needed yet */
  204.     LOCAL->dir = cpystr (tmp);    /* copy directory name for later */
  205.     gethostname(tmp,MAILTMPLEN);/* get local host name */
  206.     LOCAL->host = cpystr ((host_name = gethostbyname (tmp)) ?
  207.               host_name->h_name : tmp);
  208.     LOCAL->name = cpystr (stream->mailbox + 1);
  209.                 /* create cache */
  210.     LOCAL->number = (unsigned long *) fs_get (nmsgs * sizeof (unsigned long));
  211.     LOCAL->header = (char **) fs_get (nmsgs * sizeof (char *));
  212.     LOCAL->body = (char **) fs_get (nmsgs * sizeof (char *));
  213.     LOCAL->date = (long *) fs_get (nmsgs * sizeof (long));
  214.     LOCAL->seen = (char *) fs_get (nmsgs * sizeof (char));
  215.     for (i = 0; i<nmsgs; ++i) {    /* initialize cache */
  216.       LOCAL->number[i] = atoi (names[i]->d_name);
  217.       fs_give ((void **) &names[i]);
  218.       LOCAL->header[i] = LOCAL->body[i] = NIL;
  219.       LOCAL->date[i] = 0;
  220.       LOCAL->seen[i] = NIL;
  221.     }
  222.     fs_give ((void **) &names);    /* free directory */
  223.                 /* make temporary buffer */
  224.     LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1);
  225.     stream->sequence++;        /* bump sequence number */
  226.     stream->readonly = T;    /* make sure higher level knows readonly */
  227.     mail_exists (stream,nmsgs);    /* notify upper level that messages exist */
  228.  
  229.     i = 0;            /* slurp .newsrc file */
  230.     if (!stream->anonymous) {    /* not if anonymous you don't */
  231.       if ((fd = open (NEWSRC,O_RDONLY,NIL)) >= 0) {
  232.     fstat (fd,&sbuf);    /* get size of data */
  233.                 /* ensure enough room */
  234.     if (sbuf.st_size >= (LOCAL->buflen + 1)) {
  235.                 /* fs_resize does an unnecessary copy */
  236.       fs_give ((void **) &LOCAL->buf);
  237.       LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size+1)+1);
  238.     }
  239.     *LOCAL->buf = '\n';    /* slurp the silly thing in */
  240.     read (fd,LOCAL->buf + 1,sbuf.st_size);
  241.                 /* tie off file */
  242.     LOCAL->buf[sbuf.st_size + 1] = '\0';
  243.     close (fd);        /* flush .newsrc file */
  244.                 /* find as subscribed newsgroup */
  245.     sprintf (tmp,"\n%s:",LOCAL->name);
  246.     if (s = strstr (LOCAL->buf,tmp)) s += strlen (tmp);
  247.     else {            /* find as unsubscribed newsgroup */
  248.       sprintf (tmp,"\n%s!",LOCAL->name);
  249.       if ((s = strstr (LOCAL->buf,tmp)) && (s += strlen (tmp)))
  250.         mm_log ("Not subscribed to that newsgroup",WARN);
  251.       else mm_log ("No state for newsgroup found, reading as new",WARN);
  252.     }
  253.                 /* process until run out of messages or list */
  254.     if (s) while (*s != '\n' && i < nmsgs) {
  255.       j = strtol (s,&s,10);    /* start of possible range */
  256.                 /* other end of range */
  257.       k = (*s == '-') ? strtol (++s,&s,10) : j;
  258.                 /* skip messages before this range */
  259.       while ((LOCAL->number[i] < j) && (i < nmsgs)) LOCAL->seen[i++] = NIL;
  260.       while ((LOCAL->number[i] >= j) && (LOCAL->number[i] <= k) &&
  261.          (i < nmsgs)) {    /* mark messages within the range as seen */
  262.         LOCAL->seen[i++] = T;
  263.         mail_elt (stream,i)->seen = T;
  264.       }
  265.       if (*s == ',') s++;    /* skip past comma */
  266.       else if (*s != '\n') {
  267.         mm_log ("Bogus syntax in news state file",ERROR);
  268.         break;        /* give up fast!! */
  269.       }
  270.     }
  271.       }
  272.       else mm_log ("No news state file exists -- one will be created",WARN);
  273.     }
  274.     while (i < nmsgs) {        /* mark all remaining messages as new */
  275.       LOCAL->seen[i++] = NIL;
  276.       mail_elt (stream,i)->recent = T;
  277.       ++recent;            /* count another recent message */
  278.     }
  279.     mail_recent (stream,recent);/* notify upper level about recent */
  280.                 /* notify if empty bboard */
  281.     if (!(stream->nmsgs || stream->silent)) mm_log ("Newsgroup is empty",WARN);
  282.   }
  283.   return LOCAL ? stream : NIL;    /* if stream is alive, return to caller */
  284. }
  285.  
  286. /* Netnews file name selection test
  287.  * Accepts: candidate directory entry
  288.  * Returns: T to use file name, NIL to skip it
  289.  */
  290.  
  291. int news_select (name)
  292.     struct direct *name;
  293. {
  294.   char c;
  295.   char *s = name->d_name;
  296.   while (c = *s++) if (!isdigit (c)) return NIL;
  297.   return T;
  298. }
  299.  
  300.  
  301. /* Netnews file name comparision
  302.  * Accepts: first candidate directory entry
  303.  *        second candidate directory entry
  304.  * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
  305.  */
  306.  
  307. int news_numsort (d1,d2)
  308.     struct direct **d1;
  309.     struct direct **d2;
  310. {
  311.   return (atoi ((*d1)->d_name) - atoi ((*d2)->d_name));
  312. }
  313.  
  314.  
  315. /* Netnews mail close
  316.  * Accepts: MAIL stream
  317.  */
  318.  
  319. void news_close (stream)
  320.     MAILSTREAM *stream;
  321. {
  322.   if (LOCAL) {            /* only if a file is open */
  323.     news_check (stream);    /* dump final checkpoint */
  324.     if (LOCAL->host) fs_give ((void **) &LOCAL->host);
  325.     if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
  326.     if (LOCAL->name) fs_give ((void **) &LOCAL->name);
  327.     news_gc (stream,GC_TEXTS);    /* free local cache */
  328.     fs_give ((void **) &LOCAL->number);
  329.     fs_give ((void **) &LOCAL->header);
  330.     fs_give ((void **) &LOCAL->body);
  331.     fs_give ((void **) &LOCAL->date);
  332.     fs_give ((void **) &LOCAL->seen);
  333.                 /* free local scratch buffer */
  334.     if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
  335.                 /* nuke the local data */
  336.     fs_give ((void **) &stream->local);
  337.     stream->dtb = NIL;        /* log out the DTB */
  338.   }
  339. }
  340.  
  341. /* Netnews mail fetch fast information
  342.  * Accepts: MAIL stream
  343.  *        sequence
  344.  */
  345.  
  346. void news_fetchfast (stream,sequence)
  347.     MAILSTREAM *stream;
  348.     char *sequence;
  349. {
  350.   return;            /* no-op for local mail */
  351. }
  352.  
  353.  
  354. /* Netnews mail fetch flags
  355.  * Accepts: MAIL stream
  356.  *        sequence
  357.  */
  358.  
  359. void news_fetchflags (stream,sequence)
  360.     MAILSTREAM *stream;
  361.     char *sequence;
  362. {
  363.   return;            /* no-op for local mail */
  364. }
  365.  
  366.  
  367. /* Netnews mail fetch envelope
  368.  * Accepts: MAIL stream
  369.  *        message # to fetch
  370.  * Returns: envelope of this message
  371.  *
  372.  * Fetches the "fast" information as well
  373.  */
  374.  
  375. ENVELOPE *news_fetchenvelope (stream,msgno)
  376.     MAILSTREAM *stream;
  377.     long msgno;
  378. {
  379.   char *h,*t;
  380.   long i = msgno - 1;
  381.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  382.   if (!elt->env) {        /* make envelope now if don't have one */
  383.     h = news_fetchheader (stream,msgno);
  384.                 /* can't use fetchtext since it'll set seen */
  385.     t = LOCAL->body[i] ? LOCAL->body[i] : "";
  386.     rfc822_parse_msg (&elt->env,&elt->body,h,strlen (h),t,strlen (t),
  387.               LOCAL->host,LOCAL->buf);
  388.   }
  389.   return elt->env;        /* return the envelope */
  390. }
  391.  
  392. /* Netnews mail fetch message header
  393.  * Accepts: MAIL stream
  394.  *        message # to fetch
  395.  * Returns: message header in RFC822 format
  396.  */
  397.  
  398. char *news_fetchheader (stream,msgno)
  399.     MAILSTREAM *stream;
  400.     long msgno;
  401. {
  402.   unsigned long i,j;
  403.   int fd;
  404.   char *s,*b,*t;
  405.   long m = msgno - 1;
  406.   long lst = NIL;
  407.   struct stat sbuf;
  408.   struct tm *tm;
  409.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  410.                 /* build message file name */
  411.   sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[m]);
  412.   if (!LOCAL->header[m] && ((fd = open (LOCAL->buf,O_RDONLY,NIL)) >= 0)) {
  413.     fstat (fd,&sbuf);        /* get size of message */
  414.                 /* make plausible IMAPish date string */
  415.     tm = localtime (&sbuf.st_mtime);
  416.     sprintf (LOCAL->buf,"%2d-%s-%02d %2d:%02d:%02d-LCL",tm->tm_mday,
  417.          months[tm->tm_mon],tm->tm_year,tm->tm_hour,tm->tm_min,tm->tm_sec);
  418.     elt->internal_date = cpystr (LOCAL->buf);
  419.                 /* and local value for date comparisons */
  420.     LOCAL->date[m] = ((tm->tm_year-70)*12+tm->tm_mon)*31+tm->tm_mday-1;
  421.                 /* slurp message */
  422.     read (fd,s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size);
  423.     s[sbuf.st_size] = '\0';    /* tie off file */
  424.     close (fd);            /* flush message file */
  425.                 /* find end of header and count lines */
  426.     for (i=1,b=s; *b && !(lst && (*b == '\n'));) if (lst = (*b++ == '\n')) i++;
  427.                 /* copy header in CRLF form */
  428.     LOCAL->header[m] = (char *) fs_get (i += (j = b - s));
  429.     elt->rfc822_size = i - 1;    /* size of message header */
  430.     strcrlfcpy (&LOCAL->header[m],&i,s,j);
  431.                 /* copy body in CRLF form */
  432.     for (i = 1,t = b; *t;) if (*t++ == '\n') i++;
  433.     LOCAL->body[m] = (char *) fs_get (i += (j = t - b));
  434.     elt->rfc822_size += i - 1;    /* size of entire message */
  435.     strcrlfcpy (&LOCAL->body[m],&i,b,j);
  436.     fs_give ((void **) &s);    /* flush old data */
  437.   }
  438.   return LOCAL->header[m] ? LOCAL->header[m] : "";
  439. }
  440.  
  441. /* Netnews mail fetch message text (only)
  442.     body only;
  443.  * Accepts: MAIL stream
  444.  *        message # to fetch
  445.  * Returns: message text in RFC822 format
  446.  */
  447.  
  448. char *news_fetchtext (stream,msgno)
  449.     MAILSTREAM *stream;
  450.     long msgno;
  451. {
  452.   long i = msgno - 1;
  453.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  454.                 /* snarf message in case don't have it yet */
  455.   news_fetchheader (stream,msgno);
  456.   if (!elt->seen) {        /* if message not seen before */
  457.     elt->seen = T;        /* mark as seen */
  458.     LOCAL->dirty = T;        /* and that stream is now dirty */
  459.   }
  460.   LOCAL->seen[i] = T;
  461.   return LOCAL->body[i] ? LOCAL->body[i] : "";
  462. }
  463.  
  464. /* Netnews fetch message body as a structure
  465.  * Accepts: Mail stream
  466.  *        message # to fetch
  467.  *        section specifier
  468.  *        pointer to length
  469.  * Returns: pointer to section of message body
  470.  */
  471.  
  472. char *news_fetchbody (stream,m,s,len)
  473.     MAILSTREAM *stream;
  474.     long m;
  475.     char *s;
  476.     unsigned long *len;
  477. {
  478.   BODY *b;
  479.   PART *pt;
  480.   unsigned long i;
  481.   char *base = LOCAL->body[m - 1];
  482.   unsigned long offset = 0;
  483.   MESSAGECACHE *elt = mail_elt (stream,m);
  484.                 /* make sure have a body */
  485.   if (!(news_fetchenvelope (stream,m) && (b = mail_elt (stream,m)->body) &&
  486.     s && *s && ((i = strtol (s,&s,10)) > 0))) return NIL;
  487.   do {                /* until find desired body part */
  488.                 /* multipart content? */
  489.     if (b->type == TYPEMULTIPART) {
  490.       pt = b->contents.part;    /* yes, find desired part */
  491.       while (--i && (pt = pt->next));
  492.       if (!pt) return NIL;    /* bad specifier */
  493.                 /* note new body, check valid nesting */
  494.       if (((b = &pt->body)->type == TYPEMULTIPART) && !*s) return NIL;
  495.       base += offset;        /* calculate new base */
  496.       offset = pt->offset;    /* and its offset */
  497.     }
  498.     else if (i != 1) return NIL;/* otherwise must be section 1 */
  499.                 /* need to go down further? */
  500.     if (i = *s) switch (b->type) {
  501.     case TYPEMESSAGE:        /* embedded message, calculate new base */
  502.       base += offset + b->contents.msg.offset;
  503.       offset = 0;        /* no offset any more */
  504.       b = b->contents.msg.body;    /* get its body, drop into multipart case */
  505.     case TYPEMULTIPART:        /* multipart, get next section */
  506.       if ((*s++ == '.') && (i = strtol (s,&s,10)) > 0) break;
  507.     default:            /* bogus subpart specification */
  508.       return NIL;
  509.     }
  510.   } while (i);
  511.                 /* lose if body bogus */
  512.   if ((!b) || b->type == TYPEMULTIPART) return NIL;
  513.   if (!elt->seen) {        /* if message not seen before */
  514.     elt->seen = T;        /* mark as seen */
  515.     LOCAL->dirty = T;        /* and that stream is now dirty */
  516.   }
  517.   LOCAL->seen[m-1] = T;
  518.   return rfc822_contents (&LOCAL->buf,&LOCAL->buflen,len,base + offset,
  519.               b->size.ibytes,b->encoding);
  520. }
  521.  
  522. /* Netnews mail set flag
  523.  * Accepts: MAIL stream
  524.  *        sequence
  525.  *        flag(s)
  526.  */
  527.  
  528. void news_setflag (stream,sequence,flag)
  529.     MAILSTREAM *stream;
  530.     char *sequence;
  531.     char *flag;
  532. {
  533.   MESSAGECACHE *elt;
  534.   long i;
  535.   short f = news_getflags (stream,flag);
  536.   short f1 = f & (fSEEN|fDELETED);
  537.   if (!f) return;        /* no-op if no flags to modify */
  538.                 /* get sequence and loop on it */
  539.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  540.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  541.                 /* set all requested flags */
  542.       if (f&fSEEN) elt->seen = T;
  543.       if (f&fDELETED) {        /* deletion also purges the cache */
  544.     elt->deleted = T;    /* mark deleted */
  545.     if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  546.     if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  547.       }
  548.       if (f&fFLAGGED) elt->flagged = T;
  549.       if (f&fANSWERED) elt->answered = T;
  550.       if (f1 && !LOCAL->seen[i]) LOCAL->seen[i] = LOCAL->dirty = T;
  551.     }
  552. }
  553.  
  554.  
  555. /* Netnews mail clear flag
  556.  * Accepts: MAIL stream
  557.  *        sequence
  558.  *        flag(s)
  559.  */
  560.  
  561. void news_clearflag (stream,sequence,flag)
  562.     MAILSTREAM *stream;
  563.     char *sequence;
  564.     char *flag;
  565. {
  566.   MESSAGECACHE *elt;
  567.   long i;
  568.   short f = news_getflags (stream,flag);
  569.   short f1 = f & (fSEEN|fDELETED);
  570.   if (!f) return;        /* no-op if no flags to modify */
  571.                 /* get sequence and loop on it */
  572.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  573.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  574.                 /* clear all requested flags */
  575.       if (f&fSEEN) elt->seen = NIL;
  576.       if (f&fDELETED) elt->deleted = NIL;
  577.       if (f&fFLAGGED) elt->flagged = NIL;
  578.       if (f&fANSWERED) elt->answered = NIL;
  579.                 /* clearing either seen or deleted does this */
  580.       if (f1 && LOCAL->seen[i]) {
  581.     LOCAL->seen[i] = NIL;
  582.     LOCAL->dirty = T;    /* mark stream as dirty */
  583.       }
  584.     }
  585. }
  586.  
  587. /* Netnews mail search for messages
  588.  * Accepts: MAIL stream
  589.  *        search criteria
  590.  */
  591.  
  592. void news_search (stream,criteria)
  593.     MAILSTREAM *stream;
  594.     char *criteria;
  595. {
  596.   long i,n;
  597.   char *d;
  598.   search_t f;
  599.                 /* initially all searched */
  600.   for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->searched = T;
  601.                 /* get first criterion */
  602.   if (criteria && (criteria = strtok (criteria," "))) {
  603.                 /* for each criterion */
  604.     for (; criteria; (criteria = strtok (NIL," "))) {
  605.       f = NIL; d = NIL; n = 0;    /* init then scan the criterion */
  606.       switch (*ucase (criteria)) {
  607.       case 'A':            /* possible ALL, ANSWERED */
  608.     if (!strcmp (criteria+1,"LL")) f = news_search_all;
  609.     else if (!strcmp (criteria+1,"NSWERED")) f = news_search_answered;
  610.     break;
  611.       case 'B':            /* possible BCC, BEFORE, BODY */
  612.     if (!strcmp (criteria+1,"CC"))
  613.       f = news_search_string (news_search_bcc,&d,&n);
  614.     else if (!strcmp (criteria+1,"EFORE"))
  615.       f = news_search_date (news_search_before,&n);
  616.     else if (!strcmp (criteria+1,"ODY"))
  617.       f = news_search_string (news_search_body,&d,&n);
  618.     break;
  619.       case 'C':            /* possible CC */
  620.     if (!strcmp (criteria+1,"C"))
  621.       f = news_search_string (news_search_cc,&d,&n);
  622.     break;
  623.       case 'D':            /* possible DELETED */
  624.     if (!strcmp (criteria+1,"ELETED")) f = news_search_deleted;
  625.     break;
  626.       case 'F':            /* possible FLAGGED, FROM */
  627.     if (!strcmp (criteria+1,"LAGGED")) f = news_search_flagged;
  628.     else if (!strcmp (criteria+1,"ROM"))
  629.       f = news_search_string (news_search_from,&d,&n);
  630.     break;
  631.       case 'K':            /* possible KEYWORD */
  632.     if (!strcmp (criteria+1,"EYWORD"))
  633.       f = news_search_flag (news_search_keyword,&d);
  634.     break;
  635.       case 'N':            /* possible NEW */
  636.     if (!strcmp (criteria+1,"EW")) f = news_search_new;
  637.     break;
  638.  
  639.       case 'O':            /* possible OLD, ON */
  640.     if (!strcmp (criteria+1,"LD")) f = news_search_old;
  641.     else if (!strcmp (criteria+1,"N"))
  642.       f = news_search_date (news_search_on,&n);
  643.     break;
  644.       case 'R':            /* possible RECENT */
  645.     if (!strcmp (criteria+1,"ECENT")) f = news_search_recent;
  646.     break;
  647.       case 'S':            /* possible SEEN, SINCE, SUBJECT */
  648.     if (!strcmp (criteria+1,"EEN")) f = news_search_seen;
  649.     else if (!strcmp (criteria+1,"INCE"))
  650.       f = news_search_date (news_search_since,&n);
  651.     else if (!strcmp (criteria+1,"UBJECT"))
  652.       f = news_search_string (news_search_subject,&d,&n);
  653.     break;
  654.       case 'T':            /* possible TEXT, TO */
  655.     if (!strcmp (criteria+1,"EXT"))
  656.       f = news_search_string (news_search_text,&d,&n);
  657.     else if (!strcmp (criteria+1,"O"))
  658.       f = news_search_string (news_search_to,&d,&n);
  659.     break;
  660.       case 'U':            /* possible UN* */
  661.     if (criteria[1] == 'N') {
  662.       if (!strcmp (criteria+2,"ANSWERED")) f = news_search_unanswered;
  663.       else if (!strcmp (criteria+2,"DELETED")) f = news_search_undeleted;
  664.       else if (!strcmp (criteria+2,"FLAGGED")) f = news_search_unflagged;
  665.       else if (!strcmp (criteria+2,"KEYWORD"))
  666.         f = news_search_flag (news_search_unkeyword,&d);
  667.       else if (!strcmp (criteria+2,"SEEN")) f = news_search_unseen;
  668.     }
  669.     break;
  670.       default:            /* we will barf below */
  671.     break;
  672.       }
  673.       if (!f) {            /* if can't determine any criteria */
  674.     sprintf (LOCAL->buf,"Unknown search criterion: %.80s",criteria);
  675.     mm_log (LOCAL->buf,ERROR);
  676.     return;
  677.       }
  678.                 /* run the search criterion */
  679.       for (i = 1; i <= stream->nmsgs; ++i)
  680.     if (mail_elt (stream,i)->searched && !(*f) (stream,i,d,n))
  681.       mail_elt (stream,i)->searched = NIL;
  682.     }
  683.                 /* report search results to main program */
  684.     for (i = 1; i <= stream->nmsgs; ++i)
  685.       if (mail_elt (stream,i)->searched) mail_searched (stream,i);
  686.   }
  687. }
  688.  
  689. /* Netnews mail ping mailbox
  690.  * Accepts: MAIL stream
  691.  * Returns: T if stream alive, else NIL
  692.  */
  693.  
  694. long news_ping (stream)
  695.     MAILSTREAM *stream;
  696. {
  697.   return T;            /* always alive */
  698. }
  699.  
  700.  
  701. /* Netnews mail check mailbox
  702.  * Accepts: MAIL stream
  703.  */
  704.  
  705. void news_check (stream)
  706.     MAILSTREAM *stream;
  707. {
  708.   int fd;
  709.   long i,j,k;
  710.   char *s;
  711.   char tmp[MAILTMPLEN];
  712.   struct stat sbuf;
  713.   struct iovec iov[3];
  714.                 /* never do if anonymous or no updates */
  715.   if (stream->anonymous || !LOCAL->dirty) return;
  716.   *LOCAL->buf = '\n';        /* header to make for easier searches */
  717.                 /* open .newsrc file */
  718.   if ((fd = open (NEWSRC,O_RDWR|O_CREAT,NIL)) < 0) {
  719.     mm_log ("Can't update news state",ERROR);
  720.     return;
  721.   }
  722.   flock (fd,LOCK_EX);        /* wait for exclusive access */
  723.   fstat (fd,&sbuf);        /* get size of data */
  724.                 /* ensure enough room */
  725.   if (sbuf.st_size >= (LOCAL->buflen + 1)) {
  726.                 /* fs_resize does an unnecessary copy */
  727.     fs_give ((void **) &LOCAL->buf);
  728.     LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size + 1) + 1);
  729.   }
  730.   *LOCAL->buf = '\n';        /* slurp the silly thing in */
  731.   read (fd,iov[0].iov_base = LOCAL->buf + 1,iov[0].iov_len = sbuf.st_size);
  732.                 /* tie off file */
  733.   LOCAL->buf[sbuf.st_size + 1] = '\0';
  734.                 /* make backup file */
  735.   strcat (strcpy (tmp,getpwuid (geteuid ())->pw_dir),"/.oldnewsrc");
  736.   if ((i = open (tmp,O_WRONLY|O_CREAT,0600)) >= 0) {
  737.     write (i,LOCAL->buf + 1,sbuf.st_size);
  738.     close (i);
  739.   }
  740.                 /* find as subscribed newsgroup */
  741.   sprintf (tmp,"\n%s:",LOCAL->name);
  742.   if (s = strstr (LOCAL->buf,tmp)) s += strlen (tmp);
  743.   else {            /* find as unsubscribed newsgroup */
  744.     sprintf (tmp,"\n%s!",LOCAL->name);
  745.     if (s = strstr (LOCAL->buf,tmp)) s += strlen (tmp);
  746.   }
  747.   iov[2].iov_base = "";        /* dummy in case no third block */
  748.   iov[2].iov_len = 0;
  749.  
  750.   if (s) {            /* found existing, calculate prefix length */
  751.     iov[0].iov_len = s - (LOCAL->buf + 1);
  752.     if (s = strchr (s+1,'\n')) {/* find suffix */
  753.       iov[2].iov_base = ++s;    /* suffix base and length */
  754.       iov[2].iov_len = sbuf.st_size - (s - (LOCAL->buf + 1));
  755.     }
  756.     s = tmp;            /* pointer to dump sequence numbers */
  757.   }
  758.   else {            /* not found, append as unsubscribed group */
  759.     sprintf (tmp,"%s!",LOCAL->name);
  760.     s = tmp + strlen (tmp);    /* point to end of string */
  761.   }
  762.   *s++ = ' ';            /* leading space */
  763.   *s = '\0';            /* go through list */
  764.   for (i = 0,j = 1,k = 0; i < stream->nmsgs; ++i) {
  765.     if (LOCAL->seen[i]) {    /* seen message? */
  766.       k = LOCAL->number[i];    /* this is the top of the current range */
  767.       if (j == 0) j = k;    /* if no range in progress, start one */
  768.     }
  769.     else if (j != 0) {        /* unread message, ending a range */
  770.                 /* calculate end of range */
  771.       if (k = LOCAL->number[i] - 1) {
  772.                 /* dump range */
  773.     sprintf (s,(j == k) ? "%d," : "%d-%d,",j,k);
  774.     s += strlen (s);    /* find end of string */
  775.       }
  776.       j = 0;            /* no more range in progress */
  777.     }
  778.   }
  779.   if (j) {            /* dump trailing range */
  780.     sprintf (s,(j == k) ? "%d" : "%d-%d",j,k);
  781.     s += strlen (s);        /* find end of string */
  782.   }
  783.   else if (s[-1] == ',') s--;    /* prepare to patch out any trailing comma */
  784.   *s++ = '\n';            /* trailing newline */
  785.   iov[1].iov_base = tmp;    /* this group text */
  786.   iov[1].iov_len = s - tmp;    /* length of the text */
  787.   lseek (fd,0,L_SET);        /* go to beginning of file */
  788.   ftruncate (fd,iov[0].iov_len + iov[1].iov_len + iov[2].iov_len);
  789.   writev (fd,iov,3);        /* write the new contents */
  790.   flock (fd,LOCK_UN);        /* unlock the file */
  791.   close (fd);            /* flush .newsrc file */
  792. }
  793.  
  794.  
  795. /* Netnews mail expunge mailbox
  796.  * Accepts: MAIL stream
  797.  */
  798.  
  799. void news_expunge (stream)
  800.     MAILSTREAM *stream;
  801. {
  802.   if (!stream->silent) mm_log ("Expunge ignored on readonly mailbox",NIL);
  803. }
  804.  
  805. /* Netnews mail copy message(s)
  806.     s;
  807.  * Accepts: MAIL stream
  808.  *        sequence
  809.  *        destination mailbox
  810.  * Returns: T if copy successful, else NIL
  811.  */
  812.  
  813. long news_copy (stream,sequence,mailbox)
  814.     MAILSTREAM *stream;
  815.     char *sequence;
  816.     char *mailbox;
  817. {
  818.   char tmp[MAILTMPLEN];
  819.   char lock[MAILTMPLEN];
  820.   struct iovec iov[3];
  821.   struct stat ssbuf,dsbuf;
  822.   char *t,*v;
  823.   int sfd,dfd;
  824.   long i;
  825.   long r = NIL;
  826.                 /* get sequence to do */
  827.   if (stream->anonymous || !mail_sequence (stream,sequence)) return NIL;
  828.                 /* get destination mailbox */
  829.   if ((dfd = bezerk_lock (bezerk_file (tmp,mailbox),O_WRONLY|O_APPEND|O_CREAT,
  830.               S_IREAD|S_IWRITE,lock,LOCK_EX)) < 0) {
  831.     sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
  832.     mm_log (LOCAL->buf,ERROR);
  833.     return NIL;
  834.   }
  835.   mm_critical (stream);        /* go critical */
  836.   fstat (dfd,&dsbuf);        /* get current file size */
  837.   iov[2].iov_base = "\n\n";    /* constant trailer */
  838.   iov[2].iov_len = 2;
  839.  
  840.                 /* write all requested messages to mailbox */
  841.   for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) {
  842.                 /* build message file name */
  843.     sprintf (tmp,"%s/%lu",LOCAL->dir,LOCAL->number[i - 1]);
  844.     if ((sfd = open (tmp,O_RDONLY,NIL)) >= 0) {
  845.       fstat (sfd,&ssbuf);    /* get size of message */
  846.                 /* ensure enough room */
  847.       if (ssbuf.st_size > LOCAL->buflen) {
  848.                 /* fs_resize does an unnecessary copy */
  849.     fs_give ((void **) &LOCAL->buf);
  850.     LOCAL->buf = (char *) fs_get ((LOCAL->buflen = ssbuf.st_size) + 1);
  851.       }
  852.                 /* slurp the silly thing in */
  853.       read (sfd,iov[1].iov_base = LOCAL->buf,iov[1].iov_len = ssbuf.st_size);
  854.                 /* tie off file */
  855.       iov[1].iov_base[ssbuf.st_size] = '\0';
  856.       close (sfd);        /* flush message file */
  857.                 /* get Path: data */
  858.       if ((((t = iov[1].iov_base - 1) && t[1] == 'P' && t[2] == 'a' &&
  859.         t[3] == 't' && t[4] == 'h' && t[5] == ':' && t[6] == ' ') ||
  860.        (t = strstr (iov[1].iov_base,"\nPath: "))) &&
  861.       (v = strchr (t += 7,'\n')) && (r = v - t)) {
  862.     strcpy (tmp,"From ");    /* start text */
  863.     strncpy (v = tmp+5,t,r);/* copy that many characters */
  864.     v[r++] = ' ';        /* delimiter */
  865.     v[r] = '\0';        /* tie it off */
  866.       }
  867.       else strcpy (tmp,"From somebody ");
  868.                 /* add the time and a newline */
  869.       strcat (tmp,ctime (&ssbuf.st_mtime));
  870.                 /* build rn-compatible header lines */
  871.       sprintf (tmp + strlen (tmp),"Article %lu of %s\n",
  872.            LOCAL->number[i - 1],LOCAL->name);
  873.       iov[0].iov_len = strlen (iov[0].iov_base = tmp);
  874.                 /* now do the write */
  875.       if (r = (writev (dfd,iov,3) < 0)) {
  876.     sprintf (LOCAL->buf,"Message copy %d failed: %s",i,strerror (errno));
  877.     mm_log (LOCAL->buf,ERROR);
  878.     ftruncate (dfd,dsbuf.st_size);
  879.     break;            /* give up */
  880.       }
  881.     }
  882.   }
  883.   fsync (dfd);            /* force out the update */
  884.   bezerk_unlock (dfd,NIL,lock);    /* unlock and close mailbox */
  885.   mm_nocritical (stream);    /* release critical */
  886.   return !r;            /* return whether or not succeeded */
  887. }
  888.  
  889. /* Netnews mail move message(s)
  890.     s;
  891.  * Accepts: MAIL stream
  892.  *        sequence
  893.  *        destination mailbox
  894.  * Returns: T if move successful, else NIL
  895.  */
  896.  
  897. long news_move (stream,sequence,mailbox)
  898.     MAILSTREAM *stream;
  899.     char *sequence;
  900.     char *mailbox;
  901. {
  902.   long i;
  903.   MESSAGECACHE *elt;
  904.   if (stream->anonymous || !(mail_sequence (stream,sequence) &&
  905.                  news_copy (stream,sequence,mailbox))) return NIL;
  906.                 /* delete all requested messages */
  907.   for (i = 1; i <= stream->nmsgs; i++)
  908.     if ((elt = mail_elt (stream,i))->sequence) {
  909.       elt->deleted = T;        /* mark message deleted */
  910.       LOCAL->dirty = T;        /* mark mailbox as dirty */
  911.       LOCAL->seen[i - 1] = T;    /* and seen for .newsrc update */
  912.     }
  913.   return T;
  914. }
  915.  
  916.  
  917. /* Netnews garbage collect stream
  918.  * Accepts: Mail stream
  919.  *        garbage collection flags
  920.  */
  921.  
  922. void news_gc (stream,gcflags)
  923.     MAILSTREAM *stream;
  924.     long gcflags;
  925. {
  926.   unsigned long i;
  927.   if (gcflags & GC_TEXTS)    /* garbage collect texts? */
  928.                 /* flush texts from cache */
  929.     for (i = 0; i < stream->nmsgs; i++) {
  930.       if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  931.       if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  932.     }
  933. }
  934.  
  935. /* Internal routines */
  936.  
  937.  
  938. /* Parse flag list
  939.  * Accepts: MAIL stream
  940.  *        flag list as a character string
  941.  * Returns: flag command list
  942.  */
  943.  
  944. short news_getflags (stream,flag)
  945.     MAILSTREAM *stream;
  946.     char *flag;
  947. {
  948.   char *t;
  949.   short f = 0;
  950.   short i,j;
  951.   if (flag && *flag) {        /* no-op if no flag string */
  952.                 /* check if a list and make sure valid */
  953.     if ((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) {
  954.       mm_log ("Bad flag list",ERROR);
  955.       return NIL;
  956.     }
  957.                 /* copy the flag string w/o list construct */
  958.     strncpy (LOCAL->buf,flag+i,(j = strlen (flag) - (2*i)));
  959.     LOCAL->buf[j] = '\0';
  960.     t = ucase (LOCAL->buf);    /* uppercase only from now on */
  961.  
  962.     while (*t) {        /* parse the flags */
  963.       if (*t == '\\') {        /* system flag? */
  964.     switch (*++t) {        /* dispatch based on first character */
  965.     case 'S':        /* possible \Seen flag */
  966.       if (t[1] == 'E' && t[2] == 'E' && t[3] == 'N') i = fSEEN;
  967.       t += 4;        /* skip past flag name */
  968.       break;
  969.     case 'D':        /* possible \Deleted flag */
  970.       if (t[1] == 'E' && t[2] == 'L' && t[3] == 'E' && t[4] == 'T' &&
  971.           t[5] == 'E' && t[6] == 'D') i = fDELETED;
  972.       t += 7;        /* skip past flag name */
  973.       break;
  974.     case 'F':        /* possible \Flagged flag */
  975.       if (t[1] == 'L' && t[2] == 'A' && t[3] == 'G' && t[4] == 'G' &&
  976.           t[5] == 'E' && t[6] == 'D') i = fFLAGGED;
  977.       t += 7;        /* skip past flag name */
  978.       break;
  979.     case 'A':        /* possible \Answered flag */
  980.       if (t[1] == 'N' && t[2] == 'S' && t[3] == 'W' && t[4] == 'E' &&
  981.           t[5] == 'R' && t[6] == 'E' && t[7] == 'D') i = fANSWERED;
  982.       t += 8;        /* skip past flag name */
  983.       break;
  984.     default:        /* unknown */
  985.       i = 0;
  986.       break;
  987.     }
  988.                 /* add flag to flags list */
  989.     if (i && ((*t == '\0') || (*t++ == ' '))) f |= i;
  990.     else {            /* bitch about bogus flag */
  991.       mm_log ("Unknown system flag",ERROR);
  992.       return NIL;
  993.     }
  994.       }
  995.       else {            /* no user flags yet */
  996.     mm_log ("Unknown flag",ERROR);
  997.     return NIL;
  998.       }
  999.     }
  1000.   }
  1001.   return f;
  1002. }
  1003.  
  1004. /* Search support routines
  1005.  * Accepts: MAIL stream
  1006.  *        message number
  1007.  *        pointer to additional data
  1008.  *        pointer to temporary buffer
  1009.  * Returns: T if search matches, else NIL
  1010.  */
  1011.  
  1012. char news_search_all (stream,msgno,d,n)
  1013.     MAILSTREAM *stream;
  1014.     long msgno;
  1015.     char *d;
  1016.     long n;
  1017. {
  1018.   return T;            /* ALL always succeeds */
  1019. }
  1020.  
  1021.  
  1022. char news_search_answered (stream,msgno,d,n)
  1023.     MAILSTREAM *stream;
  1024.     long msgno;
  1025.     char *d;
  1026.     long n;
  1027. {
  1028.   return mail_elt (stream,msgno)->answered ? T : NIL;
  1029. }
  1030.  
  1031.  
  1032. char news_search_deleted (stream,msgno,d,n)
  1033.     MAILSTREAM *stream;
  1034.     long msgno;
  1035.     char *d;
  1036.     long n;
  1037. {
  1038.   return mail_elt (stream,msgno)->deleted ? T : NIL;
  1039. }
  1040.  
  1041.  
  1042. char news_search_flagged (stream,msgno,d,n)
  1043.     MAILSTREAM *stream;
  1044.     long msgno;
  1045.     char *d;
  1046.     long n;
  1047. {
  1048.   return mail_elt (stream,msgno)->flagged ? T : NIL;
  1049. }
  1050.  
  1051.  
  1052. char news_search_keyword (stream,msgno,d,n)
  1053.     MAILSTREAM *stream;
  1054.     long msgno;
  1055.     char *d;
  1056.     long n;
  1057. {
  1058.   return NIL;            /* keywords not supported yet */
  1059. }
  1060.  
  1061.  
  1062. char news_search_new (stream,msgno,d,n)
  1063.     MAILSTREAM *stream;
  1064.     long msgno;
  1065.     char *d;
  1066.     long n;
  1067. {
  1068.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  1069.   return (elt->recent && !elt->seen) ? T : NIL;
  1070. }
  1071.  
  1072. char news_search_old (stream,msgno,d,n)
  1073.     MAILSTREAM *stream;
  1074.     long msgno;
  1075.     char *d;
  1076.     long n;
  1077. {
  1078.   return mail_elt (stream,msgno)->recent ? NIL : T;
  1079. }
  1080.  
  1081.  
  1082. char news_search_recent (stream,msgno,d,n)
  1083.     MAILSTREAM *stream;
  1084.     long msgno;
  1085.     char *d;
  1086.     long n;
  1087. {
  1088.   return mail_elt (stream,msgno)->recent ? T : NIL;
  1089. }
  1090.  
  1091.  
  1092. char news_search_seen (stream,msgno,d,n)
  1093.     MAILSTREAM *stream;
  1094.     long msgno;
  1095.     char *d;
  1096.     long n;
  1097. {
  1098.   return mail_elt (stream,msgno)->seen ? T : NIL;
  1099. }
  1100.  
  1101.  
  1102. char news_search_unanswered (stream,msgno,d,n)
  1103.     MAILSTREAM *stream;
  1104.     long msgno;
  1105.     char *d;
  1106.     long n;
  1107. {
  1108.   return mail_elt (stream,msgno)->answered ? NIL : T;
  1109. }
  1110.  
  1111.  
  1112. char news_search_undeleted (stream,msgno,d,n)
  1113.     MAILSTREAM *stream;
  1114.     long msgno;
  1115.     char *d;
  1116.     long n;
  1117. {
  1118.   return mail_elt (stream,msgno)->deleted ? NIL : T;
  1119. }
  1120.  
  1121.  
  1122. char news_search_unflagged (stream,msgno,d,n)
  1123.     MAILSTREAM *stream;
  1124.     long msgno;
  1125.     char *d;
  1126.     long n;
  1127. {
  1128.   return mail_elt (stream,msgno)->flagged ? NIL : T;
  1129. }
  1130.  
  1131.  
  1132. char news_search_unkeyword (stream,msgno,d,n)
  1133.     MAILSTREAM *stream;
  1134.     long msgno;
  1135.     char *d;
  1136.     long n;
  1137. {
  1138.   return T;            /* keywords not supported yet */
  1139. }
  1140.  
  1141.  
  1142. char news_search_unseen (stream,msgno,d,n)
  1143.     MAILSTREAM *stream;
  1144.     long msgno;
  1145.     char *d;
  1146.     long n;
  1147. {
  1148.   return mail_elt (stream,msgno)->seen ? NIL : T;
  1149. }
  1150.  
  1151. char news_search_before (stream,msgno,d,n)
  1152.     MAILSTREAM *stream;
  1153.     long msgno;
  1154.     char *d;
  1155.     long n;
  1156. {
  1157.   return (char) (news_msgdate (stream,msgno) < n);
  1158. }
  1159.  
  1160.  
  1161. char news_search_on (stream,msgno,d,n)
  1162.     MAILSTREAM *stream;
  1163.     long msgno;
  1164.     char *d;
  1165.     long n;
  1166. {
  1167.   return (char) (news_msgdate (stream,msgno) == n);
  1168. }
  1169.  
  1170.  
  1171. char news_search_since (stream,msgno,d,n)
  1172.     MAILSTREAM *stream;
  1173.     long msgno;
  1174.     char *d;
  1175.     long n;
  1176. {
  1177.                 /* everybody interprets "since" as .GE. */
  1178.   return (char) (news_msgdate (stream,msgno) >= n);
  1179. }
  1180.  
  1181.  
  1182. unsigned long news_msgdate (stream,msgno)
  1183.     MAILSTREAM *stream;
  1184.     long msgno;
  1185. {
  1186.   struct stat sbuf;
  1187.   struct tm *tm;
  1188.   long i = msgno - 1;
  1189.   if (!LOCAL->date[i]) {    /* get cached date if don't have it */
  1190.                 /* build message file name */
  1191.     sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[i]);
  1192.     stat (LOCAL->buf,&sbuf);    /* get message date */
  1193.     tm = localtime (&sbuf.st_mtime);
  1194.                 /* calculate value for date comparisons */
  1195.     LOCAL->date[i] = ((tm->tm_year-70)*12+tm->tm_mon)*31+tm->tm_mday-1;
  1196.   }
  1197.   return LOCAL->date[i];
  1198. }
  1199.  
  1200. char news_search_body (stream,msgno,d,n)
  1201.     MAILSTREAM *stream;
  1202.     long msgno;
  1203.     char *d;
  1204.     long n;
  1205. {
  1206.   long i = msgno - 1;
  1207.   news_fetchheader (stream,msgno);
  1208.   return LOCAL->body[i] ?
  1209.     search (LOCAL->body[i],strlen (LOCAL->body[i]),d,n) : NIL;
  1210. }
  1211.  
  1212.  
  1213. char news_search_subject (stream,msgno,d,n)
  1214.     MAILSTREAM *stream;
  1215.     long msgno;
  1216.     char *d;
  1217.     long n;
  1218. {
  1219.   char *t = news_fetchenvelope (stream,msgno)->subject;
  1220.   return t ? search (t,strlen (t),d,n) : NIL;
  1221. }
  1222.  
  1223.  
  1224. char news_search_text (stream,msgno,d,n)
  1225.     MAILSTREAM *stream;
  1226.     long msgno;
  1227.     char *d;
  1228.     long n;
  1229. {
  1230.   char *t = news_fetchheader (stream,msgno);
  1231.   return (t && search (t,strlen (t),d,n)) ||
  1232.     news_search_body (stream,msgno,d,n);
  1233. }
  1234.  
  1235. char news_search_bcc (stream,msgno,d,n)
  1236.     MAILSTREAM *stream;
  1237.     long msgno;
  1238.     char *d;
  1239.     long n;
  1240. {
  1241.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1242.                 /* get text for address */
  1243.   rfc822_write_address (LOCAL->buf,news_fetchenvelope (stream,msgno)->bcc);
  1244.   return search (LOCAL->buf,strlen (LOCAL->buf),d,n);
  1245. }
  1246.  
  1247.  
  1248. char news_search_cc (stream,msgno,d,n)
  1249.     MAILSTREAM *stream;
  1250.     long msgno;
  1251.     char *d;
  1252.     long n;
  1253. {
  1254.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1255.                 /* get text for address */
  1256.   rfc822_write_address (LOCAL->buf,news_fetchenvelope (stream,msgno)->cc);
  1257.   return search (LOCAL->buf,strlen (LOCAL->buf),d,n);
  1258. }
  1259.  
  1260.  
  1261. char news_search_from (stream,msgno,d,n)
  1262.     MAILSTREAM *stream;
  1263.     long msgno;
  1264.     char *d;
  1265.     long n;
  1266. {
  1267.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1268.                 /* get text for address */
  1269.   rfc822_write_address (LOCAL->buf,news_fetchenvelope (stream,msgno)->from);
  1270.   return search (LOCAL->buf,strlen (LOCAL->buf),d,n);
  1271. }
  1272.  
  1273.  
  1274. char news_search_to (stream,msgno,d,n)
  1275.     MAILSTREAM *stream;
  1276.     long msgno;
  1277.     char *d;
  1278.     long n;
  1279. {
  1280.   LOCAL->buf[0] = '\0';            /* initially empty string */
  1281.                 /* get text for address */
  1282.   rfc822_write_address (LOCAL->buf,news_fetchenvelope (stream,msgno)->to);
  1283.   return search (LOCAL->buf,strlen (LOCAL->buf),d,n);
  1284. }
  1285.  
  1286. /* Search parsers */
  1287.  
  1288.  
  1289. /* Parse a date
  1290.  * Accepts: function to return
  1291.  *        pointer to date integer to return
  1292.  * Returns: function to return
  1293.  */
  1294.  
  1295. search_t news_search_date (f,n)
  1296.     search_t f;
  1297.     long *n;
  1298. {
  1299.   long i;
  1300.   char *t;
  1301.                 /* parse the date and return fn if OK */
  1302.   return (news_search_string (f,&t,&i) && (*n = news_date (t))) ? f : NIL;
  1303. }
  1304.  
  1305.  
  1306. /* Actual date parser (routine)
  1307.     minimal and stupid routine;
  1308.  * Accepts: date to parse
  1309.  * Returns: date integer
  1310.  */
  1311.  
  1312. long news_date (s)
  1313.     char *s;
  1314. {
  1315.   long ms;
  1316.   long d,m,y;
  1317.                 /* parse first number (probable month) */
  1318.   if (!(s && (m = strtol ((const char *) s,&s,10)))) return NIL;
  1319.   switch (*s) {            /* different parse based on delimiter */
  1320.   case '/':            /* mm/dd/yy format */
  1321.                 /* parse remainder of date */
  1322.     if (!((d = strtol ((const char *) ++s,&s,10)) && *s == '/' &&
  1323.       (y = strtol ((const char *) ++s,&s,10)) && *s == '\0'))
  1324.       return NIL;
  1325.     break;
  1326.  
  1327.   case '-':            /* dd-mmm-yy format */
  1328.     d = m;            /* so the number we got is a day */
  1329.                 /* make sure string is UC and long enough! */
  1330.     if (strlen (ucase (s)) < 5) return NIL;
  1331.                 /* slurp up the month string */
  1332.     ms = (((long) s[1]) << 16) + (((long) s[2]) << 8) + s[3];
  1333.     switch (ms) {        /* determine the month */
  1334.     case ('J' << 16) + ('A' << 8) + 'N':
  1335.       m = 1; break;
  1336.     case ('F' << 16) + ('E' << 8) + 'B':
  1337.       m = 2; break;
  1338.     case ('M' << 16) + ('A' << 8) + 'R':
  1339.       m = 3; break;
  1340.     case ('A' << 16) + ('P' << 8) + 'R':
  1341.       m = 4; break;
  1342.     case ('M' << 16) + ('A' << 8) + 'Y':
  1343.       m = 5; break;
  1344.     case ('J' << 16) + ('U' << 8) + 'N':
  1345.       m = 6; break;
  1346.     case ('J' << 16) + ('U' << 8) + 'L':
  1347.       m = 7; break;
  1348.     case ('A' << 16) + ('U' << 8) + 'G':
  1349.       m = 8; break;
  1350.     case ('S' << 16) + ('E' << 8) + 'P':
  1351.       m = 9; break;
  1352.     case ('O' << 16) + ('C' << 8) + 'T':
  1353.       m = 10; break;
  1354.     case ('N' << 16) + ('O' << 8) + 'V':
  1355.       m = 11; break;
  1356.     case ('D' << 16) + ('E' << 8) + 'C':
  1357.       m = 12; break;
  1358.     default:
  1359.       return NIL;
  1360.     }
  1361.                 /* parse the year */
  1362.     if (s[4] == '-' && (y = (int) strtol ((const char *) s+5,&s,10)) &&
  1363.     (*s == '\0' || *s == ' ')) break;
  1364.   default:            /* unknown format */
  1365.     return NIL;
  1366.   }
  1367.   y -= (y >= 1900) ? 1970 : 70;    /* the world began for Unix in 1970 */
  1368.                 /* minimal validity check of date */
  1369.   if (d < 1 || d > 31 || m < 1 || m > 12 || y < 0 || y >= 100) return NIL;
  1370.   return (y*12+m-1)*31+d-1;    /* calculate and return date value */
  1371. }
  1372.  
  1373. /* Parse a flag
  1374.  * Accepts: function to return
  1375.  *        pointer to string to return
  1376.  * Returns: function to return
  1377.  */
  1378.  
  1379. search_t news_search_flag (f,d)
  1380.     search_t f;
  1381.     char **d;
  1382. {
  1383.                 /* get a keyword, return if OK */
  1384.   return (*d = strtok (NIL," ")) ? f : NIL;
  1385. }
  1386.  
  1387.  
  1388. /* Parse a string
  1389.  * Accepts: function to return
  1390.  *        pointer to string to return
  1391.  *        pointer to string length to return
  1392.  * Returns: function to return
  1393.  */
  1394.  
  1395. search_t news_search_string (f,d,n)
  1396.     search_t f;
  1397.     char **d;
  1398.     long *n;
  1399. {
  1400.   char *c = strtok (NIL,"");    /* remainder of criteria */
  1401.   if (c) {            /* better be an argument */
  1402.     switch (*c) {        /* see what the argument is */
  1403.     case '\0':            /* catch bogons */
  1404.     case ' ':
  1405.       return NIL;
  1406.     case '"':            /* quoted string */
  1407.       if (!(strchr (c+1,'"') && (*d = strtok (c,"\"")) && (*n = strlen (*d))))
  1408.     return NIL;
  1409.       break;
  1410.     case '{':            /* literal string */
  1411.       *n = strtol (c+1,&c,10);    /* get its length */
  1412.       if (*c++ != '}' || *c++ != '\015' || *c++ != '\012' ||
  1413.       *n > strlen (*d = c)) return NIL;
  1414.       c[*n] = '\255';        /* write new delimiter */
  1415.       strtok (c,"\255");    /* reset the strtok mechanism */
  1416.       break;
  1417.     default:            /* atomic string */
  1418.       *n = strlen (*d = strtok (c," "));
  1419.       break;
  1420.     }
  1421.     return f;
  1422.   }
  1423.   else return NIL;
  1424. }
  1425.