- /* are there any authors in the list after our author? */
- if (thread->nonmatched_authors + author_len < thread->authors + authors_len) {
- /* we have to make changes, so let's get a temp copy */
- authors_copy = talloc_strdup(thread,thread->authors);
- /* nm_start is the offset into where the non-matched authors start */
- nm_start = thread->nonmatched_authors - thread->authors;
- /* copy this author and add the "| " - the if clause above tells us there's more */
- strncpy(thread->nonmatched_authors,author,author_len);
- strncpy(thread->nonmatched_authors+author_len,"| ",2);
- thread->nonmatched_authors += author_len+2;
- if (idx > 0) {
- /* we are actually moving authors around, not just changing the separator
- * first copy the authors that came BEFORE our author */
- strncpy(thread->nonmatched_authors, authors_copy+nm_start, idx-2);
- /* finally, if there are authors AFTER our author, copy those */
- if(author_len+nm_start+idx < authors_len) {
- strncpy(thread->nonmatched_authors + idx - 2,", ",2);
- strncpy(thread->nonmatched_authors + idx, authors_copy+nm_start + idx + author_len + 2,
- authors_len - (nm_start + idx + author_len + 2));
- }
+
+ g_ptr_array_free (thread->authors_array, TRUE);
+ thread->authors_array = NULL;
+ g_ptr_array_free (thread->matched_authors_array, TRUE);
+ thread->matched_authors_array = NULL;
+}
+
+/* clean up the ugly "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@company.com>
+ * "Last, First MI" <first.mi.last@company.com>
+ */
+static char *
+_thread_cleanup_author (notmuch_thread_t *thread,
+ const char *author, const char *from)
+{
+ char *clean_author,*test_author;
+ const char *comma;
+ char *blank;
+ int fname,lname;
+
+ if (author == NULL)
+ return NULL;
+ clean_author = talloc_strdup(thread, author);
+ if (clean_author == NULL)
+ return NULL;
+ /* check if there's a comma in the name and that there's a
+ * component of the name behind it (so the name doesn't end with
+ * the comma - in which case the string that strchr finds is just
+ * one character long ",\0").
+ * Otherwise just return the copy of the original author name that
+ * we just made*/
+ comma = strchr(author,',');
+ if (comma && strlen(comma) > 1) {
+ /* let's assemble what we think is the correct name */
+ lname = comma - author;
+
+ /* Skip all the spaces after the comma */
+ fname = strlen(author) - lname - 1;
+ comma += 1;
+ while (*comma == ' ') {
+ fname -= 1;
+ comma += 1;