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