Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 7c / 7aab1dc8a834fd6e2cf9759315b92027a468ce
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 0BC74431FBD\r
6         for <notmuch@notmuchmail.org>; Sun, 23 Mar 2014 13:01:17 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id hYhSWsgNmu3J for <notmuch@notmuchmail.org>;\r
16         Sun, 23 Mar 2014 13:01:12 -0700 (PDT)\r
17 Received: from mail-la0-f50.google.com (mail-la0-f50.google.com\r
18         [209.85.215.50]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 725A1431FB6\r
21         for <notmuch@notmuchmail.org>; Sun, 23 Mar 2014 13:01:12 -0700 (PDT)\r
22 Received: by mail-la0-f50.google.com with SMTP id y1so3035870lam.37\r
23         for <notmuch@notmuchmail.org>; Sun, 23 Mar 2014 13:01:09 -0700 (PDT)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=1e100.net; s=20130820;\r
26         h=x-gm-message-state:from:to:cc:subject:date:message-id;\r
27         bh=ClJjw42cPaOslumCFOSj7OSy36SABXAZWztCRZC8pgs=;\r
28         b=VMQQ4/0LMsJR/XdXZByg3Gsb/rGdrO5dAfqD7J0fIB6R5PdWSvaHrKo1FvpfTYU21t\r
29         EpzKkgH66RZuTVwQynNcxk6BgLu/71pGlKhKGqLrL16Yi+k9u9HzE00ekaQYVltKRXeL\r
30         c+xGYgDo2GwzLHTOdoxlWv8vARxZFIOyam5wO5ccBW9J3DzI0UCSWFVM/GKgubgNYcut\r
31         fzLo/3ifjqStY//pCb4cVSoDle3qssuIPt1sn8O4A4iZ6Uiz+LlvzZbwPIS78G7iAz1M\r
32         6pRWyKTKxTc0JiJh5jELdVy0CfHPZiribZGYqB06BNhQrsWyd3msx4B0wfmUSYMM1Vuz\r
33         aRsA==\r
34 X-Gm-Message-State:\r
35  ALoCoQn/1UI11XYEPxKi8VR9rbL4lNFLw71jeN0Pi3XD/DjHfOjLnZZUlxn69DoS9erG7309Fv+/\r
36 X-Received: by 10.153.4.134 with SMTP id ce6mr42628249lad.21.1395604869655;\r
37         Sun, 23 Mar 2014 13:01:09 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
39         [88.195.111.91])\r
40         by mx.google.com with ESMTPSA id q6sm11409334lal.3.2014.03.23.13.01.07\r
41         for <multiple recipients>\r
42         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
43         Sun, 23 Mar 2014 13:01:08 -0700 (PDT)\r
44 From: Jani Nikula <jani@nikula.org>\r
45 To: notmuch@notmuchmail.org\r
46 Subject: [PATCH v4] lib: replace the header parser with gmime\r
47 Date: Sun, 23 Mar 2014 22:01:06 +0200\r
48 Message-Id: <1395604866-19188-1-git-send-email-jani@nikula.org>\r
49 X-Mailer: git-send-email 1.9.0\r
50 X-BeenThere: notmuch@notmuchmail.org\r
51 X-Mailman-Version: 2.1.13\r
52 Precedence: list\r
53 List-Id: "Use and development of the notmuch mail system."\r
54         <notmuch.notmuchmail.org>\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
58 List-Post: <mailto:notmuch@notmuchmail.org>\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
62 X-List-Received-Date: Sun, 23 Mar 2014 20:01:17 -0000\r
63 \r
64 The notmuch library includes a full blown message header parser. Yet\r
65 the same message headers are parsed by gmime during indexing. Switch\r
66 to gmime parsing completely.\r
67 \r
68 These are the main changes:\r
69 \r
70 * Gmime stops header parsing at the first invalid header, and presumes\r
71   the message body starts from there. The current parser is quite\r
72   liberal in accepting broken headers. The change means we will be\r
73   much pickier about accepting invalid messages.\r
74 \r
75 * The current parser converts tabs used in header folding to\r
76   spaces. Gmime preserve the tabs. Due to a broken python library used\r
77   in mailman, there are plenty of mailing lists that produce headers\r
78   with tabs in header folding, and we'll see plenty of tabs. (This\r
79   change has been mitigated in preparatory patches.)\r
80 \r
81 * For pure header parsing, the current parser is likely faster than\r
82   gmime, which parses the whole message rather than just the\r
83   headers. Since we parse the message and its headers using gmime for\r
84   indexing anyway, this avoids and extra header parsing round when\r
85   adding new messages. In case of duplicate messages, we'll end up\r
86   parsing the full message although just headers would be\r
87   sufficient. All in all this should still speed up 'notmuch new'.\r
88 \r
89 * Calls to notmuch_message_get_header() may be slightly slower than\r
90   previously for headers that are not indexed in the database, due to\r
91   parsing of the whole message. Within the notmuch code base, notmuch\r
92   reply is the only such user.\r
93 \r
94 ---\r
95 \r
96 This is v4 of patches [1] and [2], combining them into one. Patch [3]\r
97 is required to make all the tests pass.\r
98 \r
99 The implementation has been changed considerably. Notably we only\r
100 cache the decoded headers to a hash table if and when they are\r
101 needed. The main reasons for doing caching at all is that gmime\r
102 provides raw headers that need decoding, and to minimize changes\r
103 outside of message-file.c we want to own and track the allocated\r
104 strings instead of pushing the responsibility to callers.\r
105 \r
106 This addresses most comments from Austin.\r
107 \r
108 The diff in message-file.c is rather confusing due to a mix of\r
109 added/removed lines. Looking at the patched file is much more\r
110 pleasant.\r
111 \r
112 BR,\r
113 Jani.\r
114 \r
115 [1] id:bdef45dc21c777130a7ae1fa650f172d8c6eaaf4.1391456555.git.jani@nikula.org\r
116 [2] id:31d785c4a3e4b90862a0fdc545d4e900a4c898e2.1391456555.git.jani@nikula.org\r
117 [3] id:1395247490-19892-1-git-send-email-jani@nikula.org\r
118 ---\r
119  lib/database.cc       |  15 +-\r
120  lib/index.cc          |  70 +--------\r
121  lib/message-file.c    | 420 ++++++++++++++++++++------------------------------\r
122  lib/notmuch-private.h |  54 +++----\r
123  4 files changed, 208 insertions(+), 351 deletions(-)\r
124 \r
125 diff --git a/lib/database.cc b/lib/database.cc\r
126 index aef748f75b5d..4750f40cf0fb 100644\r
127 --- a/lib/database.cc\r
128 +++ b/lib/database.cc\r
129 @@ -1971,15 +1971,10 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
130      if (ret)\r
131         goto DONE;\r
132  \r
133 -    notmuch_message_file_restrict_headers (message_file,\r
134 -                                          "date",\r
135 -                                          "from",\r
136 -                                          "in-reply-to",\r
137 -                                          "message-id",\r
138 -                                          "references",\r
139 -                                          "subject",\r
140 -                                          "to",\r
141 -                                          (char *) NULL);\r
142 +    /* Parse message up front to get better error status. */\r
143 +    ret = notmuch_message_file_parse (message_file);\r
144 +    if (ret)\r
145 +       goto DONE;\r
146  \r
147      try {\r
148         /* Before we do any real work, (especially before doing a\r
149 @@ -2066,7 +2061,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
150             date = notmuch_message_file_get_header (message_file, "date");\r
151             _notmuch_message_set_header_values (message, date, from, subject);\r
152  \r
153 -           ret = _notmuch_message_index_file (message, filename);\r
154 +           ret = _notmuch_message_index_file (message, message_file);\r
155             if (ret)\r
156                 goto DONE;\r
157         } else {\r
158 diff --git a/lib/index.cc b/lib/index.cc\r
159 index 78c18cf36d10..46a019325454 100644\r
160 --- a/lib/index.cc\r
161 +++ b/lib/index.cc\r
162 @@ -425,63 +425,15 @@ _index_mime_part (notmuch_message_t *message,\r
163  \r
164  notmuch_status_t\r
165  _notmuch_message_index_file (notmuch_message_t *message,\r
166 -                            const char *filename)\r
167 +                            notmuch_message_file_t *message_file)\r
168  {\r
169 -    GMimeStream *stream = NULL;\r
170 -    GMimeParser *parser = NULL;\r
171 -    GMimeMessage *mime_message = NULL;\r
172 +    GMimeMessage *mime_message;\r
173      InternetAddressList *addresses;\r
174 -    FILE *file = NULL;\r
175      const char *from, *subject;\r
176 -    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
177 -    static int initialized = 0;\r
178 -    char from_buf[5];\r
179 -    bool is_mbox = false;\r
180 -    static bool mbox_warning = false;\r
181 -\r
182 -    if (! initialized) {\r
183 -       g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
184 -       initialized = 1;\r
185 -    }\r
186 -\r
187 -    file = fopen (filename, "r");\r
188 -    if (! file) {\r
189 -       fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
190 -       ret = NOTMUCH_STATUS_FILE_ERROR;\r
191 -       goto DONE;\r
192 -    }\r
193 -\r
194 -    /* Is this mbox? */\r
195 -    if (fread (from_buf, sizeof (from_buf), 1, file) == 1 &&\r
196 -       strncmp (from_buf, "From ", 5) == 0)\r
197 -       is_mbox = true;\r
198 -    rewind (file);\r
199  \r
200 -    /* Evil GMime steals my FILE* here so I won't fclose it. */\r
201 -    stream = g_mime_stream_file_new (file);\r
202 -\r
203 -    parser = g_mime_parser_new_with_stream (stream);\r
204 -    g_mime_parser_set_scan_from (parser, is_mbox);\r
205 -\r
206 -    mime_message = g_mime_parser_construct_message (parser);\r
207 -\r
208 -    if (is_mbox) {\r
209 -       if (!g_mime_parser_eos (parser)) {\r
210 -           /* This is a multi-message mbox. */\r
211 -           ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
212 -           goto DONE;\r
213 -       }\r
214 -       /* For historical reasons, we support single-message mboxes,\r
215 -        * but this behavior is likely to change in the future, so\r
216 -        * warn. */\r
217 -       if (!mbox_warning) {\r
218 -           mbox_warning = true;\r
219 -           fprintf (stderr, "\\r
220 -Warning: %s is an mbox containing a single message,\n\\r
221 -likely caused by misconfigured mail delivery.  Support for single-message\n\\r
222 -mboxes is deprecated and may be removed in the future.\n", filename);\r
223 -       }\r
224 -    }\r
225 +    mime_message = notmuch_message_file_get_mime_message (message_file);\r
226 +    if (! mime_message)\r
227 +       return NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
228  \r
229      from = g_mime_message_get_sender (mime_message);\r
230  \r
231 @@ -502,15 +454,5 @@ mboxes is deprecated and may be removed in the future.\n", filename);\r
232  \r
233      _index_mime_part (message, g_mime_message_get_mime_part (mime_message));\r
234  \r
235 -  DONE:\r
236 -    if (mime_message)\r
237 -       g_object_unref (mime_message);\r
238 -\r
239 -    if (parser)\r
240 -       g_object_unref (parser);\r
241 -\r
242 -    if (stream)\r
243 -       g_object_unref (stream);\r
244 -\r
245 -    return ret;\r
246 +    return NOTMUCH_STATUS_SUCCESS;\r
247  }\r
248 diff --git a/lib/message-file.c b/lib/message-file.c\r
249 index a2850c278b5a..88662608d319 100644\r
250 --- a/lib/message-file.c\r
251 +++ b/lib/message-file.c\r
252 @@ -26,30 +26,15 @@\r
253  \r
254  #include <glib.h> /* GHashTable */\r
255  \r
256 -typedef struct {\r
257 -    char *str;\r
258 -    size_t size;\r
259 -    size_t len;\r
260 -} header_value_closure_t;\r
261 -\r
262  struct _notmuch_message_file {\r
263      /* File object */\r
264      FILE *file;\r
265 +    char *filename;\r
266  \r
267 -    /* Header storage */\r
268 -    int restrict_headers;\r
269 +    /* Cache for decoded headers */\r
270      GHashTable *headers;\r
271 -    int broken_headers;\r
272 -    int good_headers;\r
273 -    size_t header_size; /* Length of full message header in bytes. */\r
274 -\r
275 -    /* Parsing state */\r
276 -    char *line;\r
277 -    size_t line_size;\r
278 -    header_value_closure_t value;\r
279  \r
280 -    int parsing_started;\r
281 -    int parsing_finished;\r
282 +    GMimeMessage *message;\r
283  };\r
284  \r
285  static int\r
286 @@ -76,15 +61,12 @@ strcase_hash (const void *ptr)\r
287  static int\r
288  _notmuch_message_file_destructor (notmuch_message_file_t *message)\r
289  {\r
290 -    if (message->line)\r
291 -       free (message->line);\r
292 -\r
293 -    if (message->value.size)\r
294 -       free (message->value.str);\r
295 -\r
296      if (message->headers)\r
297         g_hash_table_destroy (message->headers);\r
298  \r
299 +    if (message->message)\r
300 +       g_object_unref (message->message);\r
301 +\r
302      if (message->file)\r
303         fclose (message->file);\r
304  \r
305 @@ -102,20 +84,17 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename)\r
306      if (unlikely (message == NULL))\r
307         return NULL;\r
308  \r
309 +    /* Only needed for error messages during parsing. */\r
310 +    message->filename = talloc_strdup (message, filename);\r
311 +    if (message->filename == NULL)\r
312 +       goto FAIL;\r
313 +\r
314      talloc_set_destructor (message, _notmuch_message_file_destructor);\r
315  \r
316      message->file = fopen (filename, "r");\r
317      if (message->file == NULL)\r
318         goto FAIL;\r
319  \r
320 -    message->headers = g_hash_table_new_full (strcase_hash,\r
321 -                                             strcase_equal,\r
322 -                                             free,\r
323 -                                             g_free);\r
324 -\r
325 -    message->parsing_started = 0;\r
326 -    message->parsing_finished = 0;\r
327 -\r
328      return message;\r
329  \r
330    FAIL:\r
331 @@ -137,264 +116,205 @@ notmuch_message_file_close (notmuch_message_file_t *message)\r
332      talloc_free (message);\r
333  }\r
334  \r
335 -void\r
336 -notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
337 -                                       va_list va_headers)\r
338 +notmuch_status_t\r
339 +notmuch_message_file_parse (notmuch_message_file_t *message)\r
340  {\r
341 -    char *header;\r
342 +    GMimeStream *stream;\r
343 +    GMimeParser *parser;\r
344 +    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
345 +    static int initialized = 0;\r
346 +    char from_buf[5];\r
347 +    notmuch_bool_t is_mbox = FALSE;\r
348 +    static notmuch_bool_t mbox_warning = FALSE;\r
349  \r
350 -    if (message->parsing_started)\r
351 -       INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started");\r
352 +    if (message->message)\r
353 +       return NOTMUCH_STATUS_SUCCESS;\r
354  \r
355 -    while (1) {\r
356 -       header = va_arg (va_headers, char*);\r
357 -       if (header == NULL)\r
358 -           break;\r
359 -       g_hash_table_insert (message->headers,\r
360 -                            xstrdup (header), NULL);\r
361 +    if (! initialized) {\r
362 +       g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
363 +       initialized = 1;\r
364      }\r
365  \r
366 -    message->restrict_headers = 1;\r
367 -}\r
368 -\r
369 -void\r
370 -notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...)\r
371 -{\r
372 -    va_list va_headers;\r
373 -\r
374 -    va_start (va_headers, message);\r
375 +    message->headers = g_hash_table_new_full (strcase_hash, strcase_equal,\r
376 +                                             free, g_free);\r
377 +    if (! message->headers)\r
378 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
379  \r
380 -    notmuch_message_file_restrict_headersv (message, va_headers);\r
381 -}\r
382 +    /* Is this mbox? */\r
383 +    if (fread (from_buf, sizeof (from_buf), 1, message->file) == 1 &&\r
384 +       strncmp (from_buf, "From ", 5) == 0)\r
385 +       is_mbox = TRUE;\r
386 +    rewind (message->file);\r
387  \r
388 -static void\r
389 -copy_header_unfolding (header_value_closure_t *value,\r
390 -                      const char *chunk)\r
391 -{\r
392 -    char *last;\r
393 +    stream = g_mime_stream_file_new (message->file);\r
394  \r
395 -    if (chunk == NULL)\r
396 -       return;\r
397 +    /* We'll own and fclose the FILE* ourselves. */\r
398 +    g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);\r
399  \r
400 -    while (*chunk == ' ' || *chunk == '\t')\r
401 -       chunk++;\r
402 +    parser = g_mime_parser_new_with_stream (stream);\r
403 +    g_mime_parser_set_scan_from (parser, is_mbox);\r
404  \r
405 -    if (value->len + 1 + strlen (chunk) + 1 > value->size) {\r
406 -       unsigned int new_size = value->size;\r
407 -       if (value->size == 0)\r
408 -           new_size = strlen (chunk) + 1;\r
409 -       else\r
410 -           while (value->len + 1 + strlen (chunk) + 1 > new_size)\r
411 -               new_size *= 2;\r
412 -       value->str = xrealloc (value->str, new_size);\r
413 -       value->size = new_size;\r
414 +    message->message = g_mime_parser_construct_message (parser);\r
415 +    if (! message->message) {\r
416 +       status = NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
417 +       goto DONE;\r
418      }\r
419  \r
420 -    last = value->str + value->len;\r
421 -    if (value->len) {\r
422 -       *last = ' ';\r
423 -       last++;\r
424 -       value->len++;\r
425 +    if (is_mbox) {\r
426 +       if (! g_mime_parser_eos (parser)) {\r
427 +           /* This is a multi-message mbox. */\r
428 +           status = NOTMUCH_STATUS_FILE_NOT_EMAIL;\r
429 +           goto DONE;\r
430 +       }\r
431 +       /*\r
432 +        * For historical reasons, we support single-message mboxes,\r
433 +        * but this behavior is likely to change in the future, so\r
434 +        * warn.\r
435 +        */\r
436 +       if (! mbox_warning) {\r
437 +           mbox_warning = TRUE;\r
438 +           fprintf (stderr, "\\r
439 +Warning: %s is an mbox containing a single message,\n\\r
440 +likely caused by misconfigured mail delivery.  Support for single-message\n\\r
441 +mboxes is deprecated and may be removed in the future.\n", message->filename);\r
442 +       }\r
443      }\r
444  \r
445 -    strcpy (last, chunk);\r
446 -    value->len += strlen (chunk);\r
447 +  DONE:\r
448 +    g_object_unref (stream);\r
449 +    g_object_unref (parser);\r
450 +\r
451 +    if (status) {\r
452 +       g_hash_table_destroy (message->headers);\r
453 +       message->headers = NULL;\r
454  \r
455 -    last = value->str + value->len - 1;\r
456 -    if (*last == '\n') {\r
457 -       *last = '\0';\r
458 -       value->len--;\r
459 +       if (message->message) {\r
460 +           g_object_unref (message->message);\r
461 +           message->message = NULL;\r
462 +       }\r
463 +\r
464 +       rewind (message->file);\r
465      }\r
466 +\r
467 +    return status;\r
468  }\r
469  \r
470 -/* As a special-case, a value of NULL for header_desired will force\r
471 - * the entire header to be parsed if it is not parsed already. This is\r
472 - * used by the _notmuch_message_file_get_headers_end function.\r
473 - * Another special case is the Received: header. For this header we\r
474 - * want to concatenate all instances of the header instead of just\r
475 - * hashing the first instance as we use this when analyzing the path\r
476 - * the mail has taken from sender to recipient.\r
477 - */\r
478 -const char *\r
479 -notmuch_message_file_get_header (notmuch_message_file_t *message,\r
480 -                                const char *header_desired)\r
481 +GMimeMessage *\r
482 +notmuch_message_file_get_mime_message (notmuch_message_file_t *message)\r
483  {\r
484 -    int contains;\r
485 -    char *header, *decoded_value, *header_sofar, *combined_header;\r
486 -    const char *s, *colon;\r
487 -    int match, newhdr, hdrsofar, is_received;\r
488 -    static int initialized = 0;\r
489 +    if (notmuch_message_file_parse (message))\r
490 +       return NULL;\r
491  \r
492 -    is_received = (strcmp(header_desired,"received") == 0);\r
493 +    return message->message;\r
494 +}\r
495  \r
496 -    if (! initialized) {\r
497 -       g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);\r
498 -       initialized = 1;\r
499 -    }\r
500 +/*\r
501 + * Get all instances of a header decoded and concatenated.\r
502 + *\r
503 + * The result must be freed using g_free().\r
504 + *\r
505 + * Return NULL on errors, empty string for non-existing headers.\r
506 + */\r
507 +static char *\r
508 +_notmuch_message_file_get_combined_header (notmuch_message_file_t *message,\r
509 +                                          const char *header)\r
510 +{\r
511 +    GMimeHeaderList *headers;\r
512 +    GMimeHeaderIter *iter;\r
513 +    char *combined = NULL;\r
514  \r
515 -    message->parsing_started = 1;\r
516 -\r
517 -    if (header_desired == NULL)\r
518 -       contains = 0;\r
519 -    else\r
520 -       contains = g_hash_table_lookup_extended (message->headers,\r
521 -                                                header_desired, NULL,\r
522 -                                                (gpointer *) &decoded_value);\r
523 -\r
524 -    if (contains && decoded_value)\r
525 -       return decoded_value;\r
526 -\r
527 -    if (message->parsing_finished)\r
528 -       return "";\r
529 -\r
530 -#define NEXT_HEADER_LINE(closure)                              \\r
531 -    while (1) {                                                        \\r
532 -       ssize_t bytes_read = getline (&message->line,           \\r
533 -                                     &message->line_size,      \\r
534 -                                     message->file);           \\r
535 -       if (bytes_read == -1) {                                 \\r
536 -           message->parsing_finished = 1;                      \\r
537 -           break;                                              \\r
538 -       }                                                       \\r
539 -       if (*message->line == '\n') {                           \\r
540 -           message->parsing_finished = 1;                      \\r
541 -           break;                                              \\r
542 -       }                                                       \\r
543 -       if (closure &&                                          \\r
544 -           (*message->line == ' ' || *message->line == '\t'))  \\r
545 -       {                                                       \\r
546 -           copy_header_unfolding ((closure), message->line);   \\r
547 -       }                                                       \\r
548 -       if (*message->line == ' ' || *message->line == '\t')    \\r
549 -           message->header_size += strlen (message->line);     \\r
550 -       else                                                    \\r
551 -           break;                                              \\r
552 -    }\r
553 +    headers = g_mime_object_get_header_list (GMIME_OBJECT (message->message));\r
554 +    if (! headers)\r
555 +       return NULL;\r
556  \r
557 -    if (message->line == NULL)\r
558 -       NEXT_HEADER_LINE (NULL);\r
559 +    iter = g_mime_header_iter_new ();\r
560 +    if (! iter)\r
561 +       return NULL;\r
562  \r
563 -    while (1) {\r
564 +    if (! g_mime_header_list_get_iter (headers, iter))\r
565 +       goto DONE;\r
566  \r
567 -       if (message->parsing_finished)\r
568 -           break;\r
569 +    do {\r
570 +    const char *value;\r
571 +       char *decoded;\r
572  \r
573 -       colon = strchr (message->line, ':');\r
574 +       if (strcasecmp (g_mime_header_iter_get_name (iter), header) != 0)\r
575 +           continue;\r
576  \r
577 -       if (colon == NULL) {\r
578 -           message->broken_headers++;\r
579 -           /* A simple heuristic for giving up on things that just\r
580 -            * don't look like mail messages. */\r
581 -           if (message->broken_headers >= 10 &&\r
582 -               message->good_headers < 5)\r
583 -           {\r
584 -               message->parsing_finished = 1;\r
585 -               break;\r
586 +       value = g_mime_header_iter_get_value (iter);\r
587 +       decoded = g_mime_utils_header_decode_text (value);\r
588 +       if (! decoded) {\r
589 +           if (combined) {\r
590 +               g_free (combined);\r
591 +               combined = NULL;\r
592             }\r
593 -           NEXT_HEADER_LINE (NULL);\r
594 -           continue;\r
595 +           goto DONE;\r
596         }\r
597  \r
598 -       message->header_size += strlen (message->line);\r
599 +       if (combined) {\r
600 +           char *tmp = g_strdup_printf ("%s %s", combined, decoded);\r
601 +           g_free (decoded);\r
602 +           g_free (combined);\r
603 +           if (! tmp) {\r
604 +               combined = NULL;\r
605 +               goto DONE;\r
606 +           }\r
607  \r
608 -       message->good_headers++;\r
609 +           combined = tmp;\r
610 +       } else {\r
611 +           combined = decoded;\r
612 +       }\r
613 +    } while (g_mime_header_iter_next (iter));\r
614  \r
615 -       header = xstrndup (message->line, colon - message->line);\r
616 +    /* Return empty string for non-existing headers. */\r
617 +    if (! combined)\r
618 +       combined = g_strdup ("");\r
619  \r
620 -       if (message->restrict_headers &&\r
621 -           ! g_hash_table_lookup_extended (message->headers,\r
622 -                                           header, NULL, NULL))\r
623 -       {\r
624 -           free (header);\r
625 -           NEXT_HEADER_LINE (NULL);\r
626 -           continue;\r
627 -       }\r
628 +  DONE:\r
629 +    g_mime_header_iter_free (iter);\r
630  \r
631 -       s = colon + 1;\r
632 -       while (*s == ' ' || *s == '\t')\r
633 -           s++;\r
634 +    return combined;\r
635 +}\r
636  \r
637 -       message->value.len = 0;\r
638 -       copy_header_unfolding (&message->value, s);\r
639 +/* Return NULL on errors, empty string for non-existing headers. */\r
640 +const char *\r
641 +notmuch_message_file_get_header (notmuch_message_file_t *message,\r
642 +                                const char *header)\r
643 +{\r
644 +    const char *value = NULL;\r
645 +    char *decoded;\r
646 +    notmuch_bool_t found;\r
647  \r
648 -       NEXT_HEADER_LINE (&message->value);\r
649 +    if (notmuch_message_file_parse (message))\r
650 +       return NULL;\r
651  \r
652 -       if (header_desired == NULL)\r
653 -           match = 0;\r
654 +    /* If we have a cached decoded value, use it. */\r
655 +    found = g_hash_table_lookup_extended (message->headers, header,\r
656 +                                         NULL, (gpointer *) &value);\r
657 +    if (found)\r
658 +       return value ? value : "";\r
659 +\r
660 +    if (strcasecmp (header, "received") == 0) {\r
661 +       /*\r
662 +        * The Received: header is special. We concatenate all\r
663 +        * instances of the header as we use this when analyzing the\r
664 +        * path the mail has taken from sender to recipient.\r
665 +        */\r
666 +       decoded = _notmuch_message_file_get_combined_header (message, header);\r
667 +    } else {\r
668 +       value = g_mime_object_get_header (GMIME_OBJECT (message->message),\r
669 +                                         header);\r
670 +       if (value)\r
671 +           decoded = g_mime_utils_header_decode_text (value);\r
672         else\r
673 -           match = (strcasecmp (header, header_desired) == 0);\r
674 -\r
675 -       decoded_value = g_mime_utils_header_decode_text (message->value.str);\r
676 -       header_sofar = (char *)g_hash_table_lookup (message->headers, header);\r
677 -       /* we treat the Received: header special - we want to concat ALL of \r
678 -        * the Received: headers we encounter.\r
679 -        * for everything else we return the first instance of a header */\r
680 -       if (strcasecmp(header, "received") == 0) {\r
681 -           if (header_sofar == NULL) {\r
682 -               /* first Received: header we encountered; just add it */\r
683 -               g_hash_table_insert (message->headers, header, decoded_value);\r
684 -           } else {\r
685 -               /* we need to add the header to those we already collected */\r
686 -               newhdr = strlen(decoded_value);\r
687 -               hdrsofar = strlen(header_sofar);\r
688 -               combined_header = g_malloc(hdrsofar + newhdr + 2);\r
689 -               strncpy(combined_header,header_sofar,hdrsofar);\r
690 -               *(combined_header+hdrsofar) = ' ';\r
691 -               strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);\r
692 -               g_free (decoded_value);\r
693 -               g_hash_table_insert (message->headers, header, combined_header);\r
694 -           }\r
695 -       } else {\r
696 -           if (header_sofar == NULL) {\r
697 -               /* Only insert if we don't have a value for this header, yet. */\r
698 -               g_hash_table_insert (message->headers, header, decoded_value);\r
699 -           } else {\r
700 -               free (header);\r
701 -               g_free (decoded_value);\r
702 -               decoded_value = header_sofar;\r
703 -           }\r
704 -       }\r
705 -       /* if we found a match we can bail - unless of course we are\r
706 -        * collecting all the Received: headers */\r
707 -       if (match && !is_received)\r
708 -           return decoded_value;\r
709 -    }\r
710 -\r
711 -    if (message->parsing_finished) {\r
712 -        fclose (message->file);\r
713 -        message->file = NULL;\r
714 +           decoded = g_strdup ("");\r
715      }\r
716  \r
717 -    if (message->line)\r
718 -       free (message->line);\r
719 -    message->line = NULL;\r
720 -\r
721 -    if (message->value.size) {\r
722 -       free (message->value.str);\r
723 -       message->value.str = NULL;\r
724 -       message->value.size = 0;\r
725 -       message->value.len = 0;\r
726 -    }\r
727 +    if (! decoded)\r
728 +       return NULL;\r
729  \r
730 -    /* For the Received: header we actually might end up here even\r
731 -     * though we found the header (as we force continued parsing\r
732 -     * in that case). So let's check if that's the header we were\r
733 -     * looking for and return the value that we found (if any)\r
734 -     */\r
735 -    if (is_received)\r
736 -       return (char *)g_hash_table_lookup (message->headers, "received");\r
737 -\r
738 -    /* We've parsed all headers and never found the one we're looking\r
739 -     * for. It's probably just not there, but let's check that we\r
740 -     * didn't make a mistake preventing us from seeing it. */\r
741 -    if (message->restrict_headers && header_desired &&\r
742 -       ! g_hash_table_lookup_extended (message->headers,\r
743 -                                       header_desired, NULL, NULL))\r
744 -    {\r
745 -       INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n"\r
746 -                       "included in call to notmuch_message_file_restrict_headers\n",\r
747 -                       header_desired);\r
748 -    }\r
749 +    /* Cache the decoded value. We also own the strings. */\r
750 +    g_hash_table_insert (message->headers, xstrdup (header), decoded);\r
751  \r
752 -    return "";\r
753 +    return decoded;\r
754  }\r
755 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
756 index 59eb2bc285a5..734a4e338554 100644\r
757 --- a/lib/notmuch-private.h\r
758 +++ b/lib/notmuch-private.h\r
759 @@ -46,6 +46,8 @@ NOTMUCH_BEGIN_DECLS\r
760  \r
761  #include <talloc.h>\r
762  \r
763 +#include <gmime/gmime.h>\r
764 +\r
765  #include "xutil.h"\r
766  #include "error_util.h"\r
767  \r
768 @@ -320,13 +322,6 @@ notmuch_message_set_author (notmuch_message_t *message, const char *author);\r
769  const char *\r
770  notmuch_message_get_author (notmuch_message_t *message);\r
771  \r
772 -\r
773 -/* index.cc */\r
774 -\r
775 -notmuch_status_t\r
776 -_notmuch_message_index_file (notmuch_message_t *message,\r
777 -                            const char *filename);\r
778 -\r
779  /* message-file.c */\r
780  \r
781  /* XXX: I haven't decided yet whether these will actually get exported\r
782 @@ -352,32 +347,31 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename);\r
783  void\r
784  notmuch_message_file_close (notmuch_message_file_t *message);\r
785  \r
786 -/* Restrict 'message' to only save the named headers.\r
787 +/* Parse the message.\r
788   *\r
789 - * When the caller is only interested in a short list of headers,\r
790 - * known in advance, calling this function can avoid wasted time and\r
791 - * memory parsing/saving header values that will never be needed.\r
792 + * This will be done automatically as necessary on other calls\r
793 + * depending on it, but an explicit call allows for better error\r
794 + * status reporting.\r
795 + */\r
796 +notmuch_status_t\r
797 +notmuch_message_file_parse (notmuch_message_file_t *message);\r
798 +\r
799 +/* Get the gmime message of a message file.\r
800   *\r
801 - * The variable arguments should be a list of const char * with a\r
802 - * final '(const char *) NULL' to terminate the list.\r
803 + * The message file is parsed as necessary.\r
804   *\r
805 - * If this function is called, it must be called before any calls to\r
806 - * notmuch_message_get_header for this message.\r
807 + * Returns GMimeMessage* on success (which the caller must not unref),\r
808 + * NULL if the message file parsing fails.\r
809   *\r
810 - * After calling this function, if notmuch_message_get_header is\r
811 - * called with a header name not in this list, then NULL will be\r
812 - * returned even if that header exists in the actual message.\r
813 + * XXX: Would be nice to not have to expose GMimeMessage here.\r
814   */\r
815 -void\r
816 -notmuch_message_file_restrict_headers (notmuch_message_file_t *message, ...);\r
817 -\r
818 -/* Identical to notmuch_message_restrict_headers but accepting a va_list. */\r
819 -void\r
820 -notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
821 -                                       va_list va_headers);\r
822 +GMimeMessage *\r
823 +notmuch_message_file_get_mime_message (notmuch_message_file_t *message);\r
824  \r
825  /* Get the value of the specified header from the message as a UTF-8 string.\r
826   *\r
827 + * The message file is parsed as necessary.\r
828 + *\r
829   * The header name is case insensitive.\r
830   *\r
831   * The Received: header is special - for it all Received: headers in\r
832 @@ -387,13 +381,19 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
833   * only until the message is closed. The caller should copy it if\r
834   * needing to modify the value or to hold onto it for longer.\r
835   *\r
836 - * Returns NULL if the message does not contain a header line matching\r
837 - * 'header'.\r
838 + * Returns NULL on errors, empty string if the message does not\r
839 + * contain a header line matching 'header'.\r
840   */\r
841  const char *\r
842  notmuch_message_file_get_header (notmuch_message_file_t *message,\r
843                                  const char *header);\r
844  \r
845 +/* index.cc */\r
846 +\r
847 +notmuch_status_t\r
848 +_notmuch_message_index_file (notmuch_message_t *message,\r
849 +                            notmuch_message_file_t *message_file);\r
850 +\r
851  /* messages.c */\r
852  \r
853  typedef struct _notmuch_message_node {\r
854 -- \r
855 1.9.0\r
856 \r