about summary refs log tree commit diff
path: root/magrep.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 16:28:35 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-02 16:28:35 +0200
commitb451fe788b26101525373c1feaa55131e663e60f (patch)
treeb42f106243879ee6d93f40772721a903c13a86b8 /magrep.c
parent27b21d7e204cfa17723702d3c04c0ee6df603fbf (diff)
downloadmblaze-b451fe788b26101525373c1feaa55131e663e60f.tar.gz
mblaze-b451fe788b26101525373c1feaa55131e663e60f.tar.xz
mblaze-b451fe788b26101525373c1feaa55131e663e60f.zip
magrep: add search in body
Diffstat (limited to 'magrep.c')
-rw-r--r--magrep.c47
1 files changed, 47 insertions, 0 deletions
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;