changeset 870:61c0c995a6ab

Fix bug where bounces could be discarded instead of processed.
author Ben Schmidt
date Tue, 27 Mar 2012 12:49:49 +1100
parents d4707be29c14
children 12c242c5effe
files src/mlmmj-process.c
diffstat 1 files changed, 58 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/src/mlmmj-process.c	Thu Mar 22 00:32:03 2012 +1100
+++ b/src/mlmmj-process.c	Tue Mar 27 12:49:49 2012 +1100
@@ -581,34 +581,6 @@
 	if(footfd >= 0)
 		close(footfd);
 
-	/* From: addresses */
-	for(i = 0; i < readhdrs[0].valuecount; i++) {
-		find_email_adr(readhdrs[0].values[i], &fromemails);
-	}
-	/* discard malformed mail with invalid From: unless it's a bounce */
-	if(fromemails.emailcount != 1 &&
-			(recipextra == NULL ||
-			strncmp(recipextra, "bounces", 7) != 0)) {
-		for(i = 0; i < fromemails.emailcount; i++)
-			printf("fromemails.emaillist[%d] = %s\n",
-					i, fromemails.emaillist[i]);
-		discardname = concatstr(3, listdir,
-				"/queue/discarded/", randomstr);
-		log_error(LOG_ARGS, "Discarding %s due to invalid From:",
-				mailfile);
-		for(i = 0; i < fromemails.emailcount; i++)
-			log_error(LOG_ARGS, "fromemails.emaillist[%d] = %s\n",
-					i, fromemails.emaillist[i]);
-		rename(mailfile, discardname);
-		unlink(donemailname);
-		myfree(donemailname);
-		myfree(discardname);
-		myfree(randomstr);
-		/* TODO: free emailstructs */
-		exit(EXIT_SUCCESS);
-	}
-	posteraddr = fromemails.emaillist[0];
-
 	/* To: addresses */
 	for(i = 0; i < readhdrs[1].valuecount; i++) {
 		find_email_adr(readhdrs[1].values[i], &toemails);
@@ -619,32 +591,11 @@
 		find_email_adr(readhdrs[2].values[i], &ccemails);
 	}
 
-	/* Return-Path: addresses */
-	for(i = 0; i < readhdrs[3].valuecount; i++) {
-		find_email_adr(readhdrs[3].values[i], &rpemails);
-	}
-
 	/* Delivered-To: addresses */
 	for(i = 0; i < readhdrs[4].valuecount; i++) {
 		find_email_adr(readhdrs[4].values[i], &dtemails);
 	}
 
-	/* Subject: */
-	if (readhdrs[5].valuecount)
-			subject = unistr_header_to_utf8(readhdrs[5].values[0]);
-	if (!subject) subject = mystrdup("");
-
-	/* envelope from */
-	if((envstr = getenv("SENDER")) != NULL) {
-		/* qmail, postfix, exim */
-		efrom = mystrdup(envstr);
-	} else if(rpemails.emailcount >= 1) {
-		/* the (first) Return-Path: header */
-		efrom = mystrdup(rpemails.emaillist[0]);
-	} else {
-		efrom = mystrdup("");
-	}
-
 	/* address extension (the "foo" part of "user+foo@domain.tld") */
 	if((envstr = getenv("DEFAULT")) != NULL) {
 		/* qmail */
@@ -670,16 +621,69 @@
 	} else {
 		recipextra = NULL;
 	}
-
 	if(recipextra && (strlen(recipextra) == 0)) {
 		myfree(recipextra);
 		recipextra = NULL;
 	}
 
-	/* Why is this here, and not in listcontrol() ?  -- mortenp 20060409 */
+	/* From: addresses */
+	for(i = 0; i < readhdrs[0].valuecount; i++) {
+		find_email_adr(readhdrs[0].values[i], &fromemails);
+	}
+	/* discard malformed mail with invalid From: unless it's a bounce */
+	if(fromemails.emailcount != 1 &&
+			(recipextra == NULL ||
+			strncmp(recipextra, "bounces", 7) != 0)) {
+		for(i = 0; i < fromemails.emailcount; i++)
+			printf("fromemails.emaillist[%d] = %s\n",
+					i, fromemails.emaillist[i]);
+		discardname = concatstr(3, listdir,
+				"/queue/discarded/", randomstr);
+		log_error(LOG_ARGS, "Discarding %s due to invalid From:",
+				mailfile);
+		for(i = 0; i < fromemails.emailcount; i++)
+			log_error(LOG_ARGS, "fromemails.emaillist[%d] = %s\n",
+					i, fromemails.emaillist[i]);
+		rename(mailfile, discardname);
+		unlink(donemailname);
+		myfree(donemailname);
+		myfree(discardname);
+		myfree(randomstr);
+		/* TODO: free emailstructs */
+		exit(EXIT_SUCCESS);
+	}
+	if (fromemails.emailcount > 0)
+			posteraddr = fromemails.emaillist[0];
+	/* The only time posteraddr will remain unset is when the mail is a
+	 * bounce, so the mail will be processed by listcontrol() and the
+	 * program will terminate before posteraddr is used. */
+
+	/* Return-Path: addresses */
+	for(i = 0; i < readhdrs[3].valuecount; i++) {
+		find_email_adr(readhdrs[3].values[i], &rpemails);
+	}
+
+	/* envelope from */
+	if((envstr = getenv("SENDER")) != NULL) {
+		/* qmail, postfix, exim */
+		efrom = mystrdup(envstr);
+	} else if(rpemails.emailcount >= 1) {
+		/* the (first) Return-Path: header */
+		efrom = mystrdup(rpemails.emaillist[0]);
+	} else {
+		efrom = mystrdup("");
+	}
+
+	/* Subject: */
+	if (readhdrs[5].valuecount)
+			subject = unistr_header_to_utf8(readhdrs[5].values[0]);
+	if (!subject) subject = mystrdup("");
+
 	if(recipextra) {
 		owner = concatstr(2, listdir, "/control/owner");
 		if(owner && strcmp(recipextra, "owner") == 0) {
+			/* Why is this here, and not in listcontrol() ?
+			 * -- mortenp 20060409 */
 			/* strip envelope from before resending */
 			delheaders->count = 0;
 			delheaders->strs = NULL;