Fix a bug in mbox parser.

Some tools only escape "From " lines in mboxs if they follow a blank line. fdm
would split at lines beginning "From " unconditionally.

From upstream 91df796046ee2c36bccc9cfda27860b5bb9dd36d

$OpenBSD: patch-fetch-mbox_c,v 1.1 2016/03/08 17:30:43 edd Exp $
--- fetch-mbox.c.orig	Fri Jun  5 19:30:34 2015
+++ fetch-mbox.c	Thu Feb 18 15:30:05 2016
@@ -404,9 +404,9 @@ fetch_mbox_state_mail(struct account *a, struct fetch_
 	struct mail			*m = fctx->mail;
 	struct fetch_mbox_mbox		*fmbox;
 	struct fetch_mbox_mail		*aux;
-	char				*line, *ptr, *lptr;
-	size_t				 llen;
-	int				 flushing;
+	char				*line, *ptr, *last_line, *lptr;
+	size_t				 llen, n;
+	int				 flushing, from;
 
 	/* Find current mbox and check for EOF. */
 	fmbox = ARRAY_ITEM(&data->fmboxes, data->index);
@@ -443,7 +443,7 @@ fetch_mbox_state_mail(struct account *a, struct fetch_
 	 * trimmed later with minimal penalty).
 	 */
 	flushing = 0;
-	for (;;) {
+	for (last_line = NULL;; last_line = line) {
 		/* Check for EOF. */
 		if (data->off == fmbox->size) {
 			aux->size = data->off - aux->off;
@@ -459,10 +459,27 @@ fetch_mbox_state_mail(struct account *a, struct fetch_
 		} else
 			data->off += ptr - line + 1;
 
-		/* Check if the line is "From ". */
-		if (line > fmbox->base &&
-		    ptr - line >= 5 && strncmp(line, "From ", 5) == 0) {
-			/* End of mail. */
+		/*
+		 * Check if we have reached the end of this message at the next
+		 * message's "From " line. To allow "From " inside message
+		 * bodes, they can be escaped by prepending with '>'. Some
+		 * tools escape all "From " lines; others only escape if they
+		 * immediately follow a blank line. We accept only "From" lines
+		 * which follow a blank line (\n or \r\n).
+		 */
+		from = 1;
+		if (line == fmbox->base || last_line == NULL)
+			from = 0;
+		else {
+			n = line - last_line;
+			if (n != 1 && n != 2)
+				from = 0;
+			else if (n == 1 && *last_line != '\n')
+				from = 0;
+			else if (n == 2 && memcmp(last_line, "\r\n", 2) != 0)
+				from = 0;
+		}
+		if (from && ptr - line >= 5 && memcmp(line, "From ", 5) == 0) {
 			aux->size = (line - fmbox->base) - aux->off;
 			break;
 		}
