X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fmessage-file.c;h=e1db26fb8c143540db9b96d5e3a67f7f3bc19da3;hp=5085506700640f2f6f541dc71a8f9db306d7123c;hb=HEAD;hpb=6682b4e686b7972883626c9b0f941ae4bf02dedb diff --git a/lib/message-file.c b/lib/message-file.c index 50855067..68f646a4 100644 --- a/lib/message-file.c +++ b/lib/message-file.c @@ -46,6 +46,9 @@ _notmuch_message_file_destructor (notmuch_message_file_t *message) if (message->message) g_object_unref (message->message); + if (message->stream) + g_object_unref (message->stream); + return 0; } @@ -61,21 +64,38 @@ _notmuch_message_file_open_ctx (notmuch_database_t *notmuch, if (unlikely (message == NULL)) return NULL; - message->filename = talloc_strdup (message, filename); + const char *prefix = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT); + + if (prefix == NULL) + goto FAIL; + + if (*filename == '/') { + if (strncmp (filename, prefix, strlen (prefix)) != 0) { + _notmuch_database_log (notmuch, "Error opening %s: path outside mail root\n", + filename); + errno = 0; + goto FAIL; + } + message->filename = talloc_strdup (message, filename); + } else { + message->filename = talloc_asprintf (message, "%s/%s", prefix, filename); + } + if (message->filename == NULL) goto FAIL; talloc_set_destructor (message, _notmuch_message_file_destructor); - message->stream = g_mime_stream_gzfile_open (filename); + message->stream = g_mime_stream_gzfile_open (message->filename); if (message->stream == NULL) goto FAIL; return message; FAIL: - _notmuch_database_log (notmuch, "Error opening %s: %s\n", - filename, strerror (errno)); + if (errno) + _notmuch_database_log (notmuch, "Error opening %s: %s\n", + filename, strerror (errno)); _notmuch_message_file_close (message); return NULL; @@ -107,7 +127,7 @@ _is_mbox (GMimeStream *stream) bool ret = false; /* Is this mbox? */ - if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof(from_buf) && + if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof (from_buf) && strncmp (from_buf, "From ", 5) == 0) ret = true; @@ -121,7 +141,6 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) { GMimeParser *parser; notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; - static int initialized = 0; bool is_mbox; if (message->message) @@ -129,10 +148,7 @@ _notmuch_message_file_parse (notmuch_message_file_t *message) is_mbox = _is_mbox (message->stream); - if (! initialized) { - g_mime_init (); - initialized = 1; - } + _notmuch_init (); message->headers = g_hash_table_new_full (strcase_hash, strcase_equal, free, g_free); @@ -198,7 +214,8 @@ _notmuch_message_file_get_mime_message (notmuch_message_file_t *message, */ static char * -_extend_header (char *combined, const char *value) { +_extend_header (char *combined, const char *value) +{ char *decoded; decoded = g_mime_utils_header_decode_text (NULL, value); @@ -223,7 +240,7 @@ _extend_header (char *combined, const char *value) { } else { combined = decoded; } - DONE: + DONE: return combined; } @@ -239,7 +256,7 @@ _notmuch_message_file_get_combined_header (notmuch_message_file_t *message, return NULL; - for (int i=0; i < g_mime_header_list_get_count (headers); i++) { + for (int i = 0; i < g_mime_header_list_get_count (headers); i++) { const char *value; GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i); @@ -261,7 +278,7 @@ _notmuch_message_file_get_combined_header (notmuch_message_file_t *message, const char * _notmuch_message_file_get_header (notmuch_message_file_t *message, - const char *header) + const char *header) { const char *value; char *decoded; @@ -274,11 +291,16 @@ _notmuch_message_file_get_header (notmuch_message_file_t *message, if (value) return value; - if (strcasecmp (header, "received") == 0) { + if (strcasecmp (header, "received") == 0 || + strcasecmp (header, "delivered-to") == 0) { /* - * The Received: header is special. We concatenate all - * instances of the header as we use this when analyzing the - * path the mail has taken from sender to recipient. + * The Received: header is special. We concatenate all instances of the + * header as we use this when analyzing the path the mail has taken + * from sender to recipient. + * + * Similarly, multiple instances of Delivered-To may be present. We + * concatenate them so the one with highest priority may be picked (eg. + * primary_email before other_email). */ decoded = _notmuch_message_file_get_combined_header (message, header); } else { @@ -363,7 +385,7 @@ _notmuch_message_file_get_headers (notmuch_message_file_t *message_file, message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1); free (sha1); } - DONE: + DONE: if (ret == NOTMUCH_STATUS_SUCCESS) { if (from_out) *from_out = from;