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 strcase_equal (const void *a, const void *b)
43 return strcasecmp (a, b) == 0;
47 strcase_hash (const void *ptr)
51 /* This is the djb2 hash. */
52 unsigned int hash = 5381;
54 hash = ((hash << 5) + hash) + tolower (*s);
62 _notmuch_message_file_destructor (notmuch_message_file_t *message)
65 g_hash_table_destroy (message->headers);
68 g_object_unref (message->message);
71 fclose (message->file);
76 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
77 * the talloc owner. */
78 notmuch_message_file_t *
79 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
80 void *ctx, const char *filename)
82 notmuch_message_file_t *message;
84 message = talloc_zero (ctx, notmuch_message_file_t);
85 if (unlikely (message == NULL))
88 /* Only needed for error messages during parsing. */
89 message->filename = talloc_strdup (message, filename);
90 if (message->filename == NULL)
93 talloc_set_destructor (message, _notmuch_message_file_destructor);
95 message->file = fopen (filename, "r");
96 if (message->file == NULL)
102 _notmuch_database_log (notmuch, "Error opening %s: %s\n",
103 filename, strerror (errno));
104 _notmuch_message_file_close (message);
109 notmuch_message_file_t *
110 _notmuch_message_file_open (notmuch_database_t *notmuch,
111 const char *filename)
113 return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
117 _notmuch_message_file_close (notmuch_message_file_t *message)
119 talloc_free (message);
122 static notmuch_bool_t
123 _is_mbox (FILE *file)
126 notmuch_bool_t ret = FALSE;
129 if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
130 strncmp (from_buf, "From ", 5) == 0)
139 _notmuch_message_file_parse (notmuch_message_file_t *message)
143 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
144 static int initialized = 0;
145 notmuch_bool_t is_mbox;
147 if (message->message)
148 return NOTMUCH_STATUS_SUCCESS;
150 is_mbox = _is_mbox (message->file);
153 g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
157 message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
159 if (! message->headers)
160 return NOTMUCH_STATUS_OUT_OF_MEMORY;
162 stream = g_mime_stream_file_new (message->file);
164 /* We'll own and fclose the FILE* ourselves. */
165 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
167 parser = g_mime_parser_new_with_stream (stream);
168 g_mime_parser_set_scan_from (parser, is_mbox);
170 message->message = g_mime_parser_construct_message (parser);
171 if (! message->message) {
172 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
176 if (is_mbox && ! g_mime_parser_eos (parser)) {
178 * This is a multi-message mbox. (For historical reasons, we
179 * do support single-message mboxes.)
181 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
185 g_object_unref (stream);
186 g_object_unref (parser);
189 g_hash_table_destroy (message->headers);
190 message->headers = NULL;
192 if (message->message) {
193 g_object_unref (message->message);
194 message->message = NULL;
197 rewind (message->file);
204 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
205 GMimeMessage **mime_message)
207 notmuch_status_t status;
209 status = _notmuch_message_file_parse (message);
213 *mime_message = message->message;
215 return NOTMUCH_STATUS_SUCCESS;
219 * Get all instances of a header decoded and concatenated.
221 * The result must be freed using g_free().
223 * Return NULL on errors, empty string for non-existing headers.
226 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
229 GMimeHeaderList *headers;
230 GMimeHeaderIter *iter;
231 char *combined = NULL;
233 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
237 iter = g_mime_header_iter_new ();
241 if (! g_mime_header_list_get_iter (headers, iter))
248 if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
251 /* Note that GMime retains ownership of value... */
252 value = g_mime_header_iter_get_value (iter);
254 /* ... while decoded needs to be freed with g_free(). */
255 decoded = g_mime_utils_header_decode_text (value);
265 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
277 } while (g_mime_header_iter_next (iter));
279 /* Return empty string for non-existing headers. */
281 combined = g_strdup ("");
284 g_mime_header_iter_free (iter);
290 _notmuch_message_file_get_header (notmuch_message_file_t *message,
296 if (_notmuch_message_file_parse (message))
299 /* If we have a cached decoded value, use it. */
300 value = g_hash_table_lookup (message->headers, header);
304 if (strcasecmp (header, "received") == 0) {
306 * The Received: header is special. We concatenate all
307 * instances of the header as we use this when analyzing the
308 * path the mail has taken from sender to recipient.
310 decoded = _notmuch_message_file_get_combined_header (message, header);
312 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
315 decoded = g_mime_utils_header_decode_text (value);
317 decoded = g_strdup ("");
323 /* Cache the decoded value. We also own the strings. */
324 g_hash_table_insert (message->headers, xstrdup (header), decoded);