]> git.notmuchmail.org Git - notmuch/blob - lib/index.cc
Merge branch 'upstream'
[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     void *local = talloc_new (message);
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 = talloc_strndup (local, addr, at - addr);
47     }
48
49     if (name)
50         _notmuch_message_gen_terms (message, prefix_name, name);
51     if (addr)
52         _notmuch_message_gen_terms (message, prefix_name, addr);
53
54     talloc_free (local);
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 multipart/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     if (body) {
199         _notmuch_message_gen_terms (message, NULL, body);
200
201         free (body);
202     }
203 }
204
205 notmuch_status_t
206 _notmuch_message_index_file (notmuch_message_t *message,
207                              const char *filename)
208 {
209     GMimeStream *stream = NULL;
210     GMimeParser *parser = NULL;
211     GMimeMessage *mime_message = NULL;
212     InternetAddressList *addresses;
213     FILE *file = NULL;
214     const char *from, *subject;
215     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
216     static int initialized = 0;
217
218     if (! initialized) {
219         g_mime_init (0);
220         initialized = 1;
221     }
222
223     file = fopen (filename, "r");
224     if (! file) {
225         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
226         ret = NOTMUCH_STATUS_FILE_ERROR;
227         goto DONE;
228     }
229
230     /* Evil GMime steals my FILE* here so I won't fclose it. */
231     stream = g_mime_stream_file_new (file);
232
233     parser = g_mime_parser_new_with_stream (stream);
234
235     mime_message = g_mime_parser_construct_message (parser);
236
237     from = g_mime_message_get_sender (mime_message);
238     addresses = internet_address_list_parse_string (from);
239
240     _index_address_list (message, "from", addresses);
241
242     addresses = g_mime_message_get_all_recipients (mime_message);
243     _index_address_list (message, "to", addresses);
244
245     subject = g_mime_message_get_subject (mime_message);
246     subject = skip_re_in_subject (subject);
247     _notmuch_message_gen_terms (message, "subject", subject);
248
249     _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
250
251   DONE:
252     if (mime_message)
253         g_object_unref (mime_message);
254
255     if (parser)
256         g_object_unref (parser);
257
258     if (stream)
259         g_object_unref (stream);
260
261     return ret;
262 }