about summary refs log tree commit diff
path: root/mexport.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-08-19 17:53:40 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-08-19 17:53:40 +0200
commitd71a1fbe44093debc05d60d56ac26ef2c63aff15 (patch)
treed47de20fc684d2a686ca0429d5b5ffbdac2b7a5f /mexport.c
parent7c9a5c9c7682c1e5a1189b410a63403c3676d5c5 (diff)
downloadmblaze-d71a1fbe44093debc05d60d56ac26ef2c63aff15.tar.gz
mblaze-d71a1fbe44093debc05d60d56ac26ef2c63aff15.tar.xz
mblaze-d71a1fbe44093debc05d60d56ac26ef2c63aff15.zip
add mexport
Diffstat (limited to 'mexport.c')
-rw-r--r--mexport.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/mexport.c b/mexport.c
new file mode 100644
index 0000000..2e06890
--- /dev/null
+++ b/mexport.c
@@ -0,0 +1,139 @@
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "blaze822.h"
+
+static int Sflag;
+
+static int status;
+
+void
+export(char *file)
+{
+	struct message *msg;
+
+	while (*file == ' ' || *file == '\t')
+		file++;
+
+	msg = blaze822(file);
+	if (!msg)
+		return;
+
+	char from[1024] = "nobody";
+
+        char *v;
+        if ((v = blaze822_hdr(msg, "return-path")) ||
+	    (v = blaze822_hdr(msg, "x-envelope-from"))) {
+		char *s = strchr(v, '<');
+		char *e = strchr(s, '>');
+		if (s && e) {
+			e++;
+			memcpy(from, s, e-s);
+			from[e-s] = 0;
+		}
+        }
+
+	time_t date = -1;
+        if ((v = blaze822_hdr(msg, "date"))) {
+		date = blaze822_date(v);
+	}
+
+	char *line = 0;
+	size_t linelen = 0;
+
+	FILE *infile = fopen(file, "r");
+	if (!infile) {
+		status = 1;
+		return;
+	}
+	
+	printf("From %s %s", from, ctime(&date));
+
+	int in_header = 1;
+	int final_nl = 0;
+
+	while (1) {
+		errno = 0;
+		ssize_t rd = getline(&line, &linelen, infile);
+		if (rd == -1) {
+			if (errno == 0)
+				break;
+			// XXX print error?
+			status = 1;
+			return;
+		}
+		
+		if (in_header && line[0] == '\n' && !line[1]) {
+			if (Sflag) {
+				char *flags = strstr(file, ":2,");
+				if (!flags)
+					flags = "";
+
+				fputs("Status: ", stdout);
+				if (strchr(flags, 'S'))
+					putchar('R');
+				char *ee = strrchr(file, '/');
+				if (!ee || 
+				    !(ee >= file + 3 && ee[-3] == 'n' && ee[-2] == 'e' && ee[-1] == 'w'))
+					putchar('O');
+				putchar('\n');
+					
+				fputs("X-Status: ", stdout);
+				if (strchr(flags, 'R')) putchar('A');
+				if (strchr(flags, 'T')) putchar('D');
+				if (strchr(flags, 'F')) putchar('F');
+				putchar('\n');
+			}
+				
+			in_header = 0;
+		}
+			
+		// MBOXRD: add first > to >>..>>From
+		char *s = line;
+		while (*s == '>')
+			s++;
+		if (strncmp("From ", s, 5) == 0)
+			putchar('>');
+
+		fputs(line, stdout);
+		final_nl = (line[rd-1] == '\n');
+	}
+
+	// ensure trailing newline
+	if (!final_nl)
+		putchar('\n');
+
+	fclose(infile);
+
+	blaze822_free(msg);
+}
+
+int
+main(int argc, char *argv[])
+{
+	int c;
+	while ((c = getopt(argc, argv, "S")) != -1)
+		switch(c) {
+		case 'S': Sflag = 1; break;
+		default:
+			fprintf(stderr, "Usage: mexport [-S] [msgs...]\n");
+			exit(2);
+		}
+
+	status = 0;
+
+	if (argc == optind && isatty(0))
+		blaze822_loop1(":", export);
+	else
+		blaze822_loop(argc-optind, argv+optind, export);
+	
+	return status;
+}