changeset 898:b0b8ccdb490f

Trim whitespace around subject headers.
author Ben Schmidt
date Mon, 24 Mar 2014 11:57:08 +1100
parents 65ef98d16f17
children 04d916168efb
files src/prepstdreply.c src/send_digest.c src/unistr.c
diffstat 3 files changed, 35 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/unistr.c	Sat Mar 08 09:35:40 2014 +1100
+++ b/src/unistr.c	Mon Mar 24 11:57:08 2014 +1100
@@ -496,15 +496,20 @@
 	char *p;
 	char c;
 	char *wsp = NULL;
-	int decoded;
+	int decoded = 0;
 	unistr *us;
 	char *ret;
 
 	my_str = mystrdup(str);
 	us = unistr_new();
 
-	p = my_str;
+	p = my_str + strspn(my_str, " \t\n");
+	wsp = p;
 	while (*p) {
+		if (!decoded) {
+			unistr_append_usascii(us, wsp, p-wsp);
+			wsp = NULL;
+		}
 		word = p;
 		p += strcspn(p, " \t\n");
 		c = *p;
@@ -513,13 +518,7 @@
 		*p = c;
 		wsp = p;
 		p += strspn(p, " \t\n");
-		if (!decoded) {
-			unistr_append_usascii(us, wsp, p-wsp);
-			wsp = NULL;
 		}
-	}
-	if (wsp != NULL)
-		unistr_append_usascii(us, wsp, p-wsp);
 
 	myfree(my_str);
 
@@ -551,27 +550,43 @@
 char *unistr_utf8_to_header(const char *str)
 {
 	unistr *us;
+	char *my_str;
 	char *ret;
+	char *wsp = NULL;
 	const char *p;
 	int clean;
 	char buf[4];
 
-	/* clean header? */
+	my_str = mystrdup(str);
+
+	/* trim whitespace and see if the header is clean */
+
+	ret = my_str + strspn(my_str, " \t\n");
+
 	clean = 1;
-	for (p=str; *p; p++) {
-		if (!is_ok_in_header(*p)) {
+	for (p=ret; *p; p++) {
+		if (*p == ' ' || *p == '\t' || *p == '\n') {
+			if (wsp == NULL)
+				wsp = p;
+		} else {
+			wsp = NULL;
+		}
+		if (clean && !is_ok_in_header(*p))
 			clean = 0;
-			break;
 		}
-	}
+	if (wsp != NULL)
+		*wsp = '\0';
+
 	if (clean) {
-		return mystrdup(str);
+		ret = mystrdup(ret);
+		myfree(my_str);
+		return ret;
 	}
 
 	us = unistr_new();
 
 	unistr_append_usascii(us, "=?utf-8?q?", 10);
-	for (p=str; *p; p++) {
+	for (p=ret; *p; p++) {
 		if (*p == 0x20) {
 			unistr_append_char(us, '_');
 		} else if (is_ok_in_header(*p)) {
@@ -585,6 +600,7 @@
 
 	ret = unistr_to_utf8(us);
 	unistr_free(us);
+	myfree(my_str);
 
 	return ret;
 }