]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
Merge branch 'release'
[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 https://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 _notmuch_message_file_destructor (notmuch_message_file_t *message)
42 {
43     if (message->headers)
44         g_hash_table_destroy (message->headers);
45
46     if (message->message)
47         g_object_unref (message->message);
48
49     if (message->file)
50         fclose (message->file);
51
52     return 0;
53 }
54
55 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
56  * the talloc owner. */
57 notmuch_message_file_t *
58 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
59                                 void *ctx, const char *filename)
60 {
61     notmuch_message_file_t *message;
62
63     message = talloc_zero (ctx, notmuch_message_file_t);
64     if (unlikely (message == NULL))
65         return NULL;
66
67     /* Only needed for error messages during parsing. */
68     message->filename = talloc_strdup (message, filename);
69     if (message->filename == NULL)
70         goto FAIL;
71
72     talloc_set_destructor (message, _notmuch_message_file_destructor);
73
74     message->file = fopen (filename, "r");
75     if (message->file == NULL)
76         goto FAIL;
77
78     return message;
79
80   FAIL:
81     _notmuch_database_log (notmuch, "Error opening %s: %s\n",
82                           filename, strerror (errno));
83     _notmuch_message_file_close (message);
84
85     return NULL;
86 }
87
88 notmuch_message_file_t *
89 _notmuch_message_file_open (notmuch_database_t *notmuch,
90                             const char *filename)
91 {
92     return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
93 }
94
95 const char *
96 _notmuch_message_file_get_filename (notmuch_message_file_t *message_file)
97 {
98     return message_file->filename;
99 }
100
101 void
102 _notmuch_message_file_close (notmuch_message_file_t *message)
103 {
104     talloc_free (message);
105 }
106
107 static bool
108 _is_mbox (FILE *file)
109 {
110     char from_buf[5];
111     bool ret = false;
112
113     /* Is this mbox? */
114     if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
115         strncmp (from_buf, "From ", 5) == 0)
116         ret = true;
117
118     rewind (file);
119
120     return ret;
121 }
122
123 notmuch_status_t
124 _notmuch_message_file_parse (notmuch_message_file_t *message)
125 {
126     GMimeStream *stream;
127     GMimeParser *parser;
128     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
129     static int initialized = 0;
130     bool is_mbox;
131
132     if (message->message)
133         return NOTMUCH_STATUS_SUCCESS;
134
135     is_mbox = _is_mbox (message->file);
136
137     if (! initialized) {
138         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
139         initialized = 1;
140     }
141
142     message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
143                                               free, g_free);
144     if (! message->headers)
145         return NOTMUCH_STATUS_OUT_OF_MEMORY;
146
147     stream = g_mime_stream_file_new (message->file);
148
149     /* We'll own and fclose the FILE* ourselves. */
150     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), false);
151
152     parser = g_mime_parser_new_with_stream (stream);
153     g_mime_parser_set_scan_from (parser, is_mbox);
154
155     message->message = g_mime_parser_construct_message (parser);
156     if (! message->message) {
157         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
158         goto DONE;
159     }
160
161     if (is_mbox && ! g_mime_parser_eos (parser)) {
162         /*
163          * This is a multi-message mbox. (For historical reasons, we
164          * do support single-message mboxes.)
165          */
166         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
167     }
168
169   DONE:
170     g_object_unref (stream);
171     g_object_unref (parser);
172
173     if (status) {
174         g_hash_table_destroy (message->headers);
175         message->headers = NULL;
176
177         if (message->message) {
178             g_object_unref (message->message);
179             message->message = NULL;
180         }
181
182         rewind (message->file);
183     }
184
185     return status;
186 }
187
188 notmuch_status_t
189 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
190                                         GMimeMessage **mime_message)
191 {
192     notmuch_status_t status;
193
194     status = _notmuch_message_file_parse (message);
195     if (status)
196         return status;
197
198     *mime_message = message->message;
199
200     return NOTMUCH_STATUS_SUCCESS;
201 }
202
203 /*
204  * Get all instances of a header decoded and concatenated.
205  *
206  * The result must be freed using g_free().
207  *
208  * Return NULL on errors, empty string for non-existing headers.
209  */
210
211 static char *
212 _extend_header (char *combined, const char *value) {
213     char *decoded;
214
215     decoded = g_mime_utils_header_decode_text (value);
216     if (! decoded) {
217         if (combined) {
218             g_free (combined);
219             combined = NULL;
220         }
221         goto DONE;
222     }
223
224     if (combined) {
225         char *tmp = g_strdup_printf ("%s %s", combined, decoded);
226         g_free (decoded);
227         g_free (combined);
228         if (! tmp) {
229             combined = NULL;
230             goto DONE;
231         }
232
233         combined = tmp;
234     } else {
235         combined = decoded;
236     }
237  DONE:
238     return combined;
239 }
240
241 #if (GMIME_MAJOR_VERSION < 3)
242 static char *
243 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
244                                            const char *header)
245 {
246     GMimeHeaderList *headers;
247     GMimeHeaderIter *iter;
248     char *combined = NULL;
249
250     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
251     if (! headers)
252         return NULL;
253
254     iter = g_mime_header_iter_new ();
255     if (! iter)
256         return NULL;
257
258     if (! g_mime_header_list_get_iter (headers, iter))
259         goto DONE;
260
261     do {
262         const char *value;
263         if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
264             continue;
265
266         /* Note that GMime retains ownership of value... */
267         value = g_mime_header_iter_get_value (iter);
268
269         combined = _extend_header (combined, value);
270     } while (g_mime_header_iter_next (iter));
271
272     /* Return empty string for non-existing headers. */
273     if (! combined)
274         combined = g_strdup ("");
275
276   DONE:
277     g_mime_header_iter_free (iter);
278
279     return combined;
280 }
281 #else
282 static char *
283 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
284                                            const char *header)
285 {
286     char *combined = NULL;
287     GMimeHeaderList *headers;
288
289     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
290     if (! headers)
291         return NULL;
292
293
294     for (int i=0; i < g_mime_header_list_get_count (headers); i++) {
295         const char *value;
296         GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);
297
298         if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
299             continue;
300
301         /* GMime retains ownership of value, we hope */
302         value = g_mime_header_get_value (g_header);
303
304         combined = _extend_header (combined, value);
305     }
306
307     /* Return empty string for non-existing headers. */
308     if (! combined)
309         combined = g_strdup ("");
310
311     return combined;
312 }
313 #endif
314
315 const char *
316 _notmuch_message_file_get_header (notmuch_message_file_t *message,
317                                  const char *header)
318 {
319     const char *value;
320     char *decoded;
321
322     if (_notmuch_message_file_parse (message))
323         return NULL;
324
325     /* If we have a cached decoded value, use it. */
326     value = g_hash_table_lookup (message->headers, header);
327     if (value)
328         return value;
329
330     if (strcasecmp (header, "received") == 0) {
331         /*
332          * The Received: header is special. We concatenate all
333          * instances of the header as we use this when analyzing the
334          * path the mail has taken from sender to recipient.
335          */
336         decoded = _notmuch_message_file_get_combined_header (message, header);
337     } else {
338         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
339                                           header);
340         if (value)
341             decoded = g_mime_utils_header_decode_text (value);
342         else
343             decoded = g_strdup ("");
344     }
345
346     if (! decoded)
347         return NULL;
348
349     /* Cache the decoded value. We also own the strings. */
350     g_hash_table_insert (message->headers, xstrdup (header), decoded);
351
352     return decoded;
353 }
354
355 notmuch_status_t
356 _notmuch_message_file_get_headers (notmuch_message_file_t *message_file,
357                                    const char **from_out,
358                                    const char **subject_out,
359                                    const char **to_out,
360                                    const char **date_out,
361                                    char **message_id_out)
362 {
363     notmuch_status_t ret;
364     const char *header;
365     const char *from, *to, *subject, *date;
366     char *message_id = NULL;
367
368     /* Parse message up front to get better error status. */
369     ret = _notmuch_message_file_parse (message_file);
370     if (ret)
371         goto DONE;
372
373     /* Before we do any real work, (especially before doing a
374      * potential SHA-1 computation on the entire file's contents),
375      * let's make sure that what we're looking at looks like an
376      * actual email message.
377      */
378     from = _notmuch_message_file_get_header (message_file, "from");
379     subject = _notmuch_message_file_get_header (message_file, "subject");
380     to = _notmuch_message_file_get_header (message_file, "to");
381     date = _notmuch_message_file_get_header (message_file, "date");
382
383     if ((from == NULL || *from == '\0') &&
384         (subject == NULL || *subject == '\0') &&
385         (to == NULL || *to == '\0')) {
386         ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
387         goto DONE;
388     }
389
390     /* Now that we're sure it's mail, the first order of business
391      * is to find a message ID (or else create one ourselves).
392      */
393     header = _notmuch_message_file_get_header (message_file, "message-id");
394     if (header && *header != '\0') {
395         message_id = _notmuch_message_id_parse (message_file, header, NULL);
396
397         /* So the header value isn't RFC-compliant, but it's
398          * better than no message-id at all.
399          */
400         if (message_id == NULL)
401             message_id = talloc_strdup (message_file, header);
402     }
403
404     if (message_id == NULL ) {
405         /* No message-id at all, let's generate one by taking a
406          * hash over the file's contents.
407          */
408         char *sha1 = _notmuch_sha1_of_file (_notmuch_message_file_get_filename (message_file));
409
410         /* If that failed too, something is really wrong. Give up. */
411         if (sha1 == NULL) {
412             ret = NOTMUCH_STATUS_FILE_ERROR;
413             goto DONE;
414         }
415
416         message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1);
417         free (sha1);
418     }
419  DONE:
420     if (ret == NOTMUCH_STATUS_SUCCESS) {
421         if (from_out)
422             *from_out = from;
423         if (subject_out)
424             *subject_out = subject;
425         if (to_out)
426             *to_out = to;
427         if (date_out)
428             *date_out = date;
429         if (message_id_out)
430             *message_id_out = message_id;
431     }
432     return ret;
433 }