Back to index

courier  0.68.2
testmaildirfilter.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "maildirfilter.h"
00007 #include      "maildirfiltertypelist.h"
00008 #include      <stdio.h>
00009 #include      <string.h>
00010 #include      <unistd.h>
00011 
00012 
00013 int main(int argc, char **argv)
00014 {
00015 const char *name;
00016 enum maildirfiltertype type;
00017 const char *header;
00018 const char *value;
00019 const char *folder;
00020 int errcode, i;
00021 struct maildirfilter mf;
00022 struct maildirfilterrule *r;
00023 int flags=0;
00024 
00025        if (argc < 6)
00026        {
00027               fprintf(stderr, "Invalid args\n");
00028               return (1);
00029        }
00030 
00031        name=argv[1];
00032 
00033        value=argv[2];
00034        if (*value == '-')
00035        {
00036               flags |= MFR_DOESNOT;
00037               ++value;
00038        }
00039        if (*value == '/')
00040        {
00041               flags |= MFR_BODY;
00042               ++value;
00043        }
00044 
00045        for (i=0; typelist[i].name; i++)
00046               if (strcasecmp(typelist[i].name, value) == 0)
00047                      break;
00048        if (!typelist[i].name)
00049        {
00050               fprintf(stderr, "unknown op: %s\n", argv[2]);
00051               return (1);
00052        }
00053        type=typelist[i].type;
00054        header=argv[3];
00055        value=argv[4];
00056        folder=argv[5];
00057 
00058        memset(&mf, 0, sizeof(mf));
00059 
00060        errcode=maildir_filter_loadrules(&mf, "testrules");
00061        if (errcode && errcode != MF_LOADNOTFOUND)
00062        {
00063               fprintf(stderr, "Error loading testrules: %d\n", errcode);
00064               return (1);
00065        }
00066 
00067        r=maildir_filter_appendrule(&mf, name, type, flags,
00068               header, value, folder, "", &errcode);
00069 
00070        if (!r)
00071        {
00072               fprintf(stderr, "Error appending %s: %d\n", name, errcode);
00073               return (1);
00074        }
00075 
00076        unlink("maildirsize");
00077        errcode=maildir_filter_saverules(&mf, "testrules2", ".", "Maildir", "nobody@example.com");
00078        if (errcode)
00079        {
00080               fprintf(stderr, "Error saving testrules2: %d\n", errcode);
00081               return (1);
00082        }
00083        rename("testrules2", "testrules");
00084        printf("Added %s\n", name);
00085        return (0);
00086 }