]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
notmuch new: Don't index uuencoded data.
[notmuch] / lib / index.cc
1 /*
2  * Copyright © 2009 Carl Worth
3  *
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.
8  *
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.
13  *
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/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #include "notmuch-private.h"
21
22 #include <gmime/gmime.h>
23 #include <gmime/gmime-filter.h>
24
25 #include <xapian.h>
26
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))
34
35 typedef struct _NotmuchFilterDiscardUuencode NotmuchFilterDiscardUuencode;
36 typedef struct _NotmuchFilterDiscardUuencodeClass NotmuchFilterDiscardUuencodeClass;
37
38 /**
39  * NotmuchFilterDiscardUuencode:
40  *
41  * @parent_object: parent #GMimeFilter
42  * @encode: encoding vs decoding
43  * @state: State of the parser
44  *
45  * A filter to discard uuencoded portions of an email.
46  *
47  * A uuencoded portion is identified as beginning with a line
48  * matching:
49  *
50  *      begin [0-7][0-7][0-7] .*
51  *
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 '`'.
56  *
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
61  * indexed.
62  **/
63 struct _NotmuchFilterDiscardUuencode {
64     GMimeFilter parent_object;
65     int state;
66 };
67
68 struct _NotmuchFilterDiscardUuencodeClass {
69     GMimeFilterClass parent_class;
70 };
71
72 GType notmuch_filter_discard_uuencode_get_type (void);
73
74 GMimeFilter *notmuch_filter_discard_uuencode_new (void);
75
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);
79
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);
86
87
88 static GMimeFilterClass *parent_class = NULL;
89
90 GType
91 notmuch_filter_discard_uuencode_get_type (void)
92 {
93     static GType type = 0;
94
95     if (!type) {
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),
104             0,    /* n_preallocs */
105             (GInstanceInitFunc) notmuch_filter_discard_uuencode_init,
106             NULL        /* value_table */
107         };
108
109         type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardUuencode", &info, (GTypeFlags) 0);
110     }
111
112     return type;
113 }
114
115 static void
116 notmuch_filter_discard_uuencode_class_init (NotmuchFilterDiscardUuencodeClass *klass)
117 {
118     GObjectClass *object_class = G_OBJECT_CLASS (klass);
119     GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
120
121     parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
122
123     object_class->finalize = notmuch_filter_discard_uuencode_finalize;
124
125     filter_class->copy = filter_copy;
126     filter_class->filter = filter_filter;
127     filter_class->complete = filter_complete;
128     filter_class->reset = filter_reset;
129 }
130
131 static void
132 notmuch_filter_discard_uuencode_init (NotmuchFilterDiscardUuencode *filter, NotmuchFilterDiscardUuencodeClass *klass)
133 {
134     (void) klass;
135     filter->state = 0;
136 }
137
138 static void
139 notmuch_filter_discard_uuencode_finalize (GObject *object)
140 {
141     G_OBJECT_CLASS (parent_class)->finalize (object);
142 }
143
144 static GMimeFilter *
145 filter_copy (GMimeFilter *gmime_filter)
146 {
147     (void) gmime_filter;
148     return notmuch_filter_discard_uuencode_new ();
149 }
150
151 static void
152 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
153                char **outbuf, size_t *outlen, size_t *outprespace)
154 {
155     NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
156     register const char *inptr = inbuf;
157     const char *inend = inbuf + inlen;
158     char *outptr;
159
160     (void) prespace;
161
162     /* Simple, linear state-transition diagram for our filter.
163      *
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.
167      *
168      * The final two states are special in that they are the states in
169      * which we discard data. */
170     static const struct {
171         int state;
172         int a;
173         int b;
174         int next_if_match;
175         int next_if_not_match;
176     } states[] = {
177         {0,  'b',  'b',  1,  0},
178         {1,  'e',  'e',  2,  0},
179         {2,  'g',  'g',  3,  0},
180         {3,  'i',  'i',  4,  0},
181         {4,  'n',  'n',  5,  0},
182         {5,  ' ',  ' ',  6,  0},
183         {6,  '0',  '7',  7,  0},
184         {7,  '0',  '7',  8,  0},
185         {8,  '0',  '7',  9,  0},
186         {9,  ' ',  ' ',  10, 0},
187         {10, '\n', '\n', 11, 10},
188         {11, 'M',  'M',  12, 0},
189         {12, ' ',  '`',  12, 11}  
190     };
191     int next;
192
193     g_mime_filter_set_size (gmime_filter, inlen, FALSE);
194     outptr = gmime_filter->outbuf;
195
196     while (inptr < inend) {
197         if (*inptr >= states[filter->state].a &&
198             *inptr <= states[filter->state].b)
199         {
200             next = states[filter->state].next_if_match;
201         }
202         else
203         {
204             next = states[filter->state].next_if_not_match;
205         }
206
207         if (filter->state < 11)
208             *outptr++ = *inptr;
209
210         filter->state = next;
211         inptr++;
212     }
213
214     *outlen = outptr - gmime_filter->outbuf;
215     *outprespace = gmime_filter->outpre;
216     *outbuf = gmime_filter->outbuf;
217 }
218
219 static void
220 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
221                  char **outbuf, size_t *outlen, size_t *outprespace)
222 {
223     if (inbuf && inlen)
224         filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
225 }
226
227 static void
228 filter_reset (GMimeFilter *gmime_filter)
229 {
230     NotmuchFilterDiscardUuencode *filter = (NotmuchFilterDiscardUuencode *) gmime_filter;
231
232     filter->state = 0;
233 }
234
235 /**
236  * notmuch_filter_discard_uuencode_new:
237  *
238  * Returns: a new #NotmuchFilterDiscardUuencode filter.
239  **/
240 GMimeFilter *
241 notmuch_filter_discard_uuencode_new (void)
242 {
243     NotmuchFilterDiscardUuencode *filter;
244
245     filter = (NotmuchFilterDiscardUuencode *) g_object_newv (NOTMUCH_TYPE_FILTER_DISCARD_UUENCODE, 0, NULL);
246     filter->state = 0;
247
248     return (GMimeFilter *) filter;
249 }
250
251 /* We're finally down to a single (NAME + address) email "mailbox". */
252 static void
253 _index_address_mailbox (notmuch_message_t *message,
254                         const char *prefix_name,
255                         InternetAddress *address)
256 {
257     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
258     const char *name, *addr;
259     void *local = talloc_new (message);
260
261     name = internet_address_get_name (address);
262     addr = internet_address_mailbox_get_addr (mailbox);
263
264     /* In the absence of a name, we'll strip the part before the @
265      * from the address. */
266     if (! name) {
267         const char *at;
268
269         at = strchr (addr, '@');
270         if (at)
271             name = talloc_strndup (local, addr, at - addr);
272     }
273
274     if (name)
275         _notmuch_message_gen_terms (message, prefix_name, name);
276     if (addr)
277         _notmuch_message_gen_terms (message, prefix_name, addr);
278
279     talloc_free (local);
280 }
281
282 static void
283 _index_address_list (notmuch_message_t *message,
284                      const char *prefix_name,
285                      InternetAddressList *addresses);
286
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.
290  */
291 static void
292 _index_address_group (notmuch_message_t *message,
293                       const char *prefix_name,
294                       InternetAddress *address)
295 {
296     InternetAddressGroup *group;
297     InternetAddressList *list;
298
299     group = INTERNET_ADDRESS_GROUP (address);
300     list = internet_address_group_get_members (group);
301
302     if (! list)
303         return;
304
305     _index_address_list (message, prefix_name, list);
306 }
307
308 static void
309 _index_address_list (notmuch_message_t *message,
310                      const char *prefix_name,
311                      InternetAddressList *addresses)
312 {
313     int i;
314     InternetAddress *address;
315
316     if (addresses == NULL)
317         return;
318
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);
325         } else {
326             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
327         }
328     }
329 }
330
331 static const char *
332 skip_re_in_subject (const char *subject)
333 {
334     const char *s = subject;
335
336     if (subject == NULL)
337         return NULL;
338
339     while (*s) {
340         while (*s && isspace (*s))
341             s++;
342         if (strncasecmp (s, "re:", 3) == 0)
343             s += 3;
344         else
345             break;
346     }
347
348     return s;
349 }
350
351 /* Callback to generate terms for each mime part of a message. */
352 static void
353 _index_mime_part (notmuch_message_t *message,
354                   GMimeObject *part)
355 {
356     GMimeStream *stream, *filter;
357     GMimeFilter *discard_uuencode_filter;
358     GMimeDataWrapper *wrapper;
359     GByteArray *byte_array;
360     GMimeContentDisposition *disposition;
361     char *body;
362
363     if (GMIME_IS_MULTIPART (part)) {
364         GMimeMultipart *multipart = GMIME_MULTIPART (part);
365         int i;
366
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. */
370                 if (i == 1)
371                     continue;
372                 if (i > 1)
373                     fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
374             }
375             _index_mime_part (message,
376                               g_mime_multipart_get_part (multipart, i));
377         }
378         return;
379     }
380
381     if (GMIME_IS_MESSAGE_PART (part)) {
382         GMimeMessage *mime_message;
383
384         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
385
386         _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
387
388         return;
389     }
390
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)));
394         return;
395     }
396
397     disposition = g_mime_object_get_content_disposition (part);
398     if (disposition &&
399         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
400     {
401         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
402
403         _notmuch_message_add_term (message, "tag", "attachment");
404         _notmuch_message_gen_terms (message, "attachment", filename);
405
406         /* XXX: Would be nice to call out to something here to parse
407          * the attachment into text and then index that. */
408         return;
409     }
410
411     byte_array = g_byte_array_new ();
412
413     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
414     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
415
416     filter = g_mime_stream_filter_new (stream);
417     discard_uuencode_filter = notmuch_filter_discard_uuencode_new ();
418
419     g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
420                               discard_uuencode_filter);
421
422     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
423     if (wrapper)
424         g_mime_data_wrapper_write_to_stream (wrapper, filter);
425
426     g_object_unref (stream);
427     g_object_unref (filter);
428     g_object_unref (discard_uuencode_filter);
429
430     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
431     body = (char *) g_byte_array_free (byte_array, FALSE);
432
433     if (body) {
434         _notmuch_message_gen_terms (message, NULL, body);
435
436         free (body);
437     }
438 }
439
440 notmuch_status_t
441 _notmuch_message_index_file (notmuch_message_t *message,
442                              const char *filename)
443 {
444     GMimeStream *stream = NULL;
445     GMimeParser *parser = NULL;
446     GMimeMessage *mime_message = NULL;
447     InternetAddressList *addresses;
448     FILE *file = NULL;
449     const char *from, *subject;
450     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
451     static int initialized = 0;
452
453     if (! initialized) {
454         g_mime_init (0);
455         initialized = 1;
456     }
457
458     file = fopen (filename, "r");
459     if (! file) {
460         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
461         ret = NOTMUCH_STATUS_FILE_ERROR;
462         goto DONE;
463     }
464
465     /* Evil GMime steals my FILE* here so I won't fclose it. */
466     stream = g_mime_stream_file_new (file);
467
468     parser = g_mime_parser_new_with_stream (stream);
469
470     mime_message = g_mime_parser_construct_message (parser);
471
472     from = g_mime_message_get_sender (mime_message);
473     addresses = internet_address_list_parse_string (from);
474
475     _index_address_list (message, "from", addresses);
476
477     addresses = g_mime_message_get_all_recipients (mime_message);
478     _index_address_list (message, "to", addresses);
479
480     subject = g_mime_message_get_subject (mime_message);
481     subject = skip_re_in_subject (subject);
482     _notmuch_message_gen_terms (message, "subject", subject);
483
484     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
485
486   DONE:
487     if (mime_message)
488         g_object_unref (mime_message);
489
490     if (parser)
491         g_object_unref (parser);
492
493     if (stream)
494         g_object_unref (stream);
495
496     return ret;
497 }