X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Findex.cc;h=78c18cf36d10898a8ea3aebcb1c8fe84fcff3df3;hp=cf930251c8c7be4c34bb53dab6930cca940d0302;hb=3fed6736a7ef8b8b1f05d0fabb136bdd3b5917ee;hpb=2bc0af15aa4cc5b4963e9ff2c2b615ea9ce4ffca diff --git a/lib/index.cc b/lib/index.cc index cf930251..78c18cf3 100644 --- a/lib/index.cc +++ b/lib/index.cc @@ -63,7 +63,7 @@ struct _NotmuchFilterDiscardUuencodeClass { GMimeFilterClass parent_class; }; -GMimeFilter *notmuch_filter_discard_uuencode_new (void); +static GMimeFilter *notmuch_filter_discard_uuencode_new (void); static void notmuch_filter_discard_uuencode_finalize (GObject *object); @@ -195,7 +195,7 @@ filter_reset (GMimeFilter *gmime_filter) * * Returns: a new #NotmuchFilterDiscardUuencode filter. **/ -GMimeFilter * +static GMimeFilter * notmuch_filter_discard_uuencode_new (void) { static GType type = 0; @@ -304,26 +304,6 @@ _index_address_list (notmuch_message_t *message, } } -static const char * -skip_re_in_subject (const char *subject) -{ - const char *s = subject; - - if (subject == NULL) - return NULL; - - while (*s) { - while (*s && isspace (*s)) - s++; - if (strncasecmp (s, "re:", 3) == 0) - s += 3; - else - break; - } - - return s; -} - /* Callback to generate terms for each mime part of a message. */ static void _index_mime_part (notmuch_message_t *message, @@ -335,11 +315,23 @@ _index_mime_part (notmuch_message_t *message, GByteArray *byte_array; GMimeContentDisposition *disposition; char *body; + const char *charset; + + if (! part) { + fprintf (stderr, "Warning: Not indexing empty mime part.\n"); + return; + } if (GMIME_IS_MULTIPART (part)) { GMimeMultipart *multipart = GMIME_MULTIPART (part); int i; + if (GMIME_IS_MULTIPART_SIGNED (multipart)) + _notmuch_message_add_term (message, "tag", "signed"); + + if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) + _notmuch_message_add_term (message, "tag", "encrypted"); + for (i = 0; i < g_mime_multipart_get_count (multipart); i++) { if (GMIME_IS_MULTIPART_SIGNED (multipart)) { /* Don't index the signature. */ @@ -348,6 +340,10 @@ _index_mime_part (notmuch_message_t *message, if (i > 1) fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n"); } + if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) { + /* Don't index encrypted parts. */ + continue; + } _index_mime_part (message, g_mime_multipart_get_part (multipart, i)); } @@ -395,6 +391,20 @@ _index_mime_part (notmuch_message_t *message, g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter), discard_uuencode_filter); + charset = g_mime_object_get_content_type_parameter (part, "charset"); + if (charset) { + GMimeFilter *charset_filter; + charset_filter = g_mime_filter_charset_new (charset, "UTF-8"); + /* This result can be NULL for things like "unknown-8bit". + * Don't set a NULL filter as that makes GMime print + * annoying assertion-failure messages on stderr. */ + if (charset_filter) { + g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter), + charset_filter); + g_object_unref (charset_filter); + } + } + wrapper = g_mime_part_get_content_object (GMIME_PART (part)); if (wrapper) g_mime_data_wrapper_write_to_stream (wrapper, filter); @@ -425,9 +435,12 @@ _notmuch_message_index_file (notmuch_message_t *message, const char *from, *subject; notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; static int initialized = 0; + char from_buf[5]; + bool is_mbox = false; + static bool mbox_warning = false; if (! initialized) { - g_mime_init (0); + g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS); initialized = 1; } @@ -438,23 +451,53 @@ _notmuch_message_index_file (notmuch_message_t *message, goto DONE; } + /* Is this mbox? */ + if (fread (from_buf, sizeof (from_buf), 1, file) == 1 && + strncmp (from_buf, "From ", 5) == 0) + is_mbox = true; + rewind (file); + /* Evil GMime steals my FILE* here so I won't fclose it. */ stream = g_mime_stream_file_new (file); parser = g_mime_parser_new_with_stream (stream); + g_mime_parser_set_scan_from (parser, is_mbox); mime_message = g_mime_parser_construct_message (parser); + if (is_mbox) { + if (!g_mime_parser_eos (parser)) { + /* This is a multi-message mbox. */ + ret = NOTMUCH_STATUS_FILE_NOT_EMAIL; + goto DONE; + } + /* For historical reasons, we support single-message mboxes, + * but this behavior is likely to change in the future, so + * warn. */ + if (!mbox_warning) { + mbox_warning = true; + fprintf (stderr, "\ +Warning: %s is an mbox containing a single message,\n\ +likely caused by misconfigured mail delivery. Support for single-message\n\ +mboxes is deprecated and may be removed in the future.\n", filename); + } + } + from = g_mime_message_get_sender (mime_message); - addresses = internet_address_list_parse_string (from); - _index_address_list (message, "from", addresses); + addresses = internet_address_list_parse_string (from); + if (addresses) { + _index_address_list (message, "from", addresses); + g_object_unref (addresses); + } addresses = g_mime_message_get_all_recipients (mime_message); - _index_address_list (message, "to", addresses); + if (addresses) { + _index_address_list (message, "to", addresses); + g_object_unref (addresses); + } subject = g_mime_message_get_subject (mime_message); - subject = skip_re_in_subject (subject); _notmuch_message_gen_terms (message, "subject", subject); _index_mime_part (message, g_mime_message_get_mime_part (mime_message));