]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
lib: eliminate fprintf from _notmuch_message_file_open
[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 http://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 strcase_equal (const void *a, const void *b)
42 {
43     return strcasecmp (a, b) == 0;
44 }
45
46 static unsigned int
47 strcase_hash (const void *ptr)
48 {
49     const char *s = ptr;
50
51     /* This is the djb2 hash. */
52     unsigned int hash = 5381;
53     while (s && *s) {
54         hash = ((hash << 5) + hash) + tolower (*s);
55         s++;
56     }
57
58     return hash;
59 }
60
61 static int
62 _notmuch_message_file_destructor (notmuch_message_file_t *message)
63 {
64     if (message->headers)
65         g_hash_table_destroy (message->headers);
66
67     if (message->message)
68         g_object_unref (message->message);
69
70     if (message->file)
71         fclose (message->file);
72
73     return 0;
74 }
75
76 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
77  * the talloc owner. */
78 notmuch_message_file_t *
79 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
80                                 void *ctx, const char *filename)
81 {
82     notmuch_message_file_t *message;
83
84     message = talloc_zero (ctx, notmuch_message_file_t);
85     if (unlikely (message == NULL))
86         return NULL;
87
88     /* Only needed for error messages during parsing. */
89     message->filename = talloc_strdup (message, filename);
90     if (message->filename == NULL)
91         goto FAIL;
92
93     talloc_set_destructor (message, _notmuch_message_file_destructor);
94
95     message->file = fopen (filename, "r");
96     if (message->file == NULL)
97         goto FAIL;
98
99     return message;
100
101   FAIL:
102     _notmuch_database_log (notmuch, "Error opening %s: %s\n",
103                           filename, strerror (errno));
104     _notmuch_message_file_close (message);
105
106     return NULL;
107 }
108
109 notmuch_message_file_t *
110 _notmuch_message_file_open (notmuch_database_t *notmuch,
111                             const char *filename)
112 {
113     return _notmuch_message_file_open_ctx (notmuch, NULL, filename);
114 }
115
116 void
117 _notmuch_message_file_close (notmuch_message_file_t *message)
118 {
119     talloc_free (message);
120 }
121
122 static notmuch_bool_t
123 _is_mbox (FILE *file)
124 {
125     char from_buf[5];
126     notmuch_bool_t ret = FALSE;
127
128     /* Is this mbox? */
129     if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&
130         strncmp (from_buf, "From ", 5) == 0)
131         ret = TRUE;
132
133     rewind (file);
134
135     return ret;
136 }
137
138 notmuch_status_t
139 _notmuch_message_file_parse (notmuch_message_file_t *message)
140 {
141     GMimeStream *stream;
142     GMimeParser *parser;
143     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
144     static int initialized = 0;
145     notmuch_bool_t is_mbox;
146
147     if (message->message)
148         return NOTMUCH_STATUS_SUCCESS;
149
150     is_mbox = _is_mbox (message->file);
151
152     if (! initialized) {
153         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
154         initialized = 1;
155     }
156
157     message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,
158                                               free, g_free);
159     if (! message->headers)
160         return NOTMUCH_STATUS_OUT_OF_MEMORY;
161
162     stream = g_mime_stream_file_new (message->file);
163
164     /* We'll own and fclose the FILE* ourselves. */
165     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
166
167     parser = g_mime_parser_new_with_stream (stream);
168     g_mime_parser_set_scan_from (parser, is_mbox);
169
170     message->message = g_mime_parser_construct_message (parser);
171     if (! message->message) {
172         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
173         goto DONE;
174     }
175
176     if (is_mbox && ! g_mime_parser_eos (parser)) {
177         /*
178          * This is a multi-message mbox. (For historical reasons, we
179          * do support single-message mboxes.)
180          */
181         status = NOTMUCH_STATUS_FILE_NOT_EMAIL;
182     }
183
184   DONE:
185     g_object_unref (stream);
186     g_object_unref (parser);
187
188     if (status) {
189         g_hash_table_destroy (message->headers);
190         message->headers = NULL;
191
192         if (message->message) {
193             g_object_unref (message->message);
194             message->message = NULL;
195         }
196
197         rewind (message->file);
198     }
199
200     return status;
201 }
202
203 notmuch_status_t
204 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
205                                         GMimeMessage **mime_message)
206 {
207     notmuch_status_t status;
208
209     status = _notmuch_message_file_parse (message);
210     if (status)
211         return status;
212
213     *mime_message = message->message;
214
215     return NOTMUCH_STATUS_SUCCESS;
216 }
217
218 /*
219  * Get all instances of a header decoded and concatenated.
220  *
221  * The result must be freed using g_free().
222  *
223  * Return NULL on errors, empty string for non-existing headers.
224  */
225 static char *
226 _notmuch_message_file_get_combined_header (notmuch_message_file_t *message,
227                                            const char *header)
228 {
229     GMimeHeaderList *headers;
230     GMimeHeaderIter *iter;
231     char *combined = NULL;
232
233     headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));
234     if (! headers)
235         return NULL;
236
237     iter = g_mime_header_iter_new ();
238     if (! iter)
239         return NULL;
240
241     if (! g_mime_header_list_get_iter (headers, iter))
242         goto DONE;
243
244     do {
245         const char *value;
246         char *decoded;
247
248         if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)
249             continue;
250
251         /* Note that GMime retains ownership of value... */
252         value = g_mime_header_iter_get_value (iter);
253
254         /* ... while decoded needs to be freed with g_free(). */
255         decoded = g_mime_utils_header_decode_text (value);
256         if (! decoded) {
257             if (combined) {
258                 g_free (combined);
259                 combined = NULL;
260             }
261             goto DONE;
262         }
263
264         if (combined) {
265             char *tmp = g_strdup_printf ("%s %s", combined, decoded);
266             g_free (decoded);
267             g_free (combined);
268             if (! tmp) {
269                 combined = NULL;
270                 goto DONE;
271             }
272
273             combined = tmp;
274         } else {
275             combined = decoded;
276         }
277     } while (g_mime_header_iter_next (iter));
278
279     /* Return empty string for non-existing headers. */
280     if (! combined)
281         combined = g_strdup ("");
282
283   DONE:
284     g_mime_header_iter_free (iter);
285
286     return combined;
287 }
288
289 const char *
290 _notmuch_message_file_get_header (notmuch_message_file_t *message,
291                                  const char *header)
292 {
293     const char *value;
294     char *decoded;
295
296     if (_notmuch_message_file_parse (message))
297         return NULL;
298
299     /* If we have a cached decoded value, use it. */
300     value = g_hash_table_lookup (message->headers, header);
301     if (value)
302         return value;
303
304     if (strcasecmp (header, "received") == 0) {
305         /*
306          * The Received: header is special. We concatenate all
307          * instances of the header as we use this when analyzing the
308          * path the mail has taken from sender to recipient.
309          */
310         decoded = _notmuch_message_file_get_combined_header (message, header);
311     } else {
312         value = g_mime_object_get_header (GMIME_OBJECT (message->message),
313                                           header);
314         if (value)
315             decoded = g_mime_utils_header_decode_text (value);
316         else
317             decoded = g_strdup ("");
318     }
319
320     if (! decoded)
321         return NULL;
322
323     /* Cache the decoded value. We also own the strings. */
324     g_hash_table_insert (message->headers, xstrdup (header), decoded);
325
326     return decoded;
327 }