commit ff22564ef6fdf78dda62bab361029c176b9b1cc7
parent 95b47238800fbf2e1a37feea3cad882fafe6817a
Author: parazyd <parazyd@dyne.org>
Date: Fri, 22 Feb 2019 18:49:30 +0100
Refactor sieve.c.
Diffstat:
M | sieve.c | | | 110 | ++++++++++++++++++++++++++++--------------------------------------------------- |
1 file changed, 39 insertions(+), 71 deletions(-)
diff --git a/sieve.c b/sieve.c
@@ -12,72 +12,43 @@
char *argv0;
int vflag;
-int gento(char *line) {
- char *type, *email, *what, *mbox;
-
- type = strtok(line, ",");
- if (!type) return 1;
-
- email = strtok(NULL, ",");
- if (!email) return 1;
-
- what = strtok(NULL, ",");
- if (!what) return 1;
-
- mbox = strtok(NULL, ",");
- if (!mbox) return 1;
-
- printf("if header :contains [ \"To\",\"Cc\" ] \"%s\"", email);
- printf(" { fileinto :create \"%s\"; stop; }\n", mbox);
-
- if (vflag)
- info("to: %s -> %s\n", email, mbox);
-
- return 0;
-}
-
-int genfrom(char *line) {
- char *type, *email, *what, *mbox;
-
- type = strtok(line, ",");
- if (!type) return 1;
-
- email = strtok(NULL, ",");
- if (!email) return 1;
-
- what = strtok(NULL, ",");
- if (!what) return 1;
-
- mbox = strtok(NULL, ",");
- if (!mbox) return 1;
+int genrule(int rt, char *line) {
+ char *type, *email, m[256];
+ char *mbox = m;
+
+ switch (rt) {
+ case 69:
+ strtok(line, "=");
+ email = strtok(NULL, ",");
+ if (!email)
+ return 1;
+
+ strcpy(mbox, "INBOX");
+ break;
+ case 1: /* To: */
+ case 2: /* From: */
+ type = strtok(line, ",");
+ if (!type) return 1;
+ email = strtok(NULL, ",");
+ if (!email) return 1;
+ mbox = strtok(NULL, ",");
+ if (!mbox)
+ return 1;
+ break;
+ default:
+ return 0;
+ }
- printf("if header :contains \"From\" \"%s\"", email);
+ printf("if header :contains %s \"%s\"", rt != 1 ? "\"From\"" : "[ \"To\",\"Cc\" ]", email);
printf(" { fileinto :create \"%s\"; stop; }\n", mbox);
-
- if (vflag)
- info("from: %s -> %s\n", email, mbox);
-
- return 0;
-}
-
-int genabook(char *line) {
- char *email;
-
- strtok(line, "=");
- email = strtok(NULL, ",");
- if (!email) return 1;
-
- printf("if header :contains \"From\" \"%s\"", email);
- printf(" { fileinto :create \"INBOX\"; stop; }\n");
-
if (vflag)
- info("from: %s -> INBOX\n", email);
+ info("%s: %s -> %s\n", rt == 1 ? "to" : "from", email, mbox);
return 0;
}
int sievemain(int argc, char *argv[]) {
- int c = 0;
+ int c = 0, rt;
char l[MAXLINESIZE];
ARGBEGIN {
@@ -90,21 +61,18 @@ int sievemain(int argc, char *argv[]) {
while (fgets(l, sizeof(l), stdin)) {
c++;
+ rt = -1;
strtok(l, "\n");
- if (!strncmp("to", l, 2)) {
- if (gento(l))
- die("Invalid syntax on line %d: %s\n", c, l);
- }
- else if (!strncmp("from", l, 4)) {
- if (genfrom(l))
- die("Invalid syntax on line %d: %s\n", c, l);
- }
- else if (!strncmp("email=", l, 6)) {
- if (genabook(l))
- die("Invalid syntax on line %d: %s\n", c, l);
- }
+ if (!strncmp("to", l, 2))
+ rt = 1;
+ else if (!strncmp("from", l, 4))
+ rt = 2;
+ else if (!strncmp("email=", l, 6))
+ rt = 69;
+
+ if (genrule(rt, l))
+ die("Invalid syntax on line %d: %s\n", c, l);
}
- printf("\n");
return 0;
}