"snoozing" with notmuch?
[notmuch-archives.git] / 1e / 73643df18b586c994b6b63ca37303c363f97c0
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 8F40B431FBC\r
6         for <notmuch@notmuchmail.org>; Sat, 30 Nov 2013 07:34:34 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id DXn2XKqJ8EbZ for <notmuch@notmuchmail.org>;\r
17         Sat, 30 Nov 2013 07:34:27 -0800 (PST)\r
18 Received: from mail-ea0-f173.google.com (mail-ea0-f173.google.com\r
19         [209.85.215.173]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 60134431FD7\r
22         for <notmuch@notmuchmail.org>; Sat, 30 Nov 2013 07:34:13 -0800 (PST)\r
23 Received: by mail-ea0-f173.google.com with SMTP id g15so7632776eak.18\r
24         for <notmuch@notmuchmail.org>; Sat, 30 Nov 2013 07:34:12 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
28         :references:in-reply-to:references;\r
29         bh=5TLswB6ZYLSpzgYHTBKMQuqK7ULcyjfn5xOnEwr4aEQ=;\r
30         b=JIcp083Z2JbwhZia8TcawqQpOmw32kPgR0/RCG4FmH7qRXGdnazwdVqcREyQ7a4fZb\r
31         O1qQ6EHrsPM4cJJG/OvRFtM+JVk2ptr55HgQ6rYheIq/4CaQRT8xIWZw8u5eybVRDZio\r
32         9eByhahqb0aWShW4Tjt4pXjQfrAR5uEA6TE7l5yvi0E43h5aUzmQqspOw7BkTTj+r0pn\r
33         hYLEFbQYgQdx5kK4dxZgVg8mWtbkk4KxHBb4tAhZVpASutfOR6qnslZJ2LITppdcScb7\r
34         JxZtfj8a+IuN1jev7mQj2mlD9z9tw2jPqhdS5oC2v9YqzUjyp4mWpDvbcHBfuk3ZYRw5\r
35         VcTg==\r
36 X-Gm-Message-State:\r
37  ALoCoQlPg7PhcFekdins5GGuToPdOX1ab8mQDAfAM0vETCqDtB5c0IG9zRpxLJ7nb7vCgBt1M2Y+\r
38 X-Received: by 10.14.182.199 with SMTP id o47mr57789251eem.7.1385825652280;\r
39         Sat, 30 Nov 2013 07:34:12 -0800 (PST)\r
40 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
41         [88.195.111.91])\r
42         by mx.google.com with ESMTPSA id v7sm34804952eel.2.2013.11.30.07.34.10\r
43         for <multiple recipients>\r
44         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
45         Sat, 30 Nov 2013 07:34:11 -0800 (PST)\r
46 From: Jani Nikula <jani@nikula.org>\r
47 To: notmuch@notmuchmail.org\r
48 Subject: [PATCH v2 5/7] lib: replace the header parser with gmime\r
49 Date: Sat, 30 Nov 2013 17:33:54 +0200\r
50 Message-Id:\r
51  <336c7d56a56685e82e06275dc4971c76f18f3d9b.1385825425.git.jani@nikula.org>\r
52 X-Mailer: git-send-email 1.8.4.2\r
53 In-Reply-To: <cover.1385825425.git.jani@nikula.org>\r
54 References: <cover.1385825425.git.jani@nikula.org>\r
55 In-Reply-To: <cover.1385825425.git.jani@nikula.org>\r
56 References: <cover.1385825425.git.jani@nikula.org>\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Sat, 30 Nov 2013 15:34:34 -0000\r
70 \r
71 The notmuch library includes a full blown message header parser. Yet\r
72 the same message headers are parsed by gmime during indexing. Switch\r
73 to gmime parsing completely.\r
74 \r
75 These are the main differences between the parsers:\r
76 \r
77 * Gmime stops header parsing at the first invalid header, and presumes\r
78   the message body starts from there. The current parser is quite\r
79   liberal in accepting broken headers. The change means we will be\r
80   much pickier about accepting invalid messages. For example the\r
81   performance test suite contains plenty of email that does not pass\r
82   as valid email. (Note that the mail body would not have been indexed\r
83   before the change anyway, as we use gmime for that.)\r
84 \r
85 * The current parser converts tabs used in header folding to\r
86   spaces. Gmime preserve the tabs. Due to a broken python library used\r
87   in mailman, there are plenty of mailing lists that produce headers\r
88   with tabs in header folding, we'll see plenty of tabs.\r
89 \r
90 At this step, we only switch the header parsing to gmime.\r
91 ---\r
92  lib/database.cc       |   4 +\r
93  lib/index.cc          |  11 --\r
94  lib/message-file.c    | 346 ++++++++++++++++----------------------------------\r
95  lib/message.cc        |   6 +\r
96  lib/notmuch-private.h |   4 +\r
97  5 files changed, 122 insertions(+), 249 deletions(-)\r
98 \r
99 diff --git a/lib/database.cc b/lib/database.cc\r
100 index f395061..d1bea88 100644\r
101 --- a/lib/database.cc\r
102 +++ b/lib/database.cc\r
103 @@ -1940,6 +1940,10 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
104                                            "to",\r
105                                            (char *) NULL);\r
106  \r
107 +    ret = notmuch_message_file_parse (message_file);\r
108 +    if (ret)\r
109 +       goto DONE;\r
110 +\r
111      try {\r
112         /* Before we do any real work, (especially before doing a\r
113          * potential SHA-1 computation on the entire file's contents),\r
114 diff --git a/lib/index.cc b/lib/index.cc\r
115 index 78c18cf..976e49f 100644\r
116 --- a/lib/index.cc\r
117 +++ b/lib/index.cc\r
118 @@ -437,7 +437,6 @@ _notmuch_message_index_file (notmuch_message_t *message,\r
119      static int initialized = 0;\r
120      char from_buf[5];\r
121      bool is_mbox = false;\r
122 -    static bool mbox_warning = false;\r
123  \r
124      if (! initialized) {\r
125         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
126 @@ -471,16 +470,6 @@ _notmuch_message_index_file (notmuch_message_t *message,\r
127             ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
128             goto DONE;\r
129         }\r
130 -       /* For historical reasons, we support single-message mboxes,\r
131 -        * but this behavior is likely to change in the future, so\r
132 -        * warn. */\r
133 -       if (!mbox_warning) {\r
134 -           mbox_warning = true;\r
135 -           fprintf (stderr, "\\r
136 -Warning: %s is an mbox containing a single message,\n\\r
137 -likely caused by misconfigured mail delivery.  Support for single-message\n\\r
138 -mboxes is deprecated and may be removed in the future.\n", filename);\r
139 -       }\r
140      }\r
141  \r
142      from = g_mime_message_get_sender (mime_message);\r
143 diff --git a/lib/message-file.c b/lib/message-file.c\r
144 index a2850c2..3c653d1 100644\r
145 --- a/lib/message-file.c\r
146 +++ b/lib/message-file.c\r
147 @@ -26,30 +26,19 @@\r
148  \r
149  #include <glib.h> /* GHashTable */\r
150  \r
151 -typedef struct {\r
152 -    char *str;\r
153 -    size_t size;\r
154 -    size_t len;\r
155 -} header_value_closure_t;\r
156 -\r
157  struct _notmuch_message_file {\r
158      /* File object */\r
159      FILE *file;\r
160 +    char *filename;\r
161  \r
162      /* Header storage */\r
163      int restrict_headers;\r
164      GHashTable *headers;\r
165 -    int broken_headers;\r
166 -    int good_headers;\r
167 -    size_t header_size; /* Length of full message header in bytes. */\r
168 -\r
169 -    /* Parsing state */\r
170 -    char *line;\r
171 -    size_t line_size;\r
172 -    header_value_closure_t value;\r
173  \r
174 -    int parsing_started;\r
175 -    int parsing_finished;\r
176 +    GMimeStream *stream;\r
177 +    GMimeParser *parser;\r
178 +    GMimeMessage *message;\r
179 +    notmuch_bool_t parsed;\r
180  };\r
181  \r
182  static int\r
183 @@ -76,16 +65,18 @@ strcase_hash (const void *ptr)\r
184  static int\r
185  _notmuch_message_file_destructor (notmuch_message_file_t *message)\r
186  {\r
187 -    if (message->line)\r
188 -       free (message->line);\r
189 -\r
190 -    if (message->value.size)\r
191 -       free (message->value.str);\r
192 -\r
193      if (message->headers)\r
194         g_hash_table_destroy (message->headers);\r
195  \r
196 -    if (message->file)\r
197 +    if (message->message)\r
198 +       g_object_unref (message->message);\r
199 +\r
200 +    if (message->parser)\r
201 +       g_object_unref (message->parser);\r
202 +\r
203 +    if (message->stream)\r
204 +       g_object_unref (message->stream);\r
205 +    else if (message->file) /* GMimeStream takes over the FILE* */\r
206         fclose (message->file);\r
207  \r
208      return 0;\r
209 @@ -102,19 +93,21 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename)\r
210      if (unlikely (message == NULL))\r
211         return NULL;\r
212  \r
213 +    /* only needed for error messages during parsing */\r
214 +    message->filename = talloc_strdup (message, filename);\r
215 +    if (message->filename == NULL)\r
216 +       goto FAIL;\r
217 +\r
218      talloc_set_destructor (message, _notmuch_message_file_destructor);\r
219  \r
220      message->file = fopen (filename, "r");\r
221      if (message->file == NULL)\r
222         goto FAIL;\r
223  \r
224 -    message->headers = g_hash_table_new_full (strcase_hash,\r
225 -                                             strcase_equal,\r
226 -                                             free,\r
227 -                                             g_free);\r
228 -\r
229 -    message->parsing_started = 0;\r
230 -    message->parsing_finished = 0;\r
231 +    message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,\r
232 +                                             free, g_free);\r
233 +    if (message->headers == NULL)\r
234 +       goto FAIL;\r
235  \r
236      return message;\r
237  \r
238 @@ -143,7 +136,7 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
239  {\r
240      char *header;\r
241  \r
242 -    if (message->parsing_started)\r
243 +    if (message->parsed)\r
244         INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started");\r
245  \r
246      while (1) {\r
247 @@ -167,234 +160,111 @@ notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)\r
248      notmuch_message_file_restrict_headersv (message, va_headers);\r
249  }\r
250  \r
251 -static void\r
252 -copy_header_unfolding (header_value_closure_t *value,\r
253 -                      const char *chunk)\r
254 +/*\r
255 + * gmime does not provide access to all headers, so we need to use the\r
256 + * parser header callback to gather them into a hash table.\r
257 + */\r
258 +static void header_cb (unused(GMimeParser *parser), const char *header,\r
259 +                      const char *value, unused(gint64 offset),\r
260 +                      gpointer user_data)\r
261  {\r
262 -    char *last;\r
263 +    notmuch_message_file_t *message = (notmuch_message_file_t *) user_data;\r
264 +    char *existing = NULL;\r
265 +    notmuch_bool_t found;\r
266  \r
267 -    if (chunk == NULL)\r
268 +    found = g_hash_table_lookup_extended (message->headers, header,\r
269 +                                         NULL, (gpointer *) &existing);\r
270 +    if (! found && message->restrict_headers)\r
271         return;\r
272  \r
273 -    while (*chunk == ' ' || *chunk == '\t')\r
274 -       chunk++;\r
275 -\r
276 -    if (value->len + 1 + strlen (chunk) + 1 > value->size) {\r
277 -       unsigned int new_size = value->size;\r
278 -       if (value->size == 0)\r
279 -           new_size = strlen (chunk) + 1;\r
280 -       else\r
281 -           while (value->len + 1 + strlen (chunk) + 1 > new_size)\r
282 -               new_size *= 2;\r
283 -       value->str = xrealloc (value->str, new_size);\r
284 -       value->size = new_size;\r
285 -    }\r
286 -\r
287 -    last = value->str + value->len;\r
288 -    if (value->len) {\r
289 -       *last = ' ';\r
290 -       last++;\r
291 -       value->len++;\r
292 -    }\r
293 -\r
294 -    strcpy (last, chunk);\r
295 -    value->len += strlen (chunk);\r
296 -\r
297 -    last = value->str + value->len - 1;\r
298 -    if (*last == '\n') {\r
299 -       *last = '\0';\r
300 -       value->len--;\r
301 +    if (existing == NULL) {\r
302 +       /* No value, add one */\r
303 +       char *decoded = g_mime_utils_header_decode_text (value);\r
304 +       g_hash_table_insert (message->headers, xstrdup (header), decoded);\r
305 +    } else if (strcasecmp (header, "received") == 0) {\r
306 +       /* Concat all of the Received: headers we encounter. */\r
307 +       char *combined, *decoded;\r
308 +       size_t combined_size;\r
309 +\r
310 +       decoded = g_mime_utils_header_decode_text (value);\r
311 +\r
312 +       combined_size = strlen(existing) + strlen(decoded) + 2;\r
313 +       combined = g_malloc (combined_size);\r
314 +       snprintf (combined, combined_size, "%s %s", existing, decoded);\r
315 +       g_free (decoded);\r
316 +       g_hash_table_insert (message->headers, xstrdup (header), combined);\r
317      }\r
318  }\r
319  \r
320 -/* As a special-case, a value of NULL for header_desired will force\r
321 - * the entire header to be parsed if it is not parsed already. This is\r
322 - * used by the _notmuch_message_file_get_headers_end function.\r
323 - * Another special case is the Received: header. For this header we\r
324 - * want to concatenate all instances of the header instead of just\r
325 - * hashing the first instance as we use this when analyzing the path\r
326 - * the mail has taken from sender to recipient.\r
327 - */\r
328 -const char *\r
329 -notmuch_message_file_get_header (notmuch_message_file_t *message,\r
330 -                                const char *header_desired)\r
331 +notmuch_status_t\r
332 +notmuch_message_file_parse (notmuch_message_file_t *message)\r
333  {\r
334 -    int contains;\r
335 -    char *header, *decoded_value, *header_sofar, *combined_header;\r
336 -    const char *s, *colon;\r
337 -    int match, newhdr, hdrsofar, is_received;\r
338      static int initialized = 0;\r
339 -\r
340 -    is_received = (strcmp(header_desired,"received") == 0);\r
341 +    char from_buf[5];\r
342 +    notmuch_bool_t is_mbox = FALSE;\r
343 +    static notmuch_bool_t mbox_warning = FALSE;\r
344  \r
345      if (! initialized) {\r
346         g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
347         initialized = 1;\r
348      }\r
349  \r
350 -    message->parsing_started = 1;\r
351 -\r
352 -    if (header_desired == NULL)\r
353 -       contains = 0;\r
354 -    else\r
355 -       contains = g_hash_table_lookup_extended (message->headers,\r
356 -                                                header_desired, NULL,\r
357 -                                                (gpointer *) &decoded_value);\r
358 -\r
359 -    if (contains && decoded_value)\r
360 -       return decoded_value;\r
361 -\r
362 -    if (message->parsing_finished)\r
363 -       return "";\r
364 -\r
365 -#define NEXT_HEADER_LINE(closure)                              \\r
366 -    while (1) {                                                        \\r
367 -       ssize_t bytes_read = getline (&message->line,           \\r
368 -                                     &message->line_size,      \\r
369 -                                     message->file);           \\r
370 -       if (bytes_read == -1) {                                 \\r
371 -           message->parsing_finished = 1;                      \\r
372 -           break;                                              \\r
373 -       }                                                       \\r
374 -       if (*message->line == '\n') {                           \\r
375 -           message->parsing_finished = 1;                      \\r
376 -           break;                                              \\r
377 -       }                                                       \\r
378 -       if (closure &&                                          \\r
379 -           (*message->line == ' ' || *message->line == '\t'))  \\r
380 -       {                                                       \\r
381 -           copy_header_unfolding ((closure), message->line);   \\r
382 -       }                                                       \\r
383 -       if (*message->line == ' ' || *message->line == '\t')    \\r
384 -           message->header_size += strlen (message->line);     \\r
385 -       else                                                    \\r
386 -           break;                                              \\r
387 -    }\r
388 -\r
389 -    if (message->line == NULL)\r
390 -       NEXT_HEADER_LINE (NULL);\r
391 -\r
392 -    while (1) {\r
393 -\r
394 -       if (message->parsing_finished)\r
395 -           break;\r
396 -\r
397 -       colon = strchr (message->line, ':');\r
398 -\r
399 -       if (colon == NULL) {\r
400 -           message->broken_headers++;\r
401 -           /* A simple heuristic for giving up on things that just\r
402 -            * don't look like mail messages. */\r
403 -           if (message->broken_headers >= 10 &&\r
404 -               message->good_headers < 5)\r
405 -           {\r
406 -               message->parsing_finished = 1;\r
407 -               break;\r
408 -           }\r
409 -           NEXT_HEADER_LINE (NULL);\r
410 -           continue;\r
411 +    /* Is this mbox? */\r
412 +    if (fread (from_buf, sizeof (from_buf), 1, message->file) == 1 &&\r
413 +       strncmp (from_buf, "From ", 5) == 0)\r
414 +       is_mbox = TRUE;\r
415 +    rewind (message->file);\r
416 +\r
417 +    message->stream = g_mime_stream_file_new (message->file);\r
418 +    message->parser = g_mime_parser_new_with_stream (message->stream);\r
419 +    g_mime_parser_set_scan_from (message->parser, is_mbox);\r
420 +    g_mime_parser_set_header_regex (message->parser, ".*", header_cb,\r
421 +                                   (gpointer) message);\r
422 +\r
423 +    message->message = g_mime_parser_construct_message (message->parser);\r
424 +    if (! message->message)\r
425 +       return NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
426 +\r
427 +    if (is_mbox) {\r
428 +       if (! g_mime_parser_eos (message->parser)) {\r
429 +           /* This is a multi-message mbox. */\r
430 +           return NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
431         }\r
432 -\r
433 -       message->header_size += strlen (message->line);\r
434 -\r
435 -       message->good_headers++;\r
436 -\r
437 -       header = xstrndup (message->line, colon - message->line);\r
438 -\r
439 -       if (message->restrict_headers &&\r
440 -           ! g_hash_table_lookup_extended (message->headers,\r
441 -                                           header, NULL, NULL))\r
442 -       {\r
443 -           free (header);\r
444 -           NEXT_HEADER_LINE (NULL);\r
445 -           continue;\r
446 -       }\r
447 -\r
448 -       s = colon + 1;\r
449 -       while (*s == ' ' || *s == '\t')\r
450 -           s++;\r
451 -\r
452 -       message->value.len = 0;\r
453 -       copy_header_unfolding (&message->value, s);\r
454 -\r
455 -       NEXT_HEADER_LINE (&message->value);\r
456 -\r
457 -       if (header_desired == NULL)\r
458 -           match = 0;\r
459 -       else\r
460 -           match = (strcasecmp (header, header_desired) == 0);\r
461 -\r
462 -       decoded_value = g_mime_utils_header_decode_text (message->value.str);\r
463 -       header_sofar = (char *)g_hash_table_lookup (message->headers, header);\r
464 -       /* we treat the Received: header special - we want to concat ALL of \r
465 -        * the Received: headers we encounter.\r
466 -        * for everything else we return the first instance of a header */\r
467 -       if (strcasecmp(header, "received") == 0) {\r
468 -           if (header_sofar == NULL) {\r
469 -               /* first Received: header we encountered; just add it */\r
470 -               g_hash_table_insert (message->headers, header, decoded_value);\r
471 -           } else {\r
472 -               /* we need to add the header to those we already collected */\r
473 -               newhdr = strlen(decoded_value);\r
474 -               hdrsofar = strlen(header_sofar);\r
475 -               combined_header = g_malloc(hdrsofar + newhdr + 2);\r
476 -               strncpy(combined_header,header_sofar,hdrsofar);\r
477 -               *(combined_header+hdrsofar) = ' ';\r
478 -               strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);\r
479 -               g_free (decoded_value);\r
480 -               g_hash_table_insert (message->headers, header, combined_header);\r
481 -           }\r
482 -       } else {\r
483 -           if (header_sofar == NULL) {\r
484 -               /* Only insert if we don't have a value for this header, yet. */\r
485 -               g_hash_table_insert (message->headers, header, decoded_value);\r
486 -           } else {\r
487 -               free (header);\r
488 -               g_free (decoded_value);\r
489 -               decoded_value = header_sofar;\r
490 -           }\r
491 +       /* For historical reasons, we support single-message mboxes,\r
492 +        * but this behavior is likely to change in the future, so\r
493 +        * warn. */\r
494 +       if (! mbox_warning) {\r
495 +           mbox_warning = TRUE;\r
496 +           fprintf (stderr, "\\r
497 +Warning: %s is an mbox containing a single message,\n\\r
498 +likely caused by misconfigured mail delivery.  Support for single-message\n\\r
499 +mboxes is deprecated and may be removed in the future.\n", message->filename);\r
500         }\r
501 -       /* if we found a match we can bail - unless of course we are\r
502 -        * collecting all the Received: headers */\r
503 -       if (match && !is_received)\r
504 -           return decoded_value;\r
505      }\r
506  \r
507 -    if (message->parsing_finished) {\r
508 -        fclose (message->file);\r
509 -        message->file = NULL;\r
510 -    }\r
511 +    message->parsed = TRUE;\r
512  \r
513 -    if (message->line)\r
514 -       free (message->line);\r
515 -    message->line = NULL;\r
516 +    return NOTMUCH_STATUS_SUCCESS;\r
517 +}\r
518  \r
519 -    if (message->value.size) {\r
520 -       free (message->value.str);\r
521 -       message->value.str = NULL;\r
522 -       message->value.size = 0;\r
523 -       message->value.len = 0;\r
524 -    }\r
525 +/* return NULL on errors, empty string for non-existing headers */\r
526 +const char *\r
527 +notmuch_message_file_get_header (notmuch_message_file_t *message,\r
528 +                                const char *header)\r
529 +{\r
530 +    const char *value = NULL;\r
531 +    notmuch_bool_t found;\r
532  \r
533 -    /* For the Received: header we actually might end up here even\r
534 -     * though we found the header (as we force continued parsing\r
535 -     * in that case). So let's check if that's the header we were\r
536 -     * looking for and return the value that we found (if any)\r
537 -     */\r
538 -    if (is_received)\r
539 -       return (char *)g_hash_table_lookup (message->headers, "received");\r
540 -\r
541 -    /* We've parsed all headers and never found the one we're looking\r
542 -     * for. It's probably just not there, but let's check that we\r
543 -     * didn't make a mistake preventing us from seeing it. */\r
544 -    if (message->restrict_headers && header_desired &&\r
545 -       ! g_hash_table_lookup_extended (message->headers,\r
546 -                                       header_desired, NULL, NULL))\r
547 -    {\r
548 -       INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n"\r
549 -                       "included in call to notmuch_message_file_restrict_headers\n",\r
550 -                       header_desired);\r
551 -    }\r
552 +    /* the caller shouldn't ask for headers before parsing */\r
553 +    if (! message->parsed)\r
554 +       return NULL;\r
555 +\r
556 +    found = g_hash_table_lookup_extended (message->headers, header,\r
557 +                                         NULL, (gpointer *) &value);\r
558 +\r
559 +    /* the caller shouldn't ask for non-restricted headers */\r
560 +    if (! found && message->restrict_headers)\r
561 +       return NULL;\r
562  \r
563 -    return "";\r
564 +    return value ? value : "";\r
565  }\r
566 diff --git a/lib/message.cc b/lib/message.cc\r
567 index 1b46379..1c7b91f 100644\r
568 --- a/lib/message.cc\r
569 +++ b/lib/message.cc\r
570 @@ -407,6 +407,12 @@ _notmuch_message_ensure_message_file (notmuch_message_t *message)\r
571         return;\r
572  \r
573      message->message_file = _notmuch_message_file_open_ctx (message, filename);\r
574 +\r
575 +    /* XXX: better return value handling */\r
576 +    if (notmuch_message_file_parse (message->message_file)) {\r
577 +       notmuch_message_file_close (message->message_file);\r
578 +       message->message_file = NULL;\r
579 +    }\r
580  }\r
581  \r
582  const char *\r
583 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
584 index af185c7..7277df1 100644\r
585 --- a/lib/notmuch-private.h\r
586 +++ b/lib/notmuch-private.h\r
587 @@ -345,6 +345,10 @@ notmuch_message_file_open (const char *filename);\r
588  notmuch_message_file_t *\r
589  _notmuch_message_file_open_ctx (void *ctx, const char *filename);\r
590  \r
591 +/* Parse the message */\r
592 +notmuch_status_t\r
593 +notmuch_message_file_parse (notmuch_message_file_t *message);\r
594 +\r
595  /* Close a notmuch message previously opened with notmuch_message_open. */\r
596  void\r
597  notmuch_message_file_close (notmuch_message_file_t *message);\r
598 -- \r
599 1.8.4.2\r
600 \r