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
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;
143 if (message->message)
144 return NOTMUCH_STATUS_SUCCESS;
146 /* We no longer support mboxes at all. */
147 if (is_mbox (message->file))
148 return NOTMUCH_STATUS_FILE_NOT_EMAIL;
151 g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
155 message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
157 if (! message->headers)
158 return NOTMUCH_STATUS_OUT_OF_MEMORY;
160 stream = g_mime_stream_file_new (message->file);
162 /* We'll own and fclose the FILE* ourselves. */
163 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
165 parser = g_mime_parser_new_with_stream (stream);
166 g_mime_parser_set_scan_from (parser, FALSE);
168 message->message = g_mime_parser_construct_message (parser);
169 if (! message->message) {
170 status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
175 g_object_unref (stream);
176 g_object_unref (parser);
179 g_hash_table_destroy (message->headers);
180 message->headers = NULL;
182 if (message->message) {
183 g_object_unref (message->message);
184 message->message = NULL;
187 rewind (message->file);
194 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
195 GMimeMessage **mime_message)
197 notmuch_status_t status;
199 status = _notmuch_message_file_parse (message);
203 *mime_message = message->message;
205 return NOTMUCH_STATUS_SUCCESS;
209 * Get all instances of a header decoded and concatenated.
211 * The result must be freed using g_free().
213 * Return NULL on errors, empty string for non-existing headers.
216 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
219 GMimeHeaderList *headers;
220 GMimeHeaderIter *iter;
221 char *combined = NULL;
223 headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
227 iter = g_mime_header_iter_new ();
231 if (! g_mime_header_list_get_iter (headers, iter))
238 if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
241 /* Note that GMime retains ownership of value... */
242 value = g_mime_header_iter_get_value (iter);
244 /* ... while decoded needs to be freed with g_free(). */
245 decoded = g_mime_utils_header_decode_text (value);
255 char *tmp = g_strdup_printf ("%s %s", combined, decoded);
267 } while (g_mime_header_iter_next (iter));
269 /* Return empty string for non-existing headers. */
271 combined = g_strdup ("");
274 g_mime_header_iter_free (iter);
280 notmuch_message_file_get_header (notmuch_message_file_t *message,
286 if (_notmuch_message_file_parse (message))
289 /* If we have a cached decoded value, use it. */
290 value = g_hash_table_lookup (message->headers, header);
294 if (strcasecmp (header, "received") == 0) {
296 * The Received: header is special. We concatenate all
297 * instances of the header as we use this when analyzing the
298 * path the mail has taken from sender to recipient.
300 decoded = _notmuch_message_file_get_combined_header (message, header);
302 value = g_mime_object_get_header (GMIME_OBJECT (message->message),
305 decoded = g_mime_utils_header_decode_text (value);
307 decoded = g_strdup ("");
313 /* Cache the decoded value. We also own the strings. */
314 g_hash_table_insert (message->headers, xstrdup (header), decoded);