home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / sqdev200.zip / samples / killrcat.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  7KB  |  382 lines

  1. /* Comments?  What comments?  :-) */
  2.  
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "msgapi.h"
  8. #include "killrcat.h"
  9.  
  10. #ifndef OS_2
  11. #include "prog.h"
  12. #else
  13. char *firstchar(char *strng,char *delim,int findword)
  14. {
  15.   int x,
  16.       isw,
  17.       sl_d,
  18.       sl_s,
  19.       wordno=0;
  20.  
  21.   char *string,
  22.        *oldstring;
  23.  
  24.   if (! *strng)
  25.     return NULL;
  26.  
  27.   string=oldstring=strng;
  28.  
  29.   sl_d=strlen(delim);
  30.  
  31.   for (string=strng;*string;string++)
  32.   {
  33.     for (x=0,isw=0;x <= sl_d;x++)
  34.       if (*string==delim[x])
  35.         isw=1;
  36.  
  37.     if (isw==0)
  38.     {
  39.       oldstring=string;
  40.       break;
  41.     }
  42.   }
  43.  
  44.   sl_s=strlen(string);
  45.  
  46.   for (wordno=0;(string-oldstring) < sl_s;string++)
  47.   {
  48.     for (x=0,isw=0;x <= sl_d;x++)
  49.       if (*string==delim[x])
  50.       {
  51.         isw=1;
  52.         break;
  53.       }
  54.  
  55.     if (!isw && string==oldstring)
  56.       wordno++;
  57.  
  58.     if (isw && (string != oldstring))
  59.     {
  60.       for (x=0,isw=0;x <= sl_d;x++) if (*(string+1)==delim[x])
  61.       {
  62.         isw=1;
  63.         break;
  64.       }
  65.  
  66.       if (isw==0)
  67.         wordno++;
  68.     }
  69.  
  70.     if (wordno==findword)
  71.       return((string==oldstring || string==oldstring+sl_s) ? string : string+1);
  72.   }
  73.  
  74.   return NULL;
  75. }
  76.  
  77.  
  78. char * Strip_Trailing(char *str,char strip)
  79. {
  80.   int x;
  81.  
  82.   if (str && *str && str[x=strlen(str)-1]==strip)
  83.     str[x]='\0';
  84.  
  85.   return str;
  86. }
  87.  
  88. #define ISLEFT(c) ((c) > (byte)0x80 && (c) < (byte)0xff)
  89. #define ISRIGHT(c) (((c) >= (byte)0x40 && (c) <= (byte)0x7e) || \
  90.                     ((c) >= (byte)0xa1 && (c) <= (byte)0xfe))
  91.  
  92. word ischin(byte *buf)
  93. {
  94.   return (ISLEFT(buf[0]) && ISRIGHT(buf[1]));
  95. }
  96.  
  97. char * stristr(char *string,char *search)
  98. {
  99.   word last_found=0;
  100.   word strlen_search=strlen(search);
  101.   byte l1, l2;
  102.   word i;
  103.  
  104.   if (string)
  105.   {
  106.     while (*string)
  107.     {
  108.       l1=(byte)(ischin(string) ? 2 : 1);
  109.       l2=(byte)(ischin(search+last_found) ? 2 : 1);
  110.  
  111.       if (l1==l2)
  112.         i=(l1==1) ? memicmp(string, search+last_found, l1) : 1;
  113.       else i=1;
  114.       
  115.       if (!i)
  116.         last_found += l1;
  117.       else
  118.       {
  119.         if (last_found != 0)
  120.         {
  121.           string -= last_found-1;
  122.           last_found=0;
  123.           continue;
  124.         }
  125.       }
  126.  
  127.       string += l1;
  128.  
  129.       if (last_found==strlen_search) return(string-last_found);
  130.     }
  131.   }
  132.  
  133.   return(NULL);
  134. }
  135. #endif
  136.  
  137. static void near KillrCatPounce(struct _ka *k)
  138. {
  139.   HAREA harea;
  140.   char *p;
  141.  
  142.   p=k->path;
  143.  
  144.   if (*p=='$')
  145.     p++;
  146.  
  147.   if ((harea=MsgOpenArea(p, MSGAREA_NORMAL,
  148.                          *k->path=='$' ? MSGTYPE_SQUISH : MSGTYPE_SDM))==NULL)
  149.   {
  150.     printf("Can't open message area `%s'!\n", p);
  151.     return;
  152.   }
  153.  
  154.   printf("Scanning %s:\n", p);
  155.   
  156.   MsgLock(harea);
  157.  
  158.   KillrCatStalkPrey(k, harea);
  159.  
  160.   MsgCloseArea(harea);
  161. }
  162.  
  163.  
  164. static void near KillrCatStalkPrey(struct _ka *k, HAREA harea)
  165. {
  166.   char *txt, *found;
  167.   HMSG hmsg;
  168.   XMSG msg;
  169.   long mn;
  170.  
  171.   for (mn=1; (dword)mn <= MsgGetHighMsg(harea); mn++)
  172.   {
  173.     if ((hmsg=MsgOpenMsg(harea, MOPEN_READ, mn))==NULL)
  174.       continue;
  175.     
  176.     if ((txt=malloc((size_t)MsgGetTextLen(hmsg))) != NULL)
  177.     {
  178.       MsgReadMsg(hmsg, &msg, 0L, MsgGetTextLen(hmsg), txt, 0L, NULL);
  179.  
  180.       if ((found=KillrCatFight(k, &msg, txt)) != NULL)
  181.       {
  182.         MsgCloseMsg(hmsg);
  183.         MsgKillMsg(harea, mn);
  184.  
  185.         printf("Nuked #%ld - Found `%s'\n", mn, found);
  186.       }
  187.  
  188.       free(txt);
  189.  
  190.       if (found)
  191.       {
  192.         mn--;
  193.         continue;
  194.       }
  195.     }
  196.  
  197.     MsgCloseMsg(hmsg);
  198.   }
  199. }
  200.  
  201.  
  202. static char * near KillrCatFight(struct _ka *k, XMSG *msg, char *txt)
  203. {
  204.   struct _srch *sr;
  205.  
  206.   for (sr=k->search; sr; sr=sr->next)
  207.   {
  208.     if ((sr->where & WHERE_TO) && stristr(msg->to, sr->text))
  209.       return msg->to;
  210.  
  211.     if ((sr->where & WHERE_FROM) && stristr(msg->from, sr->text))
  212.       return msg->from;
  213.  
  214.     if ((sr->where & WHERE_SUBJ) && stristr(msg->subj, sr->text))
  215.       return msg->subj;
  216.  
  217.     if ((sr->where & WHERE_BODY) && stristr(txt, sr->text))
  218.       return sr->text;
  219.   }
  220.  
  221.   return NULL;
  222. }
  223.  
  224. static word near KillrCatInit(void)
  225. {
  226.   struct _minf mi;
  227.  
  228.   memset(&mi, '\0', sizeof mi);
  229.   mi.def_zone=1;
  230.   mi.req_version=MSGAPI_VERSION;
  231.  
  232.   return (MsgOpenApi(&mi)==0);
  233. }
  234.  
  235.  
  236. static word KillrCatConfig(char *name, struct _ka **ka)
  237. {
  238.   char line[PATHLEN];
  239.   char orig[PATHLEN];
  240.   static char delim[]=" \t\r\n";
  241.   struct _ka *k;
  242.   struct _srch *sr;
  243.   word state=0;
  244.   FILE *fp;
  245.   char *s, *p;
  246.  
  247.   *ka=NULL;
  248.  
  249.   if ((fp=fopen(name, "r"))==NULL)
  250.   {
  251.     printf("Error opening `%s'!\n", name);
  252.     return FALSE;
  253.   }
  254.   
  255.   while (fgets(line, PATHLEN, fp))
  256.   {
  257.     strcpy(orig, line);
  258.  
  259.     if (*line==';')
  260.       continue;
  261.  
  262.     if (state==0)
  263.     {
  264.       if (*line=='\n')
  265.         continue;
  266.  
  267.       s=strtok(line, delim);
  268.  
  269.       if (!line || stricmp(line, "area") != 0)
  270.       {
  271.         printf("Invalid line: `%s'\n", line ? line : "");
  272.         continue;
  273.       }
  274.  
  275.       if ((s=strtok(NULL, delim))==NULL)
  276.       {
  277.         printf("Ran out of memory!\n");
  278.         exit(1);
  279.       }
  280.  
  281.       k=ssmalloc(sizeof(*k));
  282.       k->path=ssstrdup(s);
  283.       k->next=*ka;
  284.       *ka=k;
  285.  
  286.       state=1;
  287.     }
  288.     else
  289.     {
  290.       if (*line=='\n')
  291.       {
  292.         state=0;
  293.         continue;
  294.       }
  295.  
  296.       Strip_Trailing(orig, '\n');
  297.  
  298.       if ((s=strtok(line, delim)) != NULL)
  299.       {
  300.         sr=ssmalloc(sizeof(*sr));
  301.  
  302.         for (p=s; *p; p++)
  303.           switch (toupper(*p))
  304.           {
  305.             case 'T': sr->where |= WHERE_TO;    break;
  306.             case 'F': sr->where |= WHERE_FROM;  break;
  307.             case 'S': sr->where |= WHERE_SUBJ;  break;
  308.             case 'B': sr->where |= WHERE_BODY;  break;
  309.           }
  310.  
  311.         if ((s=firstchar(orig, delim, 2)) != NULL)
  312.           sr->text=ssstrdup(s);
  313.  
  314.         sr->next=k->search;
  315.         k->search=sr;
  316.       }
  317.     }
  318.   }
  319.   
  320.   fclose(fp);
  321.   return 0;
  322. }
  323.  
  324. static void near KillrCatTerminate(void)
  325. {
  326.   MsgCloseApi();
  327. }
  328.  
  329.  
  330.  
  331. static char * near ssstrdup(char *s)
  332. {
  333.   char *str;
  334.  
  335.   if ((str=strdup(s))==NULL)
  336.   {
  337.     puts("Not enough memory for strdup!");
  338.     exit(1);
  339.   }
  340.  
  341.   return str;
  342. }
  343.  
  344. static void * near ssmalloc(size_t size)
  345. {
  346.   void *p;
  347.  
  348.   if ((p=malloc(size))==NULL)
  349.   {
  350.     puts("Not enough memory for malloc!");
  351.     exit(1);
  352.   }
  353.  
  354.   memset(p, '\0', size);
  355.  
  356.   return p;
  357. }
  358.  
  359.  
  360.  
  361. int _stdc main(int argc, char *argv[])
  362. {
  363.   struct _ka *ka, *k;
  364.   
  365.   (void)argc;
  366.  
  367.   printf("\nKILLRCAT  Penultimate Post Pulverizer, Version " VERSION ".\n");
  368.   printf("Copyright 1990-94 by Scott J. Dudley.  All rights reserved.\n\n");
  369.   
  370.   if (!KillrCatInit())
  371.     return 1;
  372.  
  373.   KillrCatConfig(argv[1] ? argv[1] : "killrcat.cfg", &ka);
  374.  
  375.   for (k=ka; k; k=k->next)
  376.     KillrCatPounce(k);
  377.  
  378.   KillrCatTerminate();
  379.   return 0;
  380. }
  381.  
  382.