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 <glib.h> /* GHashTable */
31 } header_value_closure_t;
33 struct _notmuch_message_file {
46 header_value_closure_t value;
53 strcase_equal (const void *a, const void *b)
55 return strcasecmp (a, b) == 0;
59 strcase_hash (const void *ptr)
63 /* This is the djb2 hash. */
64 unsigned int hash = 5381;
66 hash = ((hash << 5) + hash) + tolower (*s);
73 notmuch_message_file_t *
74 notmuch_message_file_open (const char *filename)
76 notmuch_message_file_t *message;
78 message = xcalloc (1, sizeof (notmuch_message_file_t));
80 message->file = fopen (filename, "r");
81 if (message->file == NULL)
84 message->headers = g_hash_table_new_full (strcase_hash,
89 message->parsing_started = 0;
90 message->parsing_finished = 0;
95 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
96 notmuch_message_file_close (message);
102 notmuch_message_file_close (notmuch_message_file_t *message)
108 free (message->line);
110 if (message->value.size)
111 free (message->value.str);
113 if (message->headers)
114 g_hash_table_destroy (message->headers);
117 fclose (message->file);
123 notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
128 if (message->parsing_started ) {
129 fprintf (stderr, "Error: notmuch_message_file_restrict_headers called after parsing has started\n");
134 header = va_arg (va_headers, char*);
137 g_hash_table_insert (message->headers,
138 xstrdup (header), NULL);
141 message->restrict_headers = 1;
145 notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)
149 va_start (va_headers, message);
151 notmuch_message_file_restrict_headersv (message, va_headers);
155 copy_header_unfolding (header_value_closure_t *value,
163 while (*chunk == ' ' || *chunk == '\t')
166 if (value->len + 1 + strlen (chunk) + 1 > value->size) {
167 int new_size = value->size;
168 if (value->size == 0)
169 new_size = strlen (chunk) + 1;
171 while (value->len + 1 + strlen (chunk) + 1 > new_size)
173 value->str = xrealloc (value->str, new_size);
174 value->size = new_size;
177 last = value->str + value->len;
184 strcpy (last, chunk);
185 value->len += strlen (chunk);
187 last = value->str + value->len - 1;
195 notmuch_message_file_get_header (notmuch_message_file_t *message,
196 const char *header_desired)
199 char *header, *value;
200 const char *s, *colon;
203 message->parsing_started = 1;
205 contains = g_hash_table_lookup_extended (message->headers,
206 header_desired, NULL,
207 (gpointer *) &value);
208 if (contains && value)
211 if (message->parsing_finished)
214 #define NEXT_HEADER_LINE(closure) \
216 ssize_t bytes_read = getline (&message->line, \
217 &message->line_size, \
219 if (bytes_read == -1) { \
220 message->parsing_finished = 1; \
223 if (*message->line == '\n') { \
224 message->parsing_finished = 1; \
228 (*message->line == ' ' || *message->line == '\t')) \
230 copy_header_unfolding ((closure), message->line); \
232 } while (*message->line == ' ' || *message->line == '\t');
234 if (message->line == NULL)
235 NEXT_HEADER_LINE (NULL);
239 if (message->parsing_finished)
242 colon = strchr (message->line, ':');
245 message->broken_headers++;
246 /* A simple heuristic for giving up on things that just
247 * don't look like mail messages. */
248 if (message->broken_headers >= 10 &&
249 message->good_headers < 5)
251 message->parsing_finished = 1;
254 NEXT_HEADER_LINE (NULL);
258 message->good_headers++;
260 header = xstrndup (message->line, colon - message->line);
262 if (message->restrict_headers &&
263 ! g_hash_table_lookup_extended (message->headers,
267 NEXT_HEADER_LINE (NULL);
272 while (*s == ' ' || *s == '\t')
275 message->value.len = 0;
276 copy_header_unfolding (&message->value, s);
278 NEXT_HEADER_LINE (&message->value);
280 match = (strcasecmp (header, header_desired) == 0);
282 value = xstrdup (message->value.str);
284 g_hash_table_insert (message->headers, header, value);
291 free (message->line);
292 message->line = NULL;
294 if (message->value.size) {
295 free (message->value.str);
296 message->value.str = NULL;
297 message->value.size = 0;
298 message->value.len = 0;