]> git.notmuchmail.org Git - notmuch/blob - index.cc
Add full-text indexing using the GMime library for parsing.
[notmuch] / 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
24 #include <xapian.h>
25
26 /* We're finally down to a single (NAME + address) email "mailbox". */
27 static void
28 _index_address_mailbox (notmuch_message_t *message,
29                         const char *prefix_name,
30                         InternetAddress *address)
31 {
32     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
33     const char *name, *addr = internet_address_mailbox_get_addr (mailbox);
34     int own_name = 0;
35
36     if (addr)
37         _notmuch_message_gen_terms (message, prefix_name, addr);
38
39     name = internet_address_get_name (address);
40
41     /* In the absence of a name, we'll strip the part before the @
42      * from the address. */
43     if (! name) {
44         const char *at;
45
46         at = strchr (addr, '@');
47         if (at) {
48             name = strndup (addr, at - addr);
49             own_name = 1;
50         }
51     }
52
53     if (name)
54         _notmuch_message_gen_terms (message, prefix_name, name);
55 }
56
57 static void
58 _index_address_list (notmuch_message_t *message,
59                      const char *prefix_name,
60                      InternetAddressList *addresses);
61
62 /* The outer loop over the InternetAddressList wasn't quite enough.
63  * There can actually be a tree here where a single member of the list
64  * is a "group" containing another list. Recurse please.
65  */
66 static void
67 _index_address_group (notmuch_message_t *message,
68                       const char *prefix_name,
69                       InternetAddress *address)
70 {
71     InternetAddressGroup *group;
72     InternetAddressList *list;
73
74     group = INTERNET_ADDRESS_GROUP (address);
75     list = internet_address_group_get_members (group);
76
77     if (! list)
78         return;
79
80     _index_address_list (message, prefix_name, list);
81 }
82
83 static void
84 _index_address_list (notmuch_message_t *message,
85                      const char *prefix_name,
86                      InternetAddressList *addresses)
87 {
88     int i;
89     InternetAddress *address;
90
91     if (addresses == NULL)
92         return;
93
94     for (i = 0; i < internet_address_list_length (addresses); i++) {
95         address = internet_address_list_get_address (addresses, i);
96         if (INTERNET_ADDRESS_IS_MAILBOX (address)) {
97             _index_address_mailbox (message, prefix_name, address);
98         } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
99             _index_address_group (message, prefix_name, address);
100         } else {
101             INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
102         }
103     }
104 }
105
106 static const char *
107 skip_re_in_subject (const char *subject)
108 {
109     const char *s = subject;
110
111     if (subject == NULL)
112         return NULL;
113
114     while (*s) {
115         while (*s && isspace (*s))
116             s++;
117         if (strncasecmp (s, "re:", 3) == 0)
118             s += 3;
119         else
120             break;
121     }
122
123     return s;
124 }
125
126 /* Callback to generate terms for each mime part of a message. */
127 static void
128 _index_mime_part (notmuch_message_t *message,
129                   GMimeObject *part)
130 {
131     GMimeStream *stream;
132     GMimeDataWrapper *wrapper;
133     GByteArray *byte_array;
134     GMimeContentDisposition *disposition;
135     char *body;
136
137     if (GMIME_IS_MULTIPART (part)) {
138         GMimeMultipart *multipart = GMIME_MULTIPART (part);
139         int i;
140
141         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
142             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
143                 /* Don't index the signature. */
144                 if (i == 1)
145                     continue;
146                 if (i > 1)
147                     fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Indexing anyway.\n");
148             }
149             _index_mime_part (message,
150                               g_mime_multipart_get_part (multipart, i));
151         }
152         return;
153     }
154
155     if (GMIME_IS_MESSAGE_PART (part)) {
156         GMimeMessage *mime_message;
157
158         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
159
160         _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
161
162         return;
163     }
164
165     if (! (GMIME_IS_PART (part))) {
166         fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",
167                  g_type_name (G_OBJECT_TYPE (part)));
168         return;
169     }
170
171     disposition = g_mime_object_get_content_disposition (part);
172     if (disposition &&
173         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
174     {
175         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
176
177         _notmuch_message_add_term (message, "tag", "attachment");
178         _notmuch_message_gen_terms (message, "attachment", filename);
179
180         /* XXX: Would be nice to call out to something here to parse
181          * the attachment into text and then index that. */
182         return;
183     }
184
185     byte_array = g_byte_array_new ();
186
187     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
188     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
189     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
190     if (wrapper)
191         g_mime_data_wrapper_write_to_stream (wrapper, stream);
192
193     g_object_unref (stream);
194
195     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
196     body = (char *) g_byte_array_free (byte_array, FALSE);
197
198     _notmuch_message_gen_terms (message, NULL, body);
199
200     free (body);
201 }
202
203 notmuch_status_t
204 _notmuch_message_index_file (notmuch_message_t *message,
205                              const char *filename)
206 {
207     GMimeStream *stream = NULL;
208     GMimeParser *parser = NULL;
209     GMimeMessage *mime_message = NULL;
210     InternetAddressList *addresses;
211     FILE *file = NULL;
212     const char *from, *subject;
213     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
214     static int initialized = 0;
215
216     if (! initialized) {
217         g_mime_init (0);
218         initialized = 1;
219     }
220
221     file = fopen (filename, "r");
222     if (! file) {
223         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
224         ret = NOTMUCH_STATUS_FILE_ERROR;
225         goto DONE;
226     }
227
228     /* Evil GMime steals my FILE* here so I won't fclose it. */
229     stream = g_mime_stream_file_new (file);
230
231     parser = g_mime_parser_new_with_stream (stream);
232
233     mime_message = g_mime_parser_construct_message (parser);
234
235     from = g_mime_message_get_sender (mime_message);
236     addresses = internet_address_list_parse_string (from);
237
238     _index_address_list (message, "from", addresses);
239
240     addresses = g_mime_message_get_all_recipients (mime_message);
241     _index_address_list (message, "to", addresses);
242
243     subject = g_mime_message_get_subject (mime_message);
244     subject = skip_re_in_subject (subject);
245     _notmuch_message_gen_terms (message, "subject", subject);
246
247     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
248
249   DONE:
250     if (mime_message)
251         g_object_unref (mime_message);
252
253     if (parser)
254         g_object_unref (parser);
255
256     if (stream)
257         g_object_unref (stream);
258
259     return ret;
260 }