home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / utils / sndmail.c < prev   
C/C++ Source or Header  |  1997-02-02  |  6KB  |  248 lines

  1. /* sndmail.c - wrapper around IBM sendmail
  2.  * for filtering, detaching, temp file deletion and logging
  3.  *
  4.  * Author:  Kai Uwe Rommel <rommel@ars.de>
  5.  * Created: Fri Apr 22 1994
  6.  */
  7.  
  8. static char *rcsid =
  9. "$Id: sndmail.c,v 1.1 1997/02/02 20:23:36 rommel Exp rommel $";
  10. static char *rcsrev = "$Revision: 1.1 $";
  11.  
  12. /*
  13.  * $Log: sndmail.c,v $
  14.  * Revision 1.1  1997/02/02 20:23:36  rommel
  15.  * Initial revision
  16.  * 
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <process.h>
  22. #include <time.h>
  23.  
  24. #define INCL_NOPM
  25. #include <os2.h>
  26.  
  27. #include <os2elm.h>
  28.  
  29. char *strip_parens(char *string)
  30. {
  31.   /**
  32.     Remove parenthesized information from a string.  More specifically,
  33.     comments as defined in RFC822 are removed.  This procedure is
  34.     non-destructive - a pointer to static data is returned.
  35.     **/
  36.   static char  buffer[5120];
  37.   register char *bufp;
  38.   register int depth;
  39.  
  40.   for ( bufp = buffer, depth = 0 ; *string != '\0' ; ++string ) {
  41.     switch ( *string ) {
  42.     case '(':            /* begin comment on '('        */
  43.       ++depth;
  44.       break;
  45.     case ')':            /* decr nesting level on ')'    */
  46.       --depth;
  47.       break;
  48.     case '\\':            /* treat next char literally    */
  49.       if ( *++string == '\0' ) {        /* gracefully handle    */
  50.     *bufp++ = '\\';            /* '\' at end of string    */
  51.     --string;                /* even tho it's wrong    */
  52.       } else if ( depth == 0 ) {
  53.     *bufp++ = '\\';
  54.     *bufp++ = *string;
  55.       }
  56.       break;
  57.     default:            /* a regular char        */
  58.       if ( depth == 0 )
  59.     *bufp++ = *string;
  60.       break;
  61.     }
  62.   }
  63.   *bufp = '\0';
  64.   return( (char *) buffer);
  65. }
  66.  
  67. void get_address_from(char *prefix, char *line, char *buffer)
  68. {
  69.   register char *s;
  70.  
  71.   /**  Skip start of line over prefix, e.g. "From:".  **/
  72.   line += strlen(prefix);
  73.  
  74.   /**  If there is a '<' then copy from it to '>' into the buffer.  **/
  75.   if ( (s = strchr(line,'<')) != NULL ) 
  76.   {
  77.     while ( ++s , *s != '\0' && *s != '>' ) 
  78.     {
  79.       if ( !isspace(*s) )
  80.     *buffer++ = *s;
  81.     }
  82.     *buffer = '\0';
  83.     return;
  84.   }
  85.  
  86.   /**  Otherwise, strip comments and get address with whitespace elided.  **/
  87.   for ( s = strip_parens(line) ; *s != '\0' ; ++s ) 
  88.   {
  89.     if ( !isspace(*s) )
  90.       *buffer++ = *s;
  91.   }
  92.   *buffer = '\0';
  93. }
  94.  
  95. int nextline(FILE *file, char *buffer, int size)
  96. {
  97.   int len;
  98.  
  99.   if (fgets(buffer, size, file) == NULL)
  100.     return -1;
  101.  
  102.   len = strlen(buffer);
  103.  
  104.   if (buffer[len - 1] == '\n')
  105.     buffer[--len] = 0;
  106.  
  107.   return len;
  108. }
  109.  
  110. int scanmail(char *filename, char *from, char *to, char *cc)
  111. {
  112.   char buffer[5120], buffer2[5120];
  113.   FILE *mail;
  114.   int len, bytes = 0, inheader = 1;
  115.  
  116.   *from = *to = *cc = 0;
  117.  
  118.   if ((mail = fopen(filename, "r")) != NULL)
  119.   {
  120.     while ((len = nextline(mail, buffer, sizeof(buffer))) != -1)
  121.     {
  122.       bytes += len + 1; /* don't forget \n */
  123.  
  124.       if (inheader)
  125.       {
  126.     while (buffer[strlen(buffer) - 1] == ',')
  127.     {
  128.       if ((len = nextline(mail, buffer2, sizeof(buffer2))) == -1)
  129.         break;
  130.  
  131.       strcat(buffer, buffer2);
  132.       bytes += len + 1;
  133.     }
  134.  
  135.     if (strnicmp(buffer, "Resent-From:", 12) == 0)
  136.       get_address_from("Resent-From:", buffer, from);
  137.     if (strnicmp(buffer, "From:", 5) == 0 && *from == 0)
  138.       get_address_from("From:", buffer, from);
  139.  
  140.     if (strnicmp(buffer, "Resent-To:", 10) == 0)
  141.       get_address_from("Resent-To:", buffer, to);
  142.     if (strnicmp(buffer, "To:", 3) == 0 && *to == 0)
  143.       get_address_from("To:", buffer, to);
  144.  
  145.     if (strnicmp(buffer, "Resent-Cc:", 10) == 0)
  146.       get_address_from("Resent-Cc:", buffer, cc);
  147.     if (strnicmp(buffer, "Cc:", 3) == 0 && *cc == 0)
  148.       get_address_from("Cc:", buffer, cc);
  149.  
  150.     if (len == 0)
  151.       inheader = 0;
  152.       }
  153.     }
  154.  
  155.     fclose(mail);
  156.   }
  157.  
  158.   return bytes;
  159. }
  160.  
  161. void logentry(int rc, int bytes, char *from, char *to)
  162. {
  163.   char filename[256], datetime[32];
  164.   FILE *log;
  165.   time_t now;
  166.   struct tm *tm;
  167.  
  168.   time(&now);
  169.   tm = localtime(&now);
  170.   strftime(datetime, sizeof(datetime), "%m/%d-%H:%M", tm);
  171.  
  172.   strcpy(filename, logdir);
  173.   strcat(filename, "/");
  174.   strcat(filename, "sndmail.log");
  175.  
  176.   if ((log = fopen(filename, "a")) != NULL)
  177.   {
  178.     fprintf(log, "%s sent(%d) mail (%d bytes) from %s to %s\n", datetime, rc, bytes, from, to);
  179.     fclose(log);
  180.   }
  181. }
  182.  
  183. int main(int argc, char **argv)
  184. {
  185.   char from[256], to[5120], cc[5120], cmd[1024];
  186.   int rc, bytes, i;
  187.  
  188.   if (argc == 1)
  189.     return printf("This program is only called internally by a mail application.\n"), 1;
  190.  
  191.   initpaths();
  192.   
  193.   if (stricmp(argv[1], "-bg") == 0)
  194.   {
  195.     argv[1] = argv[0];
  196.     argv++;
  197.  
  198.     rc = spawnvp(P_DETACH, argv[0], (char * const *) argv);
  199.  
  200.     return (rc == -1);
  201.   }
  202.  
  203.   bytes = from[0] = to[0] = cc[0] = 0;
  204.  
  205.   if (stricmp(argv[1], "-af") == 0)
  206.   {
  207.     bytes = scanmail(argv[2], from, to, cc);
  208.     if (cc[0])
  209.       strcat(strcat(to, ","), cc);
  210.   }
  211.  
  212.   if (stricmp(argv[3], "-f") == 0)
  213.     strcpy(from, argv[4]);
  214.  
  215.   if (stricmp(argv[5], "-t") == 0)
  216.     i = 6; /* even with -t there may be more recipients, such as Bcc: ones */
  217.   else
  218.   {
  219.     to[0] = 0; /* ONLY command line passed recipients */
  220.     i = 5;
  221.   }
  222.  
  223.   for ( ; i < argc; i++)
  224.   {
  225.     if (to[0] != 0)
  226.       strcat(to, ",");
  227.     strcat(to, argv[i]);
  228.   }
  229.  
  230.   if (stricmp(argv[1], "-af") == 0)
  231.   {
  232.     sprintf(cmd,"sndfilt %s %s %s", argv[2], from, to);
  233.     system(cmd);
  234.   }
  235.  
  236.   argv[0] = "sendmail";
  237.   rc = spawnvp(P_WAIT, argv[0], (char * const *) argv);
  238.  
  239.   if (stricmp(argv[1], "-af") == 0)
  240.     unlink(argv[2]);
  241.  
  242.   logentry(rc, bytes, from, to);
  243.   
  244.   return rc;
  245. }
  246.  
  247. /* end of sndmail.c */
  248.