2 * Copyright © 2009 Carl Worth
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.
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.
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/ .
17 * Author: Carl Worth <cworth@cworth.org>
20 #include "notmuch-private.h"
22 #include <gmime/gmime.h>
23 #include <gmime/gmime-filter.h>
33 int next_if_not_match;
36 /* Simple, linear state-transition diagram for the uuencode filter.
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.
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[] = {
56 {10, '\n', '\n', 11, 10},
57 {11, 'M', 'M', 12, 0},
58 {12, ' ', '`', 12, 11}
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.
78 static const int first_html_skipping_state = 1;
79 static const scanner_state_t html_states[] = {
81 {1, '\'', '\'', 4, 2}, /* scanning for quote or > */
84 {4, '\'', '\'', 1, 4}, /* inside single quotes */
85 {5, '"', '"', 1, 5}, /* inside double quotes */
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;
94 * NotmuchFilterDiscardNonTerm:
96 * @parent_object: parent #GMimeFilter
97 * @encode: encoding vs decoding
98 * @state: State of the parser
100 * A filter to discard uuencoded portions of an email.
102 * A uuencoded portion is identified as beginning with a line
105 * begin [0-7][0-7][0-7] .*
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 '`'.
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
118 struct _NotmuchFilterDiscardNonTerm {
119 GMimeFilter parent_object;
120 GMimeContentType *content_type;
122 int first_skipping_state;
123 const scanner_state_t *states;
126 struct _NotmuchFilterDiscardNonTermClass {
127 GMimeFilterClass parent_class;
130 static GMimeFilter *notmuch_filter_discard_non_term_new (GMimeContentType *content);
132 static void notmuch_filter_discard_non_term_finalize (GObject *object);
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);
142 static GMimeFilterClass *parent_class = NULL;
145 notmuch_filter_discard_non_term_class_init (NotmuchFilterDiscardNonTermClass *klass,
146 unused (void *class_data))
148 GObjectClass *object_class = G_OBJECT_CLASS (klass);
149 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
151 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
153 object_class->finalize = notmuch_filter_discard_non_term_finalize;
155 filter_class->copy = filter_copy;
156 filter_class->filter = filter_filter;
157 filter_class->complete = filter_complete;
158 filter_class->reset = filter_reset;
162 notmuch_filter_discard_non_term_finalize (GObject *object)
164 G_OBJECT_CLASS (parent_class)->finalize (object);
168 filter_copy (GMimeFilter *gmime_filter)
170 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
171 return notmuch_filter_discard_non_term_new (filter->content_type);
175 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
176 char **outbuf, size_t *outlen, size_t *outprespace)
178 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
179 const scanner_state_t *states = filter->states;
180 const char *inptr = inbuf;
181 const char *inend = inbuf + inlen;
188 g_mime_filter_set_size (gmime_filter, inlen, false);
189 outptr = gmime_filter->outbuf;
191 next = filter->state;
192 while (inptr < inend) {
193 /* Each state is defined by a contiguous set of rows of the
194 * state table marked by a common value for '.state'. The
195 * state numbers must be equal to the index of the first row
196 * in a given state; thus the loop condition here looks for a
197 * jump to a first row of a state, which is a real transition
198 * in the underlying DFA.
201 if (*inptr >= states[next].a && *inptr <= states[next].b) {
202 next = states[next].next_if_match;
204 next = states[next].next_if_not_match;
207 } while (next != states[next].state);
209 if (filter->state < filter->first_skipping_state)
212 filter->state = next;
216 *outlen = outptr - gmime_filter->outbuf;
217 *outprespace = gmime_filter->outpre;
218 *outbuf = gmime_filter->outbuf;
222 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
223 char **outbuf, size_t *outlen, size_t *outprespace)
226 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
230 filter_reset (GMimeFilter *gmime_filter)
232 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
238 * notmuch_filter_discard_non_term_new:
240 * Returns: a new #NotmuchFilterDiscardNonTerm filter.
243 notmuch_filter_discard_non_term_new (GMimeContentType *content_type)
245 static GType type = 0;
246 NotmuchFilterDiscardNonTerm *filter;
249 static const GTypeInfo info = {
250 .class_size = sizeof (NotmuchFilterDiscardNonTermClass),
252 .base_finalize = NULL,
253 .class_init = (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
254 .class_finalize = NULL,
256 .instance_size = sizeof (NotmuchFilterDiscardNonTerm),
258 .instance_init = NULL,
262 type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
265 filter = (NotmuchFilterDiscardNonTerm *) g_object_new (type, NULL);
266 filter->content_type = content_type;
268 if (g_mime_content_type_is_type (content_type, "text", "html")) {
269 filter->states = html_states;
270 filter->first_skipping_state = first_html_skipping_state;
272 filter->states = uuencode_states;
273 filter->first_skipping_state = first_uuencode_skipping_state;
276 return (GMimeFilter *) filter;
279 /* We're finally down to a single (NAME + address) email "mailbox". */
281 _index_address_mailbox (notmuch_message_t *message,
282 const char *prefix_name,
283 InternetAddress *address)
285 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
286 const char *name, *addr, *combined;
287 void *local = talloc_new (message);
289 name = internet_address_get_name (address);
290 addr = internet_address_mailbox_get_addr (mailbox);
292 /* Combine the name and address and index them as a phrase. */
294 combined = talloc_asprintf (local, "%s %s", name, addr);
301 _notmuch_message_gen_terms (message, prefix_name, combined);
307 _index_address_list (notmuch_message_t *message,
308 const char *prefix_name,
309 InternetAddressList *addresses);
311 /* The outer loop over the InternetAddressList wasn't quite enough.
312 * There can actually be a tree here where a single member of the list
313 * is a "group" containing another list. Recurse please.
316 _index_address_group (notmuch_message_t *message,
317 const char *prefix_name,
318 InternetAddress *address)
320 InternetAddressGroup *group;
321 InternetAddressList *list;
323 group = INTERNET_ADDRESS_GROUP (address);
324 list = internet_address_group_get_members (group);
329 _index_address_list (message, prefix_name, list);
333 _index_address_list (notmuch_message_t *message,
334 const char *prefix_name,
335 InternetAddressList *addresses)
338 InternetAddress *address;
340 if (addresses == NULL)
343 for (i = 0; i < internet_address_list_length (addresses); i++) {
344 address = internet_address_list_get_address (addresses, i);
345 if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
346 _index_address_mailbox (message, prefix_name, address);
347 } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
348 _index_address_group (message, prefix_name, address);
350 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
356 _index_content_type (notmuch_message_t *message, GMimeObject *part)
358 GMimeContentType *content_type = g_mime_object_get_content_type (part);
360 char *mime_string = g_mime_content_type_get_mime_type (content_type);
362 _notmuch_message_gen_terms (message, "mimetype", mime_string);
363 g_free (mime_string);
369 _index_encrypted_mime_part (notmuch_message_t *message, notmuch_indexopts_t *indexopts,
370 GMimeMultipartEncrypted *part,
371 _notmuch_message_crypto_t *msg_crypto);
373 /* Callback to generate terms for each mime part of a message. */
375 _index_mime_part (notmuch_message_t *message,
376 notmuch_indexopts_t *indexopts,
378 _notmuch_message_crypto_t *msg_crypto)
380 GMimeStream *stream, *filter;
381 GMimeFilter *discard_non_term_filter;
382 GMimeDataWrapper *wrapper;
383 GByteArray *byte_array;
384 GMimeContentDisposition *disposition;
385 GMimeContentType *content_type;
390 _notmuch_database_log (notmuch_message_get_database (message),
391 "Warning: Not indexing empty mime part.\n");
395 _index_content_type (message, part);
397 if (GMIME_IS_MULTIPART (part)) {
398 GMimeMultipart *multipart = GMIME_MULTIPART (part);
401 if (GMIME_IS_MULTIPART_SIGNED (multipart))
402 _notmuch_message_add_term (message, "tag", "signed");
404 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
405 _notmuch_message_add_term (message, "tag", "encrypted");
407 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
408 notmuch_status_t status;
410 if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
411 /* Don't index the signature, but index its content type. */
412 if (i == GMIME_MULTIPART_SIGNED_SIGNATURE) {
413 _index_content_type (message,
414 g_mime_multipart_get_part (multipart, i));
416 } else if (i != GMIME_MULTIPART_SIGNED_CONTENT) {
417 _notmuch_database_log (notmuch_message_get_database (message),
418 "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
421 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
422 _index_content_type (message,
423 g_mime_multipart_get_part (multipart, i));
424 if (i == GMIME_MULTIPART_ENCRYPTED_CONTENT) {
425 _index_encrypted_mime_part(message, indexopts,
426 GMIME_MULTIPART_ENCRYPTED (part),
429 if (i != GMIME_MULTIPART_ENCRYPTED_VERSION) {
430 _notmuch_database_log (notmuch_message_get_database (message),
431 "Warning: Unexpected extra parts of multipart/encrypted.\n");
436 child = g_mime_multipart_get_part (multipart, i);
437 status = _notmuch_message_crypto_potential_payload (msg_crypto, child, part, i);
439 _notmuch_database_log (notmuch_message_get_database (message),
440 "Warning: failed to mark the potential cryptographic payload (%s).\n",
441 notmuch_status_to_string (status));
442 _index_mime_part (message, indexopts, child, msg_crypto);
447 if (GMIME_IS_MESSAGE_PART (part)) {
448 GMimeMessage *mime_message;
450 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
452 _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message), msg_crypto);
457 if (! (GMIME_IS_PART (part))) {
458 _notmuch_database_log (notmuch_message_get_database (message),
459 "Warning: Not indexing unknown mime part: %s.\n",
460 g_type_name (G_OBJECT_TYPE (part)));
464 disposition = g_mime_object_get_content_disposition (part);
466 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
467 GMIME_DISPOSITION_ATTACHMENT) == 0)
469 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
471 _notmuch_message_add_term (message, "tag", "attachment");
472 _notmuch_message_gen_terms (message, "attachment", filename);
474 /* XXX: Would be nice to call out to something here to parse
475 * the attachment into text and then index that. */
479 byte_array = g_byte_array_new ();
481 stream = g_mime_stream_mem_new_with_byte_array (byte_array);
482 g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), false);
484 filter = g_mime_stream_filter_new (stream);
486 content_type = g_mime_object_get_content_type (part);
487 discard_non_term_filter = notmuch_filter_discard_non_term_new (content_type);
489 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
490 discard_non_term_filter);
492 charset = g_mime_object_get_content_type_parameter (part, "charset");
494 GMimeFilter *charset_filter;
495 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
496 /* This result can be NULL for things like "unknown-8bit".
497 * Don't set a NULL filter as that makes GMime print
498 * annoying assertion-failure messages on stderr. */
499 if (charset_filter) {
500 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
502 g_object_unref (charset_filter);
506 wrapper = g_mime_part_get_content (GMIME_PART (part));
508 g_mime_data_wrapper_write_to_stream (wrapper, filter);
510 g_object_unref (stream);
511 g_object_unref (filter);
512 g_object_unref (discard_non_term_filter);
514 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
515 body = (char *) g_byte_array_free (byte_array, false);
518 _notmuch_message_gen_terms (message, NULL, body);
524 /* descend (if desired) into the cleartext part of an encrypted MIME
525 * part while indexing. */
527 _index_encrypted_mime_part (notmuch_message_t *message,
528 notmuch_indexopts_t *indexopts,
529 GMimeMultipartEncrypted *encrypted_data,
530 _notmuch_message_crypto_t *msg_crypto)
532 notmuch_status_t status;
534 notmuch_database_t * notmuch = NULL;
535 GMimeObject *clear = NULL;
537 if (!indexopts || (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_FALSE))
540 notmuch = notmuch_message_get_database (message);
542 bool attempted = false;
543 GMimeDecryptResult *decrypt_result = NULL;
544 bool get_sk = (notmuch_indexopts_get_decrypt_policy (indexopts) == NOTMUCH_DECRYPT_TRUE);
545 clear = _notmuch_crypto_decrypt (&attempted, notmuch_indexopts_get_decrypt_policy (indexopts),
546 message, encrypted_data, get_sk ? &decrypt_result : NULL, &err);
551 g_object_unref (decrypt_result);
553 _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (%d:%d) [%s]\n",
554 err->domain, err->code, err->message);
557 _notmuch_database_log (notmuch, "Failed to decrypt during indexing. (unknown error)\n");
559 /* Indicate that we failed to decrypt during indexing */
560 status = notmuch_message_add_property (message, "index.decryption", "failure");
562 _notmuch_database_log_append (notmuch, "failed to add index.decryption "
563 "property (%d)\n", status);
566 if (decrypt_result) {
567 status = _notmuch_message_crypto_successful_decryption (msg_crypto);
569 _notmuch_database_log_append (notmuch, "failed to mark the message as decrypted (%s)\n",
570 notmuch_status_to_string (status));
572 status = notmuch_message_add_property (message, "session-key",
573 g_mime_decrypt_result_get_session_key (decrypt_result));
575 _notmuch_database_log (notmuch, "failed to add session-key "
576 "property (%d)\n", status);
578 g_object_unref (decrypt_result);
580 status = _notmuch_message_crypto_potential_payload (msg_crypto, clear, GMIME_OBJECT (encrypted_data), GMIME_MULTIPART_ENCRYPTED_CONTENT);
581 _index_mime_part (message, indexopts, clear, msg_crypto);
582 g_object_unref (clear);
584 status = notmuch_message_add_property (message, "index.decryption", "success");
586 _notmuch_database_log (notmuch, "failed to add index.decryption "
587 "property (%d)\n", status);
591 static notmuch_status_t
592 _notmuch_message_index_user_headers (notmuch_message_t *message, GMimeMessage *mime_message)
595 notmuch_database_t *notmuch = notmuch_message_get_database (message);
596 notmuch_string_map_iterator_t *iter = _notmuch_database_user_headers (notmuch);
598 for (; _notmuch_string_map_iterator_valid (iter);
599 _notmuch_string_map_iterator_move_to_next (iter)) {
601 const char *prefix_name = _notmuch_string_map_iterator_key (iter);
603 const char *header_name = _notmuch_string_map_iterator_value (iter);
605 const char *header = g_mime_object_get_header (GMIME_OBJECT (mime_message), header_name);
607 _notmuch_message_gen_terms (message, prefix_name, header);
611 _notmuch_string_map_iterator_destroy (iter);
612 return NOTMUCH_STATUS_SUCCESS;
617 _notmuch_message_index_file (notmuch_message_t *message,
618 notmuch_indexopts_t *indexopts,
619 notmuch_message_file_t *message_file)
621 GMimeMessage *mime_message;
622 InternetAddressList *addresses;
624 notmuch_status_t status;
625 _notmuch_message_crypto_t *msg_crypto;
627 status = _notmuch_message_file_get_mime_message (message_file,
632 addresses = g_mime_message_get_from (mime_message);
634 _index_address_list (message, "from", addresses);
637 addresses = g_mime_message_get_all_recipients (mime_message);
639 _index_address_list (message, "to", addresses);
640 g_object_unref (addresses);
643 subject = g_mime_message_get_subject (mime_message);
644 _notmuch_message_gen_terms (message, "subject", subject);
646 status = _notmuch_message_index_user_headers (message, mime_message);
648 msg_crypto = _notmuch_message_crypto_new (NULL);
649 _index_mime_part (message, indexopts, g_mime_message_get_mime_part (mime_message), msg_crypto);
650 if (msg_crypto && msg_crypto->payload_subject) {
651 _notmuch_message_gen_terms (message, "subject", msg_crypto->payload_subject);
652 _notmuch_message_update_subject (message, msg_crypto->payload_subject);
655 talloc_free (msg_crypto);
657 return NOTMUCH_STATUS_SUCCESS;