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 --- Makefile | 2 +- magrep.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ man/magrep.1 | 10 ++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f2ed500..d280ab9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ ALL = maddr magrep mdeliver mdirs mflag mgenmid mhdr minc mlist mmime mpick msca all: $(ALL) maddr: maddr.o blaze822.o seq.o rfc2047.o mymemmem.o -magrep: magrep.o blaze822.o seq.o rfc2047.o mymemmem.o +magrep: magrep.o blaze822.o seq.o rfc2045.o rfc2047.o mymemmem.o mdeliver: mdeliver.o blaze822.o mymemmem.o mdirs: mdirs.o mflag: mflag.o blaze822.o seq.o mymemmem.o 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; diff --git a/man/magrep.1 b/man/magrep.1 index b28d37c..6d41292 100644 --- a/man/magrep.1 +++ b/man/magrep.1 @@ -20,12 +20,22 @@ where the value of .Ar header matches the POSIX Extended Regular Expression .Ar regex . +.Pp If .Ar header is empty, .Nm will instead match against the Maildir flags of the messages. .Pp +If +.Ar header +is +.Sq Li "/" , +.Nm +will instead match inside the text parts of the +.Em body +of the messages. +.Pp See .Xr mmsg 7 for the message argument syntax. -- cgit 1.4.1