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 {
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);
74 notmuch_message_open (const char *filename)
76 notmuch_message_t *message;
78 message = xcalloc (1, sizeof (notmuch_message_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_close (message);
102 notmuch_message_close (notmuch_message_t *message)
107 if (message->headers)
108 g_hash_table_unref (message->headers);
111 fclose (message->file);
117 notmuch_message_restrict_headersv (notmuch_message_t *message,
122 if (message->parsing_started ) {
123 fprintf (stderr, "Error: notmuch_message_restrict_headers called after parsing has started\n");
128 header = va_arg (va_headers, char*);
131 g_hash_table_insert (message->headers,
132 xstrdup (header), NULL);
135 message->restrict_headers = 1;
139 notmuch_message_restrict_headers (notmuch_message_t *message, ...)
143 va_start (va_headers, message);
145 notmuch_message_restrict_headersv (message, va_headers);
149 copy_header_unfolding (header_value_closure_t *value,
157 while (*chunk == ' ' || *chunk == '\t')
160 if (value->len + 1 + strlen (chunk) + 1 > value->size) {
161 int new_size = value->size;
162 if (value->size == 0)
163 new_size = strlen (chunk) + 1;
165 while (value->len + 1 + strlen (chunk) + 1 > new_size)
167 value->str = xrealloc (value->str, new_size);
168 value->size = new_size;
171 last = value->str + value->len;
178 strcpy (last, chunk);
179 value->len += strlen (chunk);
181 last = value->str + value->len - 1;
189 notmuch_message_get_header (notmuch_message_t *message,
190 const char *header_desired)
193 char *header, *value;
194 const char *s, *colon;
197 message->parsing_started = 1;
199 contains = g_hash_table_lookup_extended (message->headers,
200 header_desired, NULL,
201 (gpointer *) &value);
202 if (contains && value)
205 if (message->parsing_finished)
208 #define NEXT_HEADER_LINE(closure) \
210 ssize_t bytes_read = getline (&message->line, \
211 &message->line_size, \
213 if (bytes_read == -1) { \
214 message->parsing_finished = 1; \
217 if (*message->line == '\n') { \
218 message->parsing_finished = 1; \
222 (*message->line == ' ' || *message->line == '\t')) \
224 copy_header_unfolding ((closure), message->line); \
226 } while (*message->line == ' ' || *message->line == '\t');
228 if (message->line == NULL)
229 NEXT_HEADER_LINE (NULL);
233 if (message->parsing_finished)
236 colon = strchr (message->line, ':');
239 message->broken_headers++;
240 /* A simple heuristic for giving up on things that just
241 * don't look like mail messages. */
242 if (message->broken_headers >= 10 &&
243 message->good_headers < 5)
245 message->parsing_finished = 1;
248 NEXT_HEADER_LINE (NULL);
252 message->good_headers++;
254 header = xstrndup (message->line, colon - message->line);
256 if (message->restrict_headers &&
257 ! g_hash_table_lookup_extended (message->headers,
261 NEXT_HEADER_LINE (NULL);
266 while (*s == ' ' || *s == '\t')
269 message->value.len = 0;
270 copy_header_unfolding (&message->value, s);
272 NEXT_HEADER_LINE (&message->value);
274 match = (strcasecmp (header, header_desired) == 0);
276 g_hash_table_insert (message->headers, header,
277 xstrdup (message->value.str));
284 free (message->line);
285 message->line = NULL;
287 if (message->value.size) {
288 free (message->value.str);
289 message->value.str = NULL;
290 message->value.size = 0;
291 message->value.len = 0;