]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
emacs: Add new option notmuch-search-hide-excluded
[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     /* open stream to (possibly gzipped) file */
31     GMimeStream *stream;
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->stream)
50         g_object_unref (message->stream);
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     const char *prefix = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
68
69     if (prefix == NULL)
70         goto FAIL;
71
72     if (*filename == '/') {
73         if (strncmp (filename, prefix, strlen (prefix)) != 0) {
74             _notmuch_database_log (notmuch, "Error opening %s: path outside mail root\n",
75                                    filename);
76             errno = 0;
77             goto FAIL;
78         }
79         message->filename = talloc_strdup (message, filename);
80     } else {
81         message->filename = talloc_asprintf (message, "%s/%s", prefix, filename);
82     }
83
84     if (message->filename == NULL)
85         goto FAIL;
86
87     talloc_set_destructor (message, _notmuch_message_file_destructor);
88
89     message->stream = g_mime_stream_gzfile_open (message->filename);
90     if (message->stream == NULL)
91         goto FAIL;
92
93     return message;
94
95   FAIL:
96     if (errno)
97         _notmuch_database_log (notmuch, "Error opening %s: %s\n",
98                                filename, strerror (errno));
99     _notmuch_message_file_close (message);
100
101     return NULL;
102 }
103
104 notmuch_message_file_t *
105 _notmuch_message_file_open (notmuch_database_t *notmuch,
106                             const char *filename)
107 {
108     return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
109 }
110
111 const char *
112 _notmuch_message_file_get_filename (notmuch_message_file_t *message_file)
113 {
114     return message_file->filename;
115 }
116
117 void
118 _notmuch_message_file_close (notmuch_message_file_t *message)
119 {
120     talloc_free (message);
121 }
122
123 static bool
124 _is_mbox (GMimeStream *stream)
125 {
126     char from_buf[5];
127     bool ret = false;
128
129     /* Is this mbox? */
130     if (g_mime_stream_read (stream, from_buf, sizeof (from_buf)) == sizeof (from_buf) &&
131         strncmp (from_buf, "From ", 5) == 0)
132         ret = true;
133
134     g_mime_stream_reset (stream);
135
136     return ret;
137 }
138
139 notmuch_status_t
140 _notmuch_message_file_parse (notmuch_message_file_t *message)
141 {
142     GMimeParser *parser;
143     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
144     bool is_mbox;
145
146     if (message->message)
147         return NOTMUCH_STATUS_SUCCESS;
148
149     is_mbox = _is_mbox (message->stream);
150
151     _notmuch_init ();
152
153     message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
154                                               free, g_free);
155     if (! message->headers)
156         return NOTMUCH_STATUS_OUT_OF_MEMORY;
157
158     parser = g_mime_parser_new_with_stream (message->stream);
159     g_mime_parser_set_scan_from (parser, is_mbox);
160
161     message->message = g_mime_parser_construct_message (parser, NULL);
162     if (! message->message) {
163         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
164         goto DONE;
165     }
166
167     if (is_mbox && ! g_mime_parser_eos (parser)) {
168         /*
169          * This is a multi-message mbox. (For historical reasons, we
170          * do support single-message mboxes.)
171          */
172         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
173     }
174
175   DONE:
176     g_mime_stream_reset (message->stream);
177     g_object_unref (parser);
178
179     if (status) {
180         g_hash_table_destroy (message->headers);
181         message->headers = NULL;
182
183         if (message->message) {
184             g_object_unref (message->message);
185             message->message = NULL;
186         }
187
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
216 static char *
217 _extend_header (char *combined, const char *value)
218 {
219     char *decoded;
220
221     decoded = g_mime_utils_header_decode_text (NULL, value);
222     if (! decoded) {
223         if (combined) {
224             g_free (combined);
225             combined = NULL;
226         }
227         goto DONE;
228     }
229
230     if (combined) {
231         char *tmp = g_strdup_printf ("%s %s", combined, decoded);
232         g_free (decoded);
233         g_free (combined);
234         if (! tmp) {
235             combined = NULL;
236             goto DONE;
237         }
238
239         combined = tmp;
240     } else {
241         combined = decoded;
242     }
243   DONE:
244     return combined;
245 }
246
247 static char *
248 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
249                                            const char *header)
250 {
251     char *combined = NULL;
252     GMimeHeaderList *headers;
253
254     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
255     if (! headers)
256         return NULL;
257
258
259     for (int i = 0; i < g_mime_header_list_get_count (headers); i++) {
260         const char *value;
261         GMimeHeader *g_header = g_mime_header_list_get_header_at (headers, i);
262
263         if (strcasecmp (g_mime_header_get_name (g_header), header) != 0)
264             continue;
265
266         /* GMime retains ownership of value, we hope */
267         value = g_mime_header_get_value (g_header);
268
269         combined = _extend_header (combined, value);
270     }
271
272     /* Return empty string for non-existing headers. */
273     if (! combined)
274         combined = g_strdup ("");
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         strcasecmp (header, "delivered-to") == 0) {
296         /*
297          * The Received: header is special. We concatenate all instances of the
298          * header as we use this when analyzing the path the mail has taken
299          * from sender to recipient.
300          *
301          * Similarly, multiple instances of Delivered-To may be present. We
302          * concatenate them so the one with highest priority may be picked (eg.
303          * primary_email before other_email).
304          */
305         decoded = _notmuch_message_file_get_combined_header (message, header);
306     } else {
307         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
308                                           header);
309         if (value)
310             decoded = g_mime_utils_header_decode_text (NULL, value);
311         else
312             decoded = g_strdup ("");
313     }
314
315     if (! decoded)
316         return NULL;
317
318     /* Cache the decoded value. We also own the strings. */
319     g_hash_table_insert (message->headers, xstrdup (header), decoded);
320
321     return decoded;
322 }
323
324 notmuch_status_t
325 _notmuch_message_file_get_headers (notmuch_message_file_t *message_file,
326                                    const char **from_out,
327                                    const char **subject_out,
328                                    const char **to_out,
329                                    const char **date_out,
330                                    char **message_id_out)
331 {
332     notmuch_status_t ret;
333     const char *header;
334     const char *from, *to, *subject, *date;
335     char *message_id = NULL;
336
337     /* Parse message up front to get better error status. */
338     ret = _notmuch_message_file_parse (message_file);
339     if (ret)
340         goto DONE;
341
342     /* Before we do any real work, (especially before doing a
343      * potential SHA-1 computation on the entire file's contents),
344      * let's make sure that what we're looking at looks like an
345      * actual email message.
346      */
347     from = _notmuch_message_file_get_header (message_file, "from");
348     subject = _notmuch_message_file_get_header (message_file, "subject");
349     to = _notmuch_message_file_get_header (message_file, "to");
350     date = _notmuch_message_file_get_header (message_file, "date");
351
352     if ((from == NULL || *from == '\0') &&
353         (subject == NULL || *subject == '\0') &&
354         (to == NULL || *to == '\0')) {
355         ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
356         goto DONE;
357     }
358
359     /* Now that we're sure it's mail, the first order of business
360      * is to find a message ID (or else create one ourselves).
361      */
362     header = _notmuch_message_file_get_header (message_file, "message-id");
363     if (header && *header != '\0') {
364         message_id = _notmuch_message_id_parse (message_file, header, NULL);
365
366         /* So the header value isn't RFC-compliant, but it's
367          * better than no message-id at all.
368          */
369         if (message_id == NULL)
370             message_id = talloc_strdup (message_file, header);
371     }
372
373     if (message_id == NULL ) {
374         /* No message-id at all, let's generate one by taking a
375          * hash over the file's contents.
376          */
377         char *sha1 = _notmuch_sha1_of_file (_notmuch_message_file_get_filename (message_file));
378
379         /* If that failed too, something is really wrong. Give up. */
380         if (sha1 == NULL) {
381             ret = NOTMUCH_STATUS_FILE_ERROR;
382             goto DONE;
383         }
384
385         message_id = talloc_asprintf (message_file, "notmuch-sha1-%s", sha1);
386         free (sha1);
387     }
388   DONE:
389     if (ret == NOTMUCH_STATUS_SUCCESS) {
390         if (from_out)
391             *from_out = from;
392         if (subject_out)
393             *subject_out = subject;
394         if (to_out)
395             *to_out = to;
396         if (date_out)
397             *date_out = date;
398         if (message_id_out)
399             *message_id_out = message_id;
400     }
401     return ret;
402 }