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>
27 /* Oh, how I wish that gobject didn't require so much noisy boilerplate!
28 * (Though I have at least eliminated some of the stock set...) */
29 typedef struct _NotmuchFilterDiscardUuencode NotmuchFilterDiscardUuencode;
30 typedef struct _NotmuchFilterDiscardUuencodeClass NotmuchFilterDiscardUuencodeClass;
33 * NotmuchFilterDiscardUuencode:
35 * @parent_object: parent #GMimeFilter
36 * @encode: encoding vs decoding
37 * @state: State of the parser
39 * A filter to discard uuencoded portions of an email.
41 * A uuencoded portion is identified as beginning with a line
44 * begin [0-7][0-7][0-7] .*
46 * After that detection, and beginning with the following line,
47 * characters will be discarded as long as the first character of each
48 * line begins with M and subsequent characters on the line are within
49 * the range of ASCII characters from ' ' to '`'.
51 * This is not a perfect UUencode filter. It's possible to have a
52 * message that will legitimately match that pattern, (so that some
53 * legitimate content is discarded). And for most UUencoded files, the
54 * final line of encoded data (the line not starting with M) will be
57 struct _NotmuchFilterDiscardUuencode {
58 GMimeFilter parent_object;
62 struct _NotmuchFilterDiscardUuencodeClass {
63 GMimeFilterClass parent_class;
66 static GMimeFilter *notmuch_filter_discard_uuencode_new (void);
68 static void notmuch_filter_discard_uuencode_finalize (GObject *object);
70 static GMimeFilter *filter_copy (GMimeFilter *filter);
71 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
72 char **out, size_t *outlen, size_t *outprespace);
73 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
74 char **out, size_t *outlen, size_t *outprespace);
75 static void filter_reset (GMimeFilter *filter);
78 static GMimeFilterClass *parent_class = NULL;
81 notmuch_filter_discard_uuencode_class_init (NotmuchFilterDiscardUuencodeClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
86 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
88 object_class->finalize = notmuch_filter_discard_uuencode_finalize;
90 filter_class->copy = filter_copy;
91 filter_class->filter = filter_filter;
92 filter_class->complete = filter_complete;
93 filter_class->reset = filter_reset;
97 notmuch_filter_discard_uuencode_finalize (GObject *object)
99 G_OBJECT_CLASS (parent_class)->finalize (object);
103 filter_copy (GMimeFilter *gmime_filter)
106 return notmuch_filter_discard_uuencode_new ();
110 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
111 char **outbuf, size_t *outlen, size_t *outprespace)
113 NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
114 register const char *inptr = inbuf;
115 const char *inend = inbuf + inlen;
120 /* Simple, linear state-transition diagram for our filter.
122 * If the character being processed is within the range of [a, b]
123 * for the current state then we transition next_if_match
124 * state. If not, we transition to the next_if_not_match state.
126 * The final two states are special in that they are the states in
127 * which we discard data. */
128 static const struct {
133 int next_if_not_match;
144 {9, ' ', ' ', 10, 0},
145 {10, '\n', '\n', 11, 10},
146 {11, 'M', 'M', 12, 0},
147 {12, ' ', '`', 12, 11}
151 g_mime_filter_set_size (gmime_filter, inlen, FALSE);
152 outptr = gmime_filter->outbuf;
154 while (inptr < inend) {
155 if (*inptr >= states[filter->state].a &&
156 *inptr <= states[filter->state].b)
158 next = states[filter->state].next_if_match;
162 next = states[filter->state].next_if_not_match;
165 if (filter->state < 11)
168 filter->state = next;
172 *outlen = outptr - gmime_filter->outbuf;
173 *outprespace = gmime_filter->outpre;
174 *outbuf = gmime_filter->outbuf;
178 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
179 char **outbuf, size_t *outlen, size_t *outprespace)
182 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
186 filter_reset (GMimeFilter *gmime_filter)
188 NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
194 * notmuch_filter_discard_uuencode_new:
196 * Returns: a new #NotmuchFilterDiscardUuencode filter.
199 notmuch_filter_discard_uuencode_new (void)
201 static GType type = 0;
202 NotmuchFilterDiscardUuencode *filter;
205 static const GTypeInfo info = {
206 sizeof (NotmuchFilterDiscardUuencodeClass),
207 NULL, /* base_class_init */
208 NULL, /* base_class_finalize */
209 (GClassInitFunc) notmuch_filter_discard_uuencode_class_init,
210 NULL, /* class_finalize */
211 NULL, /* class_data */
212 sizeof (NotmuchFilterDiscardUuencode),
214 NULL, /* instance_init */
215 NULL /* value_table */
218 type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardUuencode", &info, (GTypeFlags) 0);
221 filter = (NotmuchFilterDiscardUuencode *) g_object_newv (type, 0, NULL);
224 return (GMimeFilter *) filter;
227 /* We're finally down to a single (NAME + address) email "mailbox". */
229 _index_address_mailbox (notmuch_message_t *message,
230 const char *prefix_name,
231 InternetAddress *address)
233 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
234 const char *name, *addr, *combined;
235 void *local = talloc_new (message);
237 name = internet_address_get_name (address);
238 addr = internet_address_mailbox_get_addr (mailbox);
240 /* Combine the name and address and index them as a phrase. */
242 combined = talloc_asprintf (local, "%s %s", name, addr);
249 _notmuch_message_gen_terms (message, prefix_name, combined);
255 _index_address_list (notmuch_message_t *message,
256 const char *prefix_name,
257 InternetAddressList *addresses);
259 /* The outer loop over the InternetAddressList wasn't quite enough.
260 * There can actually be a tree here where a single member of the list
261 * is a "group" containing another list. Recurse please.
264 _index_address_group (notmuch_message_t *message,
265 const char *prefix_name,
266 InternetAddress *address)
268 InternetAddressGroup *group;
269 InternetAddressList *list;
271 group = INTERNET_ADDRESS_GROUP (address);
272 list = internet_address_group_get_members (group);
277 _index_address_list (message, prefix_name, list);
281 _index_address_list (notmuch_message_t *message,
282 const char *prefix_name,
283 InternetAddressList *addresses)
286 InternetAddress *address;
288 if (addresses == NULL)
291 for (i = 0; i < internet_address_list_length (addresses); i++) {
292 address = internet_address_list_get_address (addresses, i);
293 if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
294 _index_address_mailbox (message, prefix_name, address);
295 } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
296 _index_address_group (message, prefix_name, address);
298 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
303 /* Callback to generate terms for each mime part of a message. */
305 _index_mime_part (notmuch_message_t *message,
308 GMimeStream *stream, *filter;
309 GMimeFilter *discard_uuencode_filter;
310 GMimeDataWrapper *wrapper;
311 GByteArray *byte_array;
312 GMimeContentDisposition *disposition;
317 _notmuch_database_log (_notmuch_message_database (message),
318 "Warning: Not indexing empty mime part.\n");
322 GMimeContentType *content_type = g_mime_object_get_content_type(part);
324 char *mime_string = g_mime_content_type_to_string(content_type);
327 _notmuch_message_gen_terms (message, "mimetype", mime_string);
332 if (GMIME_IS_MULTIPART (part)) {
333 GMimeMultipart *multipart = GMIME_MULTIPART (part);
336 if (GMIME_IS_MULTIPART_SIGNED (multipart))
337 _notmuch_message_add_term (message, "tag", "signed");
339 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
340 _notmuch_message_add_term (message, "tag", "encrypted");
342 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
343 if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
344 /* Don't index the signature. */
348 _notmuch_database_log (_notmuch_message_database (message),
349 "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
351 if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
352 /* Don't index encrypted parts. */
355 _index_mime_part (message,
356 g_mime_multipart_get_part (multipart, i));
361 if (GMIME_IS_MESSAGE_PART (part)) {
362 GMimeMessage *mime_message;
364 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
366 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
371 if (! (GMIME_IS_PART (part))) {
372 _notmuch_database_log (_notmuch_message_database (message),
373 "Warning: Not indexing unknown mime part: %s.\n",
374 g_type_name (G_OBJECT_TYPE (part)));
378 disposition = g_mime_object_get_content_disposition (part);
380 strcasecmp (g_mime_content_disposition_get_disposition (disposition),
381 GMIME_DISPOSITION_ATTACHMENT) == 0)
383 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
385 _notmuch_message_add_term (message, "tag", "attachment");
386 _notmuch_message_gen_terms (message, "attachment", filename);
388 /* XXX: Would be nice to call out to something here to parse
389 * the attachment into text and then index that. */
393 byte_array = g_byte_array_new ();
395 stream = g_mime_stream_mem_new_with_byte_array (byte_array);
396 g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
398 filter = g_mime_stream_filter_new (stream);
399 discard_uuencode_filter = notmuch_filter_discard_uuencode_new ();
401 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
402 discard_uuencode_filter);
404 charset = g_mime_object_get_content_type_parameter (part, "charset");
406 GMimeFilter *charset_filter;
407 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
408 /* This result can be NULL for things like "unknown-8bit".
409 * Don't set a NULL filter as that makes GMime print
410 * annoying assertion-failure messages on stderr. */
411 if (charset_filter) {
412 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
414 g_object_unref (charset_filter);
418 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
420 g_mime_data_wrapper_write_to_stream (wrapper, filter);
422 g_object_unref (stream);
423 g_object_unref (filter);
424 g_object_unref (discard_uuencode_filter);
426 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
427 body = (char *) g_byte_array_free (byte_array, FALSE);
430 _notmuch_message_gen_terms (message, NULL, body);
437 _notmuch_message_index_file (notmuch_message_t *message,
438 notmuch_message_file_t *message_file)
440 GMimeMessage *mime_message;
441 InternetAddressList *addresses;
442 const char *from, *subject;
443 notmuch_status_t status;
445 status = _notmuch_message_file_get_mime_message (message_file,
450 from = g_mime_message_get_sender (mime_message);
452 addresses = internet_address_list_parse_string (from);
454 _index_address_list (message, "from", addresses);
455 g_object_unref (addresses);
458 addresses = g_mime_message_get_all_recipients (mime_message);
460 _index_address_list (message, "to", addresses);
461 g_object_unref (addresses);
464 subject = g_mime_message_get_subject (mime_message);
465 _notmuch_message_gen_terms (message, "subject", subject);
467 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
469 return NOTMUCH_STATUS_SUCCESS;