home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / c-niftp.zip / unix-niftp-5.6 / lib / mmdf / ml_send.c < prev    next >
C/C++ Source or Header  |  1990-08-01  |  9KB  |  322 lines

  1. /* unix-niftp lib/mmdf/ml_send.c $Revision: 5.5 $ $Date: 90/08/01 13:36:15 $ */
  2. #include "util.h"
  3. #include "mmdf.h"
  4. #include "nrs.h"
  5. #include "ftp.h"
  6. #include <stdio.h>
  7. #include <signal.h>
  8.  
  9. /*
  10.  * file ml_send.c
  11.  * last modified 25-May-83
  12.  * $Log:    ml_send.c,v $
  13.  * Revision 5.5  90/08/01  13:36:15  pb
  14.  * Distribution of Aug90RealPP+sequent: Full PP release and support for Sequent X.25 board
  15.  * 
  16.  * Revision 5.4  89/08/27  14:07:35  pb
  17.  * Distribution of Aug89PPsupport: Update READMEs for PP
  18.  * 
  19.  * Revision 5.2  89/01/13  14:48:42  pb
  20.  * Distribution of Jan89SuckMail: Support Sucking of mail
  21.  * 
  22.  * Revision 5.0  87/03/23  03:45:18  bin
  23.  * Merger of latest UCL and Nottingham versions together with
  24.  * an extensive spring clean.
  25.  * Now UNIX-NIFTP prerelease.
  26.  * 
  27. */
  28. /*
  29.  *     MULTI-CHANNEL MEMO DISTRIBUTION FACILITY  (MMDF)
  30.  *
  31.  *
  32.  *     Copyright (C) 1979,1980,1981  University of Delaware
  33.  *
  34.  *     Department of Electrical Engineering
  35.  *     University of Delaware
  36.  *     Newark, Delaware  19711
  37.  *
  38.  *     Phone:  (302) 738-1163
  39.  *
  40.  *
  41.  *     This program module was developed as part of the University
  42.  *     of Delaware's Multi-Channel Memo Distribution Facility (MMDF).
  43.  *
  44.  *     Acquisition, use, and distribution of this module and its listings
  45.  *     are subject restricted to the terms of a license agreement.
  46.  *     Documents describing systems using this module must cite its source.
  47.  *
  48.  *     The above statements must be retained with all copies of this
  49.  *     program and may not be removed without the consent of the
  50.  *     University of Delaware.
  51.  *
  52.  *
  53.  *     version  -1    David H. Crocker    March   1979
  54.  *     version   0    David H. Crocker    April   1980
  55.  *     version  v7    David H. Crocker    May     1981
  56.  *     version   1    David H. Crocker    October 1981
  57.  *
  58.  */
  59. /*
  60. modified by
  61. ruth moulton, ucl april 1983
  62.     to make independent of mmdf - for use with niftp
  63. */
  64. /* send a piece of mail, using Unix mail command        */
  65.  
  66. /*  Basic sequence is:
  67.  *
  68.  *          ml_init (YES, NO, "My Name", "The Subject);
  69.  *          ml_adr ("destination address 1");
  70.  *          ml_adr ("destination address 2");
  71.  *          ...
  72.  *          ml_aend ();
  73.  *          ml_tinit ();
  74.  *          ml_txt ("Some opening text");
  75.  *          ml_txt ("maybe some more text");
  76.  *          ml_file (file-stream-descriptor-of-file-to-include);
  77.  *          if (ml_end (OK)) != OK)
  78.  *          {   error-handling code }
  79.  *
  80.  *  Arguments that are to be defaulted should be zero.
  81.  *
  82.  *  ml_init's arguments specify a) whether return-mail (to the sender
  83.  *  should be allowed, b) whether a Sender field should be used to
  84.  *  specify the correct sender (contingent on next argument), c) text
  85.  *  for the From field, and d) text for the Subject field.  If (b) is
  86.  *  NO, then (c)'s text will be followed by the correct sender
  87.  *  information.
  88.  *
  89.  *  ml_to and ml_cc are used to switch between To and CC addresses.
  90.  *  Normally, only To addresses are used and, for this, no ml_to call is
  91.  *  needed.
  92.  *
  93.  *  An "address" is whatever is valid for your system, as if you were
  94.  *  typing it to the mail command.
  95.  *
  96.  *  You may freely mix ml_txt and ml_file calls.  They just append text
  97.  *  to the message.  The text must contain its own newlines.
  98.  *
  99.  *  Note that a special version of the mail command is used, to handle all
  100.  *  the extra arguments.  If its sources weren't included with the
  101.  *  distribution of this file, you probably have a problem.
  102.  */
  103.  
  104. extern char pathmail[],           /* location of mail command           */
  105.         nammail[];
  106. extern char cmddfldir[];
  107.  
  108. static FILE *ml_fp;             /* handle on output to mail command   */
  109.  
  110. static int    ml_childid;           /* process id of mail child           */
  111. static short ml_curarg;            /* index of next argument             */
  112.  
  113. static char *ml_argv[20];          /* arguments to pass to execv         */
  114. /* */
  115.  
  116. ml_init (ret, sndr, from, sub)    /* set-up for using mail command      */
  117. int     ret,                      /* allow return mail to sender?       */
  118.     sndr;                     /* include Sender field?              */
  119. char    sub[],                    /* subject line                       */
  120.     from[];                   /* from field                         */
  121. {
  122.     ml_argv[0] = "mail";
  123.     if (ret)                      /* allow return to sender             */
  124.     ml_curarg = 1;
  125.     else
  126.     {                             /* disable return to sender           */
  127.     ml_argv[1] = "-r";
  128.     ml_curarg = 2;
  129.     }
  130.  
  131.     if (from != 0)
  132.     {                             /* user-specified From field          */
  133.     ml_argv[ml_curarg++] = (sndr) ? "-f" : "-g";
  134.                   /* f => Sender field needed           */
  135.     ml_argv[ml_curarg++] = from;
  136.     }
  137.  
  138.     if (sub != 0)
  139.     {                             /* user-specified Subject field       */
  140.     ml_argv[ml_curarg++] = "-s";
  141.     ml_argv[ml_curarg++] = sub;
  142.     }
  143.  
  144.     return (ml_to ());            /* set-up for To: addresses           */
  145. }
  146. /* */
  147.  
  148.  
  149. ml_to ()                          /* ready to specify To: address       */
  150. {
  151.     ml_argv[ml_curarg++] = "-t";
  152.     return (OK);
  153. }
  154.  
  155. ml_cc ()                          /* ready to specify CC: address       */
  156. {
  157.     ml_argv[ml_curarg++] = "-c";
  158.     return (OK);
  159. }
  160.  
  161. ml_adr (address)                  /* a destination for the mail         */
  162. char    address[];
  163. {
  164.     ml_argv[ml_curarg++] = address;
  165.     return (OK);
  166. }
  167.  
  168. ml_aend ()                        /* end of addrs                       */
  169. {
  170.     ml_argv[ml_curarg] = 0;
  171.     return (OK);
  172. }
  173. /* */
  174.  
  175. ml_tinit ()                     /* ready to send text                 */
  176. {
  177.     Pip    pipdes;              /* output pipe                        */
  178.     register short    c;
  179.     char **p;
  180.  
  181.  
  182.     if (pipe (pipdes.pipcall))  /* for output to mail                 */
  183.     return (NOTOK);
  184.  
  185.     if (ftp_print & L_LOG_EXEC){
  186.     L_LOG_1(L_LOG_EXEC, 0, "forking to execv %s\n",mailprog);
  187.     L_LOG_0(L_LOG_EXEC, 0, "with args:");
  188.     p = ml_argv;
  189.     while(*p)
  190.         L_LOG_1(L_LOG_EXEC, L_CONTINUE, "%s;",*p++);
  191.     L_LOG_0(L_LOG_EXEC, L_CONTINUE, "\n");
  192.     }
  193.     ml_childid = fork ();
  194.     switch (ml_childid)
  195.     {
  196.     case NOTOK:               /* bad day all around                 */
  197.         close (pipdes.pip.prd);
  198.         close (pipdes.pip.pwrt);
  199.         return (NOTOK);
  200.  
  201.     case 0:                   /* this is the child                  */
  202.         close (0);
  203.  
  204.         dup (pipdes.pip.prd);
  205.         for (c = HIGHFD; c > 0; c--)
  206.         close (c);
  207.         open ("/dev/null",1);
  208.               /* give Submit a place to send msgs   */
  209.         setuid(FTPuid);
  210.  
  211.         execv (mailprog, ml_argv);
  212.         L_ERROR_1(L_ALWAYS, L_DATE | L_TIME,
  213.                 "Failed to fork %s\n",mailprog);
  214.         exit (NOTOK);
  215.     }                             /* BELOW HERE is the parent           */
  216.  
  217.     close (pipdes.pip.prd);
  218.  
  219.     ml_fp = fdopen (pipdes.pip.pwrt, "w");
  220.                   /* initialize the stdio for output    */
  221.  
  222.     return (OK);
  223. }
  224. /* */
  225.  
  226. ml_file (infp)                    /* send a file to the message         */
  227. register FILE  *infp;             /* input stdio file stream pointer    */
  228. {
  229.     register short len;
  230.     char    buffer[BUFSIZE];
  231.  
  232.     if ((int) ml_fp == EOF || (int) ml_fp == NULL)
  233.     return (OK);
  234.  
  235.     while ((len = fread (buffer, sizeof (char), sizeof(buffer), infp )) > 0)
  236.     if (fwrite (buffer, sizeof (char), len, ml_fp) != len)
  237.     {                         /* do raw i/o                         */
  238.         ml_end (NOTOK);
  239.         return (NOTOK);
  240.     }
  241.  
  242.     if (len < OK)
  243.     {
  244.     ml_end (NOTOK);
  245.     return (NOTOK);
  246.     }
  247.     return (OK);
  248. }
  249.  
  250. ml_txt (text)                     /* some text for the body             */
  251. char text[];                      /* the text                           */
  252. {
  253.     L_LOG_1(L_10, 0, "ml_txt:send text - %s\n",text);
  254.  
  255.     if (ml_fp == (FILE *) EOF || ml_fp == (FILE *) NULL) {
  256.     L_LOG_1(L_GENERAL, 0, "ml_txt: immediate return on ml_fp being %s\n",
  257.         (ml_fp == (FILE *) NULL) ? "NULL" : "EOF");
  258.     return (OK);
  259.     }
  260.  
  261.     fputs (text, ml_fp);
  262.  
  263.     if  (ferror (ml_fp))
  264.     {
  265.     ml_end (NOTOK);
  266.     return (NOTOK);
  267.     }
  268.     return (OK);
  269. }
  270. /* */
  271.  
  272.  
  273. ml_end (type)                     /* message is finished                */
  274. int     type;                     /* normal ending or not               */
  275. {
  276.     short     retval;               /* wait return value                  */
  277.  
  278.     switch (ml_childid)
  279.     {
  280.     case OK:
  281.     case NOTOK:
  282.         return (OK);
  283.  
  284.     default:                /* parent */
  285.         switch ((int) ml_fp)
  286.         {
  287.         case OK:
  288.         case NOTOK:
  289.             break;
  290.  
  291.         default:
  292.             if (ferror (ml_fp) || type == NOTOK)
  293.             kill (ml_childid, SIGKILL);
  294.             fclose (ml_fp);
  295.             ml_fp = OK;
  296.         }
  297.  
  298.         retval = pgmwait (ml_childid);
  299.         ml_childid = OK;
  300.         return (retval);
  301.     }
  302. }
  303. /* */
  304.  
  305. ml_1adr (ret, sndr, from, sub, adr)
  306.                   /* all set-up overhead in 1 proc      */
  307. int     ret,                      /* allow return mail to sender?       */
  308.     sndr;                     /* include Sender field?              */
  309. char    sub[],                    /* subject line                       */
  310.     from[],                   /* from field                         */
  311.     adr[];                    /* the one address to receive msg     */
  312. {
  313.  
  314.     if (ml_init (ret, sndr, from, sub) != OK ||
  315.         ml_adr (adr) != OK ||
  316.         ml_aend () != OK ||
  317.         ml_tinit () != OK)
  318.     return (NOTOK);
  319.  
  320.     return (OK);
  321. }
  322.