X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-index-message.cc;h=b8fa205b9cc474d5fc11d795d308f9933ae15007;hp=b7a0d3e7bb37a8c440819def7d40f4ee4ceec8d2;hb=5cbdcbbec58248ef803ba8f3f035e1c49b7ad0a8;hpb=9dbb1facfb1af5f16b3311a75593693ec04fa6a6 diff --git a/notmuch-index-message.cc b/notmuch-index-message.cc index b7a0d3e7..b8fa205b 100644 --- a/notmuch-index-message.cc +++ b/notmuch-index-message.cc @@ -187,6 +187,53 @@ 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 +parse_references (GPtrArray *array, + const char *refs) +{ + const char *end, *next; + + if (refs == 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; + } +} + /* 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 +245,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,29 +260,78 @@ 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 || + strncmp (body_line, "----------", 10) == 0 || + strncmp (body_line, "__________", 10) == 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); } - -int -main (int argc, char **argv) +static void +index_file (Xapian::WritableDatabase db, + Xapian::TermGenerator term_gen, + const char *filename) { + Xapian::Document doc; + GMimeStream *stream; GMimeParser *parser; GMimeMessage *message; InternetAddressList *addresses; + GPtrArray *parents; - const char *database_path, *filename; FILE *file; const char *value, *from; @@ -243,15 +339,7 @@ main (int argc, char **argv) time_t time; struct tm gm_time_tm; char date_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */ - - if (argc < 3) { - fprintf (stderr, "Usage: %s \n", - argv[0]); - exit (1); - } - - database_path = argv[1]; - filename = argv[2]; + unsigned int i; file = fopen (filename, "r"); if (! file) { @@ -259,93 +347,152 @@ main (int argc, char **argv) exit (1); } - g_mime_init (0); - stream = g_mime_stream_file_new (file); parser = g_mime_parser_new_with_stream (stream); message = g_mime_parser_construct_message (parser); - try { - Xapian::WritableDatabase db; - Xapian::TermGenerator term_gen; - Xapian::Document doc; + doc = Xapian::Document (); - doc = Xapian::Document (); + doc.set_data (filename); - doc.set_data (filename); + term_gen.set_stemmer (Xapian::Stem ("english")); - db = Xapian::WritableDatabase (database_path, - Xapian::DB_CREATE_OR_OPEN); + term_gen.set_document (doc); - term_gen = Xapian::TermGenerator (); - term_gen.set_stemmer (Xapian::Stem ("english")); + from = g_mime_message_get_sender (message); + addresses = internet_address_list_parse_string (from); - term_gen.set_document (doc); + gen_terms_address_names (term_gen, addresses, "from_name"); - from = g_mime_message_get_sender (message); - addresses = internet_address_list_parse_string (from); + addresses = g_mime_message_get_all_recipients (message); + gen_terms_address_names (term_gen, addresses, "to_name"); - gen_terms_address_names (term_gen, addresses, "from_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); - addresses = g_mime_message_get_all_recipients (message); - gen_terms_address_names (term_gen, addresses, "to_name"); + gen_terms_body (term_gen, filename, + g_mime_parser_get_headers_end (parser)); - value = g_mime_message_get_subject (message); - gen_terms (term_gen, "subject", value); - gen_terms (term_gen, "body", value); + parents = g_ptr_array_new (); - gen_terms_body (term_gen, filename, - g_mime_parser_get_headers_end (parser)); + value = g_mime_object_get_header (GMIME_OBJECT (message), "references"); + parse_references (parents, value); - from = g_mime_message_get_sender (message); - addresses = internet_address_list_parse_string (from); + value = g_mime_object_get_header (GMIME_OBJECT (message), "in-reply-to"); + parse_references (parents, value); - add_terms_address_addrs (doc, addresses, "from_email"); + for (i = 0; i < parents->len; i++) + add_term (doc, "ref", (const char *) g_ptr_array_index (parents, i)); - add_terms_address_addrs (doc, - g_mime_message_get_all_recipients (message), - "to_email"); + for (i = 0; i < parents->len; i++) + g_free (g_ptr_array_index (parents, i)); + g_ptr_array_free (parents, TRUE); - g_mime_message_get_date (message, &time, NULL); + from = g_mime_message_get_sender (message); + addresses = internet_address_list_parse_string (from); - gmtime_r (&time, &gm_time_tm); + add_terms_address_addrs (doc, addresses, "from_email"); - if (strftime (date_str, sizeof (date_str), - "%Y%m%d%H%M%S", &gm_time_tm) == 0) { - fprintf (stderr, "Internal error formatting time\n"); - exit (1); - } + add_terms_address_addrs (doc, + g_mime_message_get_all_recipients (message), + "to_email"); - add_term (doc, "date", date_str); + g_mime_message_get_date (message, &time, NULL); - add_term (doc, "label", "inbox"); - add_term (doc, "label", "unread"); - add_term (doc, "type", "mail"); + gmtime_r (&time, &gm_time_tm); - value = g_mime_message_get_message_id (message); - add_term (doc, "msgid", value); + if (strftime (date_str, sizeof (date_str), + "%Y%m%d%H%M%S", &gm_time_tm) == 0) { + fprintf (stderr, "Internal error formatting time\n"); + exit (1); + } - add_term (doc, "source_id", "1"); + add_term (doc, "date", date_str); - add_term (doc, "thread", value); + add_term (doc, "label", "inbox"); + add_term (doc, "label", "unread"); + add_term (doc, "type", "mail"); + add_term (doc, "source_id", "1"); - doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); - doc.add_value (NOTMUCH_VALUE_THREAD, value); + value = g_mime_message_get_message_id (message); + add_term (doc, "msgid", value); + add_term (doc, "thread", value); - doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); + doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); + doc.add_value (NOTMUCH_VALUE_THREAD, value); - db.add_document (doc); + doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); - } catch (const Xapian::Error &error) { - cerr << "A Xapian exception occurred: " << error.get_msg () << endl; - exit (1); - } + db.add_document (doc); g_object_unref (message); g_object_unref (parser); g_object_unref (stream); +} + +static void +usage (const char *argv0) +{ + fprintf (stderr, "Usage: %s \n", argv0); + fprintf (stderr, "\n"); + fprintf (stderr, "Messages to be indexed are read from stdnin as absolute filenames\n"); + fprintf (stderr, "one file per line."); +} + +int +main (int argc, char **argv) +{ + const char *database_path; + char *filename; + GIOChannel *channel; + GIOStatus gio_status; + GError *error = NULL; + + if (argc < 2) { + usage (argv[0]); + exit (1); + } + + database_path = argv[1]; + + g_mime_init (0); + + try { + Xapian::WritableDatabase db; + Xapian::TermGenerator term_gen; + + db = Xapian::WritableDatabase (database_path, + Xapian::DB_CREATE_OR_OPEN); + + term_gen = Xapian::TermGenerator (); + + channel = g_io_channel_unix_new (fileno (stdin)); + + while (1) { + gio_status = g_io_channel_read_line (channel, &filename, + NULL, NULL, &error); + if (gio_status == G_IO_STATUS_EOF) + break; + if (gio_status != G_IO_STATUS_NORMAL) { + fprintf (stderr, "An error occurred reading from stdin: %s\n", + error->message); + exit (1); + } + + g_strchomp (filename); + index_file (db, term_gen, filename); + + g_free (filename); + } + + } catch (const Xapian::Error &error) { + cerr << "A Xapian exception occurred: " << error.get_msg () << endl; + exit (1); + } return 0; }