]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
a41d9ad2a719df507d3091316d594bc38b3e4fc5
[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 && ! g_mime_parser_eos (parser)) {
174         /*
175          * This is a multi-message mbox. (For historical reasons, we
176          * do support single-message mboxes.)
177          */
178         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
179     }
180
181   DONE:
182     g_object_unref (stream);
183     g_object_unref (parser);
184
185     if (status) {
186         g_hash_table_destroy (message->headers);
187         message->headers = NULL;
188
189         if (message->message) {
190             g_object_unref (message->message);
191             message->message = NULL;
192         }
193
194         rewind (message->file);
195     }
196
197     return status;
198 }
199
200 notmuch_status_t
201 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
202                                         GMimeMessage **mime_message)
203 {
204     notmuch_status_t status;
205
206     status = _notmuch_message_file_parse (message);
207     if (status)
208         return status;
209
210     *mime_message = message->message;
211
212     return NOTMUCH_STATUS_SUCCESS;
213 }
214
215 /*
216  * Get all instances of a header decoded and concatenated.
217  *
218  * The result must be freed using g_free().
219  *
220  * Return NULL on errors, empty string for non-existing headers.
221  */
222 static char *
223 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
224                                            const char *header)
225 {
226     GMimeHeaderList *headers;
227     GMimeHeaderIter *iter;
228     char *combined = NULL;
229
230     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
231     if (! headers)
232         return NULL;
233
234     iter = g_mime_header_iter_new ();
235     if (! iter)
236         return NULL;
237
238     if (! g_mime_header_list_get_iter (headers, iter))
239         goto DONE;
240
241     do {
242         const char *value;
243         char *decoded;
244
245         if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
246             continue;
247
248         /* Note that GMime retains ownership of value... */
249         value = g_mime_header_iter_get_value (iter);
250
251         /* ... while decoded needs to be freed with g_free(). */
252         decoded = g_mime_utils_header_decode_text (value);
253         if (! decoded) {
254             if (combined) {
255                 g_free (combined);
256                 combined = NULL;
257             }
258             goto DONE;
259         }
260
261         if (combined) {
262             char *tmp = g_strdup_printf ("%s %s", combined, decoded);
263             g_free (decoded);
264             g_free (combined);
265             if (! tmp) {
266                 combined = NULL;
267                 goto DONE;
268             }
269
270             combined = tmp;
271         } else {
272             combined = decoded;
273         }
274     } while (g_mime_header_iter_next (iter));
275
276     /* Return empty string for non-existing headers. */
277     if (! combined)
278         combined = g_strdup ("");
279
280   DONE:
281     g_mime_header_iter_free (iter);
282
283     return combined;
284 }
285
286 const char *
287 _notmuch_message_file_get_header (notmuch_message_file_t *message,
288                                  const char *header)
289 {
290     const char *value;
291     char *decoded;
292
293     if (_notmuch_message_file_parse (message))
294         return NULL;
295
296     /* If we have a cached decoded value, use it. */
297     value = g_hash_table_lookup (message->headers, header);
298     if (value)
299         return value;
300
301     if (strcasecmp (header, "received") == 0) {
302         /*
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.
306          */
307         decoded = _notmuch_message_file_get_combined_header (message, header);
308     } else {
309         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
310                                           header);
311         if (value)
312             decoded = g_mime_utils_header_decode_text (value);
313         else
314             decoded = g_strdup ("");
315     }
316
317     if (! decoded)
318         return NULL;
319
320     /* Cache the decoded value. We also own the strings. */
321     g_hash_table_insert (message->headers, xstrdup (header), decoded);
322
323     return decoded;
324 }