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

  1. #pragma off(unreferenced)
  2. static char rcs_id[]="$Id: KILLRCAT.C 1.7 1994/02/17 01:12:48 sjd Exp sjd $";
  3. #pragma on(unreferenced)
  4.  
  5. #include <string.h>
  6.  
  7. #define INCL_VIO
  8. #define INCL_DOS
  9.  
  10. #include <os2.h>
  11. #include "msgapi.h"
  12. #include "sqfeat.h"
  13.  
  14.  
  15. #define FLAG_BODY   0x01
  16. #define FLAG_TO     0x02
  17. #define FLAG_FROM   0x04
  18. #define FLAG_SUBJ   0x08
  19. #define FLAG_CASE   0x10
  20. #define FLAG_EXCT   0x20
  21.  
  22. struct _kksearch
  23. {
  24.   int flag;
  25.   char *txt;
  26.   char *area;
  27.   struct _kksearch *next;
  28. };
  29.  
  30. static struct _kksearch *gpSearch;
  31.  
  32.  
  33. /* Code to handle chinese characters */
  34.  
  35. #define ISLEFT(c) ((c) > (byte)0x80 && (c) < (byte)0xff)
  36. #define ISRIGHT(c) (((c) >= (byte)0x40 && (c) <= (byte)0x7e) || \
  37.                     ((c) >= (byte)0xa1 && (c) <= (byte)0xfe))
  38.  
  39. word ischin(byte *buf)
  40. {
  41.   return (ISLEFT(buf[0]) && ISRIGHT(buf[1]));
  42. }
  43.  
  44. char * stristr(char *string,char *search)
  45. {
  46.   /* "register" keyword used to fix the brain-dead MSC (opti)mizer */
  47.  
  48.   unsigned last_found=0;
  49.   unsigned strlen_search=strlen(search);
  50.   byte l1, l2;
  51.   unsigned i;
  52.  
  53.   if (string)
  54.   {
  55.     while (*string)
  56.     {
  57.       /**** start chinese modifications *****/
  58.       l1=(byte)(ischin(string) ? 2 : 1);
  59.       l2=(byte)(ischin(search+last_found) ? 2 : 1);
  60.  
  61.       if (l1==l2)
  62.         i=(l1==1) ? memicmp(string, search+last_found, l1) : 1;
  63.       else i=1;
  64.       
  65.       if (!i)
  66.         last_found += l1;
  67.       /**** end chinese modifications *****/
  68.       /* old code: if ((tolower(*string))==(tolower(search[last_found])))
  69.                      last_found++;
  70.       */
  71.       else
  72.       {
  73.         if (last_found != 0)
  74.         {
  75.           string -= last_found-1;
  76.           last_found=0;
  77.           continue;
  78.         }
  79.       }
  80.  
  81.       string += l1;
  82.  
  83.       if (last_found==strlen_search) return(string-last_found);
  84.     }
  85.   }
  86.  
  87.   return(NULL);
  88. }
  89.  
  90.  
  91.  
  92. int tolower(int c)
  93. {
  94.   if (c >= 'A' && c <= 'Z')
  95.     c='a'+(c-'A');
  96.  
  97.   return c;
  98. }
  99.  
  100. char * firstchar(char *strng,char *delim,int findword)
  101. {
  102.   int x, isw, wordno=0;
  103.   unsigned sl_d, sl_s;
  104.  
  105.   char *string,
  106.        *oldstring;
  107.  
  108.   /* We can't do *anything* if the string is blank... */
  109.  
  110.   if (! *strng)
  111.     return NULL;
  112.  
  113.   string=oldstring=strng;
  114.  
  115.   sl_d=strlen(delim);
  116.  
  117.   for (string=strng;*string;string++)
  118.   {
  119.     for (x=0,isw=0;x <= sl_d;x++)
  120.       if (*string==delim[x])
  121.         isw=1;
  122.  
  123.     if (isw==0)
  124.     {
  125.       oldstring=string;
  126.       break;
  127.     }
  128.   }
  129.  
  130.   sl_s=strlen(string);
  131.  
  132.   for (wordno=0;(string-oldstring) < sl_s;string++)
  133.   {
  134.     for (x=0,isw=0;x <= sl_d;x++)
  135.       if (*string==delim[x])
  136.       {
  137.         isw=1;
  138.         break;
  139.       }
  140.  
  141.     if (!isw && string==oldstring)
  142.       wordno++;
  143.  
  144.     if (isw && (string != oldstring))
  145.     {
  146.       for (x=0,isw=0;x <= sl_d;x++) if (*(string+1)==delim[x])
  147.       {
  148.         isw=1;
  149.         break;
  150.       }
  151.  
  152.       if (isw==0)
  153.         wordno++;
  154.     }
  155.  
  156.     if (wordno==findword)
  157.       return((string==oldstring || string==oldstring+sl_s) ? string : string+1);
  158.   }
  159.  
  160.   return NULL;
  161. }
  162.  
  163.  
  164.  
  165. void VioPuts(char *txt)
  166. {
  167.   (void)VioWrtTTY(txt, (USHORT)strlen(txt), 0);
  168. }
  169.  
  170. void VioPutc(char ch)
  171. {
  172.   char s[2];
  173.  
  174.   *s=ch;
  175.   s[1]=0;
  176.   VioPuts(s);
  177. }
  178.  
  179. /****************************************************************************
  180.  * FeatureInit:                                                             *
  181.  ****************************************************************************
  182.  *                                                                          *
  183.  * This is called at Squish startup, when the "Features" line in SQUISH.CFG *
  184.  * is processed.  This routine can be used to perform feature-specific      *
  185.  * initialization needs.                                                    *
  186.  *                                                                          *
  187.  * The only action that this function must perform is to fill out the       *
  188.  * 'feature information' structure that is passed to it by Squish.          *
  189.  * At present, the only field in this structure is the 'Config Name'        *
  190.  * option.  This can be used to register keywords for which the             *
  191.  * FeatureConfig function will be called.  This can be used to              *
  192.  * implement feature-specific keywords in the configuration file.           *
  193.  *                                                                          *
  194.  * NOTE:  External features should NOT display anything on the screen       *
  195.  * in this function.                                                        *
  196.  *                                                                          *
  197.  * A return value of 0 indicates that the feature initialized successfully. *
  198.  * A non-zero return value indicates failure and instructs Squish to        *
  199.  * terminate.                                                               *
  200.  *                                                                          *
  201.  ****************************************************************************/
  202.  
  203. word FEATENTRY _export FeatureInit(struct _feat_init far *pfi)
  204. {
  205.   /* The szConfigName field should contain a list of the keywords           *
  206.    * for which FeatureConfig should be called.  These tokens are only       *
  207.    * matched if they are the first word on a given line.  More than one     *
  208.    * token can be given, as long as a "\r" is used to separate adjacent     *
  209.    * tokens, such as "Killrcat\rKillrdog\rKillrbird".                       */
  210.  
  211.   strcpy(pfi->szConfigName, "Killrcat");
  212.   return 0;
  213. }
  214.  
  215.  
  216. /****************************************************************************
  217.  * FeatureConfig                                                            *
  218.  ****************************************************************************
  219.  *                                                                          *
  220.  * This function is called when Squish detects one of the specified         *
  221.  * feature configuration keywords in SQUISH.CFG.  The feature should        *
  222.  * the information on this line as required, and then return to Squish.     *
  223.  *                                                                          *
  224.  * A return value of 0 indicates success.  A non-zero return value          *
  225.  * instructs Squish to abort.                                               *
  226.  *                                                                          *
  227.  ****************************************************************************/
  228.  
  229. word FEATENTRY _export FeatureConfig(struct _feat_config far *pfc)
  230. {
  231.   struct _kksearch *ps;
  232.   char *p;
  233.  
  234.   NW(pfc);
  235.  
  236.   if ((ps=malloc(sizeof(struct _kksearch)))==NULL)
  237.     return 0;
  238.  
  239.   ps->flag=0;
  240.  
  241.   for (p=pfc->ppszArgs[1]; p && *p; p++)
  242.     switch (tolower(*p))
  243.     {
  244.       case 'b':   ps->flag |= FLAG_BODY;  break;
  245.       case 's':   ps->flag |= FLAG_SUBJ;  break;
  246.       case 't':   ps->flag |= FLAG_TO;    break;
  247.       case 'f':   ps->flag |= FLAG_FROM;  break;
  248.       case 'c':   ps->flag |= FLAG_CASE;  break;
  249.       case 'e':   ps->flag |= FLAG_EXCT;  break;
  250.  
  251.       default:
  252.         VioPuts("Invalid KillrCat flag: `");
  253.         VioPutc(*p);
  254.         VioPuts("\r\n");
  255.         break;
  256.     }
  257.  
  258.   if (!pfc->ppszArgs[1] || !pfc->ppszArgs[2] || !pfc->ppszArgs[3])
  259.   {
  260.     VioPuts("Invalid configuration line: `");
  261.     VioPuts(pfc->szConfigLine);
  262.     VioPuts("\r\n");
  263.     return 0;
  264.   }
  265.  
  266.   ps->area=strdup(pfc->ppszArgs[2]);
  267.   ps->txt=strdup(firstchar(pfc->szConfigLine, " \t\n", 4));
  268.  
  269.   ps->next=gpSearch;
  270.   gpSearch=ps;
  271.  
  272.   return 0;
  273. }
  274.  
  275.  
  276.  
  277. /****************************************************************************
  278.  * FeatureNetMsg                                                            *
  279.  ****************************************************************************
  280.  *                                                                          *
  281.  * This function is called just before Squish packs a mesage from a         *
  282.  * netmail area.  Squish will call this function for each netmail message,  *
  283.  * regardless of the status of the MSGSENT bit, unless otherwise defined    *
  284.  * in the feature initialization structure (see FeatureInit).               *
  285.  *                                                                          *
  286.  * Information in the feat_netmsg structure describes the current message   *
  287.  * being processed, in addition to pointers to the message header, body     *
  288.  * text, and control information.                                           *
  289.  *                                                                          *
  290.  * If any special actions are necessary, the feature should fill out the    *
  291.  * ulAction field in the structure before this function terminates.         *
  292.  *                                                                          *
  293.  * A return value of 0 indicates success.  A non-zero return value          *
  294.  * instructs Squish to terminate execution.                                 *
  295.  *                                                                          *
  296.  ****************************************************************************/
  297.  
  298.  
  299. word FEATENTRY _export FeatureNetMsg(struct _feat_netmsg far *pfn)
  300. {
  301.   NW(pfn);
  302.   return 0;
  303. }
  304.  
  305.  
  306. static int near exactcomp(char *t1, char *t2)
  307. {
  308.   return (strcmp(t1, t2)==0);
  309. }
  310.  
  311. static int near incomp(char *t1, char *t2)
  312. {
  313.   int x;
  314.  
  315.   x=!!stristr(t2, t1);
  316. /*  printf("compare %s and %s:\nComp=%d\n", t1, t2, x);*/
  317.   return x;
  318. }
  319.  
  320. word FEATENTRY _export FeatureTossMsg(struct _feat_toss far *pft)
  321. {
  322.   struct _kksearch *ps;
  323.   int (near *comp)(char *t1, char *t2);
  324.   int kill;
  325.  
  326.   for (ps=gpSearch; ps; ps=ps->next)
  327.   {
  328.     if (stricmp(ps->area, pft->szArea) != 0 && stricmp(ps->area, "all") != 0)
  329.       continue;
  330.  
  331.     comp=(ps->flag & FLAG_EXCT) ? exactcomp : incomp;
  332.     kill=FALSE;
  333.  
  334.     if (ps->flag & FLAG_TO)
  335.       kill |= (*comp)(ps->txt, pft->pMsg->to);
  336.  
  337.     if (ps->flag & FLAG_FROM)
  338.       kill |= (*comp)(ps->txt, pft->pMsg->from);
  339.  
  340.     if (ps->flag & FLAG_SUBJ)
  341.       kill |= (*comp)(ps->txt, pft->pMsg->subj);
  342.  
  343.     if (ps->flag & FLAG_BODY)
  344.     {
  345.       kill |= (*comp)(ps->txt, pft->pszCtrl);
  346.       kill |= (*comp)(ps->txt, pft->pszMsgTxt);
  347.     }
  348.  
  349.     if (kill)
  350.     {
  351.       VioPuts("\r\nKillrCat: Kill msg to `");
  352.       VioPuts(pft->pMsg->to);
  353.       VioPuts("' from `");
  354.       VioPuts(pft->pMsg->from);
  355.       VioPuts("' subj `");
  356.       VioPuts(pft->pMsg->subj);
  357.       VioPuts("'\r\n");
  358.  
  359.       strcpy(pft->szArea, "bad_msgs");
  360.       pft->ulTossAction=FTACT_AREA;
  361.       break;
  362.     }
  363.   }
  364.  
  365.   return 0;
  366. }
  367.  
  368.  
  369. word FEATENTRY _export FeatureScanMsg(struct _feat_scan far *pfs)
  370. {
  371.   NW(pfs);
  372.   return 0;
  373. }
  374.  
  375. word FEATENTRY _export FeatureTerm(struct _feat_term far *pft)
  376. {
  377.   NW(pft);
  378.   return 0;
  379. }
  380.  
  381.  
  382. #ifdef __FLAT__
  383. void FEATENTRY _export Feature32Bit(void)
  384. #else
  385. void FEATENTRY _export Feature16Bit(void)
  386. #endif
  387. {
  388. }
  389.  
  390.  
  391.