home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / comm / ums / SUMSTools.lha / UMS / Tools / SUMSTools / Source / umsfilter.c < prev    next >
C/C++ Source or Header  |  1997-01-09  |  24KB  |  474 lines

  1.  
  2. /*
  3. ** Make the generale purpose filter from filter.c to be
  4. ** a filter for the UMS message base.
  5. **
  6. ** Defines a FilterSpec with certain keywords and a bithook
  7. ** for the actual filtering.
  8. */
  9.  
  10. /*
  11. ** 03-Jan-94 [kmel] Corrected FIL_ACTION_LOWER at case ID_DATE,
  12. **                  because UMS only selects _younger_ messages
  13. **                  compared with the defined date.
  14. **
  15. ** 02-May-94 [MS]   corrected search for constant expressions on
  16. **                  header fields without index
  17. **
  18. ** 23-Mar-95 [SS]   ID_LENGTH, new filter: TextLength, HeaderLength
  19. */
  20.  
  21.  
  22. /* SMAKE */
  23.  
  24.  
  25. #include "sumstl.h"
  26.  
  27. #include <stdio.h>
  28.  
  29. #include "filter.h"
  30. #include "umsfilter.h"
  31. #include "date.h"
  32.  
  33. extern struct Library *UMSBase;
  34. extern struct DosLibrary *DOSBase;
  35.  
  36. #define ID_STRING  1
  37. #define ID_UFLAG   2
  38. #define ID_GFLAG   3
  39. #define ID_DATE    4
  40. #define ID_AGE     5
  41. #define ID_MSGNUM  6
  42. #define ID_LENGTH  7
  43.  
  44.  
  45. static BOOL chktrue(char *s)
  46. {
  47.         if (atol(s) || !stricmp(s,"yes") || !stricmp(s,"on") || !stricmp(s,"ja") || !stricmp(s,"ein") || !stricmp(s,"an"))
  48.                 return(TRUE);
  49.         else
  50.                 return(FALSE);
  51. }
  52.  
  53.  
  54. static VOID SetFromHelp(UMSAccount acc,ULONG help,struct FilterHookPacket *fhp)
  55. {
  56.         UMSSelectTags(acc,
  57.                 UMSTAG_SelReadLocal , TRUE,
  58.                 UMSTAG_SelMask      , fhp->mask  | help,
  59.                 UMSTAG_SelMatch     , fhp->match | help,
  60.                 UMSTAG_SelWriteLocal, TRUE,
  61.                 UMSTAG_SelSet       , fhp->set,
  62.                 UMSTAG_SelUnset     , fhp->clear,
  63.                 TAG_DONE);
  64.  
  65.         UMSSelectTags(acc,
  66.                 UMSTAG_SelReadLocal , TRUE,
  67.                 UMSTAG_SelMask      , help,
  68.                 UMSTAG_SelMatch     , help,
  69.                 UMSTAG_SelWriteLocal, TRUE,
  70.                 UMSTAG_SelUnset     , help,
  71.                 TAG_DONE);
  72. }
  73.  
  74.  
  75.  
  76. #ifdef _DCC
  77. __geta4 static LONG bitfunc(__A0 struct Hook *hook, __A2 UMSAccount acc, __A1 struct FilterHookPacket *fhp)
  78. #else
  79. static LONG __saveds __asm bitfunc(register __a0 struct Hook *hook, register __a2 UMSAccount acc, register __a1 struct FilterHookPacket *fhp)
  80. #endif
  81. {
  82.         LONG res = FIL_ERROR_MISC;
  83.  
  84. /* printf("func: mask %08lx match %08lx set %08lx clear %08lx action %ld data %s\n",fhp->mask,fhp->match,fhp->set,fhp->clear,fhp->action,fhp->data); */
  85.  
  86.         switch (fhp->action)
  87.         {
  88.                 case FIL_ACTION_BITS:
  89.                 {
  90.                         UMSSelectTags(acc,
  91.                                 UMSTAG_SelReadLocal , TRUE,
  92.                                 UMSTAG_SelMask      , fhp->mask ,
  93.                                 UMSTAG_SelMatch     , fhp->match,
  94.                                 UMSTAG_SelWriteLocal, TRUE,
  95.                                 UMSTAG_SelSet       , fhp->set,
  96.                                 UMSTAG_SelUnset     , fhp->clear,
  97.                                 TAG_DONE);
  98.  
  99.                         res = FIL_ERROR_OK;
  100.                 }
  101.                 break;
  102.  
  103.                 case FIL_ACTION_EQUAL:
  104.                 {
  105.                         switch (fhp->uid)
  106.                         {
  107.                                 case ID_LENGTH:
  108.                                 {
  109.                                         UMSMsgNum nr=0L;
  110.  
  111.                                         while (nr = UMSSearchTags(acc,
  112.                                                     UMSTAG_SearchLast   , nr,
  113.                                                     UMSTAG_SearchLocal  , TRUE,
  114.                                                     UMSTAG_SearchMask   , fhp->mask ,
  115.                                                     UMSTAG_SearchMatch  , fhp->match,
  116.                                                  TAG_DONE))
  117.                                         {
  118.                                                 LONG size;
  119.  
  120.                                                 if (UMSReadMsgTags(acc,
  121.                                                     UMSTAG_RMsgNum    , nr,
  122.                                                     UMSTAG_RNoUpdate  , TRUE,
  123.                                                     (ULONG)fhp->udata , &size,
  124.                                                     TAG_DONE))
  125.                                                 {
  126.                                                         if (size==atol(fhp->data))
  127.                                                         {
  128.                                                                 UMSSelectTags(acc,
  129.                                                                         UMSTAG_SelWriteLocal, TRUE,
  130.                                                                         UMSTAG_SelStart     , nr,
  131.                                                                         UMSTAG_SelStop      , nr+1,
  132.                                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  133.                                                                         TAG_DONE);
  134.                                                         }
  135.                                                 }
  136.  
  137.                                                 SetFromHelp(acc,FIL_LASTBIT,fhp);
  138.                                         }
  139.  
  140.                                         res = FIL_ERROR_OK;
  141.                                 }
  142.                                 break;
  143.  
  144.                                 case ID_STRING:
  145.                                 {
  146.                                         if (strchr(fhp->data,'#') || strchr(fhp->data,'?') || strchr(fhp->data,'*'))
  147.                                         {
  148.                                                 UMSMsgNum nr=0L;
  149.  
  150.                                                 while (nr = UMSSearchTags(acc,
  151.                                                             UMSTAG_SearchLast   , nr,
  152.                                                             UMSTAG_SearchLocal  , TRUE,
  153.                                                             UMSTAG_SearchMask   , fhp->mask ,
  154.                                                             UMSTAG_SearchMatch  , fhp->match,
  155.                                                                                 (ULONG)fhp->udata   , fhp->data,
  156.                                                                                 UMSTAG_SearchPattern, 2L,
  157.                                                          TAG_DONE))
  158.                                                 {
  159.                                                         UMSSelectTags(acc,
  160.                                                                 UMSTAG_SelWriteLocal, TRUE,
  161.                                                                 UMSTAG_SelStart     , nr,
  162.                                                                 UMSTAG_SelStop      , nr+1,
  163.                                                                 UMSTAG_SelSet       , FIL_LASTBIT,
  164.                                                                 TAG_DONE);
  165.                                                 }
  166.                                         }
  167.                                         else
  168.                                         {
  169.                                                 if (UMSSelectTags(acc,
  170.                                                         UMSTAG_SelQuick     , TRUE,
  171.                                                         UMSTAG_SelWriteLocal, TRUE,
  172.                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  173.                                                         (ULONG)fhp->udata   , fhp->data,
  174.                                                         TAG_DONE)==0L)
  175.                                                         if (UMSErrNum(acc)==UMSERR_MissingIndex)
  176.                                                         {
  177.                                                                 UMSMsgNum nr=0L;
  178.  
  179.                                                                 while (nr = UMSSearchTags(acc,
  180.                                                                     UMSTAG_SearchLast   , nr,
  181.                                                                     UMSTAG_SearchLocal  , TRUE,
  182.                                                                     UMSTAG_SearchMask   , fhp->mask ,
  183.                                                                     UMSTAG_SearchMatch  , fhp->match,
  184.                                                                                 (ULONG)fhp->udata   , fhp->data,
  185.                                                                                 UMSTAG_SearchPattern, 2L,
  186.                                                                  TAG_DONE))
  187.                                                                 {
  188.                                                                         UMSSelectTags(acc,
  189.                                                                         UMSTAG_SelWriteLocal, TRUE,
  190.                                                                         UMSTAG_SelStart     , nr,
  191.                                                                         UMSTAG_SelStop      , nr+1,
  192.                                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  193.                                                                         TAG_DONE);
  194.                                                                 }
  195.                                                         }
  196.                                         }
  197.  
  198.                                         SetFromHelp(acc,FIL_LASTBIT,fhp);
  199.  
  200.                                         res = FIL_ERROR_OK;
  201.                                 }
  202.                                 break;
  203.  
  204.                                 case ID_GFLAG:
  205.                                 case ID_UFLAG:
  206.                                 {
  207.                                         UMSSelectTags(acc,
  208.                                                 fhp->uid==ID_GFLAG ? UMSTAG_SelReadGlobal : TAG_IGNORE, TRUE,
  209.                                                 UMSTAG_SelWriteLocal, TRUE,
  210.                                                 UMSTAG_SelMask      , (ULONG)fhp->udata,
  211.                                                 UMSTAG_SelMatch     , chktrue(fhp->data) ? (ULONG)fhp->udata : 0,
  212.                                                 UMSTAG_SelSet       , FIL_LASTBIT,
  213.                                                 TAG_DONE);
  214.  
  215.                                         SetFromHelp(acc,FIL_LASTBIT,fhp);
  216.  
  217.                                         res = FIL_ERROR_OK;
  218.                                 }
  219.                                 break;
  220.  
  221.                                 case ID_AGE:
  222.                                 case ID_DATE:
  223.                                 {
  224.                                         LONG secs = fhp->uid==ID_DATE ? DateToSeconds(fhp->data) : (CurrentSeconds()/(24*60*60) - atol(fhp->data))*(24*60*60);
  225.  
  226.                                         if (secs>0)
  227.                                         {
  228.                                                 UMSSelectTags(acc,
  229.                                                         UMSTAG_SelWriteLocal, TRUE,
  230.                                                         UMSTAG_SelDate      , secs,
  231.                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  232.                                                         TAG_DONE);
  233.  
  234.                                                 UMSSelectTags(acc,
  235.                                                         UMSTAG_SelWriteLocal, TRUE,
  236.                                                         UMSTAG_SelDate      , secs + 24*60*60-1,
  237.                                                         UMSTAG_SelUnset     , FIL_LASTBIT,
  238.                                                         TAG_DONE);
  239.  
  240.                                                 SetFromHelp(acc,FIL_LASTBIT,fhp);
  241.  
  242.                                                 res = FIL_ERROR_OK;
  243.                                         }
  244.                                 }
  245.                                 break;
  246.  
  247.                                 case ID_MSGNUM:
  248.                                 {
  249.                                         UMSSelectTags(acc,
  250.                                                 UMSTAG_SelWriteLocal, TRUE,
  251.                                                 UMSTAG_SelStart     , atol(fhp->data),
  252.                                                 UMSTAG_SelStop      , atol(fhp->data)+1,
  253.                                                 UMSTAG_SelSet       , FIL_LASTBIT,
  254.                                                 TAG_DONE);
  255.  
  256.                                         SetFromHelp(acc,FIL_LASTBIT,fhp);
  257.  
  258.                                         res = FIL_ERROR_OK;
  259.                                 }
  260.                                 break;
  261.                         }
  262.                 }
  263.                 break;
  264.  
  265.                 case FIL_ACTION_LOWER:
  266.                 {
  267.                         switch (fhp->uid)
  268.                         {
  269.                                 case ID_LENGTH:
  270.                                 {
  271.                                         UMSMsgNum nr=0L;
  272.  
  273.                                         while (nr = UMSSearchTags(acc,
  274.                                                     UMSTAG_SearchLast   , nr,
  275.                                                     UMSTAG_SearchLocal  , TRUE,
  276.                                                     UMSTAG_SearchMask   , fhp->mask ,
  277.                                                     UMSTAG_SearchMatch  , fhp->match,
  278.                                                  TAG_DONE))
  279.                                         {
  280.                                                 LONG size;
  281.  
  282.                                                 if (UMSReadMsgTags(acc,
  283.                                                     UMSTAG_RMsgNum    , nr,
  284.                                                     UMSTAG_RNoUpdate  , TRUE,
  285.                                                     (ULONG)fhp->udata , &size,
  286.                                                     TAG_DONE))
  287.                                                 {
  288.                                                         if (size<atol(fhp->data))
  289.                                                         {
  290.                                                                 UMSSelectTags(acc,
  291.                                                                         UMSTAG_SelWriteLocal, TRUE,
  292.                                                                         UMSTAG_SelStart     , nr,
  293.                                                                         UMSTAG_SelStop      , nr+1,
  294.                                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  295.                                                                         TAG_DONE);
  296.                                                         }
  297.                                                 }
  298.                                         }
  299.  
  300.                                         SetFromHelp(acc,FIL_LASTBIT,fhp);
  301.  
  302.                                         res = FIL_ERROR_OK;
  303.                                 }
  304.                                 break;
  305.  
  306.                                 case ID_AGE:
  307.                                 {
  308.                                         LONG secs = (CurrentSeconds()/(24*60*60) - atol(fhp->data))*(24*60*60);
  309.  
  310.                                         if (secs>0)
  311.                                         {
  312.                                                 UMSSelectTags(acc,
  313.                                                         UMSTAG_SelWriteLocal, TRUE,
  314.                                                         UMSTAG_SelDate      , secs + 24*60*60,
  315.                                                         UMSTAG_SelSet       , fhp->set,
  316.                                                         UMSTAG_SelUnset     , fhp->clear,
  317.                                                         TAG_DONE);
  318.  
  319.                                                 res = FIL_ERROR_OK;
  320.                                         }
  321.                                 }
  322.                                 break;
  323.  
  324.                                 case ID_DATE:
  325.                                 {
  326.                                         LONG secs = DateToSeconds(fhp->data);
  327.  
  328.                                         if (secs>0)
  329.                                         {
  330.                                                 UMSSelectTags(acc,
  331.                                                         UMSTAG_SelWriteLocal, TRUE,
  332.                                                         UMSTAG_SelMask      , 0,
  333.                                                         UMSTAG_SelMatch     , 0,
  334.                                                         UMSTAG_SelSet       , FIL_LASTBIT,
  335.                                                         TAG_DONE);
  336.  
  337.                                                 UMSSelectTags(acc,
  338.                                                         UMSTAG_SelWriteLocal, TRUE,
  339.                                                         UMSTAG_SelDate      , secs,
  340.                                                         UMSTAG_SelUnset     , FIL_LASTBIT,
  341.                                                         TAG_DONE);
  342.  
  343.                                                 SetFromHelp(acc,FIL_LASTBIT,fhp);
  344.  
  345.                                                 res = FIL_ERROR_OK;
  346.                                         }
  347.                                 }
  348.                                 break;
  349.  
  350.                                 case ID_MSGNUM:
  351.                                 {
  352.                                         UMSSelectTags(acc,
  353.                                                 UMSTAG_SelWriteLocal, TRUE,
  354.                                                 UMSTAG_SelStart     , 0,
  355.                                                 UMSTAG_SelStop      , atol(fhp->data),
  356.                                                 UMSTAG_SelSet       , FIL_LASTBIT,
  357.                                                 TAG_DONE);
  358.  
  359.                                         SetFromHelp(acc,FIL_LASTBIT,fhp);
  360.  
  361.                                         res = FIL_ERROR_OK;
  362.                                 }
  363.                                 break;
  364.  
  365.                                 default:
  366.                                 {
  367.                                         res = FIL_ERROR_INVALIDRELATION;
  368.                                 }
  369.                                 break;
  370.                         }
  371.                 }
  372.                 break;
  373.  
  374.                 default:
  375.                 {
  376.                         res = FIL_ERROR_INVALIDRELATION;
  377.                 }
  378.                 break;
  379.         }
  380.         return(res);
  381. }
  382.  
  383.  
  384. static const struct Hook bithook = { { NULL,NULL },(VOID *)bitfunc,NULL,NULL };
  385.  
  386.  
  387. struct FilterSpec UmsFilterSpec[] =
  388. {
  389.         { "MsgText"      , ID_STRING , (APTR)UMSTAG_WMsgText       },
  390.         { "FromName"     , ID_STRING , (APTR)UMSTAG_WFromName      },
  391.         { "FromAddr"     , ID_STRING , (APTR)UMSTAG_WFromAddr      },
  392.         { "ToName"       , ID_STRING , (APTR)UMSTAG_WToName        },
  393.         { "ToAddr"       , ID_STRING , (APTR)UMSTAG_WToAddr        },
  394.         { "MsgID"        , ID_STRING , (APTR)UMSTAG_WMsgID         },
  395.         { "CreationDate" , ID_STRING , (APTR)UMSTAG_WCreationDate  },
  396.         { "ReceiveDate"  , ID_STRING , (APTR)UMSTAG_WReceiveDate   },
  397.         { "ReferID"      , ID_STRING , (APTR)UMSTAG_WReferID       },
  398.         { "Group"        , ID_STRING , (APTR)UMSTAG_WGroup         },
  399.         { "Subject"      , ID_STRING , (APTR)UMSTAG_WSubject       },
  400.         { "Attributes"   , ID_STRING , (APTR)UMSTAG_WAttributes    },
  401.         { "Comments"     , ID_STRING , (APTR)UMSTAG_WComments      },
  402.         { "Organization" , ID_STRING , (APTR)UMSTAG_WOrganization  },
  403.         { "Distribution" , ID_STRING , (APTR)UMSTAG_WDistribution  },
  404.         { "Folder"       , ID_STRING , (APTR)UMSTAG_WFolder        },
  405.         { "FidoID"       , ID_STRING , (APTR)UMSTAG_WFidoID        },
  406.         { "MausID"       , ID_STRING , (APTR)UMSTAG_WMausID        },
  407.         { "ReplyGroup"   , ID_STRING , (APTR)UMSTAG_WReplyGroup    },
  408.         { "ReplyName"    , ID_STRING , (APTR)UMSTAG_WReplyName     },
  409.         { "ReplyAddr"    , ID_STRING , (APTR)UMSTAG_WReplyAddr     },
  410.         { "LogicalToName", ID_STRING , (APTR)UMSTAG_WLogicalToName },
  411.         { "LogicalToAddr", ID_STRING , (APTR)UMSTAG_WLogicalToAddr },
  412.         { "FileName"     , ID_STRING , (APTR)UMSTAG_WFileName      },
  413.         { "RFCMsgNum"    , ID_STRING , (APTR)UMSTAG_WRFCMsgNum     },
  414.         { "FidoText"     , ID_STRING , (APTR)UMSTAG_WFidoText      },
  415.         { "ErrorText"    , ID_STRING , (APTR)UMSTAG_WErrorText     },
  416.         { "Newsreader"   , ID_STRING , (APTR)UMSTAG_WNewsreader    },
  417.         { "RFCAttr"      , ID_STRING , (APTR)UMSTAG_WRfcAttr       },
  418.         { "FTNAttr"      , ID_STRING , (APTR)UMSTAG_WFtnAttr       },
  419.         { "ZerAttr"      , ID_STRING , (APTR)UMSTAG_WZerAttr       },
  420.         { "MausAttr"     , ID_STRING , (APTR)UMSTAG_WMausAttr      },
  421.         { "TempFileName" , ID_STRING , (APTR)UMSTAG_WTempFileName  },
  422.  
  423.         { "Archive"      , ID_UFLAG  , (APTR)UMSUSTATF_Archive     },
  424.         { "Junk"         , ID_UFLAG  , (APTR)UMSUSTATF_Junk        },
  425.         { "PostPoned"    , ID_UFLAG  , (APTR)UMSUSTATF_PostPoned   },
  426.         { "Selected"     , ID_UFLAG  , (APTR)UMSUSTATF_Selected    },
  427.         { "Old"          , ID_UFLAG  , (APTR)UMSUSTATF_Old         },
  428.         { "WriteAccess"  , ID_UFLAG  , (APTR)UMSUSTATF_WriteAccess },
  429.         { "ReadAccess"   , ID_UFLAG  , (APTR)UMSUSTATF_ReadAccess  },
  430.         { "ViewAccess"   , ID_UFLAG  , (APTR)UMSUSTATF_ViewAccess  },
  431.         { "Owner"        , ID_UFLAG  , (APTR)UMSUSTATF_Owner       },
  432.  
  433.         { "UFlag0"       , ID_UFLAG  , (APTR)(1<<0 )               },
  434.         { "UFlag1"       , ID_UFLAG  , (APTR)(1<<1 )               },
  435.         { "UFlag2"       , ID_UFLAG  , (APTR)(1<<2 )               },
  436.         { "UFlag3"       , ID_UFLAG  , (APTR)(1<<3 )               },
  437.         { "UFlag4"       , ID_UFLAG  , (APTR)(1<<4 )               },
  438.         { "UFlag5"       , ID_UFLAG  , (APTR)(1<<5 )               },
  439.         { "UFlag6"       , ID_UFLAG  , (APTR)(1<<6 )               },
  440.         { "UFlag7"       , ID_UFLAG  , (APTR)(1<<7 )               },
  441.         { "UFlag8"       , ID_UFLAG  , (APTR)(1<<8 )               },
  442.         { "UFlag9"       , ID_UFLAG  , (APTR)(1<<9 )               },
  443.         { "UFlagA"       , ID_UFLAG  , (APTR)(1<<10)               },
  444.         { "UFlagB"       , ID_UFLAG  , (APTR)(1<<11)               },
  445.         { "UFlagC"       , ID_UFLAG  , (APTR)(1<<12)               },
  446.         { "UFlagD"       , ID_UFLAG  , (APTR)(1<<13)               },
  447.         { "UFlagE"       , ID_UFLAG  , (APTR)(1<<14)               },
  448.         { "UFlagF"       , ID_UFLAG  , (APTR)(1<<15)               },
  449.  
  450.         { "Deleted"      , ID_GFLAG  , (APTR)UMSGSTATF_Deleted     },
  451.         { "Expired"      , ID_GFLAG  , (APTR)UMSGSTATF_Expired     },
  452.         { "Exported"     , ID_GFLAG  , (APTR)UMSGSTATF_Exported    },
  453.         { "Orphan"       , ID_GFLAG  , (APTR)UMSGSTATF_Orphan      },
  454.         { "Link"         , ID_GFLAG  , (APTR)UMSGSTATF_Link        },
  455.         { "Hardlink"     , ID_GFLAG  , (APTR)UMSGSTATF_HardLink    },
  456.         { "Parked"       , ID_GFLAG  , (APTR)UMSGSTATF_Parked      },
  457.         { "HasFile"      , ID_GFLAG  , (APTR)UMSGSTATF_HasFile     },
  458.  
  459.         { "Age"          , ID_AGE    , 0                           },
  460.         { "Date"         , ID_DATE   , 0                           },
  461.         { "MsgNum"       , ID_MSGNUM , NULL                        },
  462.  
  463.         { "TextLength"   , ID_LENGTH , (APTR)UMSTAG_RTextLength    },
  464.         { "HeaderLength" , ID_LENGTH , (APTR)UMSTAG_RHeaderLength  },
  465.  
  466.         { NULL          , NULL      , NULL                        }
  467. };
  468.  
  469.  
  470. LONG UmsFilterExpression(char *str,UMSAccount acc,ULONG mask,ULONG match,ULONG setbit)
  471. {
  472.         return(FilterExpression((struct Hook *)&bithook,UmsFilterSpec,str,acc,mask,match,setbit));
  473. }
  474.