about summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--imbox.c45
1 files changed, 21 insertions, 24 deletions
diff --git a/imbox.c b/imbox.c
index 788f032..22bb36a 100644
--- a/imbox.c
+++ b/imbox.c
@@ -37,6 +37,23 @@ static void compile(regex_t *regex, const char *pattern) {
 	errx(EX_SOFTWARE, "regcomp: %s: %s", buf, pattern);
 }
 
+static void printLines(const char *lines) {
+	static regex_t fromRegex;
+	compile(&fromRegex, "^>*From ");
+	while (*lines) {
+		size_t len = strcspn(lines, "\r\n");
+		regmatch_t match;
+		if (!regexec(&fromRegex, lines, 1, &match, 0) && !match.rm_so) {
+			printf(">%.*s\n", (int)len, lines);
+		} else {
+			printf("%.*s\n", (int)len, lines);
+		}
+		lines += len;
+		if (*lines == '\r') lines++;
+		if (*lines == '\n') lines++;
+	}
+}
+
 static void mboxrd(const char *headers, const char *body) {
 	static regex_t fromRegex;
 	compile(&fromRegex, "^From: .*<([^>]+)>");
@@ -56,7 +73,7 @@ static void mboxrd(const char *headers, const char *body) {
 	error = regexec(&dateRegex, headers, 6, date, 0);
 	if (error) errx(EX_DATAERR, "missing Date header");
 	printf(
-		"%.*s %.*s %.*s %.*s %.*s\r\n",
+		"%.*s %.*s %.*s %.*s %.*s\n",
 		(int)(date[1].rm_eo - date[1].rm_so), &headers[date[1].rm_so],
 		(int)(date[3].rm_eo - date[3].rm_so), &headers[date[3].rm_so],
 		(int)(date[2].rm_eo - date[2].rm_so), &headers[date[2].rm_so],
@@ -64,29 +81,9 @@ static void mboxrd(const char *headers, const char *body) {
 		(int)(date[4].rm_eo - date[4].rm_so), &headers[date[4].rm_so]
 	);
 
-	printf("%s", headers);
-
-	static regex_t quoteRegex;
-	compile(&quoteRegex, "^>*From ");
-	regmatch_t match = {0};
-	for (const char *ptr = body;; ptr += match.rm_eo) {
-		regmatch_t match;
-		error = regexec(
-			&quoteRegex, body, 1, &match, (ptr > body ? REG_NOTBOL : 0)
-		);
-		if (error) {
-			printf("%s", ptr);
-			break;
-		}
-		printf(
-			"%.*s>%.*s",
-			(int)match.rm_so, ptr,
-			(int)(match.rm_eo - match.rm_so), &ptr[match.rm_so]
-		);
-	}
-
-	// FIXME: mbox technically shouldn't use \r\n but everything else does...
-	printf("\r\n");
+	printLines(headers);
+	printLines(body);
+	printf("\n");
 }
 
 static bool verbose;