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