about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-07-16 19:57:10 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-07-16 19:58:20 +0200
commita07d2c9d9b38f7c79a6fb2ddaaf5aa1e3f23f7bd (patch)
tree4541e6a1dd8034c383998c7dd526ce2ddc514ebe
parent963380c9f1d7e0b94ed6af420f107e13c09fb231 (diff)
downloadmblaze-a07d2c9d9b38f7c79a6fb2ddaaf5aa1e3f23f7bd.tar.gz
mblaze-a07d2c9d9b38f7c79a6fb2ddaaf5aa1e3f23f7bd.tar.xz
mblaze-a07d2c9d9b38f7c79a6fb2ddaaf5aa1e3f23f7bd.zip
unmime: call external filters
-rw-r--r--unmime.c76
1 files changed, 67 insertions, 9 deletions
diff --git a/unmime.c b/unmime.c
index 596ad9a..a7006f9 100644
--- a/unmime.c
+++ b/unmime.c
@@ -15,23 +15,80 @@
 
 #include "blaze822.h"
 
+struct message *filters;
+
+char *
+mimetype(char *ct)
+{
+	char *s;
+	for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t'; s++)
+		;
+
+	return strndup(ct, s-ct);
+}
+
+char *
+tlmimetype(char *ct)
+{
+	char *s;
+	for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t' && *s != '/'; s++)
+		;
+
+	return strndup(ct, s-ct);
+}
+
 void
 recmime(struct message *msg, int depth)
 {
-	struct message *imsg = 0;
 	char *ct, *body, *bodychunk;
 	size_t bodylen;
 
 	if (blaze822_mime_body(msg, &ct, &body, &bodylen, &bodychunk)) {
-		printf("%*.sbody %s len %zd\n", depth*2, "", ct, bodylen);
-		if (strncmp(ct, "multipart/", 10) == 0) {
-			while (blaze822_multipart(msg, &imsg))
-				recmime(imsg, depth+1);
-		} else if (strncmp(ct, "text/", 5) == 0) {
-			printf("---\n");
-			fwrite(body, bodylen, 1, stdout);
-			printf("---\n");
+		char *mt = mimetype(ct);
+		char *tlmt = tlmimetype(ct);
+		printf("%*.sbody %s len %zd\n", depth*2, "", mt, bodylen);
+
+		char *cmd;
+
+		if (filters && ((cmd = blaze822_chdr(filters, mt)) ||
+				(cmd = blaze822_chdr(filters, tlmt)))) {
+			FILE *p;
+			fflush(stdout);
+			p = popen(cmd, "w");
+			if (!p) {
+				perror("popen");
+				goto nofilter;
+			}
+			fwrite(body, bodylen, 1, p);
+			if (pclose(p) != 0) {
+				perror("pclose");
+				goto nofilter;
+			}
+		} else {
+nofilter:
+			if (strncmp(ct, "multipart/", 10) == 0) {
+				struct message *imsg = 0;
+				while (blaze822_multipart(msg, &imsg))
+					recmime(imsg, depth+1);
+			} else if (strncmp(ct, "text/", 5) == 0) {
+				printf("---\n");
+				fwrite(body, bodylen, 1, stdout);
+				printf("---\n");
+			} else if (strncmp(ct, "message/rfc822", 14) == 0) {
+				struct message *imsg = blaze822_mem(body, bodylen);
+				char *h = 0;
+				if (imsg) {
+					while ((h = blaze822_next_header(imsg, h)))
+						printf("%s\n", h);
+					printf("\n");
+					recmime(imsg, depth+1);
+				}
+			} else {
+				printf("no filter or default handler\n");
+			}
 		}
+		free(mt);
+		free(tlmt);
 		free(bodychunk);
 	}
 }
@@ -58,6 +115,7 @@ unmime(char *file)
 int
 main(int argc, char *argv[])
 {
+	filters = blaze822("filters");
 	blaze822_loop(argc-1, argv+1, unmime);
 
 	return 0;