]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
lib/index.cc: generalize filter state machine
[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 https://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
28 typedef struct {
29     int state;
30     int a;
31     int b;
32     int next_if_match;
33     int next_if_not_match;
34 } scanner_state_t;
35
36 /* Simple, linear state-transition diagram for the uuencode filter.
37  *
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.
41  *
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[] = {
46     {0,  'b',  'b',  1,  0},
47     {1,  'e',  'e',  2,  0},
48     {2,  'g',  'g',  3,  0},
49     {3,  'i',  'i',  4,  0},
50     {4,  'n',  'n',  5,  0},
51     {5,  ' ',  ' ',  6,  0},
52     {6,  '0',  '7',  7,  0},
53     {7,  '0',  '7',  8,  0},
54     {8,  '0',  '7',  9,  0},
55     {9,  ' ',  ' ',  10, 0},
56     {10, '\n', '\n', 11, 10},
57     {11, 'M',  'M',  12, 0},
58     {12, ' ',  '`',  12, 11}
59 };
60
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;
65
66 /**
67  * NotmuchFilterDiscardNonTerm:
68  *
69  * @parent_object: parent #GMimeFilter
70  * @encode: encoding vs decoding
71  * @state: State of the parser
72  *
73  * A filter to discard uuencoded portions of an email.
74  *
75  * A uuencoded portion is identified as beginning with a line
76  * matching:
77  *
78  *      begin [0-7][0-7][0-7] .*
79  *
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 '`'.
84  *
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
89  * indexed.
90  **/
91 struct _NotmuchFilterDiscardNonTerm {
92     GMimeFilter parent_object;
93     int state;
94     int first_skipping_state;
95     const scanner_state_t *states;
96 };
97
98 struct _NotmuchFilterDiscardNonTermClass {
99     GMimeFilterClass parent_class;
100 };
101
102 static GMimeFilter *notmuch_filter_discard_non_term_new (void);
103
104 static void notmuch_filter_discard_non_term_finalize (GObject *object);
105
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);
112
113
114 static GMimeFilterClass *parent_class = NULL;
115
116 static void
117 notmuch_filter_discard_non_term_class_init (NotmuchFilterDiscardNonTermClass *klass)
118 {
119     GObjectClass *object_class = G_OBJECT_CLASS (klass);
120     GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
121
122     parent_class = (GMimeFilterClass *) g_type_class_ref (GMIME_TYPE_FILTER);
123
124     object_class->finalize = notmuch_filter_discard_non_term_finalize;
125
126     filter_class->copy = filter_copy;
127     filter_class->filter = filter_filter;
128     filter_class->complete = filter_complete;
129     filter_class->reset = filter_reset;
130 }
131
132 static void
133 notmuch_filter_discard_non_term_finalize (GObject *object)
134 {
135     G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137
138 static GMimeFilter *
139 filter_copy (GMimeFilter *gmime_filter)
140 {
141     (void) gmime_filter;
142     return notmuch_filter_discard_non_term_new ();
143 }
144
145 static void
146 filter_filter (GMimeFilter *gmime_filter, char *inbuf, size_t inlen, size_t prespace,
147                char **outbuf, size_t *outlen, size_t *outprespace)
148 {
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;
153     char *outptr;
154
155     (void) prespace;
156
157     int next;
158
159     g_mime_filter_set_size (gmime_filter, inlen, FALSE);
160     outptr = gmime_filter->outbuf;
161
162     next = filter->state;
163     while (inptr < inend) {
164          /* Each state is defined by a contiguous set of rows of the
165          * state table marked by a common value for '.state'. The
166          * state numbers must be equal to the index of the first row
167          * in a given state; thus the loop condition here looks for a
168          * jump to a first row of a state, which is a real transition
169          * in the underlying DFA.
170          */
171         do {
172             if (*inptr >= states[next].a && *inptr <= states[next].b)  {
173                 next = states[next].next_if_match;
174             } else  {
175                 next = states[next].next_if_not_match;
176             }
177
178         } while (next != states[next].state);
179
180         if (filter->state < filter->first_skipping_state)
181             *outptr++ = *inptr;
182
183         filter->state = next;
184         inptr++;
185     }
186
187     *outlen = outptr - gmime_filter->outbuf;
188     *outprespace = gmime_filter->outpre;
189     *outbuf = gmime_filter->outbuf;
190 }
191
192 static void
193 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
194                  char **outbuf, size_t *outlen, size_t *outprespace)
195 {
196     if (inbuf && inlen)
197         filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
198 }
199
200 static void
201 filter_reset (GMimeFilter *gmime_filter)
202 {
203     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
204
205     filter->state = 0;
206 }
207
208 /**
209  * notmuch_filter_discard_non_term_new:
210  *
211  * Returns: a new #NotmuchFilterDiscardNonTerm filter.
212  **/
213 static GMimeFilter *
214 notmuch_filter_discard_non_term_new (void)
215 {
216     static GType type = 0;
217     NotmuchFilterDiscardNonTerm *filter;
218
219     if (!type) {
220         static const GTypeInfo info = {
221             sizeof (NotmuchFilterDiscardNonTermClass),
222             NULL, /* base_class_init */
223             NULL, /* base_class_finalize */
224             (GClassInitFunc) notmuch_filter_discard_non_term_class_init,
225             NULL, /* class_finalize */
226             NULL, /* class_data */
227             sizeof (NotmuchFilterDiscardNonTerm),
228             0,    /* n_preallocs */
229             NULL, /* instance_init */
230             NULL  /* value_table */
231         };
232
233         type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
234     }
235
236     filter = (NotmuchFilterDiscardNonTerm *) g_object_newv (type, 0, NULL);
237     filter->state = 0;
238     filter->states = uuencode_states;
239     filter->first_skipping_state = first_uuencode_skipping_state;
240
241     return (GMimeFilter *) filter;
242 }
243
244 /* We're finally down to a single (NAME + address) email "mailbox". */
245 static void
246 _index_address_mailbox (notmuch_message_t *message,
247                         const char *prefix_name,
248                         InternetAddress *address)
249 {
250     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
251     const char *name, *addr, *combined;
252     void *local = talloc_new (message);
253
254     name = internet_address_get_name (address);
255     addr = internet_address_mailbox_get_addr (mailbox);
256
257     /* Combine the name and address and index them as a phrase. */
258     if (name && addr)
259         combined = talloc_asprintf (local, "%s %s", name, addr);
260     else if (name)
261         combined = name;
262     else
263         combined = addr;
264
265     if (combined)
266         _notmuch_message_gen_terms (message, prefix_name, combined);
267
268     talloc_free (local);
269 }
270
271 static void
272 _index_address_list (notmuch_message_t *message,
273                      const char *prefix_name,
274                      InternetAddressList *addresses);
275
276 /* The outer loop over the InternetAddressList wasn't quite enough.
277  * There can actually be a tree here where a single member of the list
278  * is a "group" containing another list. Recurse please.
279  */
280 static void
281 _index_address_group (notmuch_message_t *message,
282                       const char *prefix_name,
283                       InternetAddress *address)
284 {
285     InternetAddressGroup *group;
286     InternetAddressList *list;
287
288     group = INTERNET_ADDRESS_GROUP (address);
289     list = internet_address_group_get_members (group);
290
291     if (! list)
292         return;
293
294     _index_address_list (message, prefix_name, list);
295 }
296
297 static void
298 _index_address_list (notmuch_message_t *message,
299                      const char *prefix_name,
300                      InternetAddressList *addresses)
301 {
302     int i;
303     InternetAddress *address;
304
305     if (addresses == NULL)
306         return;
307
308     for (i = 0; i < internet_address_list_length (addresses); i++) {
309         address = internet_address_list_get_address (addresses, i);
310         if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
311             _index_address_mailbox (message, prefix_name, address);
312         } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
313             _index_address_group (message, prefix_name, address);
314         } else {
315             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
316         }
317     }
318 }
319
320 /* Callback to generate terms for each mime part of a message. */
321 static void
322 _index_mime_part (notmuch_message_t *message,
323                   GMimeObject *part)
324 {
325     GMimeStream *stream, *filter;
326     GMimeFilter *discard_non_term_filter;
327     GMimeDataWrapper *wrapper;
328     GByteArray *byte_array;
329     GMimeContentDisposition *disposition;
330     char *body;
331     const char *charset;
332
333     if (! part) {
334         _notmuch_database_log (_notmuch_message_database (message),
335                               "Warning: Not indexing empty mime part.\n");
336         return;
337     }
338
339     GMimeContentType *content_type = g_mime_object_get_content_type(part);
340     if (content_type) {
341         char *mime_string = g_mime_content_type_to_string(content_type);
342         if (mime_string)
343         {
344             _notmuch_message_gen_terms (message, "mimetype", mime_string);
345             g_free(mime_string);
346         }
347     }
348
349     if (GMIME_IS_MULTIPART (part)) {
350         GMimeMultipart *multipart = GMIME_MULTIPART (part);
351         int i;
352
353         if (GMIME_IS_MULTIPART_SIGNED (multipart))
354           _notmuch_message_add_term (message, "tag", "signed");
355
356         if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
357           _notmuch_message_add_term (message, "tag", "encrypted");
358
359         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
360             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
361                 /* Don't index the signature. */
362                 if (i == 1)
363                     continue;
364                 if (i > 1)
365                     _notmuch_database_log (_notmuch_message_database (message),
366                                           "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
367             }
368             if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
369                 /* Don't index encrypted parts. */
370                 continue;
371             }
372             _index_mime_part (message,
373                               g_mime_multipart_get_part (multipart, i));
374         }
375         return;
376     }
377
378     if (GMIME_IS_MESSAGE_PART (part)) {
379         GMimeMessage *mime_message;
380
381         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
382
383         _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
384
385         return;
386     }
387
388     if (! (GMIME_IS_PART (part))) {
389         _notmuch_database_log (_notmuch_message_database (message),
390                               "Warning: Not indexing unknown mime part: %s.\n",
391                               g_type_name (G_OBJECT_TYPE (part)));
392         return;
393     }
394
395     disposition = g_mime_object_get_content_disposition (part);
396     if (disposition &&
397         strcasecmp (g_mime_content_disposition_get_disposition (disposition),
398                     GMIME_DISPOSITION_ATTACHMENT) == 0)
399     {
400         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
401
402         _notmuch_message_add_term (message, "tag", "attachment");
403         _notmuch_message_gen_terms (message, "attachment", filename);
404
405         /* XXX: Would be nice to call out to something here to parse
406          * the attachment into text and then index that. */
407         return;
408     }
409
410     byte_array = g_byte_array_new ();
411
412     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
413     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
414
415     filter = g_mime_stream_filter_new (stream);
416     discard_non_term_filter = notmuch_filter_discard_non_term_new ();
417
418     g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
419                               discard_non_term_filter);
420
421     charset = g_mime_object_get_content_type_parameter (part, "charset");
422     if (charset) {
423         GMimeFilter *charset_filter;
424         charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
425         /* This result can be NULL for things like "unknown-8bit".
426          * Don't set a NULL filter as that makes GMime print
427          * annoying assertion-failure messages on stderr. */
428         if (charset_filter) {
429             g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
430                                       charset_filter);
431             g_object_unref (charset_filter);
432         }
433     }
434
435     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
436     if (wrapper)
437         g_mime_data_wrapper_write_to_stream (wrapper, filter);
438
439     g_object_unref (stream);
440     g_object_unref (filter);
441     g_object_unref (discard_non_term_filter);
442
443     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
444     body = (char *) g_byte_array_free (byte_array, FALSE);
445
446     if (body) {
447         _notmuch_message_gen_terms (message, NULL, body);
448
449         free (body);
450     }
451 }
452
453 notmuch_status_t
454 _notmuch_message_index_file (notmuch_message_t *message,
455                              notmuch_message_file_t *message_file)
456 {
457     GMimeMessage *mime_message;
458     InternetAddressList *addresses;
459     const char *from, *subject;
460     notmuch_status_t status;
461
462     status = _notmuch_message_file_get_mime_message (message_file,
463                                                      &mime_message);
464     if (status)
465         return status;
466
467     from = g_mime_message_get_sender (mime_message);
468
469     addresses = internet_address_list_parse_string (from);
470     if (addresses) {
471         _index_address_list (message, "from", addresses);
472         g_object_unref (addresses);
473     }
474
475     addresses = g_mime_message_get_all_recipients (mime_message);
476     if (addresses) {
477         _index_address_list (message, "to", addresses);
478         g_object_unref (addresses);
479     }
480
481     subject = g_mime_message_get_subject (mime_message);
482     _notmuch_message_gen_terms (message, "subject", subject);
483
484     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
485
486     return NOTMUCH_STATUS_SUCCESS;
487 }