X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-index-message.cc;h=a0faf442dbf9ef1c04910046e3560c90df50f7a8;hp=39be64eca5156241b59c2733009b8be374ab32f5;hb=c0da89a8e02190f172e6f53073827bbdf91d60eb;hpb=f69215d41ff89998cac1ac0d7d13116d5aeb5276 diff --git a/notmuch-index-message.cc b/notmuch-index-message.cc index 39be64ec..a0faf442 100644 --- a/notmuch-index-message.cc +++ b/notmuch-index-message.cc @@ -187,21 +187,154 @@ add_terms_address_addrs (Xapian::Document doc, } } -int -main (int argc, char **argv) +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"); + + 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) { + 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). */ +static void +gen_terms_body (Xapian::TermGenerator term_gen, + const char * filename, + gint64 body_offset) +{ + GIOChannel *channel; + GIOStatus gio_status; + GError *error = NULL; + char *p, *body_line = NULL, *prev_line = NULL; + + channel = g_io_channel_new_file (filename, "r", &error); + if (channel == NULL) { + fprintf (stderr, "Error: %s\n", error->message); + exit (1); + } + + gio_status = g_io_channel_seek_position (channel, body_offset, + G_SEEK_SET, &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); + } + + if (body_line) + g_free (body_line); + + g_io_channel_close (channel); +} + +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; - GIOChannel *channel; - GIOStatus gio_status; - GError *error = NULL; - const char *database_path, *filename; FILE *file; - gint64 body_offset; - char *body_str; const char *value, *from; @@ -209,131 +342,145 @@ main (int argc, char **argv) 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]; - file = fopen (filename, "r"); if (! file) { fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); 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); + add_terms_references (doc, message); - body_offset = g_mime_parser_get_headers_end (parser); - channel = g_io_channel_new_file (filename, "r", &error); - if (channel == NULL) { - fprintf (stderr, "Error: %s\n", error->message); - exit (1); - } + from = g_mime_message_get_sender (message); + addresses = internet_address_list_parse_string (from); - gio_status = g_io_channel_seek_position (channel, body_offset, - G_SEEK_SET, &error); - if (gio_status != G_IO_STATUS_NORMAL) { - fprintf (stderr, "Error: %s\n", error->message); - exit (1); - } + add_terms_address_addrs (doc, addresses, "from_email"); - 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); - } + add_terms_address_addrs (doc, + g_mime_message_get_all_recipients (message), + "to_email"); + + g_mime_message_get_date (message, &time, NULL); + + gmtime_r (&time, &gm_time_tm); - gen_terms (term_gen, "body", body_str); + 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); + } - g_free (body_str); - g_io_channel_close (channel); + add_term (doc, "date", date_str); - from = g_mime_message_get_sender (message); - addresses = internet_address_list_parse_string (from); + add_term (doc, "label", "inbox"); + add_term (doc, "label", "unread"); + add_term (doc, "type", "mail"); + add_term (doc, "source_id", "1"); - add_terms_address_addrs (doc, addresses, "from_email"); + value = g_mime_message_get_message_id (message); + add_term (doc, "msgid", value); + add_term (doc, "thread", value); - add_terms_address_addrs (doc, - g_mime_message_get_all_recipients (message), - "to_email"); + doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); + doc.add_value (NOTMUCH_VALUE_THREAD, value); - g_mime_message_get_date (message, &time, NULL); + doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); - gmtime_r (&time, &gm_time_tm); + db.add_document (doc); - 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); - } + g_object_unref (message); + g_object_unref (parser); + g_object_unref (stream); +} - add_term (doc, "date", date_str); +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."); +} - add_term (doc, "label", "inbox"); - add_term (doc, "label", "unread"); - add_term (doc, "type", "mail"); +int +main (int argc, char **argv) +{ + const char *database_path; + char *filename; + GIOChannel *channel; + GIOStatus gio_status; + GError *error = NULL; - value = g_mime_message_get_message_id (message); - add_term (doc, "msgid", value); + if (argc < 2) { + usage (argv[0]); + exit (1); + } - add_term (doc, "source_id", "1"); + database_path = argv[1]; - add_term (doc, "thread", value); + g_mime_init (0); - doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value); - doc.add_value (NOTMUCH_VALUE_THREAD, value); + try { + Xapian::WritableDatabase db; + Xapian::TermGenerator term_gen; + + db = Xapian::WritableDatabase (database_path, + Xapian::DB_CREATE_OR_OPEN); + + term_gen = Xapian::TermGenerator (); - doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time)); + channel = g_io_channel_unix_new (fileno (stdin)); - db.add_document (doc); + 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); } - g_object_unref (message); - g_object_unref (parser); - g_object_unref (stream); - return 0; }