]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
emacs: Add new option notmuch-search-hide-excluded
[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     object_class->finalize = notmuch_filter_discard_non_term_finalize;
152
153     filter_class->copy = filter_copy;
154     filter_class->filter = filter_filter;
155     filter_class->complete = filter_complete;
156     filter_class->reset = filter_reset;
157 }
158
159 static void
160 notmuch_filter_discard_non_term_finalize (GObject *object)
161 {
162     G_OBJECT_CLASS (parent_class)->finalize (object);
163 }
164
165 static GMimeFilter *
166 filter_copy (GMimeFilter *gmime_filter)
167 {
168     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
169
170     return notmuch_filter_discard_non_term_new (filter->content_type);
171 }
172
173 static void
174 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
175                char **outbuf, size_t *outlen, size_t *outprespace)
176 {
177     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
178     const scanner_state_t *states = filter->states;
179     const char *inptr = inbuf;
180     const char *inend = inbuf + inlen;
181     char *outptr;
182
183     (void) prespace;
184
185     int next;
186
187     g_mime_filter_set_size (gmime_filter, inlen, false);
188     outptr = gmime_filter->outbuf;
189
190     next = filter->state;
191     while (inptr < inend) {
192         /* Each state is defined by a contiguous set of rows of the
193          * state table marked by a common value for '.state'. The
194          * state numbers must be equal to the index of the first row
195          * in a given state; thus the loop condition here looks for a
196          * jump to a first row of a state, which is a real transition
197          * in the underlying DFA.
198          */
199         do {
200             if (*inptr >= states[next].a && *inptr <= states[next].b) {
201                 next = states[next].next_if_match;
202             } else {
203                 next = states[next].next_if_not_match;
204             }
205
206         } while (next != states[next].state);
207
208         if (filter->state < filter->first_skipping_state)
209             *outptr++ = *inptr;
210
211         filter->state = next;
212         inptr++;
213     }
214
215     *outlen = outptr - gmime_filter->outbuf;
216     *outprespace = gmime_filter->outpre;
217     *outbuf = gmime_filter->outbuf;
218 }
219
220 static void
221 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
222                  char **outbuf, size_t *outlen, size_t *outprespace)
223 {
224     if (inbuf && inlen)
225         filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
226 }
227
228 static void
229 filter_reset (GMimeFilter *gmime_filter)
230 {
231     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
232
233     filter->state = 0;
234 }
235
236 /**
237  * notmuch_filter_discard_non_term_new:
238  *
239  * Returns: a new #NotmuchFilterDiscardNonTerm filter.
240  **/
241 static GType type = 0;
242
243 static const GTypeInfo info = {
244     .class_size = sizeof (NotmuchFilterDiscardNonTermClass),
245     .base_init = NULL,
246     .base_finalize = NULL,
247     .class_init = (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
248     .class_finalize = NULL,
249     .class_data = NULL,
250     .instance_size = sizeof (NotmuchFilterDiscardNonTerm),
251     .n_preallocs = 0,
252     .instance_init = NULL,
253     .value_table = NULL,
254 };
255
256 void
257 _notmuch_filter_init () {
258     type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info,
259                                    (GTypeFlags) 0);
260     parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
261 }
262
263 static GMimeFilter *
264 notmuch_filter_discard_non_term_new (GMimeContentType *content_type)
265 {
266     NotmuchFilterDiscardNonTerm *filter;
267
268     filter = (NotmuchFilterDiscardNonTerm *) g_object_new (type, NULL);
269     filter->content_type = content_type;
270     filter->state = 0;
271     if (g_mime_content_type_is_type (content_type, "text", "html")) {
272         filter->states = html_states;
273         filter->first_skipping_state = first_html_skipping_state;
274     } else {
275         filter->states = uuencode_states;
276         filter->first_skipping_state = first_uuencode_skipping_state;
277     }
278
279     return (GMimeFilter *) filter;
280 }
281
282 /* We're finally down to a single (NAME + address) email "mailbox". */
283 static void
284 _index_address_mailbox (notmuch_message_t *message,
285                         const char *prefix_name,
286                         InternetAddress *address)
287 {
288     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
289     const char *name, *addr, *combined;
290     void *local = talloc_new (message);
291
292     name = internet_address_get_name (address);
293     addr = internet_address_mailbox_get_addr (mailbox);
294
295     /* Combine the name and address and index them as a phrase. */
296     if (name && addr)
297         combined = talloc_asprintf (local, "%s %s", name, addr);
298     else if (name)
299         combined = name;
300     else
301         combined = addr;
302
303     if (combined)
304         _notmuch_message_gen_terms (message, prefix_name, combined);
305
306     talloc_free (local);
307 }
308
309 static void
310 _index_address_list (notmuch_message_t *message,
311                      const char *prefix_name,
312                      InternetAddressList *addresses);
313
314 /* The outer loop over the InternetAddressList wasn't quite enough.
315  * There can actually be a tree here where a single member of the list
316  * is a "group" containing another list. Recurse please.
317  */
318 static void
319 _index_address_group (notmuch_message_t *message,
320                       const char *prefix_name,
321                       InternetAddress *address)
322 {
323     InternetAddressGroup *group;
324     InternetAddressList *list;
325
326     group = INTERNET_ADDRESS_GROUP (address);
327     list = internet_address_group_get_members (group);
328
329     if (! list)
330         return;
331
332     _index_address_list (message, prefix_name, list);
333 }
334
335 static void
336 _index_address_list (notmuch_message_t *message,
337                      const char *prefix_name,
338                      InternetAddressList *addresses)
339 {
340     int i;
341     InternetAddress *address;
342
343     if (addresses == NULL)
344         return;
345
346     for (i = 0; i < internet_address_list_length (addresses); i++) {
347         address = internet_address_list_get_address (addresses, i);
348         if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
349             _index_address_mailbox (message, prefix_name, address);
350         } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
351             _index_address_group (message, prefix_name, address);
352         } else {
353             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
354         }
355     }
356 }
357
358 static void
359 _index_content_type (notmuch_message_t *message, GMimeObject *part)
360 {
361     GMimeContentType *content_type = g_mime_object_get_content_type (part);
362
363     if (content_type) {
364         char *mime_string = g_mime_content_type_get_mime_type (content_type);
365         if (mime_string) {
366             _notmuch_message_gen_terms (message, "mimetype", mime_string);
367             g_free (mime_string);
368         }
369     }
370 }
371
372 static void
373 _index_encrypted_mime_part (notmuch_message_t *message, notmuch_indexopts_t *indexopts,
374                             GMimeObject *part,
375                             _notmuch_message_crypto_t *msg_crypto);
376
377 static void
378 _index_pkcs7_part (notmuch_message_t *message,
379                    notmuch_indexopts_t *indexopts,
380                    GMimeObject *part,
381                    _notmuch_message_crypto_t *msg_crypto);
382
383 static bool
384 _indexable_as_text (notmuch_message_t *message, GMimeObject *part)
385 {
386     GMimeContentType *content_type = g_mime_object_get_content_type (part);
387     notmuch_database_t *notmuch = notmuch_message_get_database (message);
388
389     if (content_type) {
390         char *mime_string = g_mime_content_type_get_mime_type (content_type);
391         if (mime_string) {
392             bool ret = _notmuch_database_indexable_as_text (notmuch, mime_string);
393             g_free (mime_string);
394             return ret;
395         }
396     }
397     return false;
398 }
399
400 /* Callback to generate terms for each mime part of a message. */
401 static void
402 _index_mime_part (notmuch_message_t *message,
403                   notmuch_indexopts_t *indexopts,
404                   GMimeObject *part,
405                   _notmuch_message_crypto_t *msg_crypto)
406 {
407     GMimeStream *stream, *filter;
408     GMimeFilter *discard_non_term_filter;
409     GMimeDataWrapper *wrapper;
410     GByteArray *byte_array;
411     GMimeContentDisposition *disposition;
412     GMimeContentType *content_type;
413     char *body;
414     const char *charset;
415     GMimeObject *repaired_part = NULL;
416
417     if (! part) {
418         _notmuch_database_log (notmuch_message_get_database (message),
419                                "Warning: Not indexing empty mime part.\n");
420         goto DONE;
421     }
422
423     repaired_part = _notmuch_repair_mixed_up_mangled (part);
424     if (repaired_part) {
425         /* This was likely "Mixed Up" in transit!  We will instead use
426          * the more likely-to-be-correct variant. */
427         notmuch_message_add_property (message, "index.repaired", "mixedup");
428         part = repaired_part;
429     }
430
431     _index_content_type (message, part);
432
433     if (GMIME_IS_MULTIPART (part)) {
434         GMimeMultipart *multipart = GMIME_MULTIPART (part);
435         int i;
436
437         if (GMIME_IS_MULTIPART_SIGNED (multipart))
438             _notmuch_message_add_term (message, "tag", "signed");
439
440         if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
441             _notmuch_message_add_term (message, "tag", "encrypted");
442
443         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
444             GMimeObject *child;
445             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
446                 /* Don't index the signature, but index its content type. */
447                 if (i == GMIME_MULTIPART_SIGNED_SIGNATURE) {
448                     _index_content_type (message,
449                                          g_mime_multipart_get_part (multipart, i));
450                     continue;
451                 } else if (i != GMIME_MULTIPART_SIGNED_CONTENT) {
452                     _notmuch_database_log (notmuch_message_get_database (message),
453                                            "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
454                 }
455             }
456             if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
457                 _index_content_type (message,
458                                      g_mime_multipart_get_part (multipart, i));
459                 if (i == GMIME_MULTIPART_ENCRYPTED_CONTENT) {
460                     _index_encrypted_mime_part (message, indexopts,
461                                                 part,
462                                                 msg_crypto);
463                 } else {
464                     if (i != GMIME_MULTIPART_ENCRYPTED_VERSION) {
465                         _notmuch_database_log (notmuch_message_get_database (message),
466                                                "Warning: Unexpected extra parts of multipart/encrypted.\n");
467                     }
468                 }
469                 continue;
470             }
471             child = g_mime_multipart_get_part (multipart, i);
472             GMimeObject *toindex = child;
473             if (_notmuch_message_crypto_potential_payload (msg_crypto, child, part, i) &&
474                 msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL) {
475                 toindex = _notmuch_repair_crypto_payload_skip_legacy_display (child);
476                 if (toindex != child)
477                     notmuch_message_add_property (message, "index.repaired",
478                                                   "skip-protected-headers-legacy-display");
479             }
480             _index_mime_part (message, indexopts, toindex, msg_crypto);
481         }
482         goto DONE;
483     }
484
485     if (GMIME_IS_MESSAGE_PART (part)) {
486         GMimeMessage *mime_message;
487
488         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
489
490         _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message),
491                           msg_crypto);
492
493         goto DONE;
494     }
495
496     if (GMIME_IS_APPLICATION_PKCS7_MIME (part)) {
497         _index_pkcs7_part (message, indexopts, part, msg_crypto);
498         goto DONE;
499     }
500
501     if (! (GMIME_IS_PART (part))) {
502         _notmuch_database_log (notmuch_message_get_database (message),
503                                "Warning: Not indexing unknown mime part: %s.\n",
504                                g_type_name (G_OBJECT_TYPE (part)));
505         goto DONE;
506     }
507
508     disposition = g_mime_object_get_content_disposition (part);
509     if (disposition &&
510         strcasecmp (g_mime_content_disposition_get_disposition (disposition),
511                     GMIME_DISPOSITION_ATTACHMENT) == 0) {
512         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
513
514         _notmuch_message_add_term (message, "tag", "attachment");
515         _notmuch_message_gen_terms (message, "attachment", filename);
516
517         if (! _indexable_as_text (message, part)) {
518             /* XXX: Would be nice to call out to something here to parse
519              * the attachment into text and then index that. */
520             goto DONE;
521         }
522     }
523
524     byte_array = g_byte_array_new ();
525
526     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
527     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), false);
528
529     filter = g_mime_stream_filter_new (stream);
530
531     content_type = g_mime_object_get_content_type (part);
532     discard_non_term_filter = notmuch_filter_discard_non_term_new (content_type);
533
534     g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
535                               discard_non_term_filter);
536
537     charset = g_mime_object_get_content_type_parameter (part, "charset");
538     if (charset) {
539         GMimeFilter *charset_filter;
540         charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
541         /* This result can be NULL for things like "unknown-8bit".
542          * Don't set a NULL filter as that makes GMime print
543          * annoying assertion-failure messages on stderr. */
544         if (charset_filter) {
545             g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
546                                       charset_filter);
547             g_object_unref (charset_filter);
548         }
549     }
550
551     wrapper = g_mime_part_get_content (GMIME_PART (part));
552     if (wrapper)
553         g_mime_data_wrapper_write_to_stream (wrapper, filter);
554
555     g_object_unref (stream);
556     g_object_unref (filter);
557     g_object_unref (discard_non_term_filter);
558
559     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
560     body = (char *) g_byte_array_free (byte_array, false);
561
562     if (body) {
563         _notmuch_message_gen_terms (message, NULL, body);
564
565         free (body);
566     }
567   DONE:
568     if (repaired_part)
569         g_object_unref (repaired_part);
570 }
571
572 /* descend (if desired) into the cleartext part of an encrypted MIME
573  * part while indexing. */
574 static void
575 _index_encrypted_mime_part (notmuch_message_t *message,
576                             notmuch_indexopts_t *indexopts,
577                             GMimeObject *encrypted_data,
578                             _notmuch_message_crypto_t *msg_crypto)
579 {
580     notmuch_status_t status;
581     GError *err = NULL;
582     notmuch_database_t *notmuch = NULL;
583     GMimeObject *clear = NULL;
584
585     if (! indexopts || (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE))
586         return;
587
588     notmuch = notmuch_message_get_database (message);
589
590     bool attempted = false;
591     GMimeDecryptResult *decrypt_result = NULL;
592     bool get_sk = (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_TRUE);
593
594     clear = _notmuch_crypto_decrypt (&attempted, notmuch_indexopts_get_decrypt_policy (indexopts),
595                                      message, encrypted_data, get_sk ? &decrypt_result : NULL, &err);
596     if (! attempted)
597         return;
598     if (err || ! clear) {
599         if (decrypt_result)
600             g_object_unref (decrypt_result);
601         if (err) {
602             _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (%d:%d) [%s]\n",
603                                    err->domain, err->code, err->message);
604             g_error_free (err);
605         } else {
606             _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (unknown error)\n");
607         }
608         /* Indicate that we failed to decrypt during indexing */
609         status = notmuch_message_add_property (message, "index.decryption", "failure");
610         if (status)
611             _notmuch_database_log_append (notmuch, "failed to add index.decryption "
612                                           "property (%d)\n", status);
613         return;
614     }
615     if (decrypt_result) {
616         status = _notmuch_message_crypto_successful_decryption (msg_crypto);
617         if (status)
618             _notmuch_database_log_append (notmuch, "failed to mark the message as decrypted (%s)\n",
619                                           notmuch_status_to_string (status));
620         if (get_sk) {
621             status = notmuch_message_add_property (message, "session-key",
622                                                    g_mime_decrypt_result_get_session_key (
623                                                        decrypt_result));
624             if (status)
625                 _notmuch_database_log (notmuch, "failed to add session-key "
626                                        "property (%d)\n", status);
627         }
628         g_object_unref (decrypt_result);
629     }
630     GMimeObject *toindex = clear;
631
632     if (_notmuch_message_crypto_potential_payload (msg_crypto, clear, encrypted_data,
633                                                    GMIME_MULTIPART_ENCRYPTED_CONTENT) &&
634         msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL) {
635         toindex = _notmuch_repair_crypto_payload_skip_legacy_display (clear);
636         if (toindex != clear)
637             notmuch_message_add_property (message, "index.repaired",
638                                           "skip-protected-headers-legacy-display");
639     }
640     _index_mime_part (message, indexopts, toindex, msg_crypto);
641     g_object_unref (clear);
642
643     status = notmuch_message_add_property (message, "index.decryption", "success");
644     if (status)
645         _notmuch_database_log (notmuch, "failed to add index.decryption "
646                                "property (%d)\n", status);
647
648 }
649
650 static void
651 _index_pkcs7_part (notmuch_message_t *message,
652                    notmuch_indexopts_t *indexopts,
653                    GMimeObject *part,
654                    _notmuch_message_crypto_t *msg_crypto)
655 {
656     GMimeApplicationPkcs7Mime *pkcs7;
657     GMimeSecureMimeType p7type;
658     GMimeObject *mimeobj = NULL;
659     GMimeSignatureList *sigs = NULL;
660     GError *err = NULL;
661     notmuch_database_t *notmuch = NULL;
662
663     pkcs7 = GMIME_APPLICATION_PKCS7_MIME (part);
664     p7type = g_mime_application_pkcs7_mime_get_smime_type (pkcs7);
665     notmuch = notmuch_message_get_database (message);
666     _index_content_type (message, part);
667
668     if (p7type == GMIME_SECURE_MIME_TYPE_SIGNED_DATA) {
669         sigs = g_mime_application_pkcs7_mime_verify (pkcs7, GMIME_VERIFY_NONE, &mimeobj, &err);
670         if (sigs == NULL) {
671             _notmuch_database_log (notmuch,
672                                    "Failed to verify PKCS#7 SignedData during indexing. (%d:%d) [%s]\n",
673                                    err->domain, err->code, err->message);
674             g_error_free (err);
675             goto DONE;
676         }
677         _notmuch_message_add_term (message, "tag", "signed");
678         GMimeObject *toindex = mimeobj;
679         if (_notmuch_message_crypto_potential_payload (msg_crypto, mimeobj, part, 0) &&
680             msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL) {
681             toindex = _notmuch_repair_crypto_payload_skip_legacy_display (mimeobj);
682             if (toindex != mimeobj)
683                 notmuch_message_add_property (message, "index.repaired",
684                                               "skip-protected-headers-legacy-display");
685         }
686         _index_mime_part (message, indexopts, toindex, msg_crypto);
687     } else if (p7type == GMIME_SECURE_MIME_TYPE_ENVELOPED_DATA) {
688         _notmuch_message_add_term (message, "tag", "encrypted");
689         _index_encrypted_mime_part (message, indexopts,
690                                     part,
691                                     msg_crypto);
692     } else {
693         _notmuch_database_log (notmuch, "Cannot currently handle PKCS#7 smime-type '%s'\n",
694                                g_mime_object_get_content_type_parameter (part, "smime-type"));
695     }
696   DONE:
697     if (mimeobj)
698         g_object_unref (mimeobj);
699     if (sigs)
700         g_object_unref (sigs);
701 }
702
703 static notmuch_status_t
704 _notmuch_message_index_user_headers (notmuch_message_t *message, GMimeMessage *mime_message)
705 {
706
707     notmuch_database_t *notmuch = notmuch_message_get_database (message);
708     notmuch_string_map_iterator_t *iter = _notmuch_database_user_headers (notmuch);
709
710     for (; _notmuch_string_map_iterator_valid (iter);
711          _notmuch_string_map_iterator_move_to_next (iter)) {
712
713         const char *prefix_name = _notmuch_string_map_iterator_key (iter);
714
715         const char *header_name = _notmuch_string_map_iterator_value (iter);
716
717         const char *header = g_mime_object_get_header (GMIME_OBJECT (mime_message), header_name);
718         if (header)
719             _notmuch_message_gen_terms (message, prefix_name, header);
720     }
721
722     if (iter)
723         _notmuch_string_map_iterator_destroy (iter);
724     return NOTMUCH_STATUS_SUCCESS;
725
726 }
727
728 notmuch_status_t
729 _notmuch_message_index_file (notmuch_message_t *message,
730                              notmuch_indexopts_t *indexopts,
731                              notmuch_message_file_t *message_file)
732 {
733     GMimeMessage *mime_message;
734     InternetAddressList *addresses;
735     const char *subject;
736     notmuch_status_t status;
737     _notmuch_message_crypto_t *msg_crypto;
738
739     status = _notmuch_message_file_get_mime_message (message_file,
740                                                      &mime_message);
741     if (status)
742         return status;
743
744     addresses = g_mime_message_get_from (mime_message);
745     if (addresses) {
746         _index_address_list (message, "from", addresses);
747     }
748
749     addresses = g_mime_message_get_all_recipients (mime_message);
750     if (addresses) {
751         _index_address_list (message, "to", addresses);
752         g_object_unref (addresses);
753     }
754
755     subject = g_mime_message_get_subject (mime_message);
756     _notmuch_message_gen_terms (message, "subject", subject);
757
758     status = _notmuch_message_index_user_headers (message, mime_message);
759
760     msg_crypto = _notmuch_message_crypto_new (NULL);
761     _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message), msg_crypto);
762     if (msg_crypto && msg_crypto->payload_subject) {
763         _notmuch_message_gen_terms (message, "subject", msg_crypto->payload_subject);
764         _notmuch_message_update_subject (message, msg_crypto->payload_subject);
765     }
766
767     talloc_free (msg_crypto);
768
769     return NOTMUCH_STATUS_SUCCESS;
770 }