13 typedef struct keyword Keyword;
14 typedef struct word Word;
49 * RFC822 header keywords to look for for fractured header.
50 * all lengths must be less than HdrMax defined above.
71 Patterns patterns[] = {
72 [Dump] { "DUMP:", 0, 0 },
73 [HoldHeader] { "HEADER:", 0, 0 },
74 [Hold] { "HOLD:", 0, 0 },
75 [SaveLine] { "LINE:", 0, 0 },
76 [Lineoff] { "LINEOFF:", 0, 0 },
77 [Nactions] { 0, 0, 0 }
80 static char* endofhdr(char*, char*);
81 static int escape(char**);
82 static int extract(char*);
83 static int findkey(char*);
85 static int isword(Word*, char*, int);
86 static void parsealt(Biobuf*, char*, Spat**);
89 * The canonicalizer: convert input to canonical representation
92 readmsg(Biobuf *bp, int *hsize, int *bufsize)
95 int n, offset, eoh, bsize, delta;
104 buf = Realloc(buf, offset+Quanta+1);
105 n = Bread(bp, buf+offset, Quanta);
110 p = buf+offset; /* start of this chunk */
111 offset += n; /* end of this chunk */
119 if(hsize == 0) /* don't process header */
121 if(p != buf && p[-1] == '\n') /* check for EOH across buffer split */
123 p = endofhdr(p, buf+offset);
126 if(offset >= Maxread) /* gargantuan header - just punt*/
135 eoh = p-buf; /* End of header */
136 bsize = offset - eoh; /* amount of body already read */
138 /* Read at least Minbody bytes of the body */
139 if (bsize < Minbody){
140 delta = Minbody-bsize;
141 buf = Realloc(buf, offset+delta+1);
142 n = Bread(bp, buf+offset, delta);
156 isword(Word *wp, char *text, int len)
158 for(;wp->string; wp++)
159 if(len >= wp->n && strncmp(text, wp->string, wp->n) == 0)
165 endofhdr(char *raw, char *end)
172 * can't use strchr to search for newlines because
173 * there may be embedded NULL's.
175 for(p = raw; p < end; p++){
176 if(*p != '\n' || p[1] != '\n')
179 for(i = 0, q = p+1; i < sizeof(buf) && *q; q++){
180 buf[i++] = tolower(*q);
181 if(*q == ':' || *q == '\n')
184 if(!isword(hdrwords, buf, i))
191 htmlmatch(Word *wp, char *text, char *end, int *n)
198 * extract a string up to '>'
203 while (cp < end && i < sizeof(buf)-1){
224 buf[i++] = lastc = c;
230 return isword(wp, buf, i);
261 if(c == '3' && tolower(p[1]) == 'd')
270 htmlchk(char **msg, char *end)
279 ishtml = htmlmatch(htmlcmds, p, end, &n);
281 /* If not an HTML keyword, check if it's
282 * an HTML comment (<!comment>). if so,
283 * skip over it; otherwise copy it in.
285 if(ishtml == 0 && *p != '!') /* not comment */
286 return '<'; /* copy it */
288 } else if(htmlmatch(hrefs, p, end, &n)) /* if special HTML string */
289 return '<'; /* copy it */
292 * this is an uninteresting HTML command; skip over it.
300 * decode a base 64 encode body
303 conv64(char *msg, char *end, char *buf, int bufsize)
309 i = (len*3)/4+1; /* room for max chars + null */
311 len = dec64((uchar*)cp, i, msg, len);
312 convert(cp, cp+len, buf, bufsize, 1);
317 convert(char *msg, char *end, char *buf, int bufsize, int isbody)
321 int c, lastc, base64;
325 while(msg < end && bufsize > 0){
329 * In the body only, try to strip most HTML and
330 * replace certain MIME escape sequences with the character
336 c = htmlchk(&msg, end);
339 } while(p != msg && p < end);
352 case 'C': /* check for MIME base 64 encoding in header */
355 if(msg < end-32 && *msg == 'o' && msg[1] == 'n')
356 if(cistrncmp(msg+2, "tent-transfer-encoding: base64", 30) == 0)
373 * The pattern parser: build data structures from the pattern file
387 for(kp = keywords; kp->string; kp++)
388 if(strcmp(val, kp->string) == 0)
393 #define whitespace(c) ((c) == ' ' || (c) == '\t')
396 parsepats(Biobuf *bp)
400 int type, action, n, h;
404 cp = Brdline(bp, '\n');
407 cp[Blinelen(bp)-1] = 0;
408 while(*cp == ' ' || *cp == '\t')
410 if(*cp == '#' || *cp == 0)
417 qp = strchr(cp, ':');
422 fprint(2, "action = %s\n", cp);
423 action = findkey(cp);
424 if(action >= Nactions)
428 if(n <= 0 || *cp == 0)
431 qp = strstr(cp, "~~");
437 fprint(2, " Pattern: `%s'\n", cp);
439 /* Hook regexps into a chain */
441 new = Malloc(sizeof(Pattern));
442 new->action = action;
443 new->pat = regcomp(cp);
453 parsealt(bp, qp+2, &new->alt);
455 new->next = patterns[action].regexps;
456 patterns[action].regexps = new;
460 /* not a Regexp - hook strings into Pattern hash chain */
461 spat = Malloc(sizeof(*spat));
465 spat->string = Malloc(n+1);
467 strcpy(spat->string, cp);
470 parsealt(bp, qp+2, &spat->alt);
472 p = patterns[action].strings;
474 p = Malloc(sizeof(Pattern));
475 memset(p, 0, sizeof(*p));
478 patterns[action].strings = p;
480 h = hash(*spat->string);
481 spat->next = p->spat[h];
487 parsealt(Biobuf *bp, char *cp, Spat** head)
493 if(*cp == 0){ /*escaped newline*/
495 cp = Brdline(bp, '\n');
498 cp[Blinelen(bp)-1] = 0;
499 } while(extract(cp) <= 0 || *cp == 0);
503 cp = strstr(p, "~~");
509 alt = Malloc(sizeof(*alt));
510 alt->string = strdup(p);
524 while(whitespace(*p))
530 while(*p && *p != '"'){
531 if(*p == '\\' && p[1] == '"')
533 if('A' <= *p && *p <= 'Z')
534 *q++ = *p++ + ('a'-'A');
540 r = q; /* never back up over a quoted string */
542 if('A' <= c && c <= 'Z')
547 while(q > r && whitespace(q[-1]))
554 * The matching engine: compare canonical input to pattern structures
558 isalt(char *message, Spat *alt)
562 if(message != cmd && strstr(cmd, alt->string))
564 if(message != header+1 && strstr(header+1, alt->string))
566 if(strstr(message, alt->string))
574 matchpat(Pattern *p, char *message, Resub *m)
580 if(p->type == string){
582 for(s=message; c=c1; s++){
584 for(spat=p->spat[hash(c)]; spat; spat=spat->next){
586 if(memcmp(s, spat->string, spat->len) == 0)
587 if(!isalt(message, spat->alt)){
589 m->e.ep = s + spat->len;
596 m->s.sp = m->e.ep = 0;
597 if(regexec(p->pat, message, m, 1) == 0)
599 if(isalt(message, p->alt))
606 xprint(int fd, char *type, Resub *m)
611 if(m->s.sp == 0 || m->e.ep == 0)
614 /* back up approx 30 characters to whitespace */
615 for(p = m->s.sp, i = 0; *p && i < 30; i++, p--)
617 while(*p && *p != ' ')
621 /* grab about 30 more chars beyond the end of the match */
622 for(q = m->e.ep, i = 0; *q && i < 30; i++, q++)
624 while(*q && *q != ' ')
627 fprint(fd, "%s %.*s~%.*s~%.*s\n", type, (int)(m->s.sp-p), p, (int)(m->e.ep-m->s.sp), m->s.sp, (int)(q-m->e.ep), m->e.ep);
634 static uchar t64d[256] = {
635 /*00 */ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
636 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
637 /*10*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
638 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
639 /*20*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
640 INVAL, INVAL, INVAL, 62, INVAL, INVAL, INVAL, 63,
641 /*30*/ 52, 53, 54, 55, 56, 57, 58, 59,
642 60, 61, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
643 /*40*/ INVAL, 0, 1, 2, 3, 4, 5, 6,
644 7, 8, 9, 10, 11, 12, 13, 14,
645 /*50*/ 15, 16, 17, 18, 19, 20, 21, 22,
646 23, 24, 25, INVAL, INVAL, INVAL, INVAL, INVAL,
647 /*60*/ INVAL, 26, 27, 28, 29, 30, 31, 32,
648 33, 34, 35, 36, 37, 38, 39, 40,
649 /*70*/ 41, 42, 43, 44, 45, 46, 47, 48,
650 49, 50, 51, INVAL, INVAL, INVAL, INVAL, INVAL,
651 /*80*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
652 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
653 /*90*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
654 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
655 /*A0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
656 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
657 /*B0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
658 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
659 /*C0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
660 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
661 /*D0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
662 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
663 /*E0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
664 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
665 /*F0*/ INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL,
666 INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL, INVAL