home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / filter / utils.c < prev   
C/C++ Source or Header  |  1992-03-22  |  4KB  |  167 lines

  1.  
  2. static char rcsid[] ="@(#)$Id: utils.c,v 4.1.1.2 90/10/24 16:08:29 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:    utils.c,v $
  17.  * Revision 4.1.1.2  90/10/24  16:08:29  syd
  18.  * Add FAILED_SAVE case to log
  19.  * From: Steve Cambell
  20.  *
  21.  * Revision 4.1.1.1  90/07/12  20:23:19  syd
  22.  * patch fixes some minor typing mistakes in error messages
  23.  * From: hz247bi@duc220.uni-duisburg.de (Bieniek)
  24.  *
  25.  * Revision 4.1  90/04/28  22:42:03  syd
  26.  * checkin of Elm 2.3 as of Release PL0
  27.  *
  28.  *
  29.  ******************************************************************************/
  30.  
  31. /** Utility routines for the filter program...
  32.  
  33. **/
  34.  
  35. #include <stdio.h>
  36. #include <pwd.h>
  37. #include <ctype.h>
  38. #include <fcntl.h>
  39.  
  40. #include "defs.h"
  41. #include "filter.h"
  42.  
  43. #undef fflush
  44.  
  45. leave(reason)
  46. char *reason;
  47. {
  48.     if (outfd != NULL)
  49.       fprintf(outfd,"filter (%s): LEAVE %s\n", username, reason);
  50.     if (outfd != NULL) fclose(outfd);
  51.     exit(1);
  52. }
  53.  
  54. log(what)
  55. int what;
  56. {
  57.     /** make an entry in the log files for the specified entry **/
  58.  
  59.     FILE *fd;
  60.     char filename[SLEN];
  61.  
  62.     if (! show_only) {
  63.       sprintf(filename, "%s/%s", home, filtersum);    /* log action once! */
  64.       if ((fd = fopen(filename, "a")) == NULL) {
  65.         if (outfd != NULL)
  66.           fprintf(outfd, "filter (%s): Couldn't open log file %s\n",
  67.             username, filename);
  68.         fd = stdout;
  69.       }
  70.       fprintf(fd, "%d\n", rule_choosen);
  71.       fclose(fd);
  72.     }
  73.  
  74.     sprintf(filename, "%s/%s", home, filterlog);
  75.  
  76.     if (show_only)
  77.       fd = stdout;
  78.     else if ((fd = fopen(filename, "a")) == NULL) {
  79.       if (outfd != NULL)
  80.         fprintf(outfd, "filter (%s): Couldn't open log file %s\n",
  81.           username, filename);
  82.       fd = stdout;
  83.     }
  84.  
  85. #ifdef _IOFBF
  86.     setvbuf(fd, NULL, _IOFBF, BUFSIZ);
  87. #endif
  88.  
  89.     if (strlen(from) + strlen(subject) > 60)
  90.       fprintf(fd, "\nMail from %s\n\tabout %s\n", from, subject);
  91.     else
  92.       fprintf(fd, "\nMail from %s about %s\n", from, subject);
  93.  
  94.     if (rule_choosen != -1)
  95.       if (rules[rule_choosen].condition->matchwhat == TO)
  96.         fprintf(fd, "\t(addressed to %s)\n", to);
  97.  
  98.     switch (what) {
  99.       case DELETE_MSG : fprintf(fd, "\tDELETED");            break;
  100.       case FAILED_SAVE: fprintf(fd, "\tSAVE FAILED for file \"%s\"",
  101.                 rules[rule_choosen].argument2);        break;
  102.       case SAVE       : fprintf(fd, "\tSAVED in file \"%s\"",
  103.                 rules[rule_choosen].argument2);        break;
  104.       case SAVECC     : fprintf(fd,"\tSAVED in file \"%s\" AND PUT in mailbox",
  105.                 rules[rule_choosen].argument2);      break;
  106.       case FORWARD    : fprintf(fd, "\tFORWARDED to \"%s\"",
  107.                 rules[rule_choosen].argument2);        break;
  108.       case EXEC       : fprintf(fd, "\tEXECUTED \"%s\"",
  109.                 rules[rule_choosen].argument2);        break;
  110.       case LEAVE      : fprintf(fd, "\tPUT in mailbox");        break;
  111.     }
  112.  
  113.     if (rule_choosen != -1)
  114.       fprintf(fd, " by rule #%d\n", rule_choosen+1);
  115.     else
  116.       fprintf(fd, ": the default action\n");
  117.  
  118.     fflush(fd);
  119.     fclose(fd);
  120. }
  121.  
  122. int
  123. contains(string, pattern)
  124. char *string, *pattern;
  125. {
  126.     /** Returns TRUE iff pattern occurs IN IT'S ENTIRETY in buffer. **/
  127.  
  128.     register int i = 0, j = 0;
  129.  
  130.     while (string[i] != '\0') {
  131.       while (tolower(string[i++]) == tolower(pattern[j++]))
  132.         if (pattern[j] == '\0')
  133.           return(TRUE);
  134.       i = i - j + 1;
  135.       j = 0;
  136.     }
  137.     return(FALSE);
  138. }
  139.  
  140. char *itoa(i, two_digit)
  141. int i, two_digit;
  142. {
  143.     /** return 'i' as a null-terminated string.  If two-digit use that
  144.         size field explicitly!  **/
  145.  
  146.     static char value[10];
  147.  
  148.     if (two_digit)
  149.       sprintf(value, "%02d", i);
  150.     else
  151.       sprintf(value, "%d", i);
  152.  
  153.     return( (char *) value);
  154. }
  155.  
  156. lowercase(string)
  157. char *string;
  158. {
  159.     /** translate string into all lower case **/
  160.  
  161.     register int i;
  162.  
  163.     for (i= strlen(string); --i >= 0; )
  164.       if (isupper(string[i]))
  165.         string[i] = tolower(string[i]);
  166. }
  167.