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