1 /* message.c - Utility functions for parsing an email message for notmuch.
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
23 #include "notmuch-private.h"
25 #include <gmime/gmime.h>
27 #include <glib.h> /* GHashTable */
33 } header_value_closure_t;
35 struct _notmuch_message_file {
44 size_t header_size; /* Length of full message header in bytes. */
49 header_value_closure_t value;
56 strcase_equal (const void *a, const void *b)
58 return strcasecmp (a, b) == 0;
62 strcase_hash (const void *ptr)
66 /* This is the djb2 hash. */
67 unsigned int hash = 5381;
69 hash = ((hash << 5) + hash) + tolower (*s);
77 _notmuch_message_file_destructor (notmuch_message_file_t *message)
82 if (message->value.size)
83 free (message->value.str);
86 g_hash_table_destroy (message->headers);
89 fclose (message->file);
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)
99 notmuch_message_file_t *message;
101 message = talloc_zero (ctx, notmuch_message_file_t);
102 if (unlikely (message == NULL))
105 talloc_set_destructor (message, _notmuch_message_file_destructor);
107 message->file = fopen (filename, "r");
108 if (message->file == NULL)
111 message->headers = g_hash_table_new_full (strcase_hash,
116 message->parsing_started = 0;
117 message->parsing_finished = 0;
122 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
123 notmuch_message_file_close (message);
128 notmuch_message_file_t *
129 notmuch_message_file_open (const char *filename)
131 return _notmuch_message_file_open_ctx (NULL, filename);
135 notmuch_message_file_close (notmuch_message_file_t *message)
137 talloc_free (message);
141 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
146 if (message->parsing_started)
147 INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started");
150 header = va_arg (va_headers, char*);
153 g_hash_table_insert (message->headers,
154 xstrdup (header), NULL);
157 message->restrict_headers = 1;
161 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)
165 va_start (va_headers, message);
167 notmuch_message_file_restrict_headersv (message, va_headers);
171 copy_header_unfolding (header_value_closure_t *value,
179 while (*chunk == ' ' || *chunk == '\t')
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;
187 while (value->len + 1 + strlen (chunk) + 1 > new_size)
189 value->str = xrealloc (value->str, new_size);
190 value->size = new_size;
193 last = value->str + value->len;
200 strcpy (last, chunk);
201 value->len += strlen (chunk);
203 last = value->str + value->len - 1;
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. */
214 notmuch_message_file_get_header (notmuch_message_file_t *message,
215 const char *header_desired)
218 char *header, *decoded_value;
219 const char *s, *colon;
221 static int initialized = 0;
228 message->parsing_started = 1;
230 if (header_desired == NULL)
233 contains = g_hash_table_lookup_extended (message->headers,
234 header_desired, NULL,
235 (gpointer *) &decoded_value);
237 if (contains && decoded_value)
238 return decoded_value;
240 if (message->parsing_finished)
243 #define NEXT_HEADER_LINE(closure) \
245 ssize_t bytes_read = getline (&message->line, \
246 &message->line_size, \
248 if (bytes_read == -1) { \
249 message->parsing_finished = 1; \
252 if (*message->line == '\n') { \
253 message->parsing_finished = 1; \
257 (*message->line == ' ' || *message->line == '\t')) \
259 copy_header_unfolding ((closure), message->line); \
261 if (*message->line == ' ' || *message->line == '\t') \
262 message->header_size += strlen (message->line); \
267 if (message->line == NULL)
268 NEXT_HEADER_LINE (NULL);
272 if (message->parsing_finished)
275 colon = strchr (message->line, ':');
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)
284 message->parsing_finished = 1;
287 NEXT_HEADER_LINE (NULL);
291 message->header_size += strlen (message->line);
293 message->good_headers++;
295 header = xstrndup (message->line, colon - message->line);
297 if (message->restrict_headers &&
298 ! g_hash_table_lookup_extended (message->headers,
302 NEXT_HEADER_LINE (NULL);
307 while (*s == ' ' || *s == '\t')
310 message->value.len = 0;
311 copy_header_unfolding (&message->value, s);
313 NEXT_HEADER_LINE (&message->value);
315 if (header_desired == 0)
318 match = (strcasecmp (header, header_desired) == 0);
320 decoded_value = g_mime_utils_header_decode_text (message->value.str);
322 g_hash_table_insert (message->headers, header, decoded_value);
325 return decoded_value;
328 if (message->parsing_finished) {
329 fclose (message->file);
330 message->file = NULL;
334 free (message->line);
335 message->line = NULL;
337 if (message->value.size) {
338 free (message->value.str);
339 message->value.str = NULL;
340 message->value.size = 0;
341 message->value.len = 0;
344 /* We've parsed all headers and never found the one we're looking
345 * for. It's probably just not there, but let's check that we
346 * didn't make a mistake preventing us from seeing it. */
347 if (message->restrict_headers && header_desired &&
348 ! g_hash_table_lookup_extended (message->headers,
349 header_desired, NULL, NULL))
351 INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n"
352 "included in call to notmuch_message_file_restrict_headers\n",