home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / pine / c-client / mbox.c < prev    next >
C/C++ Source or Header  |  1994-01-09  |  7KB  |  224 lines

  1. /*
  2.  * Program:    MBOX 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:    10 March 1992
  13.  * Last Edited:    28 September 1993
  14.  *
  15.  * Copyright 1993 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 <netdb.h>
  39. #include <errno.h>
  40. extern int errno;        /* just in case */
  41. #include "mail.h"
  42. #include "osdep.h"
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include "mbox.h"
  47. #include "bezerk.h"
  48. #include "misc.h"
  49.  
  50. /* MBOX mail routines */
  51.  
  52.  
  53. /* Driver dispatch used by MAIL */
  54.  
  55. DRIVER mboxdriver = {
  56.   "mbox",            /* driver name */
  57.   (DRIVER *) NIL,        /* next driver */
  58.   mbox_valid,            /* mailbox is valid for us */
  59.   bezerk_parameters,        /* manipulate parameters */
  60.   bezerk_find,            /* find mailboxes */
  61.   bezerk_find_bboards,        /* find bboards */
  62.   bezerk_find_all,        /* find all mailboxes */
  63.   bezerk_find_all_bboards,    /* find all bboards */
  64.   bezerk_subscribe,        /* subscribe to mailbox */
  65.   bezerk_unsubscribe,        /* unsubscribe from mailbox */
  66.   bezerk_subscribe_bboard,    /* subscribe to bboard */
  67.   bezerk_unsubscribe_bboard,    /* unsubscribe from bboard */
  68.   bezerk_create,        /* create mailbox */
  69.   bezerk_delete,        /* delete mailbox */
  70.   bezerk_rename,        /* rename mailbox */
  71.   mbox_open,            /* open mailbox */
  72.   bezerk_close,            /* close mailbox */
  73.   bezerk_fetchfast,        /* fetch message "fast" attributes */
  74.   bezerk_fetchflags,        /* fetch message flags */
  75.   bezerk_fetchstructure,    /* fetch message structure */
  76.   bezerk_fetchheader,        /* fetch message header only */
  77.   bezerk_fetchtext,        /* fetch message body only */
  78.   bezerk_fetchbody,        /* fetch message body section */
  79.   bezerk_setflag,        /* set message flag */
  80.   bezerk_clearflag,        /* clear message flag */
  81.   bezerk_search,        /* search for message based on criteria */
  82.   mbox_ping,            /* ping mailbox to see if still alive */
  83.   mbox_check,            /* check for new messages */
  84.   mbox_expunge,            /* expunge deleted messages */
  85.   bezerk_copy,            /* copy messages to another mailbox */
  86.   bezerk_move,            /* move messages to another mailbox */
  87.   bezerk_append,        /* append string message to mailbox */
  88.   bezerk_gc            /* garbage collect stream */
  89. };
  90.  
  91.                 /* prototype stream */
  92. MAILSTREAM mboxproto = {&mboxdriver};
  93.  
  94. /* MBOX mail validate mailbox
  95.  * Accepts: mailbox name
  96.  * Returns: our driver if name is valid, NIL otherwise
  97.  */
  98.  
  99. DRIVER *mbox_valid (name)
  100.     char *name;
  101. {
  102.   int fd;
  103.   char s[MAILTMPLEN];
  104.   char *t;
  105.   int ti,zn;
  106.   int ret = NIL;
  107.   struct stat sbuf;
  108.                 /* only consider INBOX */
  109.   if (!strcmp (ucase (strcpy (s,name)),"INBOX")) {
  110.                 /* make what the file name would be */
  111.     sprintf (s,"%s/mbox",myhomedir ());
  112.                 /* file exist? */
  113.     if ((stat(s,&sbuf) == 0) && (fd = open (s,O_RDONLY,NIL)) >= 0) {
  114.                 /* allow empty or valid file */
  115.       if ((sbuf.st_size == 0) || ((read (fd,s,MAILTMPLEN-1) >= 0) &&
  116.                   (*s == 'F') && VALID (s,t,ti,zn))) ret = T;
  117.       close (fd);        /* close the file */
  118.  
  119.     }
  120.   }
  121.   return ret ? &mboxdriver : NIL;
  122. }
  123.  
  124. /* MBOX mail open
  125.  * Accepts: stream to open
  126.  * Returns: stream on success, NIL on failure
  127.  */
  128.  
  129. MAILSTREAM *mbox_open (stream)
  130.     MAILSTREAM *stream;
  131. {
  132.   unsigned long i = 1;
  133.   unsigned long recent = 0;
  134.   char tmp[MAILTMPLEN];
  135.                 /* return prototype for OP_PROTOTYPE call */
  136.   if (!stream) return &mboxproto;
  137.                 /* change mailbox file name */
  138.   sprintf (tmp,"%s/mbox",myhomedir ());
  139.   fs_give ((void **) &stream->mailbox);
  140.   stream->mailbox = cpystr (tmp);
  141.   stream->silent = T;        /* don't babble on this stream */
  142.                 /* open mailbox, snarf new mail */
  143.   if (!(bezerk_open (stream) && mbox_ping (stream))) return NIL;
  144.   stream->silent = NIL;        /* allow external events again */
  145.                 /* notify upper level of mailbox sizes */
  146.   mail_exists (stream,stream->nmsgs);
  147.   while (i <= stream->nmsgs) if (mail_elt (stream,i++)->recent) ++recent;
  148.   mail_recent (stream,recent);    /* including recent messages */
  149.   return stream;
  150. }
  151.  
  152. /* MBOX mail ping mailbox
  153.  * Accepts: MAIL stream
  154.  * Returns: T if stream alive, else NIL
  155.  * No-op for readonly files, since read/writer can expunge it from under us!
  156.  */
  157.  
  158. long mbox_ping (stream)
  159.     MAILSTREAM *stream;
  160. {
  161.   int fd,sfd;
  162.   int ti,zn;
  163.   char *s,*t;
  164.   long size;
  165.   struct stat sbuf;
  166.   char lock[MAILTMPLEN],slock[MAILTMPLEN];
  167.   if (LOCAL && !stream->readonly && !stream->lock) {
  168.     mm_critical (stream);    /* go critical */
  169.                 /* calculate name of bezerk file */
  170.     sprintf (LOCAL->buf,MAILFILE,myusername ());
  171.     if ((sfd = bezerk_lock (LOCAL->buf,O_RDWR,NIL,slock,LOCK_EX)) >= 0) {
  172.       fstat (sfd,&sbuf);    /* get size of the poop */
  173.       if (size = sbuf.st_size){ /* non-empty? */
  174.                 /* yes, read it */
  175.     read (sfd,s = (char *) fs_get (sbuf.st_size + 1),size);
  176.     s[sbuf.st_size] = '\0';    /* tie it off */
  177.     if ((*s == 'F') && VALID (s,t,ti,zn)) {
  178.       if ((fd = bezerk_lock (stream->mailbox,O_WRONLY|O_APPEND,NIL,lock,
  179.                  LOCK_EX)) >= 0) {
  180.         fstat (fd,&sbuf);    /* get current file size before write*/
  181.         if (write (fd,s,size) >= 0) ftruncate (sfd,0);
  182.         else {        /* failed */
  183.           sprintf (LOCAL->buf,"New mail copy failed: %s",strerror (errno));
  184.           mm_log (LOCAL->buf,ERROR);
  185.           ftruncate (fd,sbuf.st_size);
  186.         }
  187.         fsync (fd);        /* force out the update */
  188.         bezerk_unlock (fd,NIL,lock);
  189.       }
  190.     }
  191.     else mm_log ("Invalid data in /usr/spool/mail file",ERROR);
  192.     fs_give ((void **) &s);    /* flush the poop now */
  193.       }
  194.                 /* all done with update */
  195.       bezerk_unlock (sfd,NIL,slock);
  196.     }
  197.     mm_nocritical (stream);    /* release critical */
  198.   }
  199.   return bezerk_ping (stream);    /* do the bezerk routine now */
  200. }
  201.  
  202. /* MBOX mail check mailbox
  203.  * Accepts: MAIL stream
  204.  */
  205.  
  206. void mbox_check (stream)
  207.     MAILSTREAM *stream;
  208. {
  209.                 /* do local ping, then do bezerk routine */
  210.   if (mbox_ping (stream)) bezerk_check (stream);
  211. }
  212.  
  213.  
  214. /* MBOX mail expunge mailbox
  215.  * Accepts: MAIL stream
  216.  */
  217.  
  218. void mbox_expunge (stream)
  219.     MAILSTREAM *stream;
  220. {
  221.   bezerk_expunge (stream);    /* do expunge */
  222.   mbox_ping (stream);        /* do local ping */
  223. }
  224.