]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-index-message.cc
notmuch-index-message: Don't index the line introducing a quote.
[notmuch] / notmuch-index-message.cc
index b7a0d3e7bb37a8c440819def7d40f4ee4ceec8d2..44b1b03a1e180bc8625b13dcd02f8ddf73f1a453 100644 (file)
@@ -198,7 +198,7 @@ gen_terms_body (Xapian::TermGenerator term_gen,
     GIOChannel *channel;
     GIOStatus gio_status;
     GError *error = NULL;
-    char *body_str;
+    char *p, *body_line = NULL, *prev_line = NULL;
 
     channel = g_io_channel_new_file (filename, "r", &error);
     if (channel == NULL) {
@@ -213,16 +213,60 @@ gen_terms_body (Xapian::TermGenerator term_gen,
        exit (1);
     }
 
-    gio_status = g_io_channel_read_to_end (channel, &body_str,
-                                          NULL, &error);
-    if (gio_status != G_IO_STATUS_NORMAL) {
-       fprintf (stderr, "Error: %s\n", error->message);
-       exit (1);
+    while (1) {
+       if (body_line)
+           g_free (body_line);
+
+       gio_status = g_io_channel_read_line (channel, &body_line,
+                                            NULL, NULL, &error);
+       if (gio_status == G_IO_STATUS_EOF)
+           break;
+       if (gio_status != G_IO_STATUS_NORMAL) {
+           fprintf (stderr, "Error: %s\n", error->message);
+           exit (1);
+       }
+
+       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;
+
+       gen_terms (term_gen, "body", body_line);
     }
 
-    gen_terms (term_gen, "body", body_str);
+    if (body_line)
+       g_free (body_line);
 
-    g_free (body_str);
     g_io_channel_close (channel);
 }