]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-index-message.cc
notmuch-index-message: Index References as well.
[notmuch] / notmuch-index-message.cc
index dedbff37dec206269e09684f64edcdd12a33ae4e..7fb5a01900eece339c709788732c2ad51fdb65d2 100644 (file)
@@ -187,6 +187,55 @@ add_terms_address_addrs (Xapian::Document doc,
     }
 }
 
+static const char *
+skip_re_in_subject (const char *subject)
+{
+    const char *s = subject;
+
+    while (*s) {
+       while (*s && isspace (*s))
+           s++;
+       if (strncasecmp (s, "re:", 3) == 0)
+           s += 3;
+       else
+           break;
+    }
+
+    return s;
+}
+
+/* Add a term for each message-id in the References header of the
+ * message. */
+static void
+add_terms_references (Xapian::Document doc,
+                     GMimeMessage *message)
+{
+    const char *refs, *end, *next;
+    char *term;
+
+    refs = g_mime_object_get_header (GMIME_OBJECT (message), "references");
+
+    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) {
+           term = g_strndup (refs, end - refs + 1);
+           add_term (doc, "ref", term);
+           g_free (term);
+       }
+       refs = next;
+    }
+}
+
 /* Generate terms for the body of a message, given the filename of the
  * message and the offset at which the headers of the message end,
  * (and hence the body begins). */
@@ -198,7 +247,7 @@ gen_terms_body (Xapian::TermGenerator term_gen,
     GIOChannel *channel;
     GIOStatus gio_status;
     GError *error = NULL;
-    char *body_line = NULL;
+    char *p, *body_line = NULL, *prev_line = NULL;
 
     channel = g_io_channel_new_file (filename, "r", &error);
     if (channel == NULL) {
@@ -226,9 +275,38 @@ gen_terms_body (Xapian::TermGenerator term_gen,
            exit (1);
        }
 
-       if (body_line[0] == '>')
+       if (strlen (body_line) == 0)
+           continue;
+
+       /* If the line looks like it might be introducing a quote,
+        * save it until we see if the next line begins a quote. */
+       p = body_line + strlen (body_line) - 1;
+       while (p > body_line and isspace (*p))
+           p--;
+       if (*p == ':') {
+           prev_line = body_line;
+           body_line = NULL;
            continue;
+       }
+
+       /* Skip quoted lines, (and previous lines that introduced them) */
+       if (body_line[0] == '>') {
+           if (prev_line) {
+               g_free (prev_line);
+               prev_line = NULL;
+           }
+           continue;
+       }
 
+       /* Now that we're not looking at a quote we can add the prev_line */
+       if (prev_line) {
+           gen_terms (term_gen, "body", prev_line);
+           g_free (prev_line);
+           prev_line = NULL;
+       }
+
+       /* Skip signatures */
+       /* XXX: Should only do this if "near" the end of the message. */
        if (strncmp (body_line, "-- ", 3) == 0)
            break;
 
@@ -308,12 +386,15 @@ main (int argc, char **argv)
        gen_terms_address_names (term_gen, addresses, "to_name");
 
        value = g_mime_message_get_subject (message);
+       value = skip_re_in_subject (value);
        gen_terms (term_gen, "subject", value);
        gen_terms (term_gen, "body", value);
 
        gen_terms_body (term_gen, filename,
                        g_mime_parser_get_headers_end (parser));
 
+       add_terms_references (doc, message);
+
        from = g_mime_message_get_sender (message);
        addresses = internet_address_list_parse_string (from);
 
@@ -338,12 +419,10 @@ main (int argc, char **argv)
        add_term (doc, "label", "inbox");
        add_term (doc, "label", "unread");
        add_term (doc, "type", "mail");
+       add_term (doc, "source_id", "1");
 
        value = g_mime_message_get_message_id (message);
        add_term (doc, "msgid", value);
-
-       add_term (doc, "source_id", "1");
-
        add_term (doc, "thread", value);
 
        doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value);