home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / c-client / pop3.c < prev    next >
C/C++ Source or Header  |  1998-12-01  |  24KB  |  816 lines

  1. /*
  2.  * Program:    Post Office Protocol 3 (POP3) client 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:    6 June 1994
  13.  * Last Edited:    1 December 1998
  14.  *
  15.  * Copyright 1998 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 notices appear in all copies and that both the
  20.  * above copyright notices 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
  30.  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN
  32.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. #include "mail.h"
  38. #include "osdep.h"
  39. #include <ctype.h>
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include "pop3.h"
  43. #include "rfc822.h"
  44. #include "misc.h"
  45. #include "netmsg.h"
  46. #include "flstring.h"
  47.  
  48. /* POP3 mail routines */
  49.  
  50.  
  51. /* Driver dispatch used by MAIL */
  52.  
  53. DRIVER pop3driver = {
  54.   "pop",            /* driver name */
  55.                 /* driver flags */
  56. #ifdef INADEQUATE_MEMORY
  57.   DR_LOWMEM |
  58. #endif
  59.   DR_MAIL|DR_NOFAST|DR_CRLF|DR_NOSTICKY,
  60.   (DRIVER *) NIL,        /* next driver */
  61.   pop3_valid,            /* mailbox is valid for us */
  62.   pop3_parameters,        /* manipulate parameters */
  63.   pop3_scan,            /* scan mailboxes */
  64.   pop3_list,            /* find mailboxes */
  65.   pop3_lsub,            /* find subscribed mailboxes */
  66.   pop3_subscribe,        /* subscribe to mailbox */
  67.   pop3_unsubscribe,        /* unsubscribe from mailbox */
  68.   pop3_create,            /* create mailbox */
  69.   pop3_delete,            /* delete mailbox */
  70.   pop3_rename,            /* rename mailbox */
  71.   pop3_status,            /* status of mailbox */
  72.   pop3_open,            /* open mailbox */
  73.   pop3_close,            /* close mailbox */
  74.   pop3_fetchfast,        /* fetch message "fast" attributes */
  75.   NIL,                /* fetch message flags */
  76.   NIL,                /* fetch overview */
  77.   NIL,                /* fetch message structure */
  78.   pop3_header,            /* fetch message header */
  79.   pop3_text,            /* fetch message text */
  80.   NIL,                /* fetch message */
  81.   NIL,                /* unique identifier */
  82.   NIL,                /* message number from UID */
  83.   NIL,                /* modify flags */
  84.   NIL,                /* per-message modify flags */
  85.   NIL,                /* search for message based on criteria */
  86.   NIL,                /* sort messages */
  87.   NIL,                /* thread messages */
  88.   pop3_ping,            /* ping mailbox to see if still alive */
  89.   pop3_check,            /* check for new messages */
  90.   pop3_expunge,            /* expunge deleted messages */
  91.   pop3_copy,            /* copy messages to another mailbox */
  92.   pop3_append,            /* append string message to mailbox */
  93.   NIL                /* garbage collect stream */
  94. };
  95.  
  96.                 /* prototype stream */
  97. MAILSTREAM pop3proto = {&pop3driver};
  98.  
  99.                 /* driver parameters */
  100. static unsigned long pop3_maxlogintrials = MAXLOGINTRIALS;
  101. static long pop3_port = 0;
  102. static long pop3_altport = 0;
  103. static char *pop3_altname = NIL;
  104.  
  105. /* POP3 mail validate mailbox
  106.  * Accepts: mailbox name
  107.  * Returns: our driver if name is valid, NIL otherwise
  108.  */
  109.  
  110. DRIVER *pop3_valid (char *name)
  111. {
  112.   DRIVER *drv;
  113.   char mbx[MAILTMPLEN];
  114.   return ((drv = mail_valid_net (name,&pop3driver,NIL,mbx)) &&
  115.       !strcmp (ucase (mbx),"INBOX")) ? drv : NIL;
  116. }
  117.  
  118.  
  119. /* News manipulate driver parameters
  120.  * Accepts: function code
  121.  *        function-dependent value
  122.  * Returns: function-dependent return value
  123.  */
  124.  
  125. void *pop3_parameters (long function,void *value)
  126. {
  127.   switch ((int) function) {
  128.   case SET_MAXLOGINTRIALS:
  129.     pop3_maxlogintrials = (unsigned long) value;
  130.     break;
  131.   case GET_MAXLOGINTRIALS:
  132.     value = (void *) pop3_maxlogintrials;
  133.     break;
  134.   case SET_POP3PORT:
  135.     pop3_port = (long) value;
  136.     break;
  137.   case GET_POP3PORT:
  138.     value = (void *) pop3_port;
  139.     break;
  140.   case SET_ALTPOPPORT:
  141.     pop3_altport = (long) value;
  142.     break;
  143.   case GET_ALTPOPPORT:
  144.     value = (void *) pop3_altport;
  145.     break;
  146.   case SET_ALTPOPNAME:
  147.     pop3_altname = (char *) value;
  148.     break;
  149.   case GET_ALTPOPNAME:
  150.     value = (void *) pop3_altname;
  151.     break;
  152.   default:
  153.     value = NIL;        /* error case */
  154.     break;
  155.   }
  156.   return value;
  157. }
  158.  
  159. /* POP3 mail scan mailboxes for string
  160.  * Accepts: mail stream
  161.  *        reference
  162.  *        pattern to search
  163.  *        string to scan
  164.  */
  165.  
  166. void pop3_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
  167. {
  168.   char tmp[MAILTMPLEN];
  169.   if ((ref && *ref) ?        /* have a reference */
  170.       (pop3_valid (ref) && pmatch ("INBOX",pat)) :
  171.       (mail_valid_net (pat,&pop3driver,NIL,tmp) && pmatch ("INBOX",tmp)))
  172.     mm_log ("Scan not valid for POP3 mailboxes",ERROR);
  173. }
  174.  
  175.  
  176. /* POP3 mail find list of all mailboxes
  177.  * Accepts: mail stream
  178.  *        reference
  179.  *        pattern to search
  180.  */
  181.  
  182. void pop3_list (MAILSTREAM *stream,char *ref,char *pat)
  183. {
  184.   char tmp[MAILTMPLEN];
  185.   if (ref && *ref) {        /* have a reference */
  186.     if (pop3_valid (ref) && pmatch ("INBOX",pat)) {
  187.       strcpy (strchr (strcpy (tmp,ref),'}')+1,"INBOX");
  188.       mm_list (stream,NIL,tmp,LATT_NOINFERIORS);
  189.     }
  190.   }
  191.   else if (mail_valid_net (pat,&pop3driver,NIL,tmp) && pmatch ("INBOX",tmp)) {
  192.     strcpy (strchr (strcpy (tmp,pat),'}')+1,"INBOX");
  193.     mm_list (stream,NIL,tmp,LATT_NOINFERIORS);
  194.   }
  195. }
  196.  
  197. /* POP3 mail find list of subscribed mailboxes
  198.  * Accepts: mail stream
  199.  *        reference
  200.  *        pattern to search
  201.  */
  202.  
  203. void pop3_lsub (MAILSTREAM *stream,char *ref,char *pat)
  204. {
  205.   void *sdb = NIL;
  206.   char *s,mbx[MAILTMPLEN];
  207.   if (*pat == '{') {        /* if remote pattern, must be POP3 */
  208.     if (!pop3_valid (pat)) return;
  209.     ref = NIL;            /* good POP3 pattern, punt reference */
  210.   }
  211.                 /* if remote reference, must be valid POP3 */
  212.   if (ref && (*ref == '{') && !pop3_valid (ref)) return;
  213.                 /* kludgy application of reference */
  214.   if (ref && *ref) sprintf (mbx,"%s%s",ref,pat);
  215.   else strcpy (mbx,pat);
  216.  
  217.   if (s = sm_read (&sdb)) do if (pop3_valid (s) && pmatch (s,mbx))
  218.     mm_lsub (stream,NIL,s,NIL);
  219.   while (s = sm_read (&sdb));    /* until no more subscriptions */
  220. }
  221.  
  222.  
  223. /* POP3 mail subscribe to mailbox
  224.  * Accepts: mail stream
  225.  *        mailbox to add to subscription list
  226.  * Returns: T on success, NIL on failure
  227.  */
  228.  
  229. long pop3_subscribe (MAILSTREAM *stream,char *mailbox)
  230. {
  231.   return sm_subscribe (mailbox);
  232. }
  233.  
  234.  
  235. /* POP3 mail unsubscribe to mailbox
  236.  * Accepts: mail stream
  237.  *        mailbox to delete from subscription list
  238.  * Returns: T on success, NIL on failure
  239.  */
  240.  
  241. long pop3_unsubscribe (MAILSTREAM *stream,char *mailbox)
  242. {
  243.   return sm_unsubscribe (mailbox);
  244. }
  245.  
  246. /* POP3 mail create mailbox
  247.  * Accepts: mail stream
  248.  *        mailbox name to create
  249.  * Returns: T on success, NIL on failure
  250.  */
  251.  
  252. long pop3_create (MAILSTREAM *stream,char *mailbox)
  253. {
  254.   return NIL;            /* never valid for POP3 */
  255. }
  256.  
  257.  
  258. /* POP3 mail delete mailbox
  259.  *        mailbox name to delete
  260.  * Returns: T on success, NIL on failure
  261.  */
  262.  
  263. long pop3_delete (MAILSTREAM *stream,char *mailbox)
  264. {
  265.   return NIL;            /* never valid for POP3 */
  266. }
  267.  
  268.  
  269. /* POP3 mail rename mailbox
  270.  * Accepts: mail stream
  271.  *        old mailbox name
  272.  *        new mailbox name
  273.  * Returns: T on success, NIL on failure
  274.  */
  275.  
  276. long pop3_rename (MAILSTREAM *stream,char *old,char *newname)
  277. {
  278.   return NIL;            /* never valid for POP3 */
  279. }
  280.  
  281. /* POP3 status
  282.  * Accepts: mail stream
  283.  *        mailbox name
  284.  *        status flags
  285.  * Returns: T on success, NIL on failure
  286.  */
  287.  
  288. long pop3_status (MAILSTREAM *stream,char *mbx,long flags)
  289. {
  290.   MAILSTATUS status;
  291.   unsigned long i;
  292.   long ret = NIL;
  293.   MAILSTREAM *tstream =
  294.     (stream && LOCAL->netstream && mail_usable_network_stream (stream,mbx)) ?
  295.       stream : mail_open (NIL,mbx,OP_HALFOPEN|OP_SILENT);
  296.   if (tstream) {        /* have a usable stream? */
  297.     status.flags = flags;    /* return status values */
  298.     status.messages = tstream->nmsgs;
  299.     status.recent = tstream->recent;
  300.     if (flags & SA_UNSEEN)    /* must search to get unseen messages */
  301.       for (i = 1,status.unseen = 0; i < tstream->nmsgs; i++)
  302.     if (!mail_elt (tstream,i)->seen) status.unseen++;
  303.     status.uidnext = tstream->uid_last + 1;
  304.     status.uidvalidity = tstream->uid_validity;
  305.                 /* pass status to main program */
  306.     mm_status (tstream,mbx,&status);
  307.     if (stream != tstream) mail_close (tstream);
  308.     ret = LONGT;
  309.   }
  310.   return ret;            /* success */
  311. }
  312.  
  313. /* POP3 mail open
  314.  * Accepts: stream to open
  315.  * Returns: stream on success, NIL on failure
  316.  */
  317.  
  318. MAILSTREAM *pop3_open (MAILSTREAM *stream)
  319. {
  320.   unsigned long i;
  321.   char *s,tmp[MAILTMPLEN],usr[MAILTMPLEN];
  322.   NETMBX mb;
  323.   MESSAGECACHE *elt;
  324.                 /* return prototype for OP_PROTOTYPE call */
  325.   if (!stream) return &pop3proto;
  326.   mail_valid_net_parse (stream->mailbox,&mb);
  327.   if (stream->local) fatal ("pop3 recycle stream");
  328.                 /* /anonymous not supported */
  329.   if (mb.anoflag || stream->anonymous) {
  330.     mm_log ("Anonymous POP3 login not available",ERROR);
  331.     return NIL;
  332.   }
  333.                 /* copy other switches */
  334.   if (mb.dbgflag) stream->debug = T;
  335.   if (mb.secflag) stream->secure = T;
  336.                 /* set up host with port override */
  337.   if (mb.port || pop3_port) sprintf (s = tmp,"%s:%ld",mb.host,
  338.                      mb.port ? mb.port : pop3_port);
  339.   else s = mb.host;        /* simple host name */
  340.   stream->local = fs_get (sizeof (POP3LOCAL));
  341.   stream->sequence++;        /* bump sequence number */
  342.   stream->perm_deleted = T;    /* deleted is only valid flag */
  343.   LOCAL->response = LOCAL->reply = NIL;
  344.                 /* currently no message */
  345.   LOCAL->msgno = LOCAL->hdrsize = 0;
  346.   LOCAL->txt = NIL;        /* no file initially */
  347.  
  348.                 /* try to open connection */
  349.   if ((LOCAL->netstream = mb.altflag ?
  350.        net_open ((NETDRIVER *) mail_parameters (NIL,GET_ALTDRIVER,NIL),s,
  351.          (char *) mail_parameters (NIL,GET_ALTPOPNAME,NIL),
  352.          (unsigned long) mail_parameters (NIL,GET_ALTPOPPORT,NIL)) :
  353.        net_open (NIL,s,"pop3",POP3TCPPORT)) &&
  354.       pop3_reply (stream)) {
  355.     mm_log (LOCAL->reply,NIL);    /* give greeting */
  356.     if (!pop3_auth (stream,&mb,tmp,usr)) pop3_close (stream,NIL);
  357.     else if (pop3_send (stream,"STAT",NIL)) {
  358.       int silent = stream->silent;
  359.       stream->silent = T;
  360.       sprintf (tmp,"{%s:%lu/pop3",net_host (LOCAL->netstream),
  361.            net_port (LOCAL->netstream));
  362.       if (mb.altflag) sprintf (tmp + strlen (tmp),"/%s",(char *)
  363.                    mail_parameters (NIL,GET_ALTDRIVERNAME,NIL));
  364.       if (mb.secflag) strcat (tmp,"/secure");
  365.       sprintf (tmp + strlen (tmp),"/user=%s}INBOX",usr);
  366.       fs_give ((void **) &stream->mailbox);
  367.       stream->mailbox = cpystr (tmp);
  368.                 /* notify upper level */
  369.       mail_exists (stream,stream->uid_last = strtoul (LOCAL->reply,NIL,10));
  370.       mail_recent (stream,stream->nmsgs);
  371.                 /* instantiate elt */
  372.       for (i = 0; i < stream->nmsgs;) {
  373.     elt = mail_elt (stream,++i);
  374.     elt->valid = elt->recent = T;
  375.     elt->private.uid = i;
  376.       }
  377.       stream->silent = silent;    /* notify main program */
  378.       mail_exists (stream,stream->nmsgs);
  379.                 /* notify if empty */
  380.       if (!(stream->nmsgs || stream->silent)) mm_log ("Mailbox is empty",WARN);
  381.     }
  382.     else {            /* error in STAT */
  383.       mm_log (LOCAL->reply,ERROR);
  384.       pop3_close (stream,NIL);    /* too bad */
  385.     }
  386.   }
  387.   else {            /* connection failed */
  388.     if (LOCAL->reply) mm_log (LOCAL->reply,ERROR);
  389.     pop3_close (stream,NIL);    /* failed, clean up */
  390.   }
  391.   return LOCAL ? stream : NIL;    /* if stream is alive, return to caller */
  392. }
  393.  
  394. /* POP3 authenticate
  395.  * Accepts: stream to login
  396.  *        parsed network mailbox structure
  397.  *        scratch buffer
  398.  *        place to return user name
  399.  * Returns: T on success, NIL on failure
  400.  */
  401.  
  402. long pop3_auth (MAILSTREAM *stream,NETMBX *mb,char *tmp,char *usr)
  403. {
  404.   unsigned long i,auths = 0;
  405.   char *t;
  406.   AUTHENTICATOR *at;
  407.                 /* get list of authenticators */
  408.   if (pop3_send (stream,"AUTH",NIL)) {
  409.     while ((t = net_getline (LOCAL->netstream)) && (t[1] || (*t != '.'))) {
  410.       if (stream->debug) mm_dlog (t);
  411.       if ((i = mail_lookup_auth_name (t,stream->secure)) &&
  412.       (--i < (8*sizeof (unsigned long)))) auths |= (1 << i);
  413.       fs_give ((void **) &t);
  414.     }
  415.     if (t) {            /* flush end of text indicator */
  416.       if (stream->debug) mm_dlog (t);
  417.       fs_give ((void **) &t);
  418.     }
  419.   }
  420.  
  421.   if (auths) {            /* got any authenticators? */
  422.     for (t = NIL; LOCAL->netstream && auths &&
  423.      (at = mail_lookup_auth (find_rightmost_bit (&auths)+1)); ) {
  424.       if (t) {            /* previous authenticator failed? */
  425.     sprintf (tmp,"Retrying using %.80s authentication after %.80s",
  426.          at->name,t);
  427.     mm_log (tmp,NIL);
  428.     fs_give ((void **) &t);
  429.       }
  430.       for (i = 1; LOCAL->netstream && i && (i <= pop3_maxlogintrials);) {
  431.     if (pop3_send (stream,"AUTH",at->name) &&
  432.         (*at->client) (pop3_challenge,pop3_response,mb,stream,&i,usr) &&
  433.         LOCAL->response && (*LOCAL->response == '+')) return LONGT;
  434.     t = cpystr (LOCAL->reply);
  435.       }
  436.     }
  437.     if (t) {            /* previous authenticator failed? */
  438.       sprintf (tmp,"Can not authenticate to POP3 server: %.80s",t);
  439.       mm_log (tmp,ERROR);
  440.       fs_give ((void **) &t);
  441.     }
  442.   }
  443.   else if (stream->secure)
  444.     mm_log ("Can't do secure authentication with this server",ERROR);
  445.   else {            /* traditional login */
  446.     for (i = 0; LOCAL->netstream && (i < pop3_maxlogintrials); ++i) {
  447.       tmp[0] = '\0';        /* prompt user for password */
  448.       mm_login (mb,usr,tmp,i);
  449.       if (tmp[0]) {        /* send login sequence */
  450.     if (pop3_send (stream,"USER",usr) && pop3_send (stream,"PASS",tmp))
  451.       return LONGT;        /* success */
  452.     mm_log (LOCAL->reply,WARN);
  453.       }
  454.       else {            /* user refused to give a password */
  455.     mm_log ("Login aborted",ERROR);
  456.     return NIL;
  457.       }
  458.     }
  459.     mm_log ("Too many login failures",ERROR);
  460.   }
  461.   return NIL;            /* ran out of authenticators */
  462. }
  463.  
  464. /* Get challenge to authenticator in binary
  465.  * Accepts: stream
  466.  *        pointer to returned size
  467.  * Returns: challenge or NIL if not challenge
  468.  */
  469.  
  470. void *pop3_challenge (void *s,unsigned long *len)
  471. {
  472.   MAILSTREAM *stream = (MAILSTREAM *) s;
  473.   return ((*LOCAL->response == '+') && (LOCAL->response[1] == ' ')) ?
  474.     rfc822_base64 ((unsigned char *) LOCAL->reply,strlen (LOCAL->reply),len) :
  475.       NIL;
  476. }
  477.  
  478.  
  479. /* Send authenticator response in BASE64
  480.  * Accepts: MAIL stream
  481.  *        string to send
  482.  *        length of string
  483.  * Returns: T if successful, else NIL
  484.  */
  485.  
  486. long pop3_response (void *s,char *response,unsigned long size)
  487. {
  488.   MAILSTREAM *stream = (MAILSTREAM *) s;
  489.   unsigned long i,j,ret;
  490.   char *t,*u;
  491.   if (response) {        /* make CRLFless BASE64 string */
  492.     if (size) {
  493.       for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
  494.        j < i; j++) if (t[j] > ' ') *u++ = t[j];
  495.       *u = '\0';        /* tie off string for mm_dlog() */
  496.       if (stream->debug) mm_dlog (t);
  497.                 /* append CRLF */
  498.       *u++ = '\015'; *u++ = '\012'; *u = '\0';
  499.       ret = net_sout (LOCAL->netstream,t,u - t);
  500.       fs_give ((void **) &t);
  501.     }
  502.     else ret = net_sout (LOCAL->netstream,"\015\012",2);
  503.   }
  504.                 /* abort requested */
  505.   else ret = net_sout (LOCAL->netstream,"*\015\012",3);
  506.   pop3_reply (stream);        /* set up response */
  507.   return ret;
  508. }
  509.  
  510. /* POP3 mail close
  511.  * Accepts: MAIL stream
  512.  *        option flags
  513.  */
  514.  
  515. void pop3_close (MAILSTREAM *stream,long options)
  516. {
  517.   int silent = stream->silent;
  518.   if (LOCAL) {            /* only if a file is open */
  519.     if (LOCAL->netstream) {    /* close POP3 connection */
  520.       stream->silent = T;
  521.       if (options & CL_EXPUNGE) pop3_expunge (stream);
  522.       stream->silent = silent;
  523.       pop3_send (stream,"QUIT",NIL);
  524.       mm_notify (stream,LOCAL->reply,BYE);
  525.     }
  526.                 /* close POP3 connection */
  527.     if (LOCAL->netstream) net_close (LOCAL->netstream);
  528.     if (LOCAL->txt) fclose (LOCAL->txt);
  529.     LOCAL->txt = NIL;
  530.     if (LOCAL->response) fs_give ((void **) &LOCAL->response);
  531.                 /* nuke the local data */
  532.     fs_give ((void **) &stream->local);
  533.     stream->dtb = NIL;        /* log out the DTB */
  534.   }
  535. }
  536.  
  537. /* POP3 mail fetch fast information
  538.  * Accepts: MAIL stream
  539.  *        sequence
  540.  *        option flags
  541.  * This is ugly and slow
  542.  */
  543.  
  544. void pop3_fetchfast (MAILSTREAM *stream,char *sequence,long flags)
  545. {
  546.   unsigned long i;
  547.   MESSAGECACHE *elt;
  548.                 /* get sequence */
  549.   if (stream && LOCAL && ((flags & FT_UID) ?
  550.               mail_uid_sequence (stream,sequence) :
  551.               mail_sequence (stream,sequence)))
  552.     for (i = 1; i <= stream->nmsgs; i++)
  553.       if ((elt = mail_elt (stream,i))->sequence &&
  554.       !(elt->day && !elt->rfc822_size)) {
  555.     ENVELOPE **env = NIL;
  556.     ENVELOPE *e = NIL;
  557.     if (!stream->scache) env = &elt->private.msg.env;
  558.     else if (stream->msgno == i) env = &stream->env;
  559.     else env = &e;
  560.     if (!*env || !elt->rfc822_size) {
  561.       STRING bs;
  562.       unsigned long hs;
  563.       char *ht = (*stream->dtb->header) (stream,i,&hs,NIL);
  564.                 /* need to make an envelope? */
  565.       if (!*env) rfc822_parse_msg (env,NIL,ht,hs,NIL,BADHOST,
  566.                        stream->dtb->flags);
  567.                 /* need message size too, ugh */
  568.       if (!elt->rfc822_size) {
  569.         (*stream->dtb->text) (stream,i,&bs,FT_PEEK);
  570.         elt->rfc822_size = hs + SIZE (&bs) - GETPOS (&bs);
  571.       }
  572.     }
  573.                 /* if need date, have date in envelope? */
  574.     if (!elt->day && *env && (*env)->date)
  575.       mail_parse_date (elt,(*env)->date);
  576.                 /* sigh, fill in bogus default */
  577.     if (!elt->day) mail_parse_date (elt,"01-JAN-1969 00:00:00 +0000");
  578.     mail_free_envelope (&e);
  579.       }
  580. }
  581.  
  582. /* POP3 fetch header as text
  583.  * Accepts: mail stream
  584.  *        message number
  585.  *        pointer to return size
  586.  *        flags
  587.  * Returns: header text
  588.  */
  589.  
  590. char *pop3_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *size,
  591.            long flags)
  592. {
  593.   MESSAGECACHE *elt;
  594.   if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return NIL;
  595.                 /* have header text? */
  596.   if (!(elt = mail_elt (stream,msgno))->private.msg.header.text.data) {
  597.     elt->private.msg.header.text.size = pop3_cache (stream,elt);
  598.                 /* read the header */
  599.     fread (elt->private.msg.header.text.data = (unsigned char *)
  600.        fs_get ((size_t) elt->private.msg.header.text.size + 1),
  601.        (size_t) 1,(size_t) elt->private.msg.header.text.size,LOCAL->txt);
  602.     elt->private.msg.header.text.data[elt->private.msg.header.text.size] ='\0';
  603.   }
  604.                 /* return size of text */
  605.   if (size) *size = elt->private.msg.header.text.size;
  606.   return (char *) elt->private.msg.header.text.data;
  607. }
  608.  
  609. /* POP3 fetch body
  610.  * Accepts: mail stream
  611.  *        message number
  612.  *        pointer to stringstruct to initialize
  613.  *        flags
  614.  * Returns: T if successful, else NIL
  615.  */
  616.  
  617. long pop3_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
  618. {
  619.   MESSAGECACHE *elt;
  620.   INIT (bs,mail_string,(void *) "",0);
  621.   if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return NIL;
  622.   elt = mail_elt (stream,msgno);
  623.   pop3_cache (stream,elt);    /* make sure cache loaded */
  624.   if (!LOCAL->txt) return NIL;    /* error if don't have a file */
  625.   if (!(flags & FT_PEEK)) {    /* mark seen if needed */
  626.     elt->seen = T;
  627.     mm_flags (stream,elt->msgno);
  628.   }
  629.   INIT (bs,file_string,(void *) LOCAL->txt,elt->rfc822_size);
  630.   SETPOS (bs,LOCAL->hdrsize);    /* skip past header */
  631.   return T;
  632. }
  633.  
  634. /* POP3 cache message
  635.  * Accepts: mail stream
  636.  *        message number
  637.  * Returns: header size
  638.  */
  639.  
  640. unsigned long pop3_cache (MAILSTREAM *stream,MESSAGECACHE *elt)
  641. {
  642.                 /* already cached? */
  643.   if (LOCAL->msgno != elt->msgno) {
  644.                 /* no, close current file */
  645.     if (LOCAL->txt) fclose (LOCAL->txt);
  646.     LOCAL->txt = NIL;
  647.     LOCAL->msgno = LOCAL->hdrsize = 0;
  648.     if (pop3_send_num (stream,"RETR",elt->msgno)) {
  649.       LOCAL->msgno = elt->msgno;/* set as current message number */
  650.                 /* load the cache */
  651.       LOCAL->txt = netmsg_slurp (LOCAL->netstream,&elt->rfc822_size,
  652.                  &LOCAL->hdrsize);
  653.     }
  654.     else elt->deleted = T;
  655.   }
  656.   return LOCAL->hdrsize;
  657. }
  658.  
  659. /* POP3 mail ping mailbox
  660.  * Accepts: MAIL stream
  661.  * Returns: T if stream alive, else NIL
  662.  */
  663.  
  664. long pop3_ping (MAILSTREAM *stream)
  665. {
  666.   return pop3_send (stream,"NOOP",NIL);
  667. }
  668.  
  669.  
  670. /* POP3 mail check mailbox
  671.  * Accepts: MAIL stream
  672.  */
  673.  
  674. void pop3_check (MAILSTREAM *stream)
  675. {
  676.   if (pop3_ping (stream)) mm_log ("Check completed",NIL);
  677. }
  678.  
  679.  
  680. /* POP3 mail expunge mailbox
  681.  * Accepts: MAIL stream
  682.  */
  683.  
  684. void pop3_expunge (MAILSTREAM *stream)
  685. {
  686.   char tmp[MAILTMPLEN];
  687.   unsigned long i = 1,n = 0;
  688.   while (i <= stream->nmsgs) {
  689.     if (mail_elt (stream,i)->deleted && pop3_send_num (stream,"DELE",i)) {
  690.       mail_expunged (stream,i);
  691.       n++;
  692.     }
  693.     else i++;            /* try next message */
  694.   }
  695.   if (!stream->silent) {    /* only if not silent */
  696.     if (n) {            /* did we expunge anything? */
  697.       sprintf (tmp,"Expunged %ld messages",n);
  698.       mm_log (tmp,(long) NIL);
  699.     }
  700.     else mm_log ("No messages deleted, so no update needed",(long) NIL);
  701.   }
  702. }
  703.  
  704. /* POP3 mail copy message(s)
  705.  * Accepts: MAIL stream
  706.  *        sequence
  707.  *        destination mailbox
  708.  *        option flags
  709.  * Returns: T if copy successful, else NIL
  710.  */
  711.  
  712. long pop3_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  713. {
  714.   mailproxycopy_t pc =
  715.     (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
  716.   if (pc) return (*pc) (stream,sequence,mailbox,options);
  717.   mm_log ("Copy not valid for POP3",ERROR);
  718.   return NIL;
  719. }
  720.  
  721.  
  722. /* POP3 mail append message from stringstruct
  723.  * Accepts: MAIL stream
  724.  *        destination mailbox
  725.  *        stringstruct of messages to append
  726.  * Returns: T if append successful, else NIL
  727.  */
  728.  
  729. long pop3_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  730.           STRING *message)
  731. {
  732.   mm_log ("Append not valid for POP3",ERROR);
  733.   return NIL;
  734. }
  735.  
  736. /* Internal routines */
  737.  
  738.  
  739. /* Post Office Protocol 3 send command with number argument
  740.  * Accepts: MAIL stream
  741.  *        command
  742.  *        number
  743.  * Returns: T if successful, NIL if failure
  744.  */
  745.  
  746. long pop3_send_num (MAILSTREAM *stream,char *command,unsigned long n)
  747. {
  748.   char tmp[MAILTMPLEN];
  749.   sprintf (tmp,"%lu",mail_uid (stream,n));
  750.   return pop3_send (stream,command,tmp);
  751. }
  752.  
  753.  
  754. /* Post Office Protocol 3 send command
  755.  * Accepts: MAIL stream
  756.  *        command
  757.  *        command argument
  758.  * Returns: T if successful, NIL if failure
  759.  */
  760.  
  761. long pop3_send (MAILSTREAM *stream,char *command,char *args)
  762. {
  763.   char tmp[8*MAILTMPLEN];
  764.   long ret;
  765.   mail_lock (stream);        /* lock up the stream */
  766.   if (!LOCAL->netstream) ret = pop3_fake (stream,"No-op dead stream");
  767.   else {            /* build the complete command */
  768.     if (args) sprintf (tmp,"%s %s",command,args);
  769.     else strcpy (tmp,command);
  770.     if (stream->debug) mm_dlog (tmp);
  771.     strcat (tmp,"\015\012");
  772.                 /* send the command */
  773.     ret = net_soutr (LOCAL->netstream,tmp) ? pop3_reply (stream) :
  774.       pop3_fake (stream,"POP3 connection broken in command");
  775.   }
  776.   mail_unlock (stream);        /* unlock stream */
  777.   return ret;
  778. }
  779.  
  780. /* Post Office Protocol 3 get reply
  781.  * Accepts: MAIL stream
  782.  * Returns: T if success reply, NIL if error reply
  783.  */
  784.  
  785. long pop3_reply (MAILSTREAM *stream)
  786. {
  787.   char *s;
  788.                 /* flush old reply */
  789.   if (LOCAL->response) fs_give ((void **) &LOCAL->response);
  790.                   /* get reply */
  791.   if (!(LOCAL->response = net_getline (LOCAL->netstream)))
  792.     return pop3_fake (stream,"POP3 connection broken in response");
  793.   if (stream->debug) mm_dlog (LOCAL->response);
  794.   LOCAL->reply = (s = strchr (LOCAL->response,' ')) ? s + 1 : LOCAL->response;
  795.                 /* return success or failure */
  796.   return (*LOCAL->response =='+') ? T : NIL;
  797. }
  798.  
  799.  
  800. /* Post Office Protocol 3 set fake error
  801.  * Accepts: MAIL stream
  802.  *        error text
  803.  * Returns: NIL, always
  804.  */
  805.  
  806. long pop3_fake (MAILSTREAM *stream,char *text)
  807. {
  808.   mm_notify (stream,text,BYE);    /* send bye alert */
  809.   if (LOCAL->netstream) net_close (LOCAL->netstream);
  810.   LOCAL->netstream = NIL;    /* farewell, dear TCP stream */
  811.                 /* flush any old reply */
  812.   if (LOCAL->response) fs_give ((void **) &LOCAL->response);
  813.   LOCAL->reply = text;        /* set up pseudo-reply string */
  814.   return NIL;            /* return error code */
  815. }
  816.