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 / osdep / amiga / dummy.c < prev    next >
C/C++ Source or Header  |  1998-11-13  |  21KB  |  678 lines

  1. /*
  2.  * Program:    Dummy 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:    9 May 1991
  13.  * Last Edited:    13 November 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 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.  
  37. #include <stdio.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. extern int errno;        /* just in case */
  41. #include "mail.h"
  42. #include "osdep.h"
  43. #include <pwd.h>
  44. #include <sys/stat.h>
  45. #include "dummy.h"
  46. #include "misc.h"
  47. #include "mx.h"            /* highly unfortunate */
  48.  
  49. /* Dummy routines */
  50.  
  51.  
  52. /* Driver dispatch used by MAIL */
  53.  
  54. DRIVER dummydriver = {
  55.   "dummy",            /* driver name */
  56.   DR_LOCAL|DR_MAIL,        /* driver flags */
  57.   (DRIVER *) NIL,        /* next driver */
  58.   dummy_valid,            /* mailbox is valid for us */
  59.   dummy_parameters,        /* manipulate parameters */
  60.   dummy_scan,            /* scan mailboxes */
  61.   dummy_list,            /* list mailboxes */
  62.   dummy_lsub,            /* list subscribed mailboxes */
  63.   dummy_subscribe,        /* subscribe to mailbox */
  64.   NIL,                /* unsubscribe from mailbox */
  65.   dummy_create,            /* create mailbox */
  66.   dummy_delete,            /* delete mailbox */
  67.   dummy_rename,            /* rename mailbox */
  68.   NIL,                /* status of mailbox */
  69.   dummy_open,            /* open mailbox */
  70.   dummy_close,            /* close mailbox */
  71.   NIL,                /* fetch message "fast" attributes */
  72.   NIL,                /* fetch message flags */
  73.   NIL,                /* fetch overview */
  74.   NIL,                /* fetch message structure */
  75.   NIL,                /* fetch header */
  76.   NIL,                /* fetch text */
  77.   NIL,                /* fetch message data */
  78.   NIL,                /* unique identifier */
  79.   NIL,                /* message number from UID */
  80.   NIL,                /* modify flags */
  81.   NIL,                /* per-message modify flags */
  82.   NIL,                /* search for message based on criteria */
  83.   NIL,                /* sort messages */
  84.   NIL,                /* thread messages */
  85.   dummy_ping,            /* ping mailbox to see if still alive */
  86.   dummy_check,            /* check for new messages */
  87.   dummy_expunge,        /* expunge deleted messages */
  88.   dummy_copy,            /* copy messages to another mailbox */
  89.   dummy_append,            /* append string message to mailbox */
  90.   NIL                /* garbage collect stream */
  91. };
  92.  
  93.                 /* prototype stream */
  94. MAILSTREAM dummyproto = {&dummydriver};
  95.  
  96. /* Dummy validate mailbox
  97.  * Accepts: mailbox name
  98.  * Returns: our driver if name is valid, NIL otherwise
  99.  */
  100.  
  101. DRIVER *dummy_valid (char *name)
  102. {
  103.   char *s,tmp[MAILTMPLEN];
  104.   struct stat sbuf;
  105.                 /* must be valid local mailbox */
  106.   if (name && *name && (*name != '{') && (s = mailboxfile (tmp,name))) {
  107.                 /* indeterminate clearbox INBOX */
  108.     if (!*s) return &dummydriver;
  109.     else if (!stat (s,&sbuf)) switch (sbuf.st_mode & S_IFMT) {
  110.     case S_IFREG:
  111.     case S_IFDIR:
  112.       return &dummydriver;
  113.     }
  114.     else if (((name[0] == 'I') || (name[0] == 'i')) &&
  115.          ((name[1] == 'N') || (name[1] == 'n')) &&
  116.          ((name[2] == 'B') || (name[2] == 'b')) &&
  117.          ((name[3] == 'O') || (name[3] == 'o')) &&
  118.          ((name[4] == 'X') || (name[4] == 'x')) && !name[5])
  119.       return &dummydriver;    /* blackbox INBOX does not exist yet */
  120.   }
  121.   return NIL;
  122. }
  123.  
  124.  
  125. /* Dummy manipulate driver parameters
  126.  * Accepts: function code
  127.  *        function-dependent value
  128.  * Returns: function-dependent return value
  129.  */
  130.  
  131. void *dummy_parameters (long function,void *value)
  132. {
  133.   return NIL;
  134. }
  135.  
  136. /* Dummy scan mailboxes
  137.  * Accepts: mail stream
  138.  *        reference
  139.  *        pattern to search
  140.  *        string to scan
  141.  */
  142.  
  143. void dummy_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
  144. {
  145.   char *s,test[MAILTMPLEN],file[MAILTMPLEN];
  146.   long i;
  147.   if (!pat || !*pat) {        /* empty pattern? */
  148.     if (dummy_canonicalize (test,ref,"*")) {
  149.                 /* tie off name at root */
  150.       if (s = strchr (test,'/')) *++s = '\0';
  151.       else test[0] = '\0';
  152.       dummy_listed (stream,'/',test,LATT_NOSELECT,NIL);
  153.     }
  154.   }
  155.                 /* get canonical form of name */
  156.   else if (dummy_canonicalize (test,ref,pat)) {
  157.                 /* found any wildcards? */
  158.     if (s = strpbrk (test,"%*")) {
  159.                 /* yes, copy name up to that point */
  160.       strncpy (file,test,i = s - test);
  161.       file[i] = '\0';        /* tie off */
  162.     }
  163.     else strcpy (file,test);    /* use just that name then */
  164.     if (s = strrchr (file,'/')){/* find directory name */
  165.       *++s = '\0';        /* found, tie off at that point */
  166.       s = file;
  167.     }
  168.                 /* silly case */
  169.     else if ((file[0] == '~') || (file[0] == '#')) s = file;
  170.                 /* do the work */
  171.     dummy_list_work (stream,s,test,contents,0);
  172.                 /* always an INBOX */
  173.     if (pmatch ("INBOX",ucase (test)))
  174.       dummy_listed (stream,NIL,"INBOX",LATT_NOINFERIORS,contents);
  175.   }
  176. }
  177.  
  178.  
  179. /* Dummy list mailboxes
  180.  * Accepts: mail stream
  181.  *        reference
  182.  *        pattern to search
  183.  */
  184.  
  185. void dummy_list (MAILSTREAM *stream,char *ref,char *pat)
  186. {
  187.   dummy_scan (stream,ref,pat,NIL);
  188. }
  189.  
  190. /* Dummy list subscribed mailboxes
  191.  * Accepts: mail stream
  192.  *        reference
  193.  *        pattern to search
  194.  */
  195.  
  196. void dummy_lsub (MAILSTREAM *stream,char *ref,char *pat)
  197. {
  198.   void *sdb = NIL;
  199.   char *s,*t,test[MAILTMPLEN],tmp[MAILTMPLEN];
  200.   int showuppers = pat[strlen (pat) - 1] == '%';
  201.                 /* get canonical form of name */
  202.   if (dummy_canonicalize (test,ref,pat) && (s = sm_read (&sdb))) do
  203.     if (*s != '{') {
  204.       if (((s[0] == 'I') || (s[0] == 'i')) &&
  205.       ((s[1] == 'N') || (s[1] == 'n')) &&
  206.       ((s[2] == 'B') || (s[2] == 'b')) &&
  207.       ((s[3] == 'O') || (s[3] == 'o')) &&
  208.       ((s[4] == 'X') || (s[4] == 'x')) && !s[5] &&
  209.       pmatch ("INBOX",ucase (strcpy (tmp,test))))
  210.     mm_lsub (stream,NIL,s,LATT_NOINFERIORS);
  211.       else if (pmatch_full (s,test,'/')) mm_lsub (stream,'/',s,NIL);
  212.       else while (showuppers && (t = strrchr (s,'/'))) {
  213.     *t = '\0';        /* tie off the name */
  214.     if (pmatch_full (s,test,'/')) mm_lsub (stream,'/',s,LATT_NOSELECT);
  215.       }
  216.     }
  217.   while (s = sm_read (&sdb));    /* until no more subscriptions */
  218. }
  219.  
  220.  
  221. /* Dummy subscribe to mailbox
  222.  * Accepts: mail stream
  223.  *        mailbox to add to subscription list
  224.  * Returns: T on success, NIL on failure
  225.  */
  226.  
  227. long dummy_subscribe (MAILSTREAM *stream,char *mailbox)
  228. {
  229.   char *s,tmp[MAILTMPLEN];
  230.   struct stat sbuf;
  231.                 /* must be valid local mailbox */
  232.   if ((s = mailboxfile (tmp,mailbox)) && *s && !stat (s,&sbuf)
  233. #if 0    /* disable this temporarily for Netscape */
  234.       &&
  235.       ((sbuf.st_mode & S_IFMT) == S_IFREG)
  236. #endif
  237.       ) return sm_subscribe (mailbox);
  238.   sprintf (tmp,"Can't subscribe %s: not a mailbox",mailbox);
  239.   mm_log (tmp,ERROR);
  240.   return NIL;
  241. }
  242.  
  243. /* Dummy list mailboxes worker routine
  244.  * Accepts: mail stream
  245.  *        directory name to search
  246.  *        search pattern
  247.  *        string to scan
  248.  *        search level
  249.  */
  250.  
  251. void dummy_list_work (MAILSTREAM *stream,char *dir,char *pat,char *contents,
  252.               long level)
  253. {
  254.   DIR *dp;
  255.   struct direct *d;
  256.   struct stat sbuf;
  257.   char tmp[MAILTMPLEN];
  258.                 /* punt if bogus name */
  259.   if (!mailboxdir (tmp,dir,NIL)) return;
  260.   if (dp = opendir (tmp)) {    /* do nothing if can't open directory */
  261.                 /* list it if not at top-level */
  262.     if (!level && dir && pmatch_full (dir,pat,'/'))
  263.       dummy_listed (stream,'/',dir,LATT_NOSELECT,contents);
  264.                 /* scan directory, ignore . and .. */
  265.     if (!dir || dir[strlen (dir) - 1] == '/') while (d = readdir (dp))
  266.       if ((d->d_name[0] != '.') ||
  267.       (d->d_name[1] && (((d->d_name[1] != '.') || d->d_name[2]) &&
  268.                 strcmp (d->d_name+1,MXINDEXNAME+3)))) {
  269.                 /* see if name is useful */
  270.     if (dir) sprintf (tmp,"%s%s",dir,d->d_name);
  271.     else strcpy (tmp,d->d_name);
  272.                 /* make sure useful and can get info */
  273.     if ((pmatch_full (tmp,pat,'/') ||
  274.          pmatch_full (strcat (tmp,"/"),pat,'/') || dmatch (tmp,pat,'/')) &&
  275.         mailboxdir (tmp,dir,d->d_name) && tmp[0] && !stat (tmp,&sbuf)) {
  276.                 /* now make name we'd return */
  277.       if (dir) sprintf (tmp,"%s%s",dir,d->d_name);
  278.       else strcpy (tmp,d->d_name);
  279.                 /* only interested in file type */
  280.       switch (sbuf.st_mode &= S_IFMT) {
  281.       case S_IFDIR:        /* directory? */
  282.         if (pmatch_full (tmp,pat,'/')) {
  283.           if (!dummy_listed (stream,'/',tmp,LATT_NOSELECT,contents)) break;
  284.           strcat (tmp,"/");    /* set up for dmatch call */
  285.         }
  286.                 /* try again with trailing / */
  287.         else if (pmatch_full (strcat (tmp,"/"),pat,'/') &&
  288.              !dummy_listed (stream,'/',tmp,LATT_NOSELECT,contents))
  289.           break;
  290.         if (dmatch (tmp,pat,'/') &&
  291.         (level < (long) mail_parameters (NIL,GET_LISTMAXLEVEL,NIL)))
  292.           dummy_list_work (stream,tmp,pat,contents,level+1);
  293.         break;
  294.       case S_IFREG:        /* ordinary name */
  295.         /* Must use ctime for systems that don't update mtime properly */
  296.         if (pmatch_full (tmp,pat,'/') &&
  297.         !(((tmp[0] == 'I') || (tmp[0] == 'i')) &&
  298.           ((tmp[1] == 'N') || (tmp[1] == 'n')) &&
  299.           ((tmp[2] == 'B') || (tmp[2] == 'b')) &&
  300.           ((tmp[3] == 'O') || (tmp[3] == 'o')) &&
  301.           ((tmp[4] == 'X') || (tmp[4] == 'x')) && !tmp[5]))
  302.           dummy_listed (stream,'/',tmp,LATT_NOINFERIORS +
  303.                 ((sbuf.st_size && (sbuf.st_atime<=sbuf.st_ctime)) ?
  304.                  ((sbuf.st_atime<sbuf.st_ctime) ? LATT_MARKED : 0)
  305.                  : LATT_UNMARKED),contents);
  306.         break;
  307.       }
  308.     }
  309.       }
  310.     closedir (dp);        /* all done, flush directory */
  311.   }
  312. }
  313.  
  314. /* Mailbox found
  315.  * Accepts: hierarchy delimiter
  316.  *        mailbox name
  317.  *        attributes
  318.  *        contents to search before calling mm_list()
  319.  * Returns: NIL if should abort hierarchy search, else T
  320.  */
  321.  
  322. #define BUFSIZE 4*MAILTMPLEN
  323.  
  324. long dummy_listed (MAILSTREAM *stream,char delimiter,char *name,
  325.            long attributes,char *contents)
  326. {
  327.   struct stat sbuf;
  328.   int fd;
  329.   long csiz,ssiz,bsiz;
  330.   char *buf,tmp[MAILTMPLEN];
  331.   DRIVER *d;
  332.                 /* don't \NoSelect if have a driver for it */
  333.   if ((attributes & LATT_NOSELECT) && (d = mail_valid (NIL,name,NIL)) &&
  334.       (d != &dummydriver)) attributes &= ~LATT_NOSELECT;
  335.   if (contents) {        /* want to search contents? */
  336.                 /* forget it if can't select or open */
  337.     if ((attributes & LATT_NOSELECT) || !(csiz = strlen (contents)) ||
  338.     stat (dummy_file (tmp,name),&sbuf) || (csiz > sbuf.st_size) ||
  339.     ((fd = open (tmp,O_RDONLY,NIL)) < 0)) return T;
  340.                 /* get buffer including slop */    
  341.     buf = (char *) fs_get (BUFSIZE + (ssiz = 4 * ((csiz / 4) + 1)) + 1);
  342.     memset (buf,'\0',ssiz);    /* no slop area the first time */
  343.     while (sbuf.st_size) {    /* until end of file */
  344.       read (fd,buf+ssiz,bsiz = min (sbuf.st_size,BUFSIZE));
  345.       if (search ((unsigned char *) buf,bsiz+ssiz,
  346.           (unsigned char *) contents,csiz)) break;
  347.       memcpy (buf,buf+BUFSIZE,ssiz);
  348.       sbuf.st_size -= bsiz;    /* note that we read that much */
  349.     }
  350.     fs_give ((void **) &buf);    /* flush buffer */
  351.     close (fd);            /* finished with file */
  352.     if (!sbuf.st_size) return T;/* not found */
  353.   }
  354.                 /* notify main program */
  355.   mm_list (stream,delimiter,name,attributes);
  356.   return T;
  357. }
  358.  
  359. /* Dummy create mailbox
  360.  * Accepts: mail stream
  361.  *        mailbox name to create
  362.  * Returns: T on success, NIL on failure
  363.  */
  364.  
  365. long dummy_create (MAILSTREAM *stream,char *mailbox)
  366. {
  367.   char tmp[MAILTMPLEN];
  368.   if (strcmp (ucase (strcpy (tmp,mailbox)),"INBOX") && dummy_file(tmp,mailbox))
  369.     return dummy_create_path (stream,tmp) && set_mbx_protections (mailbox,tmp);
  370.   sprintf (tmp,"Can't create %s: invalid name",mailbox);
  371.   mm_log (tmp,ERROR);
  372.   return NIL;
  373. }
  374.  
  375.  
  376. /* Dummy create path
  377.  * Accepts: mail stream
  378.  *        path name name to create
  379.  * Returns: T on success, NIL on failure
  380.  */
  381.  
  382. long dummy_create_path (MAILSTREAM *stream,char *path)
  383. {
  384.   struct stat sbuf;
  385.   char c,*s,tmp[MAILTMPLEN];
  386.   int fd;
  387.   long ret = NIL;
  388.   char *t = strrchr (path,'/');
  389.   int wantdir = t && !t[1];
  390.   if (wantdir) *t = '\0';    /* flush trailing delimiter for directory */
  391.   if (s = strrchr (path,'/')) {    /* found superior to this name? */
  392.     c = *++s;            /* remember first character of inferior */
  393.     *s = '\0';            /* tie off to get just superior */
  394.                 /* name doesn't exist, create it */
  395.     if ((stat (path,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
  396.     !dummy_create_path (stream,path)) return NIL;
  397.     *s = c;            /* restore full name */
  398.   }
  399.   if (wantdir) {        /* want to create directory? */
  400.     ret = !mkdir (path,(int) mail_parameters (NIL,GET_DIRPROTECTION,NIL));
  401.     *t = '/';            /* restore directory delimiter */
  402.   }
  403.                 /* create file */
  404.   else if ((fd = open (path,O_WRONLY|O_CREAT|O_EXCL,
  405.                (int) mail_parameters(NIL,GET_MBXPROTECTION,NIL))) >= 0)
  406.     ret = !close (fd);
  407.   if (!ret) {            /* error? */
  408.     sprintf (tmp,"Can't create mailbox node %s: %s",path,strerror (errno));
  409.     mm_log (tmp,ERROR);
  410.   }
  411.   return ret;            /* return status */
  412. }
  413.  
  414. /* Dummy delete mailbox
  415.  * Accepts: mail stream
  416.  *        mailbox name to delete
  417.  * Returns: T on success, NIL on failure
  418.  */
  419.  
  420. long dummy_delete (MAILSTREAM *stream,char *mailbox)
  421. {
  422.   struct stat sbuf;
  423.   char *s,tmp[MAILTMPLEN];
  424.                 /* no trailing / (workaround BSD kernel bug) */
  425.   if ((s = strrchr (dummy_file (tmp,mailbox),'/')) && !s[1]) *s = '\0';
  426.   if (stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) == S_IFDIR) ?
  427.       rmdir (tmp) : unlink (tmp)) {
  428.     sprintf (tmp,"Can't delete mailbox %s: %s",mailbox,strerror (errno));
  429.     mm_log (tmp,ERROR);
  430.     return NIL;
  431.   }
  432.   return T;            /* return success */
  433. }
  434.  
  435. /* Mail rename mailbox
  436.  * Accepts: mail stream
  437.  *        old mailbox name
  438.  *        new mailbox name
  439.  * Returns: T on success, NIL on failure
  440.  */
  441.  
  442. long dummy_rename (MAILSTREAM *stream,char *old,char *newname)
  443. {
  444.   struct stat sbuf;
  445.   char c,*s,tmp[MAILTMPLEN],mbx[MAILTMPLEN];
  446.   long ret = NIL;
  447.                 /* no trailing / allowed */
  448.   if (!(s = dummy_file (mbx,newname)) || ((s = strrchr (s,'/')) && !s[1])) {
  449.     sprintf (mbx,"Can't rename %s to %s: invalid name",old,newname);
  450.     mm_log (mbx,ERROR);
  451.     return NIL;
  452.   }
  453.   if (s) {            /* found superior to destination name? */
  454.     c = *++s;            /* remember first character of inferior */
  455.     *s = '\0';            /* tie off to get just superior */
  456.                 /* name doesn't exist, create it */
  457.     if ((stat (mbx,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
  458.     !dummy_create (stream,mbx)) return NIL;
  459.     *s = c;            /* restore full name */
  460.   }
  461.                 /* rename of non-ex INBOX creates dest */
  462.   if (!strcmp (ucase (strcpy (tmp,old)),"INBOX") &&
  463.       stat (dummy_file (tmp,old),&sbuf)) return dummy_create (NIL,mbx);
  464.   if (rename (dummy_file (tmp,old),mbx)) {
  465.     sprintf (tmp,"Can't rename mailbox %s to %s: %s",old,newname,
  466.          strerror (errno));
  467.     mm_log (tmp,ERROR);
  468.     return NIL;
  469.   }
  470.   return T;            /* return success */
  471. }
  472.  
  473. /* Dummy open
  474.  * Accepts: stream to open
  475.  * Returns: stream on success, NIL on failure
  476.  */
  477.  
  478. MAILSTREAM *dummy_open (MAILSTREAM *stream)
  479. {
  480.   int fd;
  481.   char err[MAILTMPLEN],tmp[MAILTMPLEN];
  482.   struct stat sbuf;
  483.                 /* OP_PROTOTYPE call */
  484.   if (!stream) return &dummyproto;
  485.   err[0] = '\0';        /* no error message yet */
  486.                 /* can we open the file? */
  487.   if ((fd = open (dummy_file (tmp,stream->mailbox),O_RDONLY,NIL)) < 0) {
  488.                 /* no, error unless INBOX */
  489.     if (strcmp (ucase (strcpy (tmp,stream->mailbox)),"INBOX"))
  490.       sprintf (err,"%s: %s",strerror (errno),stream->mailbox);
  491.   }
  492.   else {            /* file had better be empty then */
  493.     fstat (fd,&sbuf);        /* sniff at its size */
  494.     close (fd);
  495.     if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  496.       sprintf (err,"Can't open %s: not a selectable mailbox",stream->mailbox);
  497.     else if (sbuf.st_size)    /* bogus format if non-empty */
  498.       sprintf (err,"Can't open %s (file %s): not in valid mailbox format",
  499.            stream->mailbox,tmp);
  500.   }
  501.   if (err[0]) {            /* if an error happened */
  502.     mm_log (err,stream->silent ? WARN : ERROR);
  503.     return NIL;
  504.   }
  505.   else if (!stream->silent) {    /* only if silence not requested */
  506.     mail_exists (stream,0);    /* say there are 0 messages */
  507.     mail_recent (stream,0);    /* and certainly no recent ones! */
  508.     stream->uid_validity = 1;
  509.   }
  510.   return stream;        /* return success */
  511. }
  512.  
  513.  
  514. /* Dummy close
  515.  * Accepts: MAIL stream
  516.  *        options
  517.  */
  518.  
  519. void dummy_close (MAILSTREAM *stream,long options)
  520. {
  521.                 /* return silently */
  522. }
  523.  
  524. /* Dummy ping mailbox
  525.  * Accepts: MAIL stream
  526.  * Returns: T if stream alive, else NIL
  527.  */
  528.  
  529. long dummy_ping (MAILSTREAM *stream)
  530. {
  531.                 /* time to do another test? */
  532.   if (time (0) >= (stream->gensym + 30)) {
  533.     MAILSTREAM *test = mail_open (NIL,stream->mailbox,OP_PROTOTYPE);
  534.     if (!test) return NIL;    /* can't get a prototype?? */
  535.     if (test->dtb == stream->dtb) {
  536.       stream->gensym = time (0);/* still hasn't changed */
  537.       return T;            /* try again later */
  538.     }
  539.                 /* looks like a new driver? */
  540.     if (!(test = mail_open (NIL,stream->mailbox,NIL))) return NIL;
  541.     mail_close ((MAILSTREAM *)    /* flush resources used by dummy stream */
  542.         memcpy (fs_get (sizeof (MAILSTREAM)),stream,
  543.             sizeof (MAILSTREAM)));
  544.                 /* swap the streams */
  545.     memcpy (stream,test,sizeof (MAILSTREAM));
  546.     fs_give ((void **) &test);    /* flush test now that copied */
  547.   }
  548.   return T;
  549. }
  550.  
  551.  
  552. /* Dummy check mailbox
  553.  * Accepts: MAIL stream
  554.  * No-op for readonly files, since read/writer can expunge it from under us!
  555.  */
  556.  
  557. void dummy_check (MAILSTREAM *stream)
  558. {
  559.   dummy_ping (stream);        /* invoke ping */
  560. }
  561.  
  562.  
  563. /* Dummy expunge mailbox
  564.  * Accepts: MAIL stream
  565.  */
  566.  
  567. void dummy_expunge (MAILSTREAM *stream)
  568. {
  569.                 /* return silently */
  570. }
  571.  
  572. /* Dummy copy message(s)
  573.  * Accepts: MAIL stream
  574.  *        sequence
  575.  *        destination mailbox
  576.  *        options
  577.  * Returns: T if copy successful, else NIL
  578.  */
  579.  
  580. long dummy_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  581. {
  582.   if ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
  583.       mail_sequence (stream,sequence)) fatal ("Impossible dummy_copy");
  584.   return NIL;
  585. }
  586.  
  587.  
  588. /* Dummy append message string
  589.  * Accepts: mail stream
  590.  *        destination mailbox
  591.  *        optional flags
  592.  *        optional date
  593.  *        stringstruct of message to append
  594.  * Returns: T on success, NIL on failure
  595.  */
  596.  
  597. long dummy_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  598.            STRING *message)
  599. {
  600.   struct stat sbuf;
  601.   int fd = -1;
  602.   int e;
  603.   char tmp[MAILTMPLEN];
  604.   MAILSTREAM *ts = default_proto (T);
  605.   if ((strcmp (ucase (strcpy (tmp,mailbox)),"INBOX")) &&
  606.        ((fd = open (dummy_file (tmp,mailbox),O_RDONLY,NIL)) < 0)) {
  607.     if ((e = errno) == ENOENT)    /* failed, was it no such file? */
  608.       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
  609.     sprintf (tmp,"%s: %s",strerror (e),mailbox);
  610.     mm_log (tmp,ERROR);        /* pass up error */
  611.     return NIL;            /* always fails */
  612.   }
  613.   if (fd >= 0) {        /* found file? */
  614.     fstat (fd,&sbuf);        /* get its size */
  615.     close (fd);            /* toss out the fd */
  616.     if (sbuf.st_size) ts = NIL;    /* non-empty file? */
  617.   }
  618.   if (ts) return (*ts->dtb->append) (stream,mailbox,flags,date,message);
  619.   sprintf (tmp,"Indeterminate mailbox format: %s",mailbox);
  620.   mm_log (tmp,ERROR);
  621.   return NIL;
  622. }
  623.  
  624. /* Dummy mail generate file string
  625.  * Accepts: temporary buffer to write into
  626.  *        mailbox name string
  627.  * Returns: local file string or NIL if failure
  628.  */
  629.  
  630. char *dummy_file (char *dst,char *name)
  631. {
  632.   char *s = mailboxfile (dst,name);
  633.                 /* return our standard inbox */
  634.   return (s && !*s) ? strcpy (dst,sysinbox ()) : s;
  635. }
  636.  
  637.  
  638. /* Dummy canonicalize name
  639.  * Accepts: buffer to write name
  640.  *        reference
  641.  *        pattern
  642.  * Returns: T if success, NIL if failure
  643.  */
  644.  
  645. long dummy_canonicalize (char *tmp,char *ref,char *pat)
  646. {
  647.   char *s;
  648.   if (ref) {            /* preliminary reference check */
  649.     if (*ref == '{') return NIL;/* remote reference not allowed */
  650.     else if (!*ref) ref = NIL;    /* treat empty reference as no reference */
  651.   }
  652.   switch (*pat) {
  653.   case '#':            /* namespace name */
  654.     if (mailboxfile (tmp,pat)) strcpy (tmp,pat);
  655.     else return NIL;        /* unknown namespace */
  656.     break;
  657.   case '{':            /* remote names not allowed */
  658.     return NIL;
  659.   case '/':            /* rooted name */
  660.   case '~':            /* home directory name */
  661.     if (!ref || (*ref != '#')) {/* non-namespace reference? */
  662.       strcpy (tmp,pat);        /* yes, ignore */
  663.       break;
  664.     }
  665.                 /* fall through */
  666.   default:            /* apply reference for all other names */
  667.     if (!ref) strcpy (tmp,pat);    /* just copy if no namespace */
  668.     else if ((*ref != '#') || mailboxfile (tmp,ref)) {
  669.                 /* wants root of name? */
  670.       if (*pat == '/') strcpy (strchr (strcpy (tmp,ref),'/'),pat);
  671.                 /* otherwise just append */
  672.       else sprintf (tmp,"%s%s",ref,pat);
  673.     }
  674.     else return NIL;        /* unknown namespace */
  675.   }
  676.   return T;
  677. }
  678.