]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
67828827e61d5a065b16e527013024c41b2abea4
[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
143     if (message->message)
144         return NOTMUCH_STATUS_SUCCESS;
145
146     /* We no longer support mboxes at all. */
147     if (is_mbox (message->file))
148         return NOTMUCH_STATUS_FILE_NOT_EMAIL;
149
150     if (! initialized) {
151         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
152         initialized = 1;
153     }
154
155     message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
156                                               free, g_free);
157     if (! message->headers)
158         return NOTMUCH_STATUS_OUT_OF_MEMORY;
159
160     stream = g_mime_stream_file_new (message->file);
161
162     /* We'll own and fclose the FILE* ourselves. */
163     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
164
165     parser = g_mime_parser_new_with_stream (stream);
166     g_mime_parser_set_scan_from (parser, FALSE);
167
168     message->message = g_mime_parser_construct_message (parser);
169     if (! message->message) {
170         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
171         goto DONE;
172     }
173
174   DONE:
175     g_object_unref (stream);
176     g_object_unref (parser);
177
178     if (status) {
179         g_hash_table_destroy (message->headers);
180         message->headers = NULL;
181
182         if (message->message) {
183             g_object_unref (message->message);
184             message->message = NULL;
185         }
186
187         rewind (message->file);
188     }
189
190     return status;
191 }
192
193 notmuch_status_t
194 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
195                                         GMimeMessage **mime_message)
196 {
197     notmuch_status_t status;
198
199     status = _notmuch_message_file_parse (message);
200     if (status)
201         return status;
202
203     *mime_message = message->message;
204
205     return NOTMUCH_STATUS_SUCCESS;
206 }
207
208 /*
209  * Get all instances of a header decoded and concatenated.
210  *
211  * The result must be freed using g_free().
212  *
213  * Return NULL on errors, empty string for non-existing headers.
214  */
215 static char *
216 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
217                                            const char *header)
218 {
219     GMimeHeaderList *headers;
220     GMimeHeaderIter *iter;
221     char *combined = NULL;
222
223     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
224     if (! headers)
225         return NULL;
226
227     iter = g_mime_header_iter_new ();
228     if (! iter)
229         return NULL;
230
231     if (! g_mime_header_list_get_iter (headers, iter))
232         goto DONE;
233
234     do {
235         const char *value;
236         char *decoded;
237
238         if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
239             continue;
240
241         /* Note that GMime retains ownership of value... */
242         value = g_mime_header_iter_get_value (iter);
243
244         /* ... while decoded needs to be freed with g_free(). */
245         decoded = g_mime_utils_header_decode_text (value);
246         if (! decoded) {
247             if (combined) {
248                 g_free (combined);
249                 combined = NULL;
250             }
251             goto DONE;
252         }
253
254         if (combined) {
255             char *tmp = g_strdup_printf ("%s %s", combined, decoded);
256             g_free (decoded);
257             g_free (combined);
258             if (! tmp) {
259                 combined = NULL;
260                 goto DONE;
261             }
262
263             combined = tmp;
264         } else {
265             combined = decoded;
266         }
267     } while (g_mime_header_iter_next (iter));
268
269     /* Return empty string for non-existing headers. */
270     if (! combined)
271         combined = g_strdup ("");
272
273   DONE:
274     g_mime_header_iter_free (iter);
275
276     return combined;
277 }
278
279 const char *
280 notmuch_message_file_get_header (notmuch_message_file_t *message,
281                                  const char *header)
282 {
283     const char *value;
284     char *decoded;
285
286     if (_notmuch_message_file_parse (message))
287         return NULL;
288
289     /* If we have a cached decoded value, use it. */
290     value = g_hash_table_lookup (message->headers, header);
291     if (value)
292         return value;
293
294     if (strcasecmp (header, "received") == 0) {
295         /*
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.
299          */
300         decoded = _notmuch_message_file_get_combined_header (message, header);
301     } else {
302         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
303                                           header);
304         if (value)
305             decoded = g_mime_utils_header_decode_text (value);
306         else
307             decoded = g_strdup ("");
308     }
309
310     if (! decoded)
311         return NULL;
312
313     /* Cache the decoded value. We also own the strings. */
314     g_hash_table_insert (message->headers, xstrdup (header), decoded);
315
316     return decoded;
317 }