rp

simple email tools
git clone https://git.parazyd.org/rp
Log | Files | Refs | README | LICENSE

sieve.c (1623B)


      1 /* See LICENSE file for copyright and license details */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 #include "arg.h"
      7 #include "ind.h"
      8 
      9 char *argv0;
     10 int vflag;
     11 
     12 int genrule(int rt, char *line) {
     13 	char *type, *fv, m[256], f[256];
     14 	char *mbox = m, *filt = f;
     15 
     16 	switch (rt) {
     17 	case 69: /* whitelist.abook */
     18 		strcpy(filt, "\"From\"");
     19 		strcpy(mbox, "INBOX");
     20 		break;
     21 	case 1: /* To: */
     22 		strcpy(filt, "[ \"To\",\"Cc\" ]");
     23 		break;
     24 	case 2: /* From: */
     25 		strcpy(filt, "\"From\"");
     26 		break;
     27 	case 3: /* Subject: */
     28 		strcpy(filt, "\"Subject\"");
     29 		break;
     30 	default:
     31 		return 1;
     32 	}
     33 
     34 	switch (rt) {
     35 	case 69:
     36 		strtok(line, "=");
     37 		fv= strtok(NULL, ",");
     38 		if (!fv)
     39 			return 1;
     40 		break;
     41 	case 1: /* To: */
     42 	case 2: /* From: */
     43 	case 3: /* Subject: */
     44 		type = strtok(line, "/");
     45 		if (!type) return 1;
     46 		fv = strtok(NULL, "/");
     47 		if (!fv) return 1;
     48 		mbox = strtok(NULL, "/");
     49 		if (!mbox) return 1;
     50 		break;
     51 	default:
     52 		return 1;
     53 	}
     54 
     55 	printf("if header :contains %s %s", filt, fv);
     56 	printf(" { fileinto :create \"%s\"; stop; }\n", mbox);
     57 	if (vflag)
     58 		info("%s: %s -> %s\n", filt, fv, mbox);
     59 
     60 	return 0;
     61 }
     62 
     63 int sievemain(int argc, char *argv[]) {
     64 	int c = 0, rt;
     65 	char l[MAXLINESIZE];
     66 
     67 	ARGBEGIN {
     68 	case 'v':
     69 		vflag = 1;
     70 		break;
     71 	default:
     72 		return 1;
     73 	} ARGEND;
     74 
     75 	while (fgets(l, sizeof(l), stdin)) {
     76 		c++;
     77 		rt = -1;
     78 		strtok(l, "\n");
     79 		if (!strncmp("to", l, 2))
     80 			rt = 1;
     81 		else if (!strncmp("from", l, 4))
     82 			rt = 2;
     83 		else if (!strncmp("subject", l, 7))
     84 			rt = 3;
     85 		else if (!strncmp("email=", l, 6))
     86 			rt = 69;
     87 
     88 		if (rt > 0)
     89 			if (genrule(rt, l))
     90 				die("Invalid syntax on line %d: %s\n", c, l);
     91 	}
     92 
     93 	return 0;
     94 }