]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
Merge tag '0.18.2_rc1'
[notmuch] / lib / message-file.c
1 /* message.c - Utility functions for parsing an email message for notmuch.
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include <stdarg.h>
22
23 #include "notmuch-private.h"
24
25 #include <gmime/gmime.h>
26
27 #include <glib.h> /* GHashTable */
28
29 struct _notmuch_message_file {
30     /* File object */
31     FILE *file;
32     char *filename;
33
34     /* Cache for decoded headers */
35     GHashTable *headers;
36
37     GMimeMessage *message;
38 };
39
40 static int
41 strcase_equal (const void *a, const void *b)
42 {
43     return strcasecmp (a, b) == 0;
44 }
45
46 static unsigned int
47 strcase_hash (const void *ptr)
48 {
49     const char *s = ptr;
50
51     /* This is the djb2 hash. */
52     unsigned int hash = 5381;
53     while (s && *s) {
54         hash = ((hash << 5) + hash) + tolower (*s);
55         s++;
56     }
57
58     return hash;
59 }
60
61 static int
62 _notmuch_message_file_destructor (notmuch_message_file_t *message)
63 {
64     if (message->headers)
65         g_hash_table_destroy (message->headers);
66
67     if (message->message)
68         g_object_unref (message->message);
69
70     if (message->file)
71         fclose (message->file);
72
73     return 0;
74 }
75
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)
80 {
81     notmuch_message_file_t *message;
82
83     message = talloc_zero (ctx, notmuch_message_file_t);
84     if (unlikely (message == NULL))
85         return NULL;
86
87     /* Only needed for error messages during parsing. */
88     message->filename = talloc_strdup (message, filename);
89     if (message->filename == NULL)
90         goto FAIL;
91
92     talloc_set_destructor (message, _notmuch_message_file_destructor);
93
94     message->file = fopen (filename, "r");
95     if (message->file == NULL)
96         goto FAIL;
97
98     return message;
99
100   FAIL:
101     fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
102     _notmuch_message_file_close (message);
103
104     return NULL;
105 }
106
107 notmuch_message_file_t *
108 _notmuch_message_file_open (const char *filename)
109 {
110     return _notmuch_message_file_open_ctx (NULL, filename);
111 }
112
113 void
114 _notmuch_message_file_close (notmuch_message_file_t *message)
115 {
116     talloc_free (message);
117 }
118
119 static notmuch_bool_t
120 _is_mbox (FILE *file)
121 {
122     char from_buf[5];
123     notmuch_bool_t ret = FALSE;
124
125     /* Is this mbox? */
126     if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
127         strncmp (from_buf, "From ", 5) == 0)
128         ret = TRUE;
129
130     rewind (file);
131
132     return ret;
133 }
134
135 notmuch_status_t
136 _notmuch_message_file_parse (notmuch_message_file_t *message)
137 {
138     GMimeStream *stream;
139     GMimeParser *parser;
140     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
141     static int initialized = 0;
142     notmuch_bool_t is_mbox;
143
144     if (message->message)
145         return NOTMUCH_STATUS_SUCCESS;
146
147     is_mbox = _is_mbox (message->file);
148
149     if (! initialized) {
150         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
151         initialized = 1;
152     }
153
154     message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
155                                               free, g_free);
156     if (! message->headers)
157         return NOTMUCH_STATUS_OUT_OF_MEMORY;
158
159     stream = g_mime_stream_file_new (message->file);
160
161     /* We'll own and fclose the FILE* ourselves. */
162     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
163
164     parser = g_mime_parser_new_with_stream (stream);
165     g_mime_parser_set_scan_from (parser, is_mbox);
166
167     message->message = g_mime_parser_construct_message (parser);
168     if (! message->message) {
169         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
170         goto DONE;
171     }
172
173     if (is_mbox) {
174         if (! g_mime_parser_eos (parser)) {
175             /* This is a multi-message mbox. */
176             status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
177             goto DONE;
178         }
179         /*
180          * For historical reasons, we support single-message mboxes,
181          * but this behavior is likely to change in the future, so
182          * warn.
183          */
184         static notmuch_bool_t mbox_warning = FALSE;
185         if (! mbox_warning) {
186             mbox_warning = TRUE;
187             fprintf (stderr, "\
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);
191         }
192     }
193
194   DONE:
195     g_object_unref (stream);
196     g_object_unref (parser);
197
198     if (status) {
199         g_hash_table_destroy (message->headers);
200         message->headers = NULL;
201
202         if (message->message) {
203             g_object_unref (message->message);
204             message->message = NULL;
205         }
206
207         rewind (message->file);
208     }
209
210     return status;
211 }
212
213 notmuch_status_t
214 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
215                                         GMimeMessage **mime_message)
216 {
217     notmuch_status_t status;
218
219     status = _notmuch_message_file_parse (message);
220     if (status)
221         return status;
222
223     *mime_message = message->message;
224
225     return NOTMUCH_STATUS_SUCCESS;
226 }
227
228 /*
229  * Get all instances of a header decoded and concatenated.
230  *
231  * The result must be freed using g_free().
232  *
233  * Return NULL on errors, empty string for non-existing headers.
234  */
235 static char *
236 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
237                                            const char *header)
238 {
239     GMimeHeaderList *headers;
240     GMimeHeaderIter *iter;
241     char *combined = NULL;
242
243     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
244     if (! headers)
245         return NULL;
246
247     iter = g_mime_header_iter_new ();
248     if (! iter)
249         return NULL;
250
251     if (! g_mime_header_list_get_iter (headers, iter))
252         goto DONE;
253
254     do {
255         const char *value;
256         char *decoded;
257
258         if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
259             continue;
260
261         /* Note that GMime retains ownership of value... */
262         value = g_mime_header_iter_get_value (iter);
263
264         /* ... while decoded needs to be freed with g_free(). */
265         decoded = g_mime_utils_header_decode_text (value);
266         if (! decoded) {
267             if (combined) {
268                 g_free (combined);
269                 combined = NULL;
270             }
271             goto DONE;
272         }
273
274         if (combined) {
275             char *tmp = g_strdup_printf ("%s %s", combined, decoded);
276             g_free (decoded);
277             g_free (combined);
278             if (! tmp) {
279                 combined = NULL;
280                 goto DONE;
281             }
282
283             combined = tmp;
284         } else {
285             combined = decoded;
286         }
287     } while (g_mime_header_iter_next (iter));
288
289     /* Return empty string for non-existing headers. */
290     if (! combined)
291         combined = g_strdup ("");
292
293   DONE:
294     g_mime_header_iter_free (iter);
295
296     return combined;
297 }
298
299 const char *
300 _notmuch_message_file_get_header (notmuch_message_file_t *message,
301                                  const char *header)
302 {
303     const char *value;
304     char *decoded;
305
306     if (_notmuch_message_file_parse (message))
307         return NULL;
308
309     /* If we have a cached decoded value, use it. */
310     value = g_hash_table_lookup (message->headers, header);
311     if (value)
312         return value;
313
314     if (strcasecmp (header, "received") == 0) {
315         /*
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.
319          */
320         decoded = _notmuch_message_file_get_combined_header (message, header);
321     } else {
322         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
323                                           header);
324         if (value)
325             decoded = g_mime_utils_header_decode_text (value);
326         else
327             decoded = g_strdup ("");
328     }
329
330     if (! decoded)
331         return NULL;
332
333     /* Cache the decoded value. We also own the strings. */
334     g_hash_table_insert (message->headers, xstrdup (header), decoded);
335
336     return decoded;
337 }