]> git.notmuchmail.org Git - notmuch/blob - message.c
notmuch: Revamp help message a bit.
[notmuch] / message.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 <glib.h> /* GHashTable */
26
27 typedef struct {
28     char *str;
29     size_t size;
30     size_t len;
31 } header_value_closure_t;
32
33 struct _notmuch_message {
34     /* File object */
35     FILE *file;
36
37     /* Header storage */
38     int restrict_headers;
39     GHashTable *headers;
40     int broken_headers;
41     int good_headers;
42
43     /* Parsing state */
44     char *line;
45     size_t line_size;
46     header_value_closure_t value;
47
48     int parsing_started;
49     int parsing_finished;
50 };
51
52 static int
53 strcase_equal (const void *a, const void *b)
54 {
55     return strcasecmp (a, b) == 0;
56 }
57
58 static unsigned int
59 strcase_hash (const void *ptr)
60 {
61     const char *s = ptr;
62
63     /* This is the djb2 hash. */
64     unsigned int hash = 5381;
65     while (s && *s) {
66         hash = ((hash << 5) + hash) + tolower (*s);
67         s++;
68     }
69
70     return hash;
71 }
72
73 notmuch_message_t *
74 notmuch_message_open (const char *filename)
75 {
76     notmuch_message_t *message;
77
78     message = xcalloc (1, sizeof (notmuch_message_t));
79
80     message->file = fopen (filename, "r");
81     if (message->file == NULL)
82         goto FAIL;
83
84     message->headers = g_hash_table_new_full (strcase_hash,
85                                               strcase_equal,
86                                               free,
87                                               free);
88
89     message->parsing_started = 0;
90     message->parsing_finished = 0;
91
92     return message;
93
94   FAIL:
95     fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
96     notmuch_message_close (message);
97
98     return NULL;
99 }
100
101 void
102 notmuch_message_close (notmuch_message_t *message)
103 {
104     if (message == NULL)
105         return;
106
107     if (message->headers)
108         g_hash_table_unref (message->headers);
109
110     if (message->file)
111         fclose (message->file);
112
113     free (message);
114 }
115
116 void
117 notmuch_message_restrict_headersv (notmuch_message_t *message,
118                                    va_list va_headers)
119 {
120     char *header;
121
122     if (message->parsing_started ) {
123         fprintf (stderr, "Error: notmuch_message_restrict_headers called after parsing has started\n");
124         exit (1);
125     }
126
127     while (1) {
128         header = va_arg (va_headers, char*);
129         if (header == NULL)
130             break;
131         g_hash_table_insert (message->headers,
132                              xstrdup (header), NULL);
133     }
134
135     message->restrict_headers = 1;
136 }
137
138 void
139 notmuch_message_restrict_headers (notmuch_message_t *message, ...)
140 {
141     va_list va_headers;
142
143     va_start (va_headers, message);
144
145     notmuch_message_restrict_headersv (message, va_headers);
146 }
147
148 void
149 copy_header_unfolding (header_value_closure_t *value,
150                        const char *chunk)
151 {
152     char *last;
153
154     if (chunk == NULL)
155         return;
156
157     while (*chunk == ' ' || *chunk == '\t')
158         chunk++;
159
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;
164         else
165             while (value->len + 1 + strlen (chunk) + 1 > new_size)
166                 new_size *= 2;
167         value->str = xrealloc (value->str, new_size);
168         value->size = new_size;
169     }
170
171     last = value->str + value->len;
172     if (value->len) {
173         *last = ' ';
174         last++;
175         value->len++;
176     }
177
178     strcpy (last, chunk);
179     value->len += strlen (chunk);
180
181     last = value->str + value->len - 1;
182     if (*last == '\n') {
183         *last = '\0';
184         value->len--;
185     }
186 }
187
188 const char *
189 notmuch_message_get_header (notmuch_message_t *message,
190                             const char *header_desired)
191 {
192     int contains;
193     char *header, *value;
194     const char *s, *colon;
195     int match;
196
197     message->parsing_started = 1;
198
199     contains = g_hash_table_lookup_extended (message->headers,
200                                              header_desired, NULL,
201                                              (gpointer *) &value);
202     if (contains && value)
203         return value;
204
205     if (message->parsing_finished)
206         return NULL;
207
208 #define NEXT_HEADER_LINE(closure)                               \
209     do {                                                        \
210         ssize_t bytes_read = getline (&message->line,           \
211                                       &message->line_size,      \
212                                       message->file);           \
213         if (bytes_read == -1) {                                 \
214             message->parsing_finished = 1;                      \
215             break;                                              \
216         }                                                       \
217         if (*message->line == '\n') {                           \
218             message->parsing_finished = 1;                      \
219             break;                                              \
220         }                                                       \
221         if (closure &&                                          \
222             (*message->line == ' ' || *message->line == '\t'))  \
223         {                                                       \
224             copy_header_unfolding ((closure), message->line);   \
225         }                                                       \
226     } while (*message->line == ' ' || *message->line == '\t');
227
228     if (message->line == NULL)
229         NEXT_HEADER_LINE (NULL);
230
231     while (1) {
232
233         if (message->parsing_finished)
234             break;
235
236         colon = strchr (message->line, ':');
237
238         if (colon == NULL) {
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)
244             {
245                 message->parsing_finished = 1;
246                 continue;
247             }
248             NEXT_HEADER_LINE (NULL);
249             continue;
250         }
251
252         message->good_headers++;
253
254         header = xstrndup (message->line, colon - message->line);
255
256         if (message->restrict_headers &&
257             ! g_hash_table_lookup_extended (message->headers,
258                                             header, NULL, NULL))
259         {
260             free (header);
261             NEXT_HEADER_LINE (NULL);
262             continue;
263         }
264
265         s = colon + 1;
266         while (*s == ' ' || *s == '\t')
267             s++;
268
269         message->value.len = 0;
270         copy_header_unfolding (&message->value, s);
271
272         NEXT_HEADER_LINE (&message->value);
273
274         match = (strcasecmp (header, header_desired) == 0);
275
276         g_hash_table_insert (message->headers, header,
277                              xstrdup (message->value.str));
278
279         if (match)
280             return value;
281     }
282
283     if (message->line)
284         free (message->line);
285     message->line = NULL;
286
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;
292     }
293
294     return NULL;
295 }