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)
147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
150 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
152 object_class->finalize = notmuch_filter_discard_non_term_finalize;
154 filter_class->copy = filter_copy;
155 filter_class->filter = filter_filter;
156 filter_class->complete = filter_complete;
157 filter_class->reset = filter_reset;
161 notmuch_filter_discard_non_term_finalize (GObject *object)
163 G_OBJECT_CLASS (parent_class)->finalize (object);
167 filter_copy (GMimeFilter *gmime_filter)
169 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
170 return notmuch_filter_discard_non_term_new (filter->content_type);
174 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
175 char **outbuf, size_t *outlen, size_t *outprespace)
177 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
178 const scanner_state_t *states = filter->states;
179 register const char *inptr = inbuf;
180 const char *inend = inbuf + inlen;
187 g_mime_filter_set_size (gmime_filter, inlen, FALSE);
188 outptr = gmime_filter->outbuf;
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.
200 if (*inptr >= states[next].a && *inptr <= states[next].b) {
201 next = states[next].next_if_match;
203 next = states[next].next_if_not_match;
206 } while (next != states[next].state);
208 if (filter->state < filter->first_skipping_state)
211 filter->state = next;
215 *outlen = outptr - gmime_filter->outbuf;
216 *outprespace = gmime_filter->outpre;
217 *outbuf = gmime_filter->outbuf;
221 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
222 char **outbuf, size_t *outlen, size_t *outprespace)
225 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
229 filter_reset (GMimeFilter *gmime_filter)
231 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
237 * notmuch_filter_discard_non_term_new:
239 * Returns: a new #NotmuchFilterDiscardNonTerm filter.
242 notmuch_filter_discard_non_term_new (GMimeContentType *content_type)
244 static GType type = 0;
245 NotmuchFilterDiscardNonTerm *filter;
248 static const GTypeInfo info = {
249 sizeof (NotmuchFilterDiscardNonTermClass),
250 NULL, /* base_class_init */
251 NULL, /* base_class_finalize */
252 (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
253 NULL, /* class_finalize */
254 NULL, /* class_data */
255 sizeof (NotmuchFilterDiscardNonTerm),
257 NULL, /* instance_init */
258 NULL /* value_table */
261 type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
264 filter = (NotmuchFilterDiscardNonTerm *) g_object_new (type, NULL);
265 filter->content_type = content_type;
267 if (g_mime_content_type_is_type (content_type, "text", "html")) {
268 filter->states = html_states;
269 filter->first_skipping_state = first_html_skipping_state;
271 filter->states = uuencode_states;
272 filter->first_skipping_state = first_uuencode_skipping_state;
275 return (GMimeFilter *) filter;
278 /* We're finally down to a single (NAME + address) email "mailbox". */
280 _index_address_mailbox (notmuch_message_t *message,
281 const char *prefix_name,
282 InternetAddress *address)
284 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
285 const char *name, *addr, *combined;
286 void *local = talloc_new (message);
288 name = internet_address_get_name (address);
289 addr = internet_address_mailbox_get_addr (mailbox);
291 /* Combine the name and address and index them as a phrase. */
293 combined = talloc_asprintf (local, "%s %s", name, addr);
300 _notmuch_message_gen_terms (message, prefix_name, combined);
306 _index_address_list (notmuch_message_t *message,
307 const char *prefix_name,
308 InternetAddressList *addresses);
310 /* The outer loop over the InternetAddressList wasn't quite enough.
311 * There can actually be a tree here where a single member of the list
312 * is a "group" containing another list. Recurse please.
315 _index_address_group (notmuch_message_t *message,
316 const char *prefix_name,
317 InternetAddress *address)
319 InternetAddressGroup *group;
320 InternetAddressList *list;
322 group = INTERNET_ADDRESS_GROUP (address);
323 list = internet_address_group_get_members (group);
328 _index_address_list (message, prefix_name, list);
332 _index_address_list (notmuch_message_t *message,
333 const char *prefix_name,
334 InternetAddressList *addresses)
337 InternetAddress *address;
339 if (addresses == NULL)
342 for (i = 0; i < internet_address_list_length (addresses); i++) {
343 address = internet_address_list_get_address (addresses, i);
344 if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
345 _index_address_mailbox (message, prefix_name, address);
346 } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
347 _index_address_group (message, prefix_name, address);
349 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
354 /* Callback to generate terms for each mime part of a message. */
356 _index_mime_part (notmuch_message_t *message,
359 GMimeStream *stream, *filter;
360 GMimeFilter *discard_non_term_filter;
361 GMimeDataWrapper *wrapper;
362 GByteArray *byte_array;
363 GMimeContentDisposition *disposition;
368 _notmuch_database_log (_notmuch_message_database (message),
369 "Warning: Not indexing empty mime part.\n");
373 GMimeContentType *content_type = g_mime_object_get_content_type(part);
375 char *mime_string = g_mime_content_type_to_string(content_type);
378 _notmuch_message_gen_terms (message, "mimetype", mime_string);
383 if (GMIME_IS_MULTIPART (part)) {
384 GMimeMultipart *multipart = GMIME_MULTIPART (part);
387 if (GMIME_IS_MULTIPART_SIGNED (multipart))
388 _notmuch_message_add_term (message, "tag", "signed");
390 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
391 _notmuch_message_add_term (message, "tag", "encrypted");
393 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
394 if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
395 /* Don't index the signature. */
399 _notmuch_database_log (_notmuch_message_database (message),
400 "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
402 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
403 /* Don't index encrypted parts. */
406 _index_mime_part (message,
407 g_mime_multipart_get_part (multipart, i));
412 if (GMIME_IS_MESSAGE_PART (part)) {
413 GMimeMessage *mime_message;
415 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
417 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
422 if (! (GMIME_IS_PART (part))) {
423 _notmuch_database_log (_notmuch_message_database (message),
424 "Warning: Not indexing unknown mime part: %s.\n",
425 g_type_name (G_OBJECT_TYPE (part)));
429 disposition = g_mime_object_get_content_disposition (part);
431 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
432 GMIME_DISPOSITION_ATTACHMENT) == 0)
434 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
436 _notmuch_message_add_term (message, "tag", "attachment");
437 _notmuch_message_gen_terms (message, "attachment", filename);
439 /* XXX: Would be nice to call out to something here to parse
440 * the attachment into text and then index that. */
444 byte_array = g_byte_array_new ();
446 stream = g_mime_stream_mem_new_with_byte_array (byte_array);
447 g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
449 filter = g_mime_stream_filter_new (stream);
450 discard_non_term_filter = notmuch_filter_discard_non_term_new (content_type);
452 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
453 discard_non_term_filter);
455 charset = g_mime_object_get_content_type_parameter (part, "charset");
457 GMimeFilter *charset_filter;
458 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
459 /* This result can be NULL for things like "unknown-8bit".
460 * Don't set a NULL filter as that makes GMime print
461 * annoying assertion-failure messages on stderr. */
462 if (charset_filter) {
463 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
465 g_object_unref (charset_filter);
469 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
471 g_mime_data_wrapper_write_to_stream (wrapper, filter);
473 g_object_unref (stream);
474 g_object_unref (filter);
475 g_object_unref (discard_non_term_filter);
477 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
478 body = (char *) g_byte_array_free (byte_array, FALSE);
481 _notmuch_message_gen_terms (message, NULL, body);
488 _notmuch_message_index_file (notmuch_message_t *message,
489 notmuch_message_file_t *message_file)
491 GMimeMessage *mime_message;
492 InternetAddressList *addresses;
494 notmuch_status_t status;
496 status = _notmuch_message_file_get_mime_message (message_file,
501 addresses = g_mime_message_get_from (mime_message);
503 _index_address_list (message, "from", addresses);
504 g_mime_2_6_unref (addresses);
507 addresses = g_mime_message_get_all_recipients (mime_message);
509 _index_address_list (message, "to", addresses);
510 g_object_unref (addresses);
513 subject = g_mime_message_get_subject (mime_message);
514 _notmuch_message_gen_terms (message, "subject", subject);
516 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
518 return NOTMUCH_STATUS_SUCCESS;