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;
173 if (is_mbox && ! g_mime_parser_eos (parser)) {
175 * This is a multi-message mbox. (For historical reasons, we
176 * do support single-message mboxes.)
178 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
182 g_object_unref (stream);
183 g_object_unref (parser);
186 g_hash_table_destroy (message->headers);
187 message->headers = NULL;
189 if (message->message) {
190 g_object_unref (message->message);
191 message->message = NULL;
194 rewind (message->file);
201 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
202 GMimeMessage **mime_message)
204 notmuch_status_t status;
206 status = _notmuch_message_file_parse (message);
210 *mime_message = message->message;
212 return NOTMUCH_STATUS_SUCCESS;
216 * Get all instances of a header decoded and concatenated.
218 * The result must be freed using g_free().
220 * Return NULL on errors, empty string for non-existing headers.
223 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
226 GMimeHeaderList *headers;
227 GMimeHeaderIter *iter;
228 char *combined = NULL;
230 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
234 iter = g_mime_header_iter_new ();
238 if (! g_mime_header_list_get_iter (headers, iter))
245 if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
248 /* Note that GMime retains ownership of value... */
249 value = g_mime_header_iter_get_value (iter);
251 /* ... while decoded needs to be freed with g_free(). */
252 decoded = g_mime_utils_header_decode_text (value);
262 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
274 } while (g_mime_header_iter_next (iter));
276 /* Return empty string for non-existing headers. */
278 combined = g_strdup ("");
281 g_mime_header_iter_free (iter);
287 _notmuch_message_file_get_header (notmuch_message_file_t *message,
293 if (_notmuch_message_file_parse (message))
296 /* If we have a cached decoded value, use it. */
297 value = g_hash_table_lookup (message->headers, header);
301 if (strcasecmp (header, "received") == 0) {
303 * The Received: header is special. We concatenate all
304 * instances of the header as we use this when analyzing the
305 * path the mail has taken from sender to recipient.
307 decoded = _notmuch_message_file_get_combined_header (message, header);
309 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
312 decoded = g_mime_utils_header_decode_text (value);
314 decoded = g_strdup ("");
320 /* Cache the decoded value. We also own the strings. */
321 g_hash_table_insert (message->headers, xstrdup (header), decoded);