[PATCH] Clean up author display for some "Last, First" cases

Dirk Hohndel hohndel at infradead.org
Wed Apr 21 22:04:39 PDT 2010


We specifically check if this is one of these two patterns:
 "Last, First" <first.last at company.com>
 "Last, First MI" <first.mi.last at company.com>
If this is the case, we rewrite the author name in a more
reader friendly manner

Signed-off-by: Dirk Hohndel <hohndel at infradead.org>
---
 lib/thread.cc |   51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/lib/thread.cc b/lib/thread.cc
index baa0d7f..7e72114 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -144,6 +144,51 @@ _thread_move_matched_author (notmuch_thread_t *thread,
     return;
 }
 
+/* clean up the uggly "Lastname, Firstname" format that some mail systems
+ * (most notably, Exchange) are creating to be "Firstname Lastname" 
+ * To make sure that we don't change other potential situations where a 
+ * comma is in the name, we check that we match one of these patterns
+ * "Last, First" <first.last at company.com>
+ * "Last, First MI" <first.mi.last at company.com>
+ */
+char *
+_thread_cleanup_author (notmuch_thread_t *thread,
+			const char *author, const char *from)
+{
+    char *cleanauthor,*testauthor;
+    const char *comma;
+    char *blank;
+    int fname,lname;
+
+    cleanauthor = talloc_strdup(thread, author);
+    if (cleanauthor == NULL)
+	return NULL;
+    comma = strchr(author,',');
+    if (comma) {
+	/* let's assemble what we think is the correct name */
+	lname = comma - author;
+	fname = strlen(author) - lname - 2;
+	strncpy(cleanauthor, comma + 2, fname);
+	*(cleanauthor+fname) = ' ';
+	strncpy(cleanauthor + fname + 1, author, lname);
+	*(cleanauthor+fname+1+lname) = '\0';
+	/* make a temporary copy and see if it matches the email */
+	testauthor = xstrdup(cleanauthor);
+	
+	blank=strchr(testauthor,' ');
+	while (blank != NULL) {
+	    *blank = '.';
+	    blank=strchr(testauthor,' ');
+	}
+	if (strcasestr(from, testauthor) == NULL)
+	    /* we didn't identify this as part of the email address 
+	    * so let's punt and return the original author */
+	    strcpy (cleanauthor, author);
+	       
+    }
+    return cleanauthor;
+}
+
 /* Add 'message' as a message that belongs to 'thread'.
  *
  * The 'thread' will talloc_steal the 'message' and hold onto a
@@ -158,6 +203,7 @@ _thread_add_message (notmuch_thread_t *thread,
     InternetAddressList *list;
     InternetAddress *address;
     const char *from, *author;
+    char *cleanauthor;
 
     _notmuch_message_list_add_message (thread->message_list,
 				       talloc_steal (thread, message));
@@ -178,8 +224,9 @@ _thread_add_message (notmuch_thread_t *thread,
 		mailbox = INTERNET_ADDRESS_MAILBOX (address);
 		author = internet_address_mailbox_get_addr (mailbox);
 	    }
-	    _thread_add_author (thread, author);
-	    notmuch_message_set_author (message, author);
+	    cleanauthor = _thread_cleanup_author (thread, author, from);
+	    _thread_add_author (thread, cleanauthor);
+	    notmuch_message_set_author (message, cleanauthor);
 	}
 	g_object_unref (G_OBJECT (list));
     }
-- 
1.6.6.1


-- 
Dirk Hohndel
Intel Open Source Technology Center


More information about the notmuch mailing list