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