]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
Merge tag 'debian/0.29.3-1'
[notmuch] / lib / index.cc
1 /*
2  * Copyright © 2009 Carl Worth
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see https://www.gnu.org/licenses/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #include "notmuch-private.h"
21
22 #include <gmime/gmime.h>
23 #include <gmime/gmime-filter.h>
24
25 #include <xapian.h>
26
27
28 typedef struct {
29     int state;
30     int a;
31     int b;
32     int next_if_match;
33     int next_if_not_match;
34 } scanner_state_t;
35
36 /* Simple, linear state-transition diagram for the uuencode filter.
37  *
38  * If the character being processed is within the range of [a, b]
39  * for the current state then we transition next_if_match
40  * state. If not, we transition to the next_if_not_match state.
41  *
42  * The final two states are special in that they are the states in
43  * which we discard data. */
44 static const int first_uuencode_skipping_state = 11;
45 static const scanner_state_t uuencode_states[] = {
46     { 0,  'b',  'b',  1,  0 },
47     { 1,  'e',  'e',  2,  0 },
48     { 2,  'g',  'g',  3,  0 },
49     { 3,  'i',  'i',  4,  0 },
50     { 4,  'n',  'n',  5,  0 },
51     { 5,  ' ',  ' ',  6,  0 },
52     { 6,  '0',  '7',  7,  0 },
53     { 7,  '0',  '7',  8,  0 },
54     { 8,  '0',  '7',  9,  0 },
55     { 9,  ' ',  ' ',  10, 0 },
56     { 10, '\n', '\n', 11, 10 },
57     { 11, 'M',  'M',  12, 0 },
58     { 12, ' ',  '`',  12, 11 }
59 };
60
61 /* The following table is intended to implement this DFA (in 'dot'
62  * format). Note that 2 and 3 are "hidden" states used to step through
63  * the possible out edges of state 1.
64  *
65  * digraph html_filter {
66  *     0 -> 1  [label="<"];
67  *     0 -> 0;
68  *     1 -> 4 [label="'"];
69  *     1 -> 5 [label="\""];
70  *     1 -> 0 [label=">"];
71  *     1 -> 1;
72  *     4 -> 1 [label="'"];
73  *     4 -> 4;
74  *     5 -> 1 [label="\""];
75  *     5 -> 5;
76  * }
77  */
78 static const int first_html_skipping_state = 1;
79 static const scanner_state_t html_states[] = {
80     { 0,  '<',  '<',  1,  0 },
81     { 1,  '\'', '\'', 4,  2 },  /* scanning for quote or > */
82     { 1,  '"',  '"',  5,  3 },
83     { 1,  '>',  '>',  0,  1 },
84     { 4,  '\'', '\'', 1,  4 },  /* inside single quotes */
85     { 5,  '"', '"',   1,  5 },  /* inside double quotes */
86 };
87
88 /* Oh, how I wish that gobject didn't require so much noisy boilerplate!
89  * (Though I have at least eliminated some of the stock set...) */
90 typedef struct _NotmuchFilterDiscardNonTerm NotmuchFilterDiscardNonTerm;
91 typedef struct _NotmuchFilterDiscardNonTermClass NotmuchFilterDiscardNonTermClass;
92
93 /**
94  * NotmuchFilterDiscardNonTerm:
95  *
96  * @parent_object: parent #GMimeFilter
97  * @encode: encoding vs decoding
98  * @state: State of the parser
99  *
100  * A filter to discard uuencoded portions of an email.
101  *
102  * A uuencoded portion is identified as beginning with a line
103  * matching:
104  *
105  *      begin [0-7][0-7][0-7] .*
106  *
107  * After that detection, and beginning with the following line,
108  * characters will be discarded as long as the first character of each
109  * line begins with M and subsequent characters on the line are within
110  * the range of ASCII characters from ' ' to '`'.
111  *
112  * This is not a perfect UUencode filter. It's possible to have a
113  * message that will legitimately match that pattern, (so that some
114  * legitimate content is discarded). And for most UUencoded files, the
115  * final line of encoded data (the line not starting with M) will be
116  * indexed.
117  **/
118 struct _NotmuchFilterDiscardNonTerm {
119     GMimeFilter parent_object;
120     GMimeContentType *content_type;
121     int state;
122     int first_skipping_state;
123     const scanner_state_t *states;
124 };
125
126 struct _NotmuchFilterDiscardNonTermClass {
127     GMimeFilterClass parent_class;
128 };
129
130 static GMimeFilter *notmuch_filter_discard_non_term_new (GMimeContentType *content);
131
132 static void notmuch_filter_discard_non_term_finalize (GObject *object);
133
134 static GMimeFilter *filter_copy (GMimeFilter *filter);
135 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
136                            char **out, size_t *outlen, size_t *outprespace);
137 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
138                              char **out, size_t *outlen, size_t *outprespace);
139 static void filter_reset (GMimeFilter *filter);
140
141
142 static GMimeFilterClass *parent_class = NULL;
143
144 static void
145 notmuch_filter_discard_non_term_class_init (NotmuchFilterDiscardNonTermClass *klass,
146                                             unused (void *class_data))
147 {
148     GObjectClass *object_class = G_OBJECT_CLASS (klass);
149     GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
150
151     parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
152
153     object_class->finalize = notmuch_filter_discard_non_term_finalize;
154
155     filter_class->copy = filter_copy;
156     filter_class->filter = filter_filter;
157     filter_class->complete = filter_complete;
158     filter_class->reset = filter_reset;
159 }
160
161 static void
162 notmuch_filter_discard_non_term_finalize (GObject *object)
163 {
164     G_OBJECT_CLASS (parent_class)->finalize (object);
165 }
166
167 static GMimeFilter *
168 filter_copy (GMimeFilter *gmime_filter)
169 {
170     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
171
172     return notmuch_filter_discard_non_term_new (filter->content_type);
173 }
174
175 static void
176 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
177                char **outbuf, size_t *outlen, size_t *outprespace)
178 {
179     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
180     const scanner_state_t *states = filter->states;
181     const char *inptr = inbuf;
182     const char *inend = inbuf + inlen;
183     char *outptr;
184
185     (void) prespace;
186
187     int next;
188
189     g_mime_filter_set_size (gmime_filter, inlen, false);
190     outptr = gmime_filter->outbuf;
191
192     next = filter->state;
193     while (inptr < inend) {
194         /* Each state is defined by a contiguous set of rows of the
195          * state table marked by a common value for '.state'. The
196          * state numbers must be equal to the index of the first row
197          * in a given state; thus the loop condition here looks for a
198          * jump to a first row of a state, which is a real transition
199          * in the underlying DFA.
200          */
201         do {
202             if (*inptr >= states[next].a && *inptr <= states[next].b) {
203                 next = states[next].next_if_match;
204             } else {
205                 next = states[next].next_if_not_match;
206             }
207
208         } while (next != states[next].state);
209
210         if (filter->state < filter->first_skipping_state)
211             *outptr++ = *inptr;
212
213         filter->state = next;
214         inptr++;
215     }
216
217     *outlen = outptr - gmime_filter->outbuf;
218     *outprespace = gmime_filter->outpre;
219     *outbuf = gmime_filter->outbuf;
220 }
221
222 static void
223 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
224                  char **outbuf, size_t *outlen, size_t *outprespace)
225 {
226     if (inbuf && inlen)
227         filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
228 }
229
230 static void
231 filter_reset (GMimeFilter *gmime_filter)
232 {
233     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
234
235     filter->state = 0;
236 }
237
238 /**
239  * notmuch_filter_discard_non_term_new:
240  *
241  * Returns: a new #NotmuchFilterDiscardNonTerm filter.
242  **/
243 static GMimeFilter *
244 notmuch_filter_discard_non_term_new (GMimeContentType *content_type)
245 {
246     static GType type = 0;
247     NotmuchFilterDiscardNonTerm *filter;
248
249     if (! type) {
250         static const GTypeInfo info = {
251             .class_size = sizeof (NotmuchFilterDiscardNonTermClass),
252             .base_init = NULL,
253             .base_finalize = NULL,
254             .class_init = (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
255             .class_finalize = NULL,
256             .class_data = NULL,
257             .instance_size = sizeof (NotmuchFilterDiscardNonTerm),
258             .n_preallocs = 0,
259             .instance_init = NULL,
260             .value_table = NULL,
261         };
262
263         type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
264     }
265
266     filter = (NotmuchFilterDiscardNonTerm *) g_object_new (type, NULL);
267     filter->content_type = content_type;
268     filter->state = 0;
269     if (g_mime_content_type_is_type (content_type, "text", "html")) {
270         filter->states = html_states;
271         filter->first_skipping_state = first_html_skipping_state;
272     } else {
273         filter->states = uuencode_states;
274         filter->first_skipping_state = first_uuencode_skipping_state;
275     }
276
277     return (GMimeFilter *) filter;
278 }
279
280 /* We're finally down to a single (NAME + address) email "mailbox". */
281 static void
282 _index_address_mailbox (notmuch_message_t *message,
283                         const char *prefix_name,
284                         InternetAddress *address)
285 {
286     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
287     const char *name, *addr, *combined;
288     void *local = talloc_new (message);
289
290     name = internet_address_get_name (address);
291     addr = internet_address_mailbox_get_addr (mailbox);
292
293     /* Combine the name and address and index them as a phrase. */
294     if (name && addr)
295         combined = talloc_asprintf (local, "%s %s", name, addr);
296     else if (name)
297         combined = name;
298     else
299         combined = addr;
300
301     if (combined)
302         _notmuch_message_gen_terms (message, prefix_name, combined);
303
304     talloc_free (local);
305 }
306
307 static void
308 _index_address_list (notmuch_message_t *message,
309                      const char *prefix_name,
310                      InternetAddressList *addresses);
311
312 /* The outer loop over the InternetAddressList wasn't quite enough.
313  * There can actually be a tree here where a single member of the list
314  * is a "group" containing another list. Recurse please.
315  */
316 static void
317 _index_address_group (notmuch_message_t *message,
318                       const char *prefix_name,
319                       InternetAddress *address)
320 {
321     InternetAddressGroup *group;
322     InternetAddressList *list;
323
324     group = INTERNET_ADDRESS_GROUP (address);
325     list = internet_address_group_get_members (group);
326
327     if (! list)
328         return;
329
330     _index_address_list (message, prefix_name, list);
331 }
332
333 static void
334 _index_address_list (notmuch_message_t *message,
335                      const char *prefix_name,
336                      InternetAddressList *addresses)
337 {
338     int i;
339     InternetAddress *address;
340
341     if (addresses == NULL)
342         return;
343
344     for (i = 0; i < internet_address_list_length (addresses); i++) {
345         address = internet_address_list_get_address (addresses, i);
346         if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
347             _index_address_mailbox (message, prefix_name, address);
348         } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
349             _index_address_group (message, prefix_name, address);
350         } else {
351             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
352         }
353     }
354 }
355
356 static void
357 _index_content_type (notmuch_message_t *message, GMimeObject *part)
358 {
359     GMimeContentType *content_type = g_mime_object_get_content_type (part);
360
361     if (content_type) {
362         char *mime_string = g_mime_content_type_get_mime_type (content_type);
363         if (mime_string) {
364             _notmuch_message_gen_terms (message, "mimetype", mime_string);
365             g_free (mime_string);
366         }
367     }
368 }
369
370 static void
371 _index_encrypted_mime_part (notmuch_message_t *message, notmuch_indexopts_t *indexopts,
372                             GMimeMultipartEncrypted *part,
373                             _notmuch_message_crypto_t *msg_crypto);
374
375 /* Callback to generate terms for each mime part of a message. */
376 static void
377 _index_mime_part (notmuch_message_t *message,
378                   notmuch_indexopts_t *indexopts,
379                   GMimeObject *part,
380                   _notmuch_message_crypto_t *msg_crypto)
381 {
382     GMimeStream *stream, *filter;
383     GMimeFilter *discard_non_term_filter;
384     GMimeDataWrapper *wrapper;
385     GByteArray *byte_array;
386     GMimeContentDisposition *disposition;
387     GMimeContentType *content_type;
388     char *body;
389     const char *charset;
390     GMimeObject *repaired_part = NULL;
391
392     if (! part) {
393         _notmuch_database_log (notmuch_message_get_database (message),
394                                "Warning: Not indexing empty mime part.\n");
395         goto DONE;
396     }
397
398     repaired_part = _notmuch_repair_mixed_up_mangled (part);
399     if (repaired_part) {
400         /* This was likely "Mixed Up" in transit!  We will instead use
401          * the more likely-to-be-correct variant. */
402         notmuch_message_add_property (message, "index.repaired", "mixedup");
403         part = repaired_part;
404     }
405
406     _index_content_type (message, part);
407
408     if (GMIME_IS_MULTIPART (part)) {
409         GMimeMultipart *multipart = GMIME_MULTIPART (part);
410         int i;
411
412         if (GMIME_IS_MULTIPART_SIGNED (multipart))
413             _notmuch_message_add_term (message, "tag", "signed");
414
415         if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
416             _notmuch_message_add_term (message, "tag", "encrypted");
417
418         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
419             GMimeObject *child;
420             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
421                 /* Don't index the signature, but index its content type. */
422                 if (i == GMIME_MULTIPART_SIGNED_SIGNATURE) {
423                     _index_content_type (message,
424                                          g_mime_multipart_get_part (multipart, i));
425                     continue;
426                 } else if (i != GMIME_MULTIPART_SIGNED_CONTENT) {
427                     _notmuch_database_log (notmuch_message_get_database (message),
428                                            "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
429                 }
430             }
431             if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
432                 _index_content_type (message,
433                                      g_mime_multipart_get_part (multipart, i));
434                 if (i == GMIME_MULTIPART_ENCRYPTED_CONTENT) {
435                     _index_encrypted_mime_part (message, indexopts,
436                                                 GMIME_MULTIPART_ENCRYPTED (part),
437                                                 msg_crypto);
438                 } else {
439                     if (i != GMIME_MULTIPART_ENCRYPTED_VERSION) {
440                         _notmuch_database_log (notmuch_message_get_database (message),
441                                                "Warning: Unexpected extra parts of multipart/encrypted.\n");
442                     }
443                 }
444                 continue;
445             }
446             child = g_mime_multipart_get_part (multipart, i);
447             GMimeObject *toindex = child;
448             if (_notmuch_message_crypto_potential_payload (msg_crypto, child, part, i) &&
449                 msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL) {
450                 toindex = _notmuch_repair_crypto_payload_skip_legacy_display (child);
451                 if (toindex != child)
452                     notmuch_message_add_property (message, "index.repaired", "skip-protected-headers-legacy-display");
453             }
454             _index_mime_part (message, indexopts, toindex, msg_crypto);
455         }
456         goto DONE;
457     }
458
459     if (GMIME_IS_MESSAGE_PART (part)) {
460         GMimeMessage *mime_message;
461
462         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
463
464         _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message), msg_crypto);
465
466         goto DONE;
467     }
468
469     if (! (GMIME_IS_PART (part))) {
470         _notmuch_database_log (notmuch_message_get_database (message),
471                                "Warning: Not indexing unknown mime part: %s.\n",
472                                g_type_name (G_OBJECT_TYPE (part)));
473         goto DONE;
474     }
475
476     disposition = g_mime_object_get_content_disposition (part);
477     if (disposition &&
478         strcasecmp (g_mime_content_disposition_get_disposition (disposition),
479                     GMIME_DISPOSITION_ATTACHMENT) == 0) {
480         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
481
482         _notmuch_message_add_term (message, "tag", "attachment");
483         _notmuch_message_gen_terms (message, "attachment", filename);
484
485         /* XXX: Would be nice to call out to something here to parse
486          * the attachment into text and then index that. */
487         goto DONE;
488     }
489
490     byte_array = g_byte_array_new ();
491
492     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
493     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), false);
494
495     filter = g_mime_stream_filter_new (stream);
496
497     content_type = g_mime_object_get_content_type (part);
498     discard_non_term_filter = notmuch_filter_discard_non_term_new (content_type);
499
500     g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
501                               discard_non_term_filter);
502
503     charset = g_mime_object_get_content_type_parameter (part, "charset");
504     if (charset) {
505         GMimeFilter *charset_filter;
506         charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
507         /* This result can be NULL for things like "unknown-8bit".
508          * Don't set a NULL filter as that makes GMime print
509          * annoying assertion-failure messages on stderr. */
510         if (charset_filter) {
511             g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
512                                       charset_filter);
513             g_object_unref (charset_filter);
514         }
515     }
516
517     wrapper = g_mime_part_get_content (GMIME_PART (part));
518     if (wrapper)
519         g_mime_data_wrapper_write_to_stream (wrapper, filter);
520
521     g_object_unref (stream);
522     g_object_unref (filter);
523     g_object_unref (discard_non_term_filter);
524
525     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
526     body = (char *) g_byte_array_free (byte_array, false);
527
528     if (body) {
529         _notmuch_message_gen_terms (message, NULL, body);
530
531         free (body);
532     }
533   DONE:
534     if (repaired_part)
535         g_object_unref (repaired_part);
536 }
537
538 /* descend (if desired) into the cleartext part of an encrypted MIME
539  * part while indexing. */
540 static void
541 _index_encrypted_mime_part (notmuch_message_t *message,
542                             notmuch_indexopts_t *indexopts,
543                             GMimeMultipartEncrypted *encrypted_data,
544                             _notmuch_message_crypto_t *msg_crypto)
545 {
546     notmuch_status_t status;
547     GError *err = NULL;
548     notmuch_database_t *notmuch = NULL;
549     GMimeObject *clear = NULL;
550
551     if (! indexopts || (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE))
552         return;
553
554     notmuch = notmuch_message_get_database (message);
555
556     bool attempted = false;
557     GMimeDecryptResult *decrypt_result = NULL;
558     bool get_sk = (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_TRUE);
559     clear = _notmuch_crypto_decrypt (&attempted, notmuch_indexopts_get_decrypt_policy (indexopts),
560                                      message, encrypted_data, get_sk ? &decrypt_result : NULL, &err);
561     if (! attempted)
562         return;
563     if (err || ! clear) {
564         if (decrypt_result)
565             g_object_unref (decrypt_result);
566         if (err) {
567             _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (%d:%d) [%s]\n",
568                                    err->domain, err->code, err->message);
569             g_error_free (err);
570         } else {
571             _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (unknown error)\n");
572         }
573         /* Indicate that we failed to decrypt during indexing */
574         status = notmuch_message_add_property (message, "index.decryption", "failure");
575         if (status)
576             _notmuch_database_log_append (notmuch, "failed to add index.decryption "
577                                           "property (%d)\n", status);
578         return;
579     }
580     if (decrypt_result) {
581         status = _notmuch_message_crypto_successful_decryption (msg_crypto);
582         if (status)
583             _notmuch_database_log_append (notmuch, "failed to mark the message as decrypted (%s)\n",
584                                           notmuch_status_to_string (status));
585         if (get_sk) {
586             status = notmuch_message_add_property (message, "session-key",
587                                                    g_mime_decrypt_result_get_session_key (decrypt_result));
588             if (status)
589                 _notmuch_database_log (notmuch, "failed to add session-key "
590                                        "property (%d)\n", status);
591         }
592         g_object_unref (decrypt_result);
593     }
594     GMimeObject *toindex = clear;
595     if (_notmuch_message_crypto_potential_payload (msg_crypto, clear, GMIME_OBJECT (encrypted_data), GMIME_MULTIPART_ENCRYPTED_CONTENT) &&
596         msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL) {
597         toindex = _notmuch_repair_crypto_payload_skip_legacy_display (clear);
598         if (toindex != clear)
599             notmuch_message_add_property (message, "index.repaired", "skip-protected-headers-legacy-display");
600     }
601     _index_mime_part (message, indexopts, toindex, msg_crypto);
602     g_object_unref (clear);
603
604     status = notmuch_message_add_property (message, "index.decryption", "success");
605     if (status)
606         _notmuch_database_log (notmuch, "failed to add index.decryption "
607                                "property (%d)\n", status);
608
609 }
610
611 static notmuch_status_t
612 _notmuch_message_index_user_headers (notmuch_message_t *message, GMimeMessage *mime_message)
613 {
614
615     notmuch_database_t *notmuch = notmuch_message_get_database (message);
616     notmuch_string_map_iterator_t *iter = _notmuch_database_user_headers (notmuch);
617
618     for (; _notmuch_string_map_iterator_valid (iter);
619          _notmuch_string_map_iterator_move_to_next (iter)) {
620
621         const char *prefix_name = _notmuch_string_map_iterator_key (iter);
622
623         const char *header_name = _notmuch_string_map_iterator_value (iter);
624
625         const char *header = g_mime_object_get_header (GMIME_OBJECT (mime_message), header_name);
626         if (header)
627             _notmuch_message_gen_terms (message, prefix_name, header);
628     }
629
630     if (iter)
631         _notmuch_string_map_iterator_destroy (iter);
632     return NOTMUCH_STATUS_SUCCESS;
633
634 }
635
636 notmuch_status_t
637 _notmuch_message_index_file (notmuch_message_t *message,
638                              notmuch_indexopts_t *indexopts,
639                              notmuch_message_file_t *message_file)
640 {
641     GMimeMessage *mime_message;
642     InternetAddressList *addresses;
643     const char *subject;
644     notmuch_status_t status;
645     _notmuch_message_crypto_t *msg_crypto;
646
647     status = _notmuch_message_file_get_mime_message (message_file,
648                                                      &mime_message);
649     if (status)
650         return status;
651
652     addresses = g_mime_message_get_from (mime_message);
653     if (addresses) {
654         _index_address_list (message, "from", addresses);
655     }
656
657     addresses = g_mime_message_get_all_recipients (mime_message);
658     if (addresses) {
659         _index_address_list (message, "to", addresses);
660         g_object_unref (addresses);
661     }
662
663     subject = g_mime_message_get_subject (mime_message);
664     _notmuch_message_gen_terms (message, "subject", subject);
665
666     status = _notmuch_message_index_user_headers (message, mime_message);
667
668     msg_crypto = _notmuch_message_crypto_new (NULL);
669     _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message), msg_crypto);
670     if (msg_crypto && msg_crypto->payload_subject) {
671         _notmuch_message_gen_terms (message, "subject", msg_crypto->payload_subject);
672         _notmuch_message_update_subject (message, msg_crypto->payload_subject);
673     }
674
675     talloc_free (msg_crypto);
676
677     return NOTMUCH_STATUS_SUCCESS;
678 }