]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
add_message: Don't add any self-references to the database.
[notmuch] / lib / database.cc
index 4524016b37aecb1d0ffbacfbe72d4b5c8b03b94d..07a2bdd60eac7a8b51dd8ceeda7de4791d1d6652 100644 (file)
@@ -107,7 +107,6 @@ prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
     { "ref", "XREFERENCE" },
     { "replyto", "XREPLYTO" },
     { "timestamp", "XTIMESTAMP" },
-    { "contact", "XCONTACT" }
 };
 
 prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
@@ -313,8 +312,8 @@ skip_space_and_comments (const char **str)
  * Returns a newly talloc'ed string belonging to 'ctx'.
  *
  * Returns NULL if there is any error parsing the message-id. */
-static char *
-parse_message_id (void *ctx, const char *message_id, const char **next)
+char *
+_parse_message_id (void *ctx, const char *message_id, const char **next)
 {
     const char *s, *end;
     char *result;
@@ -372,9 +371,16 @@ parse_message_id (void *ctx, const char *message_id, const char **next)
 }
 
 /* Parse a References header value, putting a (talloc'ed under 'ctx')
- * copy of each referenced message-id into 'hash'. */
+ * copy of each referenced message-id into 'hash'.
+ *
+ * We explicitly avoid including any reference identical to
+ * 'message_id' in the result (to avoid mass confusion when a single
+ * message references itself cyclically---and yes, mail messages are
+ * not infrequent in the wild that do this---don't ask me why).
+*/
 static void
 parse_references (void *ctx,
+                 const char *message_id,
                  GHashTable *hash,
                  const char *refs)
 {
@@ -384,29 +390,13 @@ parse_references (void *ctx,
        return;
 
     while (*refs) {
-       ref = parse_message_id (ctx, refs, &refs);
+       ref = _parse_message_id (ctx, refs, &refs);
 
-       if (ref)
+       if (ref && strcmp (ref, message_id))
            g_hash_table_insert (hash, ref, NULL);
     }
 }
 
-char *
-notmuch_database_default_path (void)
-{
-    char *path;
-
-    if (getenv ("NOTMUCH_BASE"))
-       return strdup (getenv ("NOTMUCH_BASE"));
-
-    if (asprintf (&path, "%s/mail", getenv ("HOME")) == -1) {
-       fprintf (stderr, "Out of memory.\n");
-       return xstrdup("");
-    }
-
-    return path;
-}
-
 notmuch_database_t *
 notmuch_database_create (const char *path)
 {
@@ -414,10 +404,11 @@ notmuch_database_create (const char *path)
     char *notmuch_path = NULL;
     struct stat st;
     int err;
-    char *local_path = NULL;
 
-    if (path == NULL)
-       path = local_path = notmuch_database_default_path ();
+    if (path == NULL) {
+       fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
+       goto DONE;
+    }
 
     err = stat (path, &st);
     if (err) {
@@ -447,8 +438,6 @@ notmuch_database_create (const char *path)
   DONE:
     if (notmuch_path)
        talloc_free (notmuch_path);
-    if (local_path)
-       free (local_path);
 
     return notmuch;
 }
@@ -460,12 +449,8 @@ notmuch_database_open (const char *path)
     char *notmuch_path = NULL, *xapian_path = NULL;
     struct stat st;
     int err;
-    char *local_path = NULL;
     unsigned int i;
 
-    if (path == NULL)
-       path = local_path = notmuch_database_default_path ();
-
     if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
        notmuch_path = NULL;
        fprintf (stderr, "Out of memory\n");
@@ -520,8 +505,6 @@ notmuch_database_open (const char *path)
     }
     
   DONE:
-    if (local_path)
-       free (local_path);
     if (notmuch_path)
        free (notmuch_path);
     if (xapian_path)
@@ -721,7 +704,7 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
                                           const char **thread_id)
 {
     GHashTable *parents = NULL;
-    const char *refs, *in_reply_to;
+    const char *refs, *in_reply_to, *in_reply_to_message_id;
     GList *l, *keys = NULL;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
@@ -729,12 +712,21 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
                                     _my_talloc_free_for_g_hash, NULL);
 
     refs = notmuch_message_file_get_header (message_file, "references");
-    parse_references (message, parents, refs);
+    parse_references (message, notmuch_message_get_message_id (message),
+                     parents, refs);
 
     in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
-    parse_references (message, parents, in_reply_to);
-    _notmuch_message_add_term (message, "replyto",
-                              parse_message_id (message, in_reply_to, NULL));
+    parse_references (message, notmuch_message_get_message_id (message),
+                     parents, in_reply_to);
+
+    /* Carefully avoid adding any self-referential in-reply-to term. */
+    in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
+    if (strcmp (in_reply_to_message_id,
+               notmuch_message_get_message_id (message)))
+    {
+       _notmuch_message_add_term (message, "replyto",
+                            _parse_message_id (message, in_reply_to, NULL));
+    }
 
     keys = g_hash_table_get_keys (parents);
     for (l = keys; l; l = l->next) {
@@ -820,7 +812,7 @@ _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
 /* Given a (mostly empty) 'message' and its corresponding
  * 'message_file' link it to existing threads in the database.
  *
- * We first looke at 'message_file' and its link-relevant headers
+ * We first look at 'message_file' and its link-relevant headers
  * (References and In-Reply-To) for message IDs. We also look in the
  * database for existing message that reference 'message'.p
  *
@@ -910,7 +902,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
        header = notmuch_message_file_get_header (message_file, "message-id");
        if (header) {
-           message_id = parse_message_id (message_file, header, NULL);
+           message_id = _parse_message_id (message_file, header, NULL);
            /* So the header value isn't RFC-compliant, but it's
             * better than no message-id at all. */
            if (message_id == NULL)
@@ -935,9 +927,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
         * (which may or may not reference an existing document in the
         * database). */
 
-       /* Use NULL for owner since we want to free this locally. */
-       message = _notmuch_message_create_for_message_id (NULL,
-                                                         notmuch,
+       message = _notmuch_message_create_for_message_id (notmuch,
                                                          message_id,
                                                          &private_status);