]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
lib/index: separate state table definition from scanner.
[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     while (inptr < inend) {
163         if (*inptr >= states[filter->state].a &&
164             *inptr <= states[filter->state].b)
165         {
166             next = states[filter->state].next_if_match;
167         }
168         else
169         {
170             next = states[filter->state].next_if_not_match;
171         }
172
173         if (filter->state < filter->first_skipping_state)
174             *outptr++ = *inptr;
175
176         filter->state = next;
177         inptr++;
178     }
179
180     *outlen = outptr - gmime_filter->outbuf;
181     *outprespace = gmime_filter->outpre;
182     *outbuf = gmime_filter->outbuf;
183 }
184
185 static void
186 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
187                  char **outbuf, size_t *outlen, size_t *outprespace)
188 {
189     if (inbuf && inlen)
190         filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
191 }
192
193 static void
194 filter_reset (GMimeFilter *gmime_filter)
195 {
196     NotmuchFilterDiscardNonTerm *filter = (NotmuchFilterDiscardNonTerm *) gmime_filter;
197
198     filter->state = 0;
199 }
200
201 /**
202  * notmuch_filter_discard_non_term_new:
203  *
204  * Returns: a new #NotmuchFilterDiscardNonTerm filter.
205  **/
206 static GMimeFilter *
207 notmuch_filter_discard_non_term_new (void)
208 {
209     static GType type = 0;
210     NotmuchFilterDiscardNonTerm *filter;
211
212     if (!type) {
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),
221             0,    /* n_preallocs */
222             NULL, /* instance_init */
223             NULL  /* value_table */
224         };
225
226         type = g_type_register_static (GMIME_TYPE_FILTER, "NotmuchFilterDiscardNonTerm", &info, (GTypeFlags) 0);
227     }
228
229     filter = (NotmuchFilterDiscardNonTerm *) g_object_newv (type, 0, NULL);
230     filter->state = 0;
231     filter->states = uuencode_states;
232     filter->first_skipping_state = first_uuencode_skipping_state;
233
234     return (GMimeFilter *) filter;
235 }
236
237 /* We're finally down to a single (NAME + address) email "mailbox". */
238 static void
239 _index_address_mailbox (notmuch_message_t *message,
240                         const char *prefix_name,
241                         InternetAddress *address)
242 {
243     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
244     const char *name, *addr, *combined;
245     void *local = talloc_new (message);
246
247     name = internet_address_get_name (address);
248     addr = internet_address_mailbox_get_addr (mailbox);
249
250     /* Combine the name and address and index them as a phrase. */
251     if (name && addr)
252         combined = talloc_asprintf (local, "%s %s", name, addr);
253     else if (name)
254         combined = name;
255     else
256         combined = addr;
257
258     if (combined)
259         _notmuch_message_gen_terms (message, prefix_name, combined);
260
261     talloc_free (local);
262 }
263
264 static void
265 _index_address_list (notmuch_message_t *message,
266                      const char *prefix_name,
267                      InternetAddressList *addresses);
268
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.
272  */
273 static void
274 _index_address_group (notmuch_message_t *message,
275                       const char *prefix_name,
276                       InternetAddress *address)
277 {
278     InternetAddressGroup *group;
279     InternetAddressList *list;
280
281     group = INTERNET_ADDRESS_GROUP (address);
282     list = internet_address_group_get_members (group);
283
284     if (! list)
285         return;
286
287     _index_address_list (message, prefix_name, list);
288 }
289
290 static void
291 _index_address_list (notmuch_message_t *message,
292                      const char *prefix_name,
293                      InternetAddressList *addresses)
294 {
295     int i;
296     InternetAddress *address;
297
298     if (addresses == NULL)
299         return;
300
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);
307         } else {
308             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
309         }
310     }
311 }
312
313 /* Callback to generate terms for each mime part of a message. */
314 static void
315 _index_mime_part (notmuch_message_t *message,
316                   GMimeObject *part)
317 {
318     GMimeStream *stream, *filter;
319     GMimeFilter *discard_non_term_filter;
320     GMimeDataWrapper *wrapper;
321     GByteArray *byte_array;
322     GMimeContentDisposition *disposition;
323     char *body;
324     const char *charset;
325
326     if (! part) {
327         _notmuch_database_log (_notmuch_message_database (message),
328                               "Warning: Not indexing empty mime part.\n");
329         return;
330     }
331
332     GMimeContentType *content_type = g_mime_object_get_content_type(part);
333     if (content_type) {
334         char *mime_string = g_mime_content_type_to_string(content_type);
335         if (mime_string)
336         {
337             _notmuch_message_gen_terms (message, "mimetype", mime_string);
338             g_free(mime_string);
339         }
340     }
341
342     if (GMIME_IS_MULTIPART (part)) {
343         GMimeMultipart *multipart = GMIME_MULTIPART (part);
344         int i;
345
346         if (GMIME_IS_MULTIPART_SIGNED (multipart))
347           _notmuch_message_add_term (message, "tag", "signed");
348
349         if (GMIME_IS_MULTIPART_ENCRYPTED (multipart))
350           _notmuch_message_add_term (message, "tag", "encrypted");
351
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. */
355                 if (i == 1)
356                     continue;
357                 if (i > 1)
358                     _notmuch_database_log (_notmuch_message_database (message),
359                                           "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
360             }
361             if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
362                 /* Don't index encrypted parts. */
363                 continue;
364             }
365             _index_mime_part (message,
366                               g_mime_multipart_get_part (multipart, i));
367         }
368         return;
369     }
370
371     if (GMIME_IS_MESSAGE_PART (part)) {
372         GMimeMessage *mime_message;
373
374         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
375
376         _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
377
378         return;
379     }
380
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)));
385         return;
386     }
387
388     disposition = g_mime_object_get_content_disposition (part);
389     if (disposition &&
390         strcasecmp (g_mime_content_disposition_get_disposition (disposition),
391                     GMIME_DISPOSITION_ATTACHMENT) == 0)
392     {
393         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
394
395         _notmuch_message_add_term (message, "tag", "attachment");
396         _notmuch_message_gen_terms (message, "attachment", filename);
397
398         /* XXX: Would be nice to call out to something here to parse
399          * the attachment into text and then index that. */
400         return;
401     }
402
403     byte_array = g_byte_array_new ();
404
405     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
406     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
407
408     filter = g_mime_stream_filter_new (stream);
409     discard_non_term_filter = notmuch_filter_discard_non_term_new ();
410
411     g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
412                               discard_non_term_filter);
413
414     charset = g_mime_object_get_content_type_parameter (part, "charset");
415     if (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),
423                                       charset_filter);
424             g_object_unref (charset_filter);
425         }
426     }
427
428     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
429     if (wrapper)
430         g_mime_data_wrapper_write_to_stream (wrapper, filter);
431
432     g_object_unref (stream);
433     g_object_unref (filter);
434     g_object_unref (discard_non_term_filter);
435
436     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
437     body = (char *) g_byte_array_free (byte_array, FALSE);
438
439     if (body) {
440         _notmuch_message_gen_terms (message, NULL, body);
441
442         free (body);
443     }
444 }
445
446 notmuch_status_t
447 _notmuch_message_index_file (notmuch_message_t *message,
448                              notmuch_message_file_t *message_file)
449 {
450     GMimeMessage *mime_message;
451     InternetAddressList *addresses;
452     const char *from, *subject;
453     notmuch_status_t status;
454
455     status = _notmuch_message_file_get_mime_message (message_file,
456                                                      &mime_message);
457     if (status)
458         return status;
459
460     from = g_mime_message_get_sender (mime_message);
461
462     addresses = internet_address_list_parse_string (from);
463     if (addresses) {
464         _index_address_list (message, "from", addresses);
465         g_object_unref (addresses);
466     }
467
468     addresses = g_mime_message_get_all_recipients (mime_message);
469     if (addresses) {
470         _index_address_list (message, "to", addresses);
471         g_object_unref (addresses);
472     }
473
474     subject = g_mime_message_get_subject (mime_message);
475     _notmuch_message_gen_terms (message, "subject", subject);
476
477     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
478
479     return NOTMUCH_STATUS_SUCCESS;
480 }