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