]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-index-message.cc
notmuch-index-message: Fix handling of thread_id terms.
[notmuch] / notmuch-index-message.cc
index f7ea340f977329d5da7354a2a613ec5766259720..dfcb3ba50c67b56f3757c4c8311b4aa96146fec5 100644 (file)
@@ -139,11 +139,29 @@ gen_terms_address_name (Xapian::TermGenerator term_gen,
                        const char *prefix_name)
 {
     const char *name;
+    int own_name = 0;
 
     name = internet_address_get_name (address);
 
+    /* In the absence of a name, we'll strip the part before the @
+     * from the address. */
+    if (! name) {
+       InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
+       const char *addr = internet_address_mailbox_get_addr (mailbox);
+       const char *at;
+
+       at = strchr (addr, '@');
+       if (at) {
+           name = strndup (addr, at - addr);
+           own_name = 1;
+       }
+    }
+
     if (name)
        gen_terms (term_gen, prefix_name, name);
+
+    if (own_name)
+       free ((void *) name);
 }
 
 static void
@@ -259,18 +277,25 @@ insert_thread_id (GHashTable *thread_ids, Xapian::Document doc)
        g_hash_table_insert (thread_ids, strdup (value), NULL);
 }
 
-static char *
-find_thread_id (Xapian::Database db,
-               GPtrArray *parents,
-               const char *message_id)
+/* Return one or more thread_ids, (as a GPtrArray of strings), for the
+ * given message based on looking into the database for any messages
+ * referenced in parents, and also for any messages in the database
+ * referencing message_id.
+ *
+ * Caller should free all strings in the array and the array itself,
+ * (g_ptr_array_free) when done. */
+static GPtrArray *
+find_thread_ids (Xapian::Database db,
+                GPtrArray *parents,
+                const char *message_id)
 {
     Xapian::PostingIterator child, children_end;
     Xapian::Document doc;
     GHashTable *thread_ids;
     GList *keys, *l;
-    GString *result = NULL;
     unsigned int i;
     const char *parent_message_id;
+    GPtrArray *result;
 
     thread_ids = g_hash_table_new (g_str_hash, g_str_equal);
 
@@ -286,51 +311,37 @@ find_thread_id (Xapian::Database db,
        insert_thread_id (thread_ids, doc);
     }
 
+    result = g_ptr_array_new ();
+
     keys = g_hash_table_get_keys (thread_ids);
     for (l = keys; l; l = l->next) {
        char *id = (char *) l->data;
-       if (result == NULL) {
-           result = g_string_new (id);
-       } else {
-           g_string_append_printf (result, ",%s", id);
-       }
-       free (id);
+       g_ptr_array_add (result, id);
     }
 
-    if (result)
-       return g_string_free (result, FALSE);
-    else
-       return NULL;
+    return result;
 }
 
 /* Add a term for each message-id in the References header of the
  * message. */
 static void
 parse_references (GPtrArray *array,
-                 const char *refs)
+                 const char *refs_str)
 {
-    const char *end, *next;
+    GMimeReferences *refs, *r;
+    const char *message_id;
 
-    if (refs == NULL)
+    if (refs_str == NULL)
        return;
 
-    while (*refs) {
-       while (*refs && isspace (*refs))
-           refs++;
-       if (*refs == '<')
-           refs++;
-       end = refs;
-       while (*end && !isspace (*end))
-           end++;
-       next = end;
-       end--;
-       if (end > refs && *end == '>')
-           end--;
-       if (end > refs) {
-           g_ptr_array_add (array, g_strndup (refs, end - refs + 1));
-       }
-       refs = next;
+    refs = g_mime_references_decode (refs_str);
+
+    for (r = refs; r; r = r->next) {
+       message_id = g_mime_references_get_message_id (r);
+       g_ptr_array_add (array, g_strdup (message_id));
     }
+
+    g_mime_references_free (refs);
 }
 
 /* Given a string representing the body of a message, generate terms
@@ -430,12 +441,12 @@ index_file (Xapian::WritableDatabase db,
     GMimeParser *parser;
     GMimeMessage *message;
     InternetAddressList *addresses;
-    GPtrArray *parents;
+    GPtrArray *parents, *thread_ids;
 
     FILE *file;
 
     const char *subject, *refs, *in_reply_to, *from;
-    const char *message_id, *thread_id;
+    const char *message_id;
 
     time_t time;
     struct tm gm_time_tm;
@@ -491,7 +502,7 @@ index_file (Xapian::WritableDatabase db,
 
     message_id = g_mime_message_get_message_id (message);
 
-    thread_id = find_thread_id (db, parents, message_id);
+    thread_ids = find_thread_ids (db, parents, message_id);
 
     for (i = 0; i < parents->len; i++)
        g_free (g_ptr_array_index (parents, i));
@@ -526,10 +537,28 @@ index_file (Xapian::WritableDatabase db,
     add_term (doc, "msgid", message_id);
     doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
 
-    if (thread_id) {
-       add_term (doc, "thread", thread_id);
-       doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
-       free ((void *) thread_id);
+    if (thread_ids->len) {
+       unsigned int i;
+       GString *thread_id;
+       char *id;
+
+       for (i = 0; i < thread_ids->len; i++) {
+           id = (char *) thread_ids->pdata[i];
+
+           add_term (doc, "thread", id);
+
+           if (i == 0)
+               thread_id = g_string_new (id);
+           else
+               g_string_append_printf (thread_id, ",%s", id);
+
+           free (id);
+       }
+       g_ptr_array_free (thread_ids, TRUE);
+
+       doc.add_value (NOTMUCH_VALUE_THREAD, thread_id->str);
+
+       g_string_free (thread_id, TRUE);
     } else {
        /* If not referenced thread, use the message ID */
        add_term (doc, "thread", message_id);