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 (void *ctx, const char *filename)
81 notmuch_message_file_t *message;
83 message = talloc_zero (ctx, notmuch_message_file_t);
84 if (unlikely (message == NULL))
87 /* Only needed for error messages during parsing. */
88 message->filename = talloc_strdup (message, filename);
89 if (message->filename == NULL)
92 talloc_set_destructor (message, _notmuch_message_file_destructor);
94 message->file = fopen (filename, "r");
95 if (message->file == NULL)
101 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
102 _notmuch_message_file_close (message);
107 notmuch_message_file_t *
108 _notmuch_message_file_open (const char *filename)
110 return _notmuch_message_file_open_ctx (NULL, filename);
114 _notmuch_message_file_close (notmuch_message_file_t *message)
116 talloc_free (message);
119 static notmuch_bool_t
120 _is_mbox (FILE *file)
123 notmuch_bool_t ret = FALSE;
126 if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
127 strncmp (from_buf, "From ", 5) == 0)
136 _notmuch_message_file_parse (notmuch_message_file_t *message)
140 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
141 static int initialized = 0;
142 notmuch_bool_t is_mbox;
144 if (message->message)
145 return NOTMUCH_STATUS_SUCCESS;
147 is_mbox = _is_mbox (message->file);
150 g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
154 message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
156 if (! message->headers)
157 return NOTMUCH_STATUS_OUT_OF_MEMORY;
159 stream = g_mime_stream_file_new (message->file);
161 /* We'll own and fclose the FILE* ourselves. */
162 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
164 parser = g_mime_parser_new_with_stream (stream);
165 g_mime_parser_set_scan_from (parser, is_mbox);
167 message->message = g_mime_parser_construct_message (parser);
168 if (! message->message) {
169 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
174 if (! g_mime_parser_eos (parser)) {
175 /* This is a multi-message mbox. */
176 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
180 * For historical reasons, we support single-message mboxes,
181 * but this behavior is likely to change in the future, so
184 static notmuch_bool_t mbox_warning = FALSE;
185 if (! mbox_warning) {
188 Warning: %s is an mbox containing a single message,\n\
189 likely caused by misconfigured mail delivery. Support for single-message\n\
190 mboxes is deprecated and may be removed in the future.\n", message->filename);
195 g_object_unref (stream);
196 g_object_unref (parser);
199 g_hash_table_destroy (message->headers);
200 message->headers = NULL;
202 if (message->message) {
203 g_object_unref (message->message);
204 message->message = NULL;
207 rewind (message->file);
214 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
215 GMimeMessage **mime_message)
217 notmuch_status_t status;
219 status = _notmuch_message_file_parse (message);
223 *mime_message = message->message;
225 return NOTMUCH_STATUS_SUCCESS;
229 * Get all instances of a header decoded and concatenated.
231 * The result must be freed using g_free().
233 * Return NULL on errors, empty string for non-existing headers.
236 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
239 GMimeHeaderList *headers;
240 GMimeHeaderIter *iter;
241 char *combined = NULL;
243 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
247 iter = g_mime_header_iter_new ();
251 if (! g_mime_header_list_get_iter (headers, iter))
258 if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
261 /* Note that GMime retains ownership of value... */
262 value = g_mime_header_iter_get_value (iter);
264 /* ... while decoded needs to be freed with g_free(). */
265 decoded = g_mime_utils_header_decode_text (value);
275 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
287 } while (g_mime_header_iter_next (iter));
289 /* Return empty string for non-existing headers. */
291 combined = g_strdup ("");
294 g_mime_header_iter_free (iter);
300 _notmuch_message_file_get_header (notmuch_message_file_t *message,
306 if (_notmuch_message_file_parse (message))
309 /* If we have a cached decoded value, use it. */
310 value = g_hash_table_lookup (message->headers, header);
314 if (strcasecmp (header, "received") == 0) {
316 * The Received: header is special. We concatenate all
317 * instances of the header as we use this when analyzing the
318 * path the mail has taken from sender to recipient.
320 decoded = _notmuch_message_file_get_combined_header (message, header);
322 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
325 decoded = g_mime_utils_header_decode_text (value);
327 decoded = g_strdup ("");
333 /* Cache the decoded value. We also own the strings. */
334 g_hash_table_insert (message->headers, xstrdup (header), decoded);