about summary refs log tree commit diff
path: root/mshow.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-07-29 17:07:25 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-07-29 17:07:25 +0200
commit08f5a8e1ddc2e4699f0e3aa4704fc37f1577ed9d (patch)
treeb0e89982e7642afbca1d22c4ba05353ea3c3afd0 /mshow.c
parent01e4e942dddd1c933ba92f32f77902390860d187 (diff)
downloadmblaze-08f5a8e1ddc2e4699f0e3aa4704fc37f1577ed9d.tar.gz
mblaze-08f5a8e1ddc2e4699f0e3aa4704fc37f1577ed9d.tar.xz
mblaze-08f5a8e1ddc2e4699f0e3aa4704fc37f1577ed9d.zip
mshow: hard-code simple multipart/alternative policy
Diffstat (limited to 'mshow.c')
-rw-r--r--mshow.c51
1 files changed, 47 insertions, 4 deletions
diff --git a/mshow.c b/mshow.c
index d5bd971..3b592b6 100644
--- a/mshow.c
+++ b/mshow.c
@@ -31,6 +31,9 @@ struct message *filters;
 
 static int mimecount;
 
+static char defaultAflags[] = "text/plain:text/html";
+static char *Aflag = defaultAflags;
+
 void
 printhdr(char *hdr)
 {
@@ -167,6 +170,8 @@ mime_filename(struct message *msg)
 	return filename;
 }
 
+static void choose_alternative(struct message *msg, int depth);
+
 mime_action
 render_mime(int depth, struct message *msg, char *body, size_t bodylen)
 {
@@ -207,7 +212,7 @@ render_mime(int depth, struct message *msg, char *body, size_t bodylen)
 		size_t outlen;
 		int e = filter(body, bodylen, cmd, &output, &outlen);
 
-		if (e == 0) {
+		if (e == 0) { // replace output
 			printf(" render=\"%s\" ---\n", cmd);
 			print_ascii(output, outlen);
 		} else if (e == 63) { // skip filter
@@ -262,8 +267,12 @@ nofilter:
 				printhdr(d);
 			}
 			printf("\n");
+		} else if (strncmp(ct, "multipart/alternative", 21) == 0) {
+			choose_alternative(msg, depth);
+
+			r = MIME_PRUNE;
 		} else if (strncmp(ct, "multipart/", 10) == 0) {
-			;
+			; // default mime_walk action
 		} else {
 			printf("no filter or default handler\n");
 		}
@@ -275,6 +284,39 @@ nofilter:
 	return r;
 }
 
+static void
+choose_alternative(struct message *msg, int depth)
+{
+	int n = 1;
+	int m = 0;
+	char *p = Aflag + strlen(Aflag);
+
+	struct message *imsg = 0;
+	while (blaze822_multipart(msg, &imsg)) {
+		m++;
+		char *ict = blaze822_hdr(imsg, "content-type");
+		if (!ict)
+			ict = "text/x-unknown";
+		char *imt = mimetype(ict);
+
+		char *s = strstr(Aflag, imt);
+		if (s && s < p &&
+		    (s[strlen(imt)] == 0 || s[strlen(imt)] == ':')) {
+			p = s;
+			n = m;
+		}
+
+		free(imt);
+	}
+	blaze822_free(imsg);
+
+	imsg = 0;
+	while (blaze822_multipart(msg, &imsg))
+		if (--n == 0)
+			walk_mime(imsg, depth+1, render_mime);
+	blaze822_free(imsg);
+}
+
 mime_action
 reply_mime(int depth, struct message *msg, char *body, size_t bodylen)
 {
@@ -676,9 +718,10 @@ int
 main(int argc, char *argv[])
 {
 	int c;
-	while ((c = getopt(argc, argv, "h:qrtHLx:O:Rn")) != -1)
+	while ((c = getopt(argc, argv, "h:A:qrtHLx:O:Rn")) != -1)
 		switch(c) {
 		case 'h': hflag = optarg; break;
+		case 'A': Aflag = optarg; break;
 		case 'q': qflag = 1; break;
 		case 'r': rflag = 1; break;
 		case 'H': Hflag = 1; break;
@@ -690,7 +733,7 @@ main(int argc, char *argv[])
 		case 'n': nflag = 1; break;
                 default:
 			fprintf(stderr,
-			    "Usage: mshow [-h headers] [-qrHL] [msgs...]\n"
+			    "Usage: mshow [-h headers] [-A mimetypes] [-qrHL] [msgs...]\n"
 			    "       mshow -x msg parts...\n"
 			    "       mshow -O msg parts...\n"
 			    "       mshow -t msgs...\n"