]> git.notmuchmail.org Git - notmuch/blob - index.cc
notmuch.el: Make magic space bar advance to next unread messages.
[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 /* Given a string representing the body of a message, generate terms
139  * for it, (skipping quoted portions and signatures).
140  *
141  * This function is evil in that it modifies the string passed to it,
142  * (changing some newlines into '\0').
143  */
144 static void
145 _index_body_text (notmuch_message_t *message, char *body)
146 {
147     char *line, *line_end, *next_line;
148
149     if (body == NULL)
150         return;
151
152     next_line = body;
153
154     while (1) {
155         line = next_line;
156         if (*line == '\0')
157             break;
158
159         next_line = strchr (line, '\n');
160         if (next_line == NULL) {
161             next_line = line + strlen (line);
162         }
163         line_end = next_line - 1;
164
165         /* Get to the next non-blank line. */
166         while (*next_line == '\n')
167             next_line++;
168
169         /* Skip blank lines. */
170         if (line_end < line)
171             continue;
172
173         /* Skip lines that are quotes. */
174         if (*line == '>')
175             continue;
176
177         /* Also skip lines introducing a quote on the next line. */
178         if (*line_end == ':' && *next_line == '>')
179             continue;
180
181         /* Finally, bail as soon as we see a signature. */
182         /* XXX: Should only do this if "near" the end of the message. */
183         if (strncmp (line, "-- ", 3) == 0)
184             break;
185
186         *(line_end + 1) = '\0';
187
188         _notmuch_message_gen_terms (message, NULL, line);
189     }
190 }
191
192 /* Callback to generate terms for each mime part of a message. */
193 static void
194 _index_mime_part (notmuch_message_t *message,
195                   GMimeObject *part)
196 {
197     GMimeStream *stream;
198     GMimeDataWrapper *wrapper;
199     GByteArray *byte_array;
200     GMimeContentDisposition *disposition;
201     char *body;
202
203     if (GMIME_IS_MULTIPART (part)) {
204         GMimeMultipart *multipart = GMIME_MULTIPART (part);
205         int i;
206
207         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
208             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
209                 /* Don't index the signature. */
210                 if (i == 1)
211                     continue;
212                 if (i > 1)
213                     fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Indexing anyway.\n");
214             }
215             _index_mime_part (message,
216                               g_mime_multipart_get_part (multipart, i));
217         }
218         return;
219     }
220
221     if (GMIME_IS_MESSAGE_PART (part)) {
222         GMimeMessage *mime_message;
223
224         mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
225
226         _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
227
228         return;
229     }
230
231     if (! (GMIME_IS_PART (part))) {
232         fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",
233                  g_type_name (G_OBJECT_TYPE (part)));
234         return;
235     }
236
237     disposition = g_mime_object_get_content_disposition (part);
238     if (disposition &&
239         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
240     {
241         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
242
243         _notmuch_message_add_term (message, "tag", "attachment");
244         _notmuch_message_gen_terms (message, "attachment", filename);
245
246         /* XXX: Would be nice to call out to something here to parse
247          * the attachment into text and then index that. */
248         return;
249     }
250
251     byte_array = g_byte_array_new ();
252
253     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
254     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
255     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
256     if (wrapper)
257         g_mime_data_wrapper_write_to_stream (wrapper, stream);
258
259     g_object_unref (stream);
260
261     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
262     body = (char *) g_byte_array_free (byte_array, FALSE);
263
264     _index_body_text (message, body);
265
266     free (body);
267 }
268
269 notmuch_status_t
270 _notmuch_message_index_file (notmuch_message_t *message,
271                              const char *filename)
272 {
273     GMimeStream *stream = NULL;
274     GMimeParser *parser = NULL;
275     GMimeMessage *mime_message = NULL;
276     InternetAddressList *addresses;
277     FILE *file = NULL;
278     const char *from, *subject;
279     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
280     static int initialized = 0;
281
282     if (! initialized) {
283         g_mime_init (0);
284         initialized = 1;
285     }
286
287     file = fopen (filename, "r");
288     if (! file) {
289         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
290         ret = NOTMUCH_STATUS_FILE_ERROR;
291         goto DONE;
292     }
293
294     /* Evil GMime steals my FILE* here so I won't fclose it. */
295     stream = g_mime_stream_file_new (file);
296
297     parser = g_mime_parser_new_with_stream (stream);
298
299     mime_message = g_mime_parser_construct_message (parser);
300
301     from = g_mime_message_get_sender (mime_message);
302     addresses = internet_address_list_parse_string (from);
303
304     _index_address_list (message, "from", addresses);
305
306     addresses = g_mime_message_get_all_recipients (mime_message);
307     _index_address_list (message, "to", addresses);
308
309     subject = g_mime_message_get_subject (mime_message);
310     subject = skip_re_in_subject (subject);
311     _notmuch_message_gen_terms (message, "subject", subject);
312
313     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
314
315   DONE:
316     if (mime_message)
317         g_object_unref (mime_message);
318
319     if (parser)
320         g_object_unref (parser);
321
322     if (stream)
323         g_object_unref (stream);
324
325     return ret;
326 }