]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
ac5545b3f1dd761d49ec67122122c75e39f9ae71
[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 static char *
242 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
243                                            const char *header)
244 {
245     char *combined = NULL;
246     GMimeHeaderList *headers;
247
248     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
249     if (! headers)
250         return NULL;
251
252
253     for (int i=0; i < g_mime_header_list_get_count (headers); i++) {
254         const char *value;
255         GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);
256
257         if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
258             continue;
259
260         /* GMime retains ownership of value, we hope */
261         value = g_mime_header_get_value (g_header);
262
263         combined = _extend_header (combined, value);
264     }
265
266     /* Return empty string for non-existing headers. */
267     if (! combined)
268         combined = g_strdup ("");
269
270     return combined;
271 }
272
273 const char *
274 _notmuch_message_file_get_header (notmuch_message_file_t *message,
275                                  const char *header)
276 {
277     const char *value;
278     char *decoded;
279
280     if (_notmuch_message_file_parse (message))
281         return NULL;
282
283     /* If we have a cached decoded value, use it. */
284     value = g_hash_table_lookup (message->headers, header);
285     if (value)
286         return value;
287
288     if (strcasecmp (header, "received") == 0) {
289         /*
290          * The Received: header is special. We concatenate all
291          * instances of the header as we use this when analyzing the
292          * path the mail has taken from sender to recipient.
293          */
294         decoded = _notmuch_message_file_get_combined_header (message, header);
295     } else {
296         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
297                                           header);
298         if (value)
299             decoded = g_mime_utils_header_decode_text (value);
300         else
301             decoded = g_strdup ("");
302     }
303
304     if (! decoded)
305         return NULL;
306
307     /* Cache the decoded value. We also own the strings. */
308     g_hash_table_insert (message->headers, xstrdup (header), decoded);
309
310     return decoded;
311 }
312
313 notmuch_status_t
314 _notmuch_message_file_get_headers (notmuch_message_file_t *message_file,
315                                    const char **from_out,
316                                    const char **subject_out,
317                                    const char **to_out,
318                                    const char **date_out,
319                                    char **message_id_out)
320 {
321     notmuch_status_t ret;
322     const char *header;
323     const char *from, *to, *subject, *date;
324     char *message_id = NULL;
325
326     /* Parse message up front to get better error status. */
327     ret = _notmuch_message_file_parse (message_file);
328     if (ret)
329         goto DONE;
330
331     /* Before we do any real work, (especially before doing a
332      * potential SHA-1 computation on the entire file's contents),
333      * let's make sure that what we're looking at looks like an
334      * actual email message.
335      */
336     from = _notmuch_message_file_get_header (message_file, "from");
337     subject = _notmuch_message_file_get_header (message_file, "subject");
338     to = _notmuch_message_file_get_header (message_file, "to");
339     date = _notmuch_message_file_get_header (message_file, "date");
340
341     if ((from == NULL || *from == '\0') &&
342         (subject == NULL || *subject == '\0') &&
343         (to == NULL || *to == '\0')) {
344         ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
345         goto DONE;
346     }
347
348     /* Now that we're sure it's mail, the first order of business
349      * is to find a message ID (or else create one ourselves).
350      */
351     header = _notmuch_message_file_get_header (message_file, "message-id");
352     if (header && *header != '\0') {
353         message_id = _notmuch_message_id_parse (message_file, header, NULL);
354
355         /* So the header value isn't RFC-compliant, but it's
356          * better than no message-id at all.
357          */
358         if (message_id == NULL)
359             message_id = talloc_strdup (message_file, header);
360     }
361
362     if (message_id == NULL ) {
363         /* No message-id at all, let's generate one by taking a
364          * hash over the file's contents.
365          */
366         char *sha1 = _notmuch_sha1_of_file (_notmuch_message_file_get_filename (message_file));
367
368         /* If that failed too, something is really wrong. Give up. */
369         if (sha1 == NULL) {
370             ret = NOTMUCH_STATUS_FILE_ERROR;
371             goto DONE;
372         }
373
374         message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1);
375         free (sha1);
376     }
377  DONE:
378     if (ret == NOTMUCH_STATUS_SUCCESS) {
379         if (from_out)
380             *from_out = from;
381         if (subject_out)
382             *subject_out = subject;
383         if (to_out)
384             *to_out = to;
385         if (date_out)
386             *date_out = date;
387         if (message_id_out)
388             *message_id_out = message_id;
389     }
390     return ret;
391 }