summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-19 17:36:05 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-19 17:36:05 +0200
commit7c9a5c9c7682c1e5a1189b410a63403c3676d5c5 (patch)
tree422bf4cf328ccae8bb4ac4aa58f43a5e120ed43a
parentce625878de93a1388ae94dd21f5644f63152e299 (diff)
downloadmblaze-7c9a5c9c7682c1e5a1189b410a63403c3676d5c5.tar.gz
mblaze-7c9a5c9c7682c1e5a1189b410a63403c3676d5c5.tar.xz
mblaze-7c9a5c9c7682c1e5a1189b410a63403c3676d5c5.zip
mdeliver: parse both Status: and X-Status:, then drop them
-rw-r--r--mdeliver.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/mdeliver.c b/mdeliver.c
index c6311f3..c8cef61 100644
--- a/mdeliver.c
+++ b/mdeliver.c
@@ -92,6 +92,10 @@ tryagain:
 
 		outfile = fdopen(outfd, "w");
 
+		char statusflags[5] = { 0 };
+
+		int in_header = 1;
+		int is_old = 0;
 		while (1) {
 			errno = 0;
 			ssize_t rd = getline(&line, &linelen, infile);
@@ -101,9 +105,25 @@ tryagain:
 				break;
 			}
 
+			if (line[0] == '\n' && !line[1])
+				in_header = 0;
+
 			if (Mflag && strncmp("From ", line, 5) == 0)
 				break;
 
+			if (Mflag && in_header &&
+			    (strncasecmp("status:", line, 6) == 0 ||
+			     strncasecmp("x-status:", line, 8) == 0)) {
+				char *v = strchr(line, ':');
+				if (strchr(v, 'F')) statusflags[0] = 'F';
+				if (strchr(v, 'A')) statusflags[1] = 'R';
+				if (strchr(v, 'R')) statusflags[2] = 'S';
+				if (strchr(v, 'D')) statusflags[3] = 'T';
+				if (strchr(v, 'O')) is_old = 1;
+
+				continue;  // drop header
+			}
+
 			if (Mflag) {
 				// MBOXRD: strip first > from >>..>>From
 				char *s = line;
@@ -125,8 +145,12 @@ tryagain:
 		if (fclose(outfile) == EOF)
 			return -1;
 
-		char statusflags[5] = { 0 };
-		char *f = statusflags;
+		// compress flags
+		int i, j;
+		for (i = sizeof statusflags - 1; i >= 0; i--)
+			if (!statusflags[i])
+				for (j = i+1; j < (int) sizeof statusflags; j++)
+					statusflags[j-1] = statusflags[j];
 
 		if (Mflag) {
 			struct message *msg = blaze822_file(tmp);
@@ -143,19 +167,10 @@ tryagain:
 					utimes(tmp, times);
 				}
 			}
-			if (msg && ((v = blaze822_hdr(msg, "status")) ||
-				    (v = blaze822_hdr(msg, "x-status")))) {
-				if (strchr(v, 'F')) *f++ = 'F';
-				if (strchr(v, 'A')) *f++ = 'R';
-				if (strchr(v, 'R') || strchr(v, 'O'))
-					*f++ = 'S';
-				if (strchr(v, 'D')) *f++ = 'T';
-			}
 		}
-		*f = 0;
 
 		snprintf(dst, sizeof dst, "%s/%s/%s:2,%s",
-			 targetdir, cflag ? "cur" : "new", id,
+			 targetdir, (cflag || is_old) ? "cur" : "new", id,
 			 Xflag ? Xflag : statusflags);
 		if (rename(tmp, dst) != 0)
 			return -1;