home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / dummy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  14.1 KB  |  625 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:    15 July 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.  
  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/file.h>
  45. #include "dummy.h"
  46. #include "misc.h"
  47.  
  48. /* Dummy routines */
  49.  
  50.  
  51. /* Driver dispatch used by MAIL */
  52.  
  53. DRIVER dummydriver = {
  54.   "dummy",            /* driver name */
  55.   (DRIVER *) NIL,        /* next driver */
  56.   dummy_valid,            /* mailbox is valid for us */
  57.   dummy_parameters,        /* manipulate parameters */
  58.   dummy_find,            /* find mailboxes */
  59.   dummy_find_bboards,        /* find bboards */
  60.   dummy_find_all,        /* find all mailboxes */
  61.   dummy_find_all_bboards,    /* find all bboards */
  62.   dummy_subscribe,        /* subscribe to mailbox */
  63.   dummy_unsubscribe,        /* unsubscribe from mailbox */
  64.   dummy_subscribe_bboard,    /* subscribe to bboard */
  65.   dummy_unsubscribe_bboard,    /* unsubscribe from bboard */
  66.   dummy_create,            /* create mailbox */
  67.   dummy_delete,            /* delete mailbox */
  68.   dummy_rename,            /* rename mailbox */
  69.   dummy_open,            /* open mailbox */
  70.   dummy_close,            /* close mailbox */
  71.   dummy_fetchfast,        /* fetch message "fast" attributes */
  72.   dummy_fetchflags,        /* fetch message flags */
  73.   dummy_fetchstructure,        /* fetch message structure */
  74.   dummy_fetchheader,        /* fetch message header only */
  75.   dummy_fetchtext,        /* fetch message body only */
  76.   dummy_fetchbody,        /* fetch message body section */
  77.   dummy_setflag,        /* set message flag */
  78.   dummy_clearflag,        /* clear message flag */
  79.   dummy_search,            /* search for message based on criteria */
  80.   dummy_ping,            /* ping mailbox to see if still alive */
  81.   dummy_check,            /* check for new messages */
  82.   dummy_expunge,        /* expunge deleted messages */
  83.   dummy_copy,            /* copy messages to another mailbox */
  84.   dummy_move,            /* move messages to another mailbox */
  85.   dummy_append,            /* append string message to mailbox */
  86.   dummy_gc            /* garbage collect stream */
  87. };
  88.  
  89. /* Dummy validate mailbox
  90.  * Accepts: mailbox name
  91.  * Returns: our driver if name is valid, NIL otherwise
  92.  */
  93.  
  94. DRIVER *dummy_valid (name)
  95.     char *name;
  96. {
  97.   return (name && *name) ? &dummydriver : NIL;
  98. }
  99.  
  100.  
  101. /* Dummy manipulate driver parameters
  102.  * Accepts: function code
  103.  *        function-dependent value
  104.  * Returns: function-dependent return value
  105.  */
  106.  
  107. void *dummy_parameters (function,value)
  108.     long function;
  109.     void *value;
  110. {
  111.   return NIL;
  112. }
  113.  
  114. /* Dummy find list of subscribed mailboxes
  115.  * Accepts: mail stream
  116.  *        pattern to search
  117.  */
  118.  
  119. void dummy_find (stream,pat)
  120.     MAILSTREAM *stream;
  121.     char *pat;
  122. {
  123.   void *sdb = NIL;
  124.   DIR *dirp;
  125.   struct direct *d;
  126.   char tmp[MAILTMPLEN],file[MAILTMPLEN];
  127.   char *s,*t;
  128.   int i = 0;
  129.                 /* no-op if have a subscription database */
  130.   if (sm_read (&sdb)) fs_give ((void **) &sdb);
  131.   else {            /* no subscriptions, do a directory */
  132.     if (s = strrchr (pat,'/')) {/* directory specified in pattern? */
  133.       strncpy (file,pat,i = (++s) - pat);
  134.       file[i] = '\0';        /* tie off prefix */
  135.       t = dummy_file (tmp,pat);    /* make fully-qualified file name */
  136.                 /* tie off directory name */
  137.       if (s = strrchr (t,'/')) *s = '\0';
  138.     }
  139.     else t = myhomedir ();    /* use home directory to search */
  140.     if (dirp = opendir (t)) {    /* now open that directory */
  141.       while (d = readdir (dirp)) {/* for each directory entry */
  142.     strcpy (file + i,d->d_name);
  143.     if (pmatch (file,pat)) mm_mailbox (file);
  144.       }
  145.       closedir (dirp);        /* flush directory */
  146.     }
  147.   }
  148. }
  149.  
  150. /* Dummy find list of subscribed bboards
  151.  * Accepts: mail stream
  152.  *        pattern to search
  153.  */
  154.  
  155. void dummy_find_bboards (stream,pat)
  156.     MAILSTREAM *stream;
  157.     char *pat;
  158. {
  159.   void *sdb = NIL;
  160.   DIR *dirp;
  161.   struct direct *d;
  162.   struct passwd *pw;
  163.   char tmp[MAILTMPLEN],file[MAILTMPLEN];
  164.   int i = 1;
  165.   char *s;
  166.                 /* no-op if have a subscription database */
  167.   if (sm_read (&sdb)) fs_give ((void **) &sdb);
  168.   else {
  169.     if (!((pw = getpwnam ("ftp")) && pw->pw_dir)) return;
  170.     file[0] = '*';        /* bboard designator */
  171.                 /* directory specified in pattern? */
  172.     if (s = strrchr (pat,'/')) strncpy (file + 1,pat,i += (++s) - pat);
  173.     file[i] = '\0';        /* tie off prefix */
  174.     sprintf (tmp,"%s/%s",pw->pw_dir,(file[1] == '/') ? file + 2 : file + 1);
  175.     if (dirp = opendir (tmp)) {    /* now open that directory */
  176.       while (d = readdir (dirp)) {/* for each directory entry */
  177.     strcpy (file + i,d->d_name);
  178.     if (pmatch (file + 1,pat)) mm_bboard (file + 1);
  179.       }
  180.       closedir (dirp);        /* flush directory */
  181.     }
  182.   }
  183. }
  184.  
  185. /* Dummy find list of all mailboxes
  186.  * Accepts: mail stream
  187.  *        pattern to search
  188.  */
  189.  
  190. void dummy_find_all (stream,pat)
  191.     MAILSTREAM *stream;
  192.     char *pat;
  193. {
  194.                 /* always an INBOX */
  195.   if (pmatch ("INBOX",pat)) mm_mailbox ("INBOX");
  196. }
  197.  
  198.  
  199. /* Dummy find list of all bboards
  200.  * Accepts: mail stream
  201.  *        pattern to search
  202.  */
  203.  
  204. void dummy_find_all_bboards (stream,pat)
  205.     MAILSTREAM *stream;
  206.     char *pat;
  207. {
  208.   /* Exit quietly */
  209. }
  210.  
  211. /* Dummy subscribe to mailbox
  212.  * Accepts: mail stream
  213.  *        mailbox to add to subscription list
  214.  * Returns: T on success, NIL on failure
  215.  */
  216.  
  217. long dummy_subscribe (stream,mailbox)
  218.     MAILSTREAM *stream;
  219.     char *mailbox;
  220. {
  221.   return NIL;            /* always fails */
  222. }
  223.  
  224.  
  225. /* Dummy unsubscribe to mailbox
  226.  * Accepts: mail stream
  227.  *        mailbox to delete from subscription list
  228.  * Returns: T on success, NIL on failure
  229.  */
  230.  
  231. long dummy_unsubscribe (stream,mailbox)
  232.     MAILSTREAM *stream;
  233.     char *mailbox;
  234. {
  235.   return NIL;            /* always fails */
  236. }
  237.  
  238.  
  239. /* Dummy subscribe to bboard
  240.  * Accepts: mail stream
  241.  *        bboard to add to subscription list
  242.  * Returns: T on success, NIL on failure
  243.  */
  244.  
  245. long dummy_subscribe_bboard (stream,mailbox)
  246.     MAILSTREAM *stream;
  247.     char *mailbox;
  248. {
  249.   return NIL;            /* always fails */
  250. }
  251.  
  252.  
  253. /* Dummy unsubscribe to bboard
  254.  * Accepts: mail stream
  255.  *        bboard to delete from subscription list
  256.  * Returns: T on success, NIL on failure
  257.  */
  258.  
  259. long dummy_unsubscribe_bboard (stream,mailbox)
  260.     MAILSTREAM *stream;
  261.     char *mailbox;
  262. {
  263.   return NIL;            /* always fails */
  264. }
  265.  
  266. /* Dummy create mailbox
  267.  * Accepts: mail stream
  268.  *        mailbox name to create
  269.  *        driver type to use
  270.  * Returns: T on success, NIL on failure
  271.  */
  272.  
  273. long dummy_create (stream,mailbox)
  274.     MAILSTREAM *stream;
  275.     char *mailbox;
  276. {
  277.   return NIL;            /* always fails */
  278. }
  279.  
  280.  
  281. /* Dummy delete mailbox
  282.  * Accepts: mail stream
  283.  *        mailbox name to delete
  284.  * Returns: T on success, NIL on failure
  285.  */
  286.  
  287. long dummy_delete (stream,mailbox)
  288.     MAILSTREAM *stream;
  289.     char *mailbox;
  290. {
  291.   return NIL;            /* always fails */
  292. }
  293.  
  294.  
  295. /* Mail rename mailbox
  296.  * Accepts: mail stream
  297.  *        old mailbox name
  298.  *        new mailbox name
  299.  * Returns: T on success, NIL on failure
  300.  */
  301.  
  302. long dummy_rename (stream,old,new)
  303.     MAILSTREAM *stream;
  304.     char *old;
  305.     char *new;
  306. {
  307.   return NIL;            /* always fails */
  308. }
  309.  
  310. /* Dummy open
  311.  * Accepts: stream to open
  312.  * Returns: stream on success, NIL on failure
  313.  */
  314.  
  315. MAILSTREAM *dummy_open (stream)
  316.     MAILSTREAM *stream;
  317. {
  318.   int fd;
  319.   char tmp[MAILTMPLEN];
  320.                 /* OP_PROTOTYPE call or silence */
  321.   if (!stream || stream->silent) return NIL;
  322.   if (*stream->mailbox == '*')    /* is it a bboard? */
  323.     sprintf (tmp,"No such bboard: %s",stream->mailbox+1);
  324.                 /* remote specification? */
  325.   else if (*stream->mailbox == '{')
  326.     sprintf (tmp,"Invalid remote specification: %s",stream->mailbox);
  327.   else if ((fd = open (dummy_file (tmp,stream->mailbox),O_RDONLY,NIL)) < 0)
  328.     sprintf (tmp,"%s: %s",strerror (errno),stream->mailbox);
  329.   else {            /* must be bogus format file */
  330.     sprintf (tmp,"%s is not a mailbox",stream->mailbox);
  331.     close (fd);            /* toss out the fd */
  332.   }
  333.   mm_log (tmp,ERROR);
  334.   return NIL;            /* always fails */
  335. }
  336.  
  337.  
  338. /* Dummy close
  339.  * Accepts: MAIL stream
  340.  */
  341.  
  342. void dummy_close (stream)
  343.     MAILSTREAM *stream;
  344. {
  345.   /* Exit quietly */
  346. }
  347.  
  348. /* Dummy fetch fast information
  349.  * Accepts: MAIL stream
  350.  *        sequence
  351.  */
  352.  
  353. void dummy_fetchfast (stream,sequence)
  354.     MAILSTREAM *stream;
  355.     char *sequence;
  356. {
  357.   fatal ("Impossible dummy_fetchfast");
  358. }
  359.  
  360.  
  361. /* Dummy fetch flags
  362.  * Accepts: MAIL stream
  363.  *        sequence
  364.  */
  365.  
  366. void dummy_fetchflags (stream,sequence)
  367.     MAILSTREAM *stream;
  368.     char *sequence;
  369. {
  370.   fatal ("Impossible dummy_fetchflags");
  371. }
  372.  
  373.  
  374. /* Dummy fetch envelope
  375.  * Accepts: MAIL stream
  376.  *        message # to fetch
  377.  *        pointer to return body
  378.  * Returns: envelope of this message, body returned in body value
  379.  */
  380.  
  381. ENVELOPE *dummy_fetchstructure (stream,msgno,body)
  382.     MAILSTREAM *stream;
  383.     long msgno;
  384.     BODY **body;
  385. {
  386.   fatal ("Impossible dummy_fetchstructure");
  387.   return NIL;
  388. }
  389.  
  390.  
  391. /* Dummy fetch message header
  392.  * Accepts: MAIL stream
  393.  *        message # to fetch
  394.  * Returns: message header in RFC822 format
  395.  */
  396.  
  397. char *dummy_fetchheader (stream,msgno)
  398.     MAILSTREAM *stream;
  399.     long msgno;
  400. {
  401.   fatal ("Impossible dummy_fetchheader");
  402.   return NIL;
  403. }
  404.  
  405. /* Dummy fetch message text (only)
  406.     body only;
  407.  * Accepts: MAIL stream
  408.  *        message # to fetch
  409.  * Returns: message text in RFC822 format
  410.  */
  411.  
  412. char *dummy_fetchtext (stream,msgno)
  413.     MAILSTREAM *stream;
  414.     long msgno;
  415. {
  416.   fatal ("Impossible dummy_fetchtext");
  417.   return NIL;
  418. }
  419.  
  420.  
  421. /* Berkeley fetch message body as a structure
  422.  * Accepts: Mail stream
  423.  *        message # to fetch
  424.  *        section specifier
  425.  * Returns: pointer to section of message body
  426.  */
  427.  
  428. char *dummy_fetchbody (stream,m,sec,len)
  429.     MAILSTREAM *stream;
  430.     long m;
  431.     char *sec;
  432.     unsigned long *len;
  433. {
  434.   fatal ("Impossible dummy_fetchbody");
  435.   return NIL;
  436. }
  437.  
  438.  
  439. /* Dummy set flag
  440.  * Accepts: MAIL stream
  441.  *        sequence
  442.  *        flag(s)
  443.  */
  444.  
  445. void dummy_setflag (stream,sequence,flag)
  446.     MAILSTREAM *stream;
  447.     char *sequence;
  448.     char *flag;
  449. {
  450.   fatal ("Impossible dummy_setflag");
  451. }
  452.  
  453.  
  454. /* Dummy clear flag
  455.  * Accepts: MAIL stream
  456.  *        sequence
  457.  *        flag(s)
  458.  */
  459.  
  460. void dummy_clearflag (stream,sequence,flag)
  461.     MAILSTREAM *stream;
  462.     char *sequence;
  463.     char *flag;
  464. {
  465.   fatal ("Impossible dummy_clearflag");
  466. }
  467.  
  468.  
  469. /* Dummy search for messages
  470.  * Accepts: MAIL stream
  471.  *        search criteria
  472.  */
  473.  
  474. void dummy_search (stream,criteria)
  475.     MAILSTREAM *stream;
  476.     char *criteria;
  477. {
  478.   fatal ("Impossible dummy_search");
  479. }
  480.  
  481. /* Dummy ping mailbox
  482.  * Accepts: MAIL stream
  483.  * Returns: T if stream alive, else NIL
  484.  * No-op for readonly files, since read/writer can expunge it from under us!
  485.  */
  486.  
  487. long dummy_ping (stream)
  488.     MAILSTREAM *stream;
  489. {
  490.   fatal ("Impossible dummy_ping");
  491.   return NIL;
  492. }
  493.  
  494.  
  495. /* Dummy check mailbox
  496.  * Accepts: MAIL stream
  497.  * No-op for readonly files, since read/writer can expunge it from under us!
  498.  */
  499.  
  500. void dummy_check (stream)
  501.     MAILSTREAM *stream;
  502. {
  503.   fatal ("Impossible dummy_check");
  504. }
  505.  
  506.  
  507. /* Dummy expunge mailbox
  508.  * Accepts: MAIL stream
  509.  */
  510.  
  511. void dummy_expunge (stream)
  512.     MAILSTREAM *stream;
  513. {
  514.   fatal ("Impossible dummy_expunge");
  515. }
  516.  
  517. /* Dummy copy message(s)
  518.     s;
  519.  * Accepts: MAIL stream
  520.  *        sequence
  521.  *        destination mailbox
  522.  * Returns: T if copy successful, else NIL
  523.  */
  524.  
  525. long dummy_copy (stream,sequence,mailbox)
  526.     MAILSTREAM *stream;
  527.     char *sequence;
  528.     char *mailbox;
  529. {
  530.   fatal ("Impossible dummy_copy");
  531.   return NIL;
  532. }
  533.  
  534.  
  535. /* Dummy move message(s)
  536.     s;
  537.  * Accepts: MAIL stream
  538.  *        sequence
  539.  *        destination mailbox
  540.  * Returns: T if move successful, else NIL
  541.  */
  542.  
  543. long dummy_move (stream,sequence,mailbox)
  544.     MAILSTREAM *stream;
  545.     char *sequence;
  546.     char *mailbox;
  547. {
  548.   fatal ("Impossible dummy_move");
  549.   return NIL;
  550. }
  551.  
  552.  
  553. /* Dummy append message string
  554.  * Accepts: mail stream
  555.  *        destination mailbox
  556.  *        stringstruct of message to append
  557.  * Returns: T on success, NIL on failure
  558.  */
  559.  
  560. long dummy_append (stream,mailbox,message)
  561.     MAILSTREAM *stream;
  562.     char *mailbox;
  563.     STRING *message;
  564. {
  565.   int fd = -1,e;
  566.   char tmp[MAILTMPLEN];
  567.                 /* see if such a file */
  568.   if ((*mailbox != '*') && (*mailbox != '{') &&
  569.       (fd = open (dummy_file (tmp,mailbox),O_RDONLY,NIL)) < 0) {
  570.     if ((e = errno) == ENOENT)    /* failed, was it no such file? */
  571.       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
  572.     sprintf (tmp,"%s: %s",strerror (e),mailbox);
  573.   }
  574.   else {            /* must be bogus format file */
  575.     sprintf (tmp,"%s is not a valid mailbox",mailbox);
  576.     close (fd);            /* toss out the fd */
  577.   }
  578.   mm_log (tmp,ERROR);        /* pass up error */
  579.   return NIL;            /* always fails */
  580. }
  581.  
  582.  
  583. /* Dummy garbage collect stream
  584.  * Accepts: mail stream
  585.  *        garbage collection flags
  586.  */
  587.  
  588. void dummy_gc (stream,gcflags)
  589.     MAILSTREAM *stream;
  590.     long gcflags;
  591. {
  592.   fatal ("Impossible dummy_gc");
  593. }
  594.  
  595. /* Dummy mail generate file string
  596.  * Accepts: temporary buffer to write into
  597.  *        mailbox name string
  598.  * Returns: local file string
  599.  */
  600.  
  601. char *dummy_file (dst,name)
  602.     char *dst;
  603.     char *name;
  604. {
  605.   struct passwd *pw;
  606.   char *s,*t,tmp[MAILTMPLEN];
  607.   switch (*name) {
  608.   case '/':            /* absolute file path */
  609.     strcpy (dst,name);        /* copy the mailbox name */
  610.     break;
  611.   case '~':            /* home directory */
  612.     if (name[1] == '/') t = myhomedir ();
  613.     else {
  614.       strcpy (tmp,name + 1);    /* copy user name */
  615.       if (s = strchr (tmp,'/')) *s = '\0';
  616.       t = ((pw = getpwnam (tmp)) && pw->pw_dir) ? pw->pw_dir : "/NOSUCHUSER";
  617.     }
  618.     sprintf (dst,"%s%s",t,(s = strchr (name,'/')) ? s : "");
  619.     break;
  620.   default:            /* any other name */
  621.     sprintf (dst,"%s/%s",myhomedir (),name);
  622.   }
  623.   return dst;
  624. }
  625.