+ g_ptr_array_add (thread->authors_array, author_copy);
+}
+
+/* Add each matched author of the thread to the thread's
+ * matched_authors_hash and to the thread's matched_authors_array. */
+static void
+_thread_add_matched_author (notmuch_thread_t *thread,
+ const char *author)
+{
+ char *author_copy;
+
+ if (author == NULL)
+ return;
+
+ if (g_hash_table_lookup_extended (thread->matched_authors_hash,
+ author, NULL, NULL))
+ return;
+
+ author_copy = talloc_strdup (thread, author);
+
+ g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
+
+ g_ptr_array_add (thread->matched_authors_array, author_copy);
+}
+
+/* Construct an authors string from matched_authors_array and
+ * authors_array. The string contains matched authors first, then
+ * non-matched authors (with the two groups separated by '|'). Within
+ * each group, authors are listed in date order. */
+static void
+_resolve_thread_authors_string (notmuch_thread_t *thread)
+{
+ unsigned int i;
+ char *author;
+ int first_non_matched_author = 1;
+
+ /* First, list all matched authors in date order. */
+ for (i = 0; i < thread->matched_authors_array->len; i++) {
+ author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
+ if (thread->authors)
+ thread->authors = talloc_asprintf (thread, "%s, %s",
+ thread->authors,
+ author);
+ else
+ thread->authors = author;
+ }
+
+ /* Next, append any non-matched authors that haven't already appeared. */
+ for (i = 0; i < thread->authors_array->len; i++) {
+ author = (char *) g_ptr_array_index (thread->authors_array, i);
+ if (g_hash_table_lookup_extended (thread->matched_authors_hash,
+ author, NULL, NULL))
+ continue;
+ if (first_non_matched_author) {
+ thread->authors = talloc_asprintf (thread, "%s| %s",
+ thread->authors,
+ author);
+ } else {
+ thread->authors = talloc_asprintf (thread, "%s, %s",
+ thread->authors,
+ author);
+ }
+
+ first_non_matched_author = 0;
+ }
+
+ 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;
+ fname = strlen(author) - lname - 2;
+ strncpy(clean_author, comma + 2, fname);
+ *(clean_author+fname) = ' ';
+ strncpy(clean_author + fname + 1, author, lname);
+ *(clean_author+fname+1+lname) = '\0';
+ /* make a temporary copy and see if it matches the email */
+ test_author = talloc_strdup(thread,clean_author);
+
+ blank=strchr(test_author,' ');
+ while (blank != NULL) {
+ *blank = '.';
+ blank=strchr(test_author,' ');
+ }
+ if (strcasestr(from, test_author) == NULL)
+ /* we didn't identify this as part of the email address
+ * so let's punt and return the original author */
+ strcpy (clean_author, author);
+ }
+ return clean_author;