home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / elm23-2.zip / filter / actions.c next >
C/C++ Source or Header  |  1995-08-07  |  8KB  |  287 lines

  1.  
  2. static char rcsid[] ="@(#)$Id: actions.c,v 4.1.1.2 90/10/07 20:36:41 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.2 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein - elm@DSI.COM
  13.  *            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    actions.c,v $
  17.  * Revision 4.1.1.2  90/10/07  20:36:41  syd
  18.  * allow non-elm mailers to correctly parse filter's folders.
  19.  * From: sane!genmri!doug@crdgw1.ge.com (Doug Becker)
  20.  *
  21.  * Revision 4.1.1.1  90/06/05  20:28:51  syd
  22.  * The open system call in actions.c for EMERGENCY_MAILBOX and EMER_MBOX
  23.  * were tested with the inequality >= 0 exactly backwards.
  24.  * If the user's system mail box (/usr/spool/mail/user_id) is
  25.  * removed the attempt of filter to flock it fails.  If it does not exist then
  26.  * it should create it and then lock it.
  27.  * From: john@hopf.math.nwu.edu (John Franks)
  28.  *
  29.  * Revision 4.1  90/04/28  22:41:53  syd
  30.  * checkin of Elm 2.3 as of Release PL0
  31.  *
  32.  *
  33.  ******************************************************************************/
  34.  
  35.  
  36. /** RESULT oriented routines *chuckle*.  These routines implement the
  37.     actions that result from either a specified rule being true or from
  38.     the default action being taken.
  39. **/
  40.  
  41. #include <stdio.h>
  42. #include <pwd.h>
  43. #include <ctype.h>
  44. #include <fcntl.h>
  45.  
  46. #include "defs.h"
  47. #include "filter.h"
  48.  
  49. FILE *emergency_local_delivery();
  50.  
  51. mail_message(address)
  52. char *address;
  53. {
  54.     /** Called with an address to send mail to.   For various reasons
  55.         that are too disgusting to go into herein, we're going to actually
  56.         open the users mailbox and by hand add this message.  Yech.
  57.         NOTE, of course, that if we're going to MAIL the message to someone
  58.         else, that we'll try to do nice things with it on the fly...
  59.     **/
  60.  
  61.     FILE *pipefd, *tempfd, *mailfd;
  62.     int  in_header = TRUE, line_count = 0, mailunit;
  63.     char tempfile[SLEN], mailbox[SLEN], lockfile[SLEN],
  64.          buffer[VERY_LONG_STRING];
  65.  
  66.     if (verbose && ! log_actions_only && outfd != NULL)
  67.       fprintf(outfd, "filter (%s): Mailing message to %s\n",
  68.            username, address);
  69.  
  70.     if (! show_only) {
  71.       sprintf(tempfile, "%s%d.fil", tempdir, getpid());
  72.  
  73.       if ((tempfd = fopen(tempfile, "r")) == NULL) {
  74.         if (outfd != NULL)
  75.           fprintf(outfd, "filter (%s): Can't open temp file %s!!\n",
  76.             username, tempfile);
  77.         if (outfd != NULL) fclose(outfd);
  78.         exit(1);
  79.       }
  80.  
  81.       if (strcmp(address, username) != 0) {    /* mailing to someone else */
  82.  
  83.         if (already_been_forwarded) {    /* potential looping! */
  84.           if (contains(from, username)) {
  85.         if (outfd != NULL)
  86.               fprintf(outfd,
  87.     "filter (%s): Filter loop detected!  Message left in file %s\n",
  88.             username, tempfile);
  89.             if (outfd != NULL) fclose(outfd);
  90.             exit(0);
  91.           }
  92.         }
  93.  
  94.         if (strcmp(sendmail, mailer) == 0)
  95.           sprintf(buffer, "%s -f %s@%s %s", sendmail, username, hostfromname, address);
  96.         else
  97.           sprintf(buffer, "%s -t", mailer);
  98.  
  99.         if ((pipefd = popen(buffer, "w")) == NULL) {
  100.           if (outfd != NULL)
  101.             fprintf(outfd, "filter (%s): popen %s failed!\n", username, buffer);
  102.           sprintf(buffer, "%s -t <%s", mailer, tempfile);
  103.           system(buffer);
  104.               unlink(tempfile);
  105.           return;
  106.         }
  107.  
  108.         fprintf(pipefd, "Subject: \"%s\"\n", subject);
  109.         fprintf(pipefd, "From: %s@%s (The Filter of %s)\n",
  110.             username, hostfromname, username);
  111.         fprintf(pipefd, "To: %s\n", address);
  112.         fprintf(pipefd, "X-Filtered-By: filter, version %s\n\n", VERSION);
  113.  
  114.         fprintf(pipefd, "-- Begin filtered message --\n\n");
  115.  
  116.         while (fgets(buffer, SLEN, tempfd) != NULL)
  117.           if (already_been_forwarded && in_header)
  118.             in_header = (strlen(buffer) == 1? 0 : in_header);
  119.           else
  120.             fprintf(pipefd," %s", buffer);
  121.  
  122.         fprintf(pipefd, "\n-- End of filtered message --\n");
  123.         pclose(pipefd);
  124.         fclose(tempfd);
  125.  
  126.         return;        /* YEAH!  Wot a slick program, eh? */
  127.  
  128.       }
  129.  
  130.       /** OTHERWISE it is to the current user... **/
  131.  
  132.       sprintf(mailbox,  "%s%s", mailhome, username);
  133.  
  134.       if (! lock()) {
  135.         if (outfd != NULL) {
  136.           fprintf(outfd, "filter (%s): Couldn't create lockfile %s\n",
  137.             username, lockfile);
  138.           fprintf(outfd, "filter (%s): Can't open mailbox %s!\n",
  139.             username, mailbox);
  140.         }
  141.         if ((mailfd = emergency_local_delivery()) == NULL)
  142.           exit(1);
  143.       }
  144.       else if ((mailunit = open(mailbox, O_APPEND | O_WRONLY | O_CREAT, 0600)) >= 0)
  145.         mailfd = fdopen(mailunit, "a");
  146.       else if ((mailfd = emergency_local_delivery()) == NULL)
  147.         exit(1);
  148.  
  149. #ifdef MMDF
  150.       fputs(MSG_SEPERATOR, mailfd);
  151. #endif
  152.  
  153.       while (fgets(buffer, sizeof(buffer), tempfd) != NULL) {
  154.         line_count++;
  155.         if (the_same(buffer, "From ") && line_count > 1)
  156.           fprintf(mailfd, ">%s", buffer);
  157.         else
  158.           fputs(buffer, mailfd);
  159.       }
  160.  
  161.       fputs("\n\n", mailfd);
  162.  
  163.       fclose(mailfd);
  164.       unlock();        /* blamo or not?  Let it decide! */
  165.       fclose(tempfd);
  166.     } /* end if show only */
  167. }
  168.  
  169. save_message(foldername)
  170. char *foldername;
  171. {
  172.     /** Save the message in a folder.  Use full file buffering to
  173.         make this work without contention problems **/
  174.  
  175.     FILE  *fd, *tempfd;
  176.     char  filename[SLEN], buffer[SLEN];
  177.     int   fdunit;
  178.  
  179.     if (verbose && outfd != NULL)
  180.       fprintf(outfd, "filter (%s): Message saved in folder %s\n",
  181.           username, foldername);
  182.  
  183.     if (!show_only) {
  184.       sprintf(filename, "%s%d.fil", tempdir, getpid());
  185.  
  186.       if ((fdunit = open(foldername, O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
  187.         if (outfd != NULL)
  188.           fprintf(outfd,
  189.          "filter (%s): can't save message to requested folder %s!\n",
  190.             username, foldername);
  191.         return(1);
  192.       }
  193.       fd = fdopen(fdunit,"a");
  194.  
  195.       if ((tempfd = fopen(filename, "r")) == NULL) {
  196.         if (outfd != NULL)
  197.           fprintf(outfd,
  198.              "filter (%s): can't open temp file for reading!\n",
  199.              username);
  200.          return(1);
  201.       }
  202.  
  203. #ifdef MMDF
  204.       fputs(MSG_SEPERATOR, fd);
  205. #endif
  206.  
  207.       while (fgets(buffer, sizeof(buffer), tempfd) != NULL)
  208.         fputs(buffer, fd);
  209.  
  210.       /*
  211.        * Add two newlines, to ensure that other mailers (which, unlike
  212.        * elm, may only look for \n\nFrom_ as the start-of-message
  213.        * indicator).
  214.        */
  215.       fprintf(fd, "%s", "\n\n");
  216.  
  217.       fclose(fd);
  218.       fclose(tempfd);
  219.     }
  220.  
  221.      return(0);
  222. }
  223.  
  224. execute(command)
  225. char *command;
  226. {
  227.     /** execute the indicated command, feeding as standard input the
  228.         message we have.
  229.     **/
  230.  
  231.     char buffer[SLEN];
  232.  
  233.     if (verbose && outfd != NULL)
  234.       fprintf(outfd, "filter (%s): Executing %s\n", username, command);
  235.  
  236.     if (! show_only) {
  237.       sprintf(buffer, "%s <%s%d.fil", command, tempdir, getpid());
  238.       system(buffer);
  239.     }
  240. }
  241.  
  242. FILE *
  243. emergency_local_delivery()
  244. {
  245.     /** This is called when we can't deliver the mail to the usual
  246.         mailbox in the usual way ...
  247.     **/
  248.  
  249.     FILE *tempfd;
  250.     char  mailbox[SLEN];
  251.     int   mailunit;
  252.  
  253.     sprintf(mailbox, "%s/%s", home, EMERGENCY_MAILBOX);
  254.  
  255.     if ((mailunit = open(mailbox, O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
  256.       if (outfd != NULL)
  257.         fprintf(outfd, "filter (%s): Can't open %s either!!\n",
  258.             username, mailbox);
  259.  
  260.       sprintf(mailbox,"%s/%s", home, EMERG_MBOX);
  261.  
  262.       if ((mailunit = open(mailbox, O_APPEND | O_WRONLY | O_CREAT, 0600)) < 0) {
  263.  
  264.         if (outfd != NULL) {
  265.           fprintf(outfd,"filter (%s): Can't open %s either!!!!\n",
  266.               username, mailbox);
  267.           fprintf(outfd,
  268.               "filter (%s): I can't open ANY mailboxes!  Augh!!\n",
  269.                username);
  270.          }
  271.  
  272.          leave("Cannot open any mailbox");        /* DIE DIE DIE DIE!! */
  273.        }
  274.        else
  275.          if (outfd != NULL)
  276.            fprintf(outfd, "filter (%s): Using %s as emergency mailbox\n",
  277.                username, mailbox);
  278.       }
  279.       else
  280.         if (outfd != NULL)
  281.           fprintf(outfd, "filter (%s): Using %s as emergency mailbox\n",
  282.               username, mailbox);
  283.  
  284.     tempfd = fdopen(mailunit, "a");
  285.     return((FILE *) tempfd);
  286. }
  287.