1 /* message.c - Utility functions for parsing an email message for notmuch.
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
23 #include "notmuch-private.h"
25 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
29 struct _notmuch_message_file {
34 /* Cache for decoded headers */
37 GMimeMessage *message;
41 _notmuch_message_file_destructor (notmuch_message_file_t *message)
44 g_hash_table_destroy (message->headers);
47 g_object_unref (message->message);
50 fclose (message->file);
55 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
56 * the talloc owner. */
57 notmuch_message_file_t *
58 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
59 void *ctx, const char *filename)
61 notmuch_message_file_t *message;
63 message = talloc_zero (ctx, notmuch_message_file_t);
64 if (unlikely (message == NULL))
67 /* Only needed for error messages during parsing. */
68 message->filename = talloc_strdup (message, filename);
69 if (message->filename == NULL)
72 talloc_set_destructor (message, _notmuch_message_file_destructor);
74 message->file = fopen (filename, "r");
75 if (message->file == NULL)
81 _notmuch_database_log (notmuch, "Error opening %s: %s\n",
82 filename, strerror (errno));
83 _notmuch_message_file_close (message);
88 notmuch_message_file_t *
89 _notmuch_message_file_open (notmuch_database_t *notmuch,
92 return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
96 _notmuch_message_file_close (notmuch_message_file_t *message)
98 talloc_free (message);
101 static notmuch_bool_t
102 _is_mbox (FILE *file)
105 notmuch_bool_t ret = FALSE;
108 if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
109 strncmp (from_buf, "From ", 5) == 0)
118 _notmuch_message_file_parse (notmuch_message_file_t *message)
122 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
123 static int initialized = 0;
124 notmuch_bool_t is_mbox;
126 if (message->message)
127 return NOTMUCH_STATUS_SUCCESS;
129 is_mbox = _is_mbox (message->file);
132 g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
136 message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
138 if (! message->headers)
139 return NOTMUCH_STATUS_OUT_OF_MEMORY;
141 stream = g_mime_stream_file_new (message->file);
143 /* We'll own and fclose the FILE* ourselves. */
144 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
146 parser = g_mime_parser_new_with_stream (stream);
147 g_mime_parser_set_scan_from (parser, is_mbox);
149 message->message = g_mime_parser_construct_message (parser);
150 if (! message->message) {
151 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
155 if (is_mbox && ! g_mime_parser_eos (parser)) {
157 * This is a multi-message mbox. (For historical reasons, we
158 * do support single-message mboxes.)
160 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
164 g_object_unref (stream);
165 g_object_unref (parser);
168 g_hash_table_destroy (message->headers);
169 message->headers = NULL;
171 if (message->message) {
172 g_object_unref (message->message);
173 message->message = NULL;
176 rewind (message->file);
183 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
184 GMimeMessage **mime_message)
186 notmuch_status_t status;
188 status = _notmuch_message_file_parse (message);
192 *mime_message = message->message;
194 return NOTMUCH_STATUS_SUCCESS;
198 * Get all instances of a header decoded and concatenated.
200 * The result must be freed using g_free().
202 * Return NULL on errors, empty string for non-existing headers.
205 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
208 GMimeHeaderList *headers;
209 GMimeHeaderIter *iter;
210 char *combined = NULL;
212 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
216 iter = g_mime_header_iter_new ();
220 if (! g_mime_header_list_get_iter (headers, iter))
227 if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
230 /* Note that GMime retains ownership of value... */
231 value = g_mime_header_iter_get_value (iter);
233 /* ... while decoded needs to be freed with g_free(). */
234 decoded = g_mime_utils_header_decode_text (value);
244 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
256 } while (g_mime_header_iter_next (iter));
258 /* Return empty string for non-existing headers. */
260 combined = g_strdup ("");
263 g_mime_header_iter_free (iter);
269 _notmuch_message_file_get_header (notmuch_message_file_t *message,
275 if (_notmuch_message_file_parse (message))
278 /* If we have a cached decoded value, use it. */
279 value = g_hash_table_lookup (message->headers, header);
283 if (strcasecmp (header, "received") == 0) {
285 * The Received: header is special. We concatenate all
286 * instances of the header as we use this when analyzing the
287 * path the mail has taken from sender to recipient.
289 decoded = _notmuch_message_file_get_combined_header (message, header);
291 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
294 decoded = g_mime_utils_header_decode_text (value);
296 decoded = g_strdup ("");
302 /* Cache the decoded value. We also own the strings. */
303 g_hash_table_insert (message->headers, xstrdup (header), decoded);