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 /* Oh, how I wish that gobject didn't require so much noisy boilerplate!
62 * (Though I have at least eliminated some of the stock set...) */
63 typedef struct _NotmuchFilterDiscardNonTerm NotmuchFilterDiscardNonTerm;
64 typedef struct _NotmuchFilterDiscardNonTermClass NotmuchFilterDiscardNonTermClass;
67 * NotmuchFilterDiscardNonTerm:
69 * @parent_object: parent #GMimeFilter
70 * @encode: encoding vs decoding
71 * @state: State of the parser
73 * A filter to discard uuencoded portions of an email.
75 * A uuencoded portion is identified as beginning with a line
78 * begin [0-7][0-7][0-7] .*
80 * After that detection, and beginning with the following line,
81 * characters will be discarded as long as the first character of each
82 * line begins with M and subsequent characters on the line are within
83 * the range of ASCII characters from ' ' to '`'.
85 * This is not a perfect UUencode filter. It's possible to have a
86 * message that will legitimately match that pattern, (so that some
87 * legitimate content is discarded). And for most UUencoded files, the
88 * final line of encoded data (the line not starting with M) will be
91 struct _NotmuchFilterDiscardNonTerm {
92 GMimeFilter parent_object;
94 int first_skipping_state;
95 const scanner_state_t *states;
98 struct _NotmuchFilterDiscardNonTermClass {
99 GMimeFilterClass parent_class;
102 static GMimeFilter *notmuch_filter_discard_non_term_new (void);
104 static void notmuch_filter_discard_non_term_finalize (GObject *object);
106 static GMimeFilter *filter_copy (GMimeFilter *filter);
107 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
108 char **out, size_t *outlen, size_t *outprespace);
109 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
110 char **out, size_t *outlen, size_t *outprespace);
111 static void filter_reset (GMimeFilter *filter);
114 static GMimeFilterClass *parent_class = NULL;
117 notmuch_filter_discard_non_term_class_init (NotmuchFilterDiscardNonTermClass *klass)
119 GObjectClass *object_class = G_OBJECT_CLASS (klass);
120 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
122 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
124 object_class->finalize = notmuch_filter_discard_non_term_finalize;
126 filter_class->copy = filter_copy;
127 filter_class->filter = filter_filter;
128 filter_class->complete = filter_complete;
129 filter_class->reset = filter_reset;
133 notmuch_filter_discard_non_term_finalize (GObject *object)
135 G_OBJECT_CLASS (parent_class)->finalize (object);
139 filter_copy (GMimeFilter *gmime_filter)
142 return notmuch_filter_discard_non_term_new ();
146 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
147 char **outbuf, size_t *outlen, size_t *outprespace)
149 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
150 const scanner_state_t *states = filter->states;
151 register const char *inptr = inbuf;
152 const char *inend = inbuf + inlen;
159 g_mime_filter_set_size (gmime_filter, inlen, FALSE);
160 outptr = gmime_filter->outbuf;
162 while (inptr < inend) {
163 if (*inptr >= states[filter->state].a &&
164 *inptr <= states[filter->state].b)
166 next = states[filter->state].next_if_match;
170 next = states[filter->state].next_if_not_match;
173 if (filter->state < filter->first_skipping_state)
176 filter->state = next;
180 *outlen = outptr - gmime_filter->outbuf;
181 *outprespace = gmime_filter->outpre;
182 *outbuf = gmime_filter->outbuf;
186 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
187 char **outbuf, size_t *outlen, size_t *outprespace)
190 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
194 filter_reset (GMimeFilter *gmime_filter)
196 NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
202 * notmuch_filter_discard_non_term_new:
204 * Returns: a new #NotmuchFilterDiscardNonTerm filter.
207 notmuch_filter_discard_non_term_new (void)
209 static GType type = 0;
210 NotmuchFilterDiscardNonTerm *filter;
213 static const GTypeInfo info = {
214 sizeof (NotmuchFilterDiscardNonTermClass),
215 NULL, /* base_class_init */
216 NULL, /* base_class_finalize */
217 (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
218 NULL, /* class_finalize */
219 NULL, /* class_data */
220 sizeof (NotmuchFilterDiscardNonTerm),
222 NULL, /* instance_init */
223 NULL /* value_table */
226 type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
229 filter = (NotmuchFilterDiscardNonTerm *) g_object_newv (type, 0, NULL);
231 filter->states = uuencode_states;
232 filter->first_skipping_state = first_uuencode_skipping_state;
234 return (GMimeFilter *) filter;
237 /* We're finally down to a single (NAME + address) email "mailbox". */
239 _index_address_mailbox (notmuch_message_t *message,
240 const char *prefix_name,
241 InternetAddress *address)
243 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
244 const char *name, *addr, *combined;
245 void *local = talloc_new (message);
247 name = internet_address_get_name (address);
248 addr = internet_address_mailbox_get_addr (mailbox);
250 /* Combine the name and address and index them as a phrase. */
252 combined = talloc_asprintf (local, "%s %s", name, addr);
259 _notmuch_message_gen_terms (message, prefix_name, combined);
265 _index_address_list (notmuch_message_t *message,
266 const char *prefix_name,
267 InternetAddressList *addresses);
269 /* The outer loop over the InternetAddressList wasn't quite enough.
270 * There can actually be a tree here where a single member of the list
271 * is a "group" containing another list. Recurse please.
274 _index_address_group (notmuch_message_t *message,
275 const char *prefix_name,
276 InternetAddress *address)
278 InternetAddressGroup *group;
279 InternetAddressList *list;
281 group = INTERNET_ADDRESS_GROUP (address);
282 list = internet_address_group_get_members (group);
287 _index_address_list (message, prefix_name, list);
291 _index_address_list (notmuch_message_t *message,
292 const char *prefix_name,
293 InternetAddressList *addresses)
296 InternetAddress *address;
298 if (addresses == NULL)
301 for (i = 0; i < internet_address_list_length (addresses); i++) {
302 address = internet_address_list_get_address (addresses, i);
303 if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
304 _index_address_mailbox (message, prefix_name, address);
305 } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
306 _index_address_group (message, prefix_name, address);
308 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
313 /* Callback to generate terms for each mime part of a message. */
315 _index_mime_part (notmuch_message_t *message,
318 GMimeStream *stream, *filter;
319 GMimeFilter *discard_non_term_filter;
320 GMimeDataWrapper *wrapper;
321 GByteArray *byte_array;
322 GMimeContentDisposition *disposition;
327 _notmuch_database_log (_notmuch_message_database (message),
328 "Warning: Not indexing empty mime part.\n");
332 GMimeContentType *content_type = g_mime_object_get_content_type(part);
334 char *mime_string = g_mime_content_type_to_string(content_type);
337 _notmuch_message_gen_terms (message, "mimetype", mime_string);
342 if (GMIME_IS_MULTIPART (part)) {
343 GMimeMultipart *multipart = GMIME_MULTIPART (part);
346 if (GMIME_IS_MULTIPART_SIGNED (multipart))
347 _notmuch_message_add_term (message, "tag", "signed");
349 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
350 _notmuch_message_add_term (message, "tag", "encrypted");
352 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
353 if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
354 /* Don't index the signature. */
358 _notmuch_database_log (_notmuch_message_database (message),
359 "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
361 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
362 /* Don't index encrypted parts. */
365 _index_mime_part (message,
366 g_mime_multipart_get_part (multipart, i));
371 if (GMIME_IS_MESSAGE_PART (part)) {
372 GMimeMessage *mime_message;
374 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
376 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
381 if (! (GMIME_IS_PART (part))) {
382 _notmuch_database_log (_notmuch_message_database (message),
383 "Warning: Not indexing unknown mime part: %s.\n",
384 g_type_name (G_OBJECT_TYPE (part)));
388 disposition = g_mime_object_get_content_disposition (part);
390 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
391 GMIME_DISPOSITION_ATTACHMENT) == 0)
393 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
395 _notmuch_message_add_term (message, "tag", "attachment");
396 _notmuch_message_gen_terms (message, "attachment", filename);
398 /* XXX: Would be nice to call out to something here to parse
399 * the attachment into text and then index that. */
403 byte_array = g_byte_array_new ();
405 stream = g_mime_stream_mem_new_with_byte_array (byte_array);
406 g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
408 filter = g_mime_stream_filter_new (stream);
409 discard_non_term_filter = notmuch_filter_discard_non_term_new ();
411 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
412 discard_non_term_filter);
414 charset = g_mime_object_get_content_type_parameter (part, "charset");
416 GMimeFilter *charset_filter;
417 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
418 /* This result can be NULL for things like "unknown-8bit".
419 * Don't set a NULL filter as that makes GMime print
420 * annoying assertion-failure messages on stderr. */
421 if (charset_filter) {
422 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
424 g_object_unref (charset_filter);
428 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
430 g_mime_data_wrapper_write_to_stream (wrapper, filter);
432 g_object_unref (stream);
433 g_object_unref (filter);
434 g_object_unref (discard_non_term_filter);
436 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
437 body = (char *) g_byte_array_free (byte_array, FALSE);
440 _notmuch_message_gen_terms (message, NULL, body);
447 _notmuch_message_index_file (notmuch_message_t *message,
448 notmuch_message_file_t *message_file)
450 GMimeMessage *mime_message;
451 InternetAddressList *addresses;
452 const char *from, *subject;
453 notmuch_status_t status;
455 status = _notmuch_message_file_get_mime_message (message_file,
460 from = g_mime_message_get_sender (mime_message);
462 addresses = internet_address_list_parse_string (from);
464 _index_address_list (message, "from", addresses);
465 g_object_unref (addresses);
468 addresses = g_mime_message_get_all_recipients (mime_message);
470 _index_address_list (message, "to", addresses);
471 g_object_unref (addresses);
474 subject = g_mime_message_get_subject (mime_message);
475 _notmuch_message_gen_terms (message, "subject", subject);
477 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
479 return NOTMUCH_STATUS_SUCCESS;