home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / utils / fastmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-06  |  8.1 KB  |  283 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: fastmail.c,v 5.7 1993/06/06 18:31:43 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.7 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988-1992 USENET Community Trust
  8.  *             Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: fastmail.c,v $
  17.  * Revision 5.7  1993/06/06  18:31:43  syd
  18.  * fix typo
  19.  *
  20.  * Revision 5.6  1993/06/06  18:08:51  syd
  21.  * Make it use the message catalog already defined
  22.  * From: Super Y.S.T. <tabata@matsumoto.dcl.co.jp>
  23.  *
  24.  * Revision 5.5  1993/02/03  16:49:11  syd
  25.  * added the RFC822 fields Comments, In-Reply-To and References.
  26.  * to fastmail.
  27.  * From: Greg Smith <smith@heliotrope.bucknell.edu>
  28.  *
  29.  * Revision 5.4  1992/11/22  01:26:12  syd
  30.  * The fastmail utility appears to work incorrectly when multiple addresses are
  31.  * supplied. Spaces were inserted between addresses rather than commas.
  32.  * From: little@carina.hks.com (Jim Littlefield)
  33.  *
  34.  * Revision 5.3  1992/10/30  21:12:40  syd
  35.  * Make patchlevel a text string to allow local additions to the variable
  36.  * From: syd via a request from Dave Wolfe
  37.  *
  38.  * Revision 5.2  1992/10/11  00:59:39  syd
  39.  * Fix some compiler warnings that I receive compiling Elm on my SVR4
  40.  * machine.
  41.  * From: Tom Moore <tmoore@fievel.DaytonOH.NCR.COM>
  42.  *
  43.  * Revision 5.1  1992/10/04  00:46:45  syd
  44.  * Initial checkin as of 2.4 Release at PL0
  45.  *
  46.  *
  47.  ******************************************************************************/
  48.  
  49. /** This program is specifically written for group mailing lists and
  50.     such batch type mail processing.  It does NOT use aliases at all,
  51.     it does NOT read the /etc/password file to find the From: name
  52.     of the user and does NOT expand any addresses.  It is meant 
  53.     purely as a front-end for either /bin/mail or /usr/lib/sendmail
  54.     (according to what is available on the current system).
  55.  
  56.          **** This program should be used with CAUTION *****
  57.  
  58. **/
  59.  
  60. /** The calling sequence for this program is:
  61.  
  62.     fastmail {args}  [ filename | - ] full-email-address 
  63.  
  64.    where args could be any (or all) of;
  65.  
  66.        -b bcc-list        (Blind carbon copies to)
  67.        -c cc-list        (carbon copies to)
  68.        -C comment-line      (Comments:)
  69.        -d            (debug on)
  70.        -f from         (from name)
  71.        -F from-addr        (the actual address to be put in the From: line)
  72.        -i msg-id            (In-Reply-To: msgid)
  73.        -r reply-to-address     (Reply-To:)
  74.        -R references        (References:)
  75.        -s subject         (subject of message)
  76. **/
  77.  
  78. #include "elmutil.h"
  79. #include "s_fastmail.h"
  80. #include "patchlevel.h"
  81.  
  82. #define  binrmail    "/bin/rmail"
  83. #define  temphome    "/tmp/fastmail."
  84.  
  85. char *get_arpa_date();
  86. static void usage();
  87.  
  88.  
  89. main(argc, argv)
  90. int argc;
  91. char *argv[];
  92. {
  93.  
  94.     extern char *optarg;
  95.     extern int optind;
  96.     FILE *tempfile;
  97.     char hostname[NLEN], username[NLEN], from_string[SLEN], subject[SLEN];
  98.     char filename[SLEN], tempfilename[SLEN], command_buffer[256];
  99.     char replyto[SLEN], cc_list[SLEN], bcc_list[SLEN], to_list[SLEN];
  100.     char from_addr[SLEN], comments[SLEN], inreplyto[NLEN];
  101.     char references[SLEN];
  102.     char *tmplogname;
  103.     int  c, sendmail_available, debug = 0;
  104.  
  105.     elm_msg_cat = catopen("elm2.4", 0);
  106.  
  107.     from_string[0] = '\0';
  108.     subject[0] = '\0';
  109.     replyto[0] = '\0';
  110.     cc_list[0] = '\0';
  111.     bcc_list[0] = '\0';
  112.     to_list[0] = '\0';
  113.     from_addr[0] = '\0';
  114.     comments[0] = '\0';
  115.     inreplyto[0] = '\0';
  116.     references[0] = '\0';
  117.  
  118.     while ((c = getopt(argc, argv, "b:c:C:df:F:i:r:R:s:")) != EOF) {
  119.       switch (c) {
  120.         case 'b' : strcpy(bcc_list, optarg);        break;
  121.         case 'c' : strcpy(cc_list, optarg);        break;
  122.         case 'C' : strcpy(comments, optarg);        break;
  123.         case 'd' : debug++;                    break;    
  124.         case 'f' : strcpy(from_string, optarg);    break;
  125.         case 'F' : strcpy(from_addr, optarg);        break;
  126.         case 'i' : strcpy(inreplyto, optarg);        break;
  127.         case 'r' : strcpy(replyto, optarg);        break;
  128.         case 'R' : strcpy(references, optarg);        break;
  129.         case 's' : strcpy(subject, optarg);        break;
  130.         case '?' : usage();
  131.        }
  132.     }    
  133.  
  134.     if (optind >= argc) {
  135.       usage();
  136.     }
  137.  
  138.     strcpy(filename, argv[optind++]);
  139.  
  140.     if (optind >= argc) {
  141.       usage();
  142.     }
  143.  
  144. #ifdef HOSTCOMPILED
  145.     strncpy(hostname, HOSTNAME, sizeof(hostname));
  146. #else
  147.     gethostname(hostname, sizeof(hostname));
  148. #endif
  149.  
  150.     tmplogname = getlogin();
  151.     if (tmplogname != NULL)
  152.       strcpy(username, tmplogname);
  153.     else
  154.       username[0] = '\0';
  155.  
  156.     if (strlen(username) == 0)
  157.       cuserid(username);
  158.  
  159.     if (strcmp(filename, "-")) {
  160.       if (access(filename, READ_ACCESS) == -1) {
  161.         fprintf(stderr, "Error: can't find file %s!\n", filename);
  162.         exit(1);
  163.       }
  164.     }
  165.  
  166.     sprintf(tempfilename, "%s%d", temphome, getpid());
  167.  
  168.     if ((tempfile = fopen(tempfilename, "w")) == NULL) {
  169.       fprintf(stderr, "Couldn't open temp file %s\n", tempfilename);
  170.       exit(1);
  171.     }
  172.  
  173.     /** Subject must appear even if "null" and must be first
  174.         at top of headers for mail because the
  175.         pure System V.3 mailer, in its infinite wisdom, now
  176.         assumes that anything the user sends is part of the 
  177.         message body unless either:
  178.         1. the "-s" flag is used (although it doesn't seem
  179.            to be supported on all implementations?)
  180.         2. the first line is "Subject:".  If so, then it'll
  181.            read until a blank line and assume all are meant
  182.            to be headers.
  183.         So the gory solution here is to move the Subject: line
  184.         up to the top.  I assume it won't break anyone elses program
  185.         or anything anyway (besides, RFC-822 specifies that the *order*
  186.         of headers is irrelevant).  Gahhhhh....
  187.  
  188.         If we have been configured for a smart mailer then we don't want
  189.         to add a from line.  If the user has specified one then we have
  190.         to honor their wishes.  If they've just given a 'from name' then
  191.         we'll just put in the username and hope the mailer can add the
  192.         correct domain in.
  193.     **/
  194.     fprintf(tempfile, "Subject: %s\n", subject);
  195.  
  196.     if (*from_string)
  197.       if (*from_addr)
  198.           fprintf(tempfile, "From: %s (%s)\n", from_addr, from_string);
  199.       else
  200. #ifdef DONT_ADD_FROM
  201.           fprintf(tempfile, "From: %s (%s)\n", username, from_string);
  202. #else
  203.           fprintf(tempfile, "From: %s!%s (%s)\n", hostname, username, 
  204.               from_string);
  205. #endif
  206.     else
  207.       if (*from_addr)
  208.         fprintf(tempfile, "From: %s\n", from_addr);
  209. #ifndef DONT_ADD_FROM
  210.       else
  211.         fprintf(tempfile, "From: %s!%s\n", hostname, username);
  212. #endif
  213.  
  214.     fprintf(tempfile, "Date: %s\n", get_arpa_date());
  215.  
  216.     if (strlen(replyto) > 0)
  217.       fprintf(tempfile, "Reply-To: %s\n", replyto);
  218.  
  219.     while (optind < argc) 
  220.           sprintf(to_list, "%s%s%s", to_list, (strlen(to_list) > 0? ",":""), 
  221.           argv[optind++]);
  222.     
  223.     fprintf(tempfile, "To: %s\n", to_list);
  224.  
  225.     if (strlen(cc_list) > 0)
  226.       fprintf(tempfile, "Cc: %s\n", cc_list);
  227.  
  228.     if (strlen(references) > 0)
  229.       fprintf(tempfile, "References: %s\n", references);
  230.  
  231.     if (strlen(inreplyto) > 0)
  232.       fprintf(tempfile, "In-Reply-To: %s\n", inreplyto);
  233.  
  234.     if (strlen(comments) > 0)
  235.       fprintf(tempfile, "Comments: %s\n", comments);
  236.  
  237. #ifndef NO_XHEADER
  238.     fprintf(tempfile, "X-Mailer: fastmail [version %s PL%s]\n",
  239.       VERSION, PATCHLEVEL);
  240. #endif /* !NO_XHEADER */
  241.     fprintf(tempfile, "\n");
  242.  
  243.     fclose(tempfile);
  244.  
  245.     /** now we'll cat both files to /bin/rmail or sendmail... **/
  246.  
  247.     sendmail_available = (access(sendmail, EXECUTE_ACCESS) != -1);
  248.  
  249.     if (debug)
  250.         printf("Mailing to %s%s%s%s%s [via %s]\n", to_list,
  251.             (strlen(cc_list) > 0 ? " ":""), cc_list,
  252.             (strlen(bcc_list) > 0 ? " ":""), bcc_list,
  253.             sendmail_available? "sendmail" : "rmail");
  254.  
  255.     sprintf(command_buffer, "cat %s %s | %s %s %s %s", 
  256.         tempfilename, filename, 
  257.             sendmail_available? sendmail : mailer,
  258.         to_list, cc_list, bcc_list);
  259.  
  260.     if (debug)
  261.       printf("%s\n", command_buffer);
  262.  
  263.     c = system(command_buffer);
  264.  
  265.     unlink(tempfilename);
  266.  
  267.     exit(c != 0);
  268. }
  269.  
  270. static void usage()
  271. {
  272.  
  273.     fprintf(stderr, catgets(elm_msg_cat, FastmailSet, FastmailUsage,
  274. "Usage: fastmail {args} [ filename | - ] address(es)\n\
  275.    where {args} can be;\n\
  276. \t-b bcc-list\n\t-c cc-list\n\t-d\n\
  277. \t-C comments\n\t-f from-name\n\t-F from-addr\n\
  278. \t-i msg-id\n\t-r reply-to\n\t-R references\n\
  279. \t-s subject\n\n"));
  280.  
  281.     exit(1);
  282. }
  283.