From b451fe788b26101525373c1feaa55131e663e60f Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Tue, 2 Aug 2016 16:28:35 +0200 Subject: magrep: add search in body --- magrep.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'magrep.c') diff --git a/magrep.c b/magrep.c index 0c53c9f..321a481 100644 --- a/magrep.c +++ b/magrep.c @@ -21,6 +21,7 @@ static long matches; static regex_t pattern; static char *header; +static char *curfile; int match(char *file, char *s) @@ -37,6 +38,49 @@ match(char *file, char *s) return 0; } +blaze822_mime_action +match_part(int depth, struct message *msg, char *body, size_t bodylen) +{ + (void) depth; + + char *ct = blaze822_hdr(msg, "content-type"); + + blaze822_mime_action r = MIME_CONTINUE; + + if (!ct || strncmp(ct, "text/plain", 10) == 0) { + char *charset = 0, *cs, *cse; + if (blaze822_mime_parameter(ct, "charset", &cs, &cse)) + charset = strndup(cs, cse-cs); + if (!charset || + strcasecmp(charset, "utf-8") == 0 || + strcasecmp(charset, "utf8") == 0 || + strcasecmp(charset, "us-ascii") == 0) { + (void) bodylen; /* XXX */ + if (match(curfile, body)) + r = MIME_STOP; + } else { + /* XXX decode here */ + } + free(charset); + } + + return r; +} + +void +match_body(char *file) +{ + curfile = file; + while (*curfile == ' ' || *curfile == '\t') + curfile++; + + struct message *msg = blaze822_file(curfile); + if (!msg) + return; + + blaze822_walk_mime(msg, 0, match_part); +} + void magrep(char *file) { @@ -45,6 +89,9 @@ magrep(char *file) if (flags) match(file, flags+3); return; + } else if (strcmp(header, "/") == 0) { + match_body(file); + return; } char *filename = file; -- cgit 1.4.1