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 {
44 header_value_closure_t value;
51 strcase_equal (const void *a, const void *b)
53 return strcasecmp (a, b) == 0;
57 strcase_hash (const void *ptr)
61 /* This is the djb2 hash. */
62 unsigned int hash = 5381;
64 hash = ((hash << 5) + hash) + tolower (*s);
72 notmuch_message_open (const char *filename)
74 notmuch_message_t *message;
76 message = xcalloc (1, sizeof (notmuch_message_t));
78 message->file = fopen (filename, "r");
79 if (message->file == NULL)
82 message->headers = g_hash_table_new_full (strcase_hash,
87 message->parsing_started = 0;
88 message->parsing_finished = 0;
93 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
94 notmuch_message_close (message);
100 notmuch_message_close (notmuch_message_t *message)
105 if (message->headers)
106 g_hash_table_unref (message->headers);
109 fclose (message->file);
115 notmuch_message_restrict_headersv (notmuch_message_t *message,
120 if (message->parsing_started ) {
121 fprintf (stderr, "Error: notmuch_message_restrict_headers called after parsing has started\n");
126 header = va_arg (va_headers, char*);
129 g_hash_table_insert (message->headers,
130 xstrdup (header), NULL);
133 message->restrict_headers = 1;
137 notmuch_message_restrict_headers (notmuch_message_t *message, ...)
141 va_start (va_headers, message);
143 notmuch_message_restrict_headersv (message, va_headers);
147 copy_header_unfolding (header_value_closure_t *value,
155 while (*chunk == ' ' || *chunk == '\t')
158 if (value->len + 1 + strlen (chunk) + 1 > value->size) {
159 int new_size = value->size;
160 if (value->size == 0)
161 new_size = strlen (chunk) + 1;
163 while (value->len + 1 + strlen (chunk) + 1 > new_size)
165 value->str = xrealloc (value->str, new_size);
166 value->size = new_size;
169 last = value->str + value->len;
176 strcpy (last, chunk);
177 value->len += strlen (chunk);
179 last = value->str + value->len - 1;
187 notmuch_message_get_header (notmuch_message_t *message,
188 const char *header_desired)
191 char *header, *value;
192 const char *s, *colon;
195 message->parsing_started = 1;
197 contains = g_hash_table_lookup_extended (message->headers,
198 header_desired, NULL,
199 (gpointer *) &value);
200 if (contains && value)
203 if (message->parsing_finished)
206 #define NEXT_HEADER_LINE(closure) \
208 ssize_t bytes_read = getline (&message->line, \
209 &message->line_size, \
211 if (bytes_read == -1) { \
212 message->parsing_finished = 1; \
215 if (*message->line == '\n') { \
216 message->parsing_finished = 1; \
220 (*message->line == ' ' || *message->line == '\t')) \
222 copy_header_unfolding ((closure), message->line); \
224 } while (*message->line == ' ' || *message->line == '\t');
226 if (message->line == NULL)
227 NEXT_HEADER_LINE (NULL);
231 if (message->parsing_finished)
234 colon = strchr (message->line, ':');
237 fprintf (stderr, "Warning: Unexpected non-header line: %s\n",
239 NEXT_HEADER_LINE (NULL);
243 header = xstrndup (message->line, colon - message->line);
245 if (message->restrict_headers &&
246 ! g_hash_table_lookup_extended (message->headers,
250 NEXT_HEADER_LINE (NULL);
255 while (*s == ' ' || *s == '\t')
258 message->value.len = 0;
259 copy_header_unfolding (&message->value, s);
261 NEXT_HEADER_LINE (&message->value);
263 match = (strcasecmp (header, header_desired) == 0);
265 g_hash_table_insert (message->headers, header,
266 xstrdup (message->value.str));
273 free (message->line);
274 message->line = NULL;
276 if (message->value.size) {
277 free (message->value.str);
278 message->value.str = NULL;
279 message->value.size = 0;
280 message->value.len = 0;