]> git.notmuchmail.org Git - notmuch/blob - lib/message-file.c
75caba6d8b055e5c3b57b2d72d1926381ff11fc8
[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 typedef struct {
30     char *str;
31     size_t size;
32     size_t len;
33 } header_value_closure_t;
34
35 struct _notmuch_message_file {
36     /* File object */
37     FILE *file;
38
39     /* Header storage */
40     int restrict_headers;
41     GHashTable *headers;
42     int broken_headers;
43     int good_headers;
44     size_t header_size; /* Length of full message header in bytes. */
45
46     /* Parsing state */
47     char *line;
48     size_t line_size;
49     header_value_closure_t value;
50
51     int parsing_started;
52     int parsing_finished;
53 };
54
55 static int
56 strcase_equal (const void *a, const void *b)
57 {
58     return strcasecmp (a, b) == 0;
59 }
60
61 static unsigned int
62 strcase_hash (const void *ptr)
63 {
64     const char *s = ptr;
65
66     /* This is the djb2 hash. */
67     unsigned int hash = 5381;
68     while (s && *s) {
69         hash = ((hash << 5) + hash) + tolower (*s);
70         s++;
71     }
72
73     return hash;
74 }
75
76 static int
77 _notmuch_message_file_destructor (notmuch_message_file_t *message)
78 {
79     if (message->line)
80         free (message->line);
81
82     if (message->value.size)
83         free (message->value.str);
84
85     if (message->headers)
86         g_hash_table_destroy (message->headers);
87
88     if (message->file)
89         fclose (message->file);
90
91     return 0;
92 }
93
94 /* Create a new notmuch_message_file_t for 'filename' with 'ctx' as
95  * the talloc owner. */
96 notmuch_message_file_t *
97 _notmuch_message_file_open_ctx (void *ctx, const char *filename)
98 {
99     notmuch_message_file_t *message;
100
101     message = talloc_zero (ctx, notmuch_message_file_t);
102     if (unlikely (message == NULL))
103         return NULL;
104
105     talloc_set_destructor (message, _notmuch_message_file_destructor);
106
107     message->file = fopen (filename, "r");
108     if (message->file == NULL)
109         goto FAIL;
110
111     message->headers = g_hash_table_new_full (strcase_hash,
112                                               strcase_equal,
113                                               free,
114                                               free);
115
116     message->parsing_started = 0;
117     message->parsing_finished = 0;
118
119     return message;
120
121   FAIL:
122     fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
123     notmuch_message_file_close (message);
124
125     return NULL;
126 }
127
128 notmuch_message_file_t *
129 notmuch_message_file_open (const char *filename)
130 {
131     return _notmuch_message_file_open_ctx (NULL, filename);
132 }
133
134 void
135 notmuch_message_file_close (notmuch_message_file_t *message)
136 {
137     talloc_free (message);
138 }
139
140 void
141 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
142                                         va_list va_headers)
143 {
144     char *header;
145
146     if (message->parsing_started)
147         INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started");
148
149     while (1) {
150         header = va_arg (va_headers, char*);
151         if (header == NULL)
152             break;
153         g_hash_table_insert (message->headers,
154                              xstrdup (header), NULL);
155     }
156
157     message->restrict_headers = 1;
158 }
159
160 void
161 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)
162 {
163     va_list va_headers;
164
165     va_start (va_headers, message);
166
167     notmuch_message_file_restrict_headersv (message, va_headers);
168 }
169
170 static void
171 copy_header_unfolding (header_value_closure_t *value,
172                        const char *chunk)
173 {
174     char *last;
175
176     if (chunk == NULL)
177         return;
178
179     while (*chunk == ' ' || *chunk == '\t')
180         chunk++;
181
182     if (value->len + 1 + strlen (chunk) + 1 > value->size) {
183         unsigned int new_size = value->size;
184         if (value->size == 0)
185             new_size = strlen (chunk) + 1;
186         else
187             while (value->len + 1 + strlen (chunk) + 1 > new_size)
188                 new_size *= 2;
189         value->str = xrealloc (value->str, new_size);
190         value->size = new_size;
191     }
192
193     last = value->str + value->len;
194     if (value->len) {
195         *last = ' ';
196         last++;
197         value->len++;
198     }
199
200     strcpy (last, chunk);
201     value->len += strlen (chunk);
202
203     last = value->str + value->len - 1;
204     if (*last == '\n') {
205         *last = '\0';
206         value->len--;
207     }
208 }
209
210 /* As a special-case, a value of NULL for header_desired will force
211  * the entire header to be parsed if it is not parsed already. This is
212  * used by the _notmuch_message_file_get_headers_end function. */
213 const char *
214 notmuch_message_file_get_header (notmuch_message_file_t *message,
215                                  const char *header_desired)
216 {
217     int contains;
218     char *header, *decoded_value;
219     const char *s, *colon;
220     int match;
221     static int initialized = 0;
222
223     if (! initialized) {
224         g_mime_init (0);
225         initialized = 1;
226     }
227
228     message->parsing_started = 1;
229
230     if (header_desired == NULL)
231         contains = 0;
232     else
233         contains = g_hash_table_lookup_extended (message->headers,
234                                                  header_desired, NULL,
235                                                  (gpointer *) &decoded_value);
236
237     if (contains && decoded_value)
238         return decoded_value;
239
240     if (message->parsing_finished)
241         return NULL;
242
243 #define NEXT_HEADER_LINE(closure)                               \
244     while (1) {                                                 \
245         ssize_t bytes_read = getline (&message->line,           \
246                                       &message->line_size,      \
247                                       message->file);           \
248         if (bytes_read == -1) {                                 \
249             message->parsing_finished = 1;                      \
250             break;                                              \
251         }                                                       \
252         if (*message->line == '\n') {                           \
253             message->parsing_finished = 1;                      \
254             break;                                              \
255         }                                                       \
256         if (closure &&                                          \
257             (*message->line == ' ' || *message->line == '\t'))  \
258         {                                                       \
259             copy_header_unfolding ((closure), message->line);   \
260         }                                                       \
261         if (*message->line == ' ' || *message->line == '\t')    \
262             message->header_size += strlen (message->line);     \
263         else                                                    \
264             break;                                              \
265     }
266
267     if (message->line == NULL)
268         NEXT_HEADER_LINE (NULL);
269
270     while (1) {
271
272         if (message->parsing_finished)
273             break;
274
275         colon = strchr (message->line, ':');
276
277         if (colon == NULL) {
278             message->broken_headers++;
279             /* A simple heuristic for giving up on things that just
280              * don't look like mail messages. */
281             if (message->broken_headers >= 10 &&
282                 message->good_headers < 5)
283             {
284                 message->parsing_finished = 1;
285                 continue;
286             }
287             NEXT_HEADER_LINE (NULL);
288             continue;
289         }
290
291         message->header_size += strlen (message->line);
292
293         message->good_headers++;
294
295         header = xstrndup (message->line, colon - message->line);
296
297         if (message->restrict_headers &&
298             ! g_hash_table_lookup_extended (message->headers,
299                                             header, NULL, NULL))
300         {
301             free (header);
302             NEXT_HEADER_LINE (NULL);
303             continue;
304         }
305
306         s = colon + 1;
307         while (*s == ' ' || *s == '\t')
308             s++;
309
310         message->value.len = 0;
311         copy_header_unfolding (&message->value, s);
312
313         NEXT_HEADER_LINE (&message->value);
314
315         if (header_desired == 0)
316             match = 0;
317         else
318             match = (strcasecmp (header, header_desired) == 0);
319
320         decoded_value = g_mime_utils_header_decode_text (message->value.str);
321
322         g_hash_table_insert (message->headers, header, decoded_value);
323
324         if (match)
325             return decoded_value;
326     }
327
328     if (message->line)
329         free (message->line);
330     message->line = NULL;
331
332     if (message->value.size) {
333         free (message->value.str);
334         message->value.str = NULL;
335         message->value.size = 0;
336         message->value.len = 0;
337     }
338
339     /* We've parsed all headers and never found the one we're looking
340      * for. It's probably just not there, but let's check that we
341      * didn't make a mistake preventing us from seeing it. */
342     if (message->restrict_headers && header_desired &&
343         ! g_hash_table_lookup_extended (message->headers,
344                                         header_desired, NULL, NULL))
345     {
346         INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n"
347                         "included in call to notmuch_message_file_restrict_headers\n",
348                         header_desired);
349     }
350
351     return NULL;
352 }