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 http://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 #define NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE (notmuch_filter_discard_uuencode_get_type ())
29 #define NOTMUCH_FILTER_DISCARD_UUENCODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE, NotmuchFilterDiscardUuencode))
30 #define NOTMUCH_FILTER_DISCARD_UUENCODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE, NotmuchFilterDiscardUuencodeClass))
31 #define NOTMUCH_IS_FILTER_DISCARD_UUENCODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE))
32 #define NOTMUCH_IS_FILTER_DISCARD_UUENCODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE))
33 #define NOTMUCH_FILTER_DISCARD_UUENCODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE, NotmuchFilterDiscardUuencodeClass))
35 typedef struct _NotmuchFilterDiscardUuencode NotmuchFilterDiscardUuencode;
36 typedef struct _NotmuchFilterDiscardUuencodeClass NotmuchFilterDiscardUuencodeClass;
39 * NotmuchFilterDiscardUuencode:
41 * @parent_object: parent #GMimeFilter
42 * @encode: encoding vs decoding
43 * @state: State of the parser
45 * A filter to discard uuencoded portions of an email.
47 * A uuencoded portion is identified as beginning with a line
50 * begin [0-7][0-7][0-7] .*
52 * After that detection, and beginning with the following line,
53 * characters will be discarded as long as the first character of each
54 * line begins with M and subsequent characters on the line are within
55 * the range of ASCII characters from ' ' to '`'.
57 * This is not a perfect UUencode filter. It's possible to have a
58 * message that will legitimately match that pattern, (so that some
59 * legitimate content is discarded). And for most UUencoded files, the
60 * final line of encoded data (the line not starting with M) will be
63 struct _NotmuchFilterDiscardUuencode {
64 GMimeFilter parent_object;
68 struct _NotmuchFilterDiscardUuencodeClass {
69 GMimeFilterClass parent_class;
72 GType notmuch_filter_discard_uuencode_get_type (void);
74 GMimeFilter *notmuch_filter_discard_uuencode_new (void);
76 static void notmuch_filter_discard_uuencode_class_init (NotmuchFilterDiscardUuencodeClass *klass);
77 static void notmuch_filter_discard_uuencode_init (NotmuchFilterDiscardUuencode *filter, NotmuchFilterDiscardUuencodeClass *klass);
78 static void notmuch_filter_discard_uuencode_finalize (GObject *object);
80 static GMimeFilter *filter_copy (GMimeFilter *filter);
81 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
82 char **out, size_t *outlen, size_t *outprespace);
83 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
84 char **out, size_t *outlen, size_t *outprespace);
85 static void filter_reset (GMimeFilter *filter);
88 static GMimeFilterClass *parent_class = NULL;
91 notmuch_filter_discard_uuencode_get_type (void)
93 static GType type = 0;
96 static const GTypeInfo info = {
97 sizeof (NotmuchFilterDiscardUuencodeClass),
98 NULL, /* base_class_init */
99 NULL, /* base_class_finalize */
100 (GClassInitFunc) notmuch_filter_discard_uuencode_class_init,
101 NULL, /* class_finalize */
102 NULL, /* class_data */
103 sizeof (NotmuchFilterDiscardUuencode),
105 (GInstanceInitFunc) notmuch_filter_discard_uuencode_init,
106 NULL /* value_table */
109 type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardUuencode", &info, (GTypeFlags) 0);
116 notmuch_filter_discard_uuencode_class_init (NotmuchFilterDiscardUuencodeClass *klass)
118 GObjectClass *object_class = G_OBJECT_CLASS (klass);
119 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
121 parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
123 object_class->finalize = notmuch_filter_discard_uuencode_finalize;
125 filter_class->copy = filter_copy;
126 filter_class->filter = filter_filter;
127 filter_class->complete = filter_complete;
128 filter_class->reset = filter_reset;
132 notmuch_filter_discard_uuencode_init (NotmuchFilterDiscardUuencode *filter, NotmuchFilterDiscardUuencodeClass *klass)
139 notmuch_filter_discard_uuencode_finalize (GObject *object)
141 G_OBJECT_CLASS (parent_class)->finalize (object);
145 filter_copy (GMimeFilter *gmime_filter)
148 return notmuch_filter_discard_uuencode_new ();
152 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
153 char **outbuf, size_t *outlen, size_t *outprespace)
155 NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
156 register const char *inptr = inbuf;
157 const char *inend = inbuf + inlen;
162 /* Simple, linear state-transition diagram for our filter.
164 * If the character being processed is within the range of [a, b]
165 * for the current state then we transition next_if_match
166 * state. If not, we transition to the next_if_not_match state.
168 * The final two states are special in that they are the states in
169 * which we discard data. */
170 static const struct {
175 int next_if_not_match;
186 {9, ' ', ' ', 10, 0},
187 {10, '\n', '\n', 11, 10},
188 {11, 'M', 'M', 12, 0},
189 {12, ' ', '`', 12, 11}
193 g_mime_filter_set_size (gmime_filter, inlen, FALSE);
194 outptr = gmime_filter->outbuf;
196 while (inptr < inend) {
197 if (*inptr >= states[filter->state].a &&
198 *inptr <= states[filter->state].b)
200 next = states[filter->state].next_if_match;
204 next = states[filter->state].next_if_not_match;
207 if (filter->state < 11)
210 filter->state = next;
214 *outlen = outptr - gmime_filter->outbuf;
215 *outprespace = gmime_filter->outpre;
216 *outbuf = gmime_filter->outbuf;
220 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
221 char **outbuf, size_t *outlen, size_t *outprespace)
224 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
228 filter_reset (GMimeFilter *gmime_filter)
230 NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
236 * notmuch_filter_discard_uuencode_new:
238 * Returns: a new #NotmuchFilterDiscardUuencode filter.
241 notmuch_filter_discard_uuencode_new (void)
243 NotmuchFilterDiscardUuencode *filter;
245 filter = (NotmuchFilterDiscardUuencode *) g_object_newv (NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE, 0, NULL);
248 return (GMimeFilter *) filter;
251 /* We're finally down to a single (NAME + address) email "mailbox". */
253 _index_address_mailbox (notmuch_message_t *message,
254 const char *prefix_name,
255 InternetAddress *address)
257 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
258 const char *name, *addr;
259 void *local = talloc_new (message);
261 name = internet_address_get_name (address);
262 addr = internet_address_mailbox_get_addr (mailbox);
264 /* In the absence of a name, we'll strip the part before the @
265 * from the address. */
269 at = strchr (addr, '@');
271 name = talloc_strndup (local, addr, at - addr);
275 _notmuch_message_gen_terms (message, prefix_name, name);
277 _notmuch_message_gen_terms (message, prefix_name, addr);
283 _index_address_list (notmuch_message_t *message,
284 const char *prefix_name,
285 InternetAddressList *addresses);
287 /* The outer loop over the InternetAddressList wasn't quite enough.
288 * There can actually be a tree here where a single member of the list
289 * is a "group" containing another list. Recurse please.
292 _index_address_group (notmuch_message_t *message,
293 const char *prefix_name,
294 InternetAddress *address)
296 InternetAddressGroup *group;
297 InternetAddressList *list;
299 group = INTERNET_ADDRESS_GROUP (address);
300 list = internet_address_group_get_members (group);
305 _index_address_list (message, prefix_name, list);
309 _index_address_list (notmuch_message_t *message,
310 const char *prefix_name,
311 InternetAddressList *addresses)
314 InternetAddress *address;
316 if (addresses == NULL)
319 for (i = 0; i < internet_address_list_length (addresses); i++) {
320 address = internet_address_list_get_address (addresses, i);
321 if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
322 _index_address_mailbox (message, prefix_name, address);
323 } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
324 _index_address_group (message, prefix_name, address);
326 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
332 skip_re_in_subject (const char *subject)
334 const char *s = subject;
340 while (*s && isspace (*s))
342 if (strncasecmp (s, "re:", 3) == 0)
351 /* Callback to generate terms for each mime part of a message. */
353 _index_mime_part (notmuch_message_t *message,
356 GMimeStream *stream, *filter;
357 GMimeFilter *discard_uuencode_filter;
358 GMimeDataWrapper *wrapper;
359 GByteArray *byte_array;
360 GMimeContentDisposition *disposition;
363 if (GMIME_IS_MULTIPART (part)) {
364 GMimeMultipart *multipart = GMIME_MULTIPART (part);
367 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
368 if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
369 /* Don't index the signature. */
373 fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
375 _index_mime_part (message,
376 g_mime_multipart_get_part (multipart, i));
381 if (GMIME_IS_MESSAGE_PART (part)) {
382 GMimeMessage *mime_message;
384 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
386 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
391 if (! (GMIME_IS_PART (part))) {
392 fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",
393 g_type_name (G_OBJECT_TYPE (part)));
397 disposition = g_mime_object_get_content_disposition (part);
399 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
401 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
403 _notmuch_message_add_term (message, "tag", "attachment");
404 _notmuch_message_gen_terms (message, "attachment", filename);
406 /* XXX: Would be nice to call out to something here to parse
407 * the attachment into text and then index that. */
411 byte_array = g_byte_array_new ();
413 stream = g_mime_stream_mem_new_with_byte_array (byte_array);
414 g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
416 filter = g_mime_stream_filter_new (stream);
417 discard_uuencode_filter = notmuch_filter_discard_uuencode_new ();
419 g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
420 discard_uuencode_filter);
422 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
424 g_mime_data_wrapper_write_to_stream (wrapper, filter);
426 g_object_unref (stream);
427 g_object_unref (filter);
428 g_object_unref (discard_uuencode_filter);
430 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
431 body = (char *) g_byte_array_free (byte_array, FALSE);
434 _notmuch_message_gen_terms (message, NULL, body);
441 _notmuch_message_index_file (notmuch_message_t *message,
442 const char *filename)
444 GMimeStream *stream = NULL;
445 GMimeParser *parser = NULL;
446 GMimeMessage *mime_message = NULL;
447 InternetAddressList *addresses;
449 const char *from, *subject;
450 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
451 static int initialized = 0;
458 file = fopen (filename, "r");
460 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
461 ret = NOTMUCH_STATUS_FILE_ERROR;
465 /* Evil GMime steals my FILE* here so I won't fclose it. */
466 stream = g_mime_stream_file_new (file);
468 parser = g_mime_parser_new_with_stream (stream);
470 mime_message = g_mime_parser_construct_message (parser);
472 from = g_mime_message_get_sender (mime_message);
473 addresses = internet_address_list_parse_string (from);
475 _index_address_list (message, "from", addresses);
477 addresses = g_mime_message_get_all_recipients (mime_message);
478 _index_address_list (message, "to", addresses);
480 subject = g_mime_message_get_subject (mime_message);
481 subject = skip_re_in_subject (subject);
482 _notmuch_message_gen_terms (message, "subject", subject);
484 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
488 g_object_unref (mime_message);
491 g_object_unref (parser);
494 g_object_unref (stream);