2 * Copyright © 2009 Carl Worth
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.
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.
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/ .
17 * Author: Carl Worth <cworth@cworth.org>
20 #include "notmuch-private.h"
22 #include <gmime/gmime.h>
26 /* We're finally down to a single (NAME + address) email "mailbox". */
28 _index_address_mailbox (notmuch_message_t *message,
29 const char *prefix_name,
30 InternetAddress *address)
32 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
33 const char *name, *addr;
36 name = internet_address_get_name (address);
37 addr = internet_address_mailbox_get_addr (mailbox);
39 /* In the absence of a name, we'll strip the part before the @
40 * from the address. */
44 at = strchr (addr, '@');
46 name = strndup (addr, at - addr);
52 _notmuch_message_gen_terms (message, prefix_name, name);
54 _notmuch_message_gen_terms (message, prefix_name, addr);
58 _index_address_list (notmuch_message_t *message,
59 const char *prefix_name,
60 InternetAddressList *addresses);
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.
67 _index_address_group (notmuch_message_t *message,
68 const char *prefix_name,
69 InternetAddress *address)
71 InternetAddressGroup *group;
72 InternetAddressList *list;
74 group = INTERNET_ADDRESS_GROUP (address);
75 list = internet_address_group_get_members (group);
80 _index_address_list (message, prefix_name, list);
84 _index_address_list (notmuch_message_t *message,
85 const char *prefix_name,
86 InternetAddressList *addresses)
89 InternetAddress *address;
91 if (addresses == NULL)
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);
101 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
107 skip_re_in_subject (const char *subject)
109 const char *s = subject;
115 while (*s && isspace (*s))
117 if (strncasecmp (s, "re:", 3) == 0)
126 /* Given a string representing the body of a message, generate terms
127 * for it, (skipping quoted portions and signatures).
129 * This function is evil in that it modifies the string passed to it,
130 * (changing some newlines into '\0').
133 _index_body_text (notmuch_message_t *message, char *body)
135 char *line, *line_end, *next_line;
147 next_line = strchr (line, '\n');
148 if (next_line == NULL) {
149 next_line = line + strlen (line);
151 line_end = next_line - 1;
153 /* Get to the next non-blank line. */
154 while (*next_line == '\n')
157 /* Skip blank lines. */
161 /* Skip lines that are quotes. */
165 /* Also skip lines introducing a quote on the next line. */
166 if (*line_end == ':' && *next_line == '>')
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)
174 *(line_end + 1) = '\0';
176 _notmuch_message_gen_terms (message, NULL, line);
180 /* Callback to generate terms for each mime part of a message. */
182 _index_mime_part (notmuch_message_t *message,
186 GMimeDataWrapper *wrapper;
187 GByteArray *byte_array;
188 GMimeContentDisposition *disposition;
191 if (GMIME_IS_MULTIPART (part)) {
192 GMimeMultipart *multipart = GMIME_MULTIPART (part);
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. */
201 fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
203 _index_mime_part (message,
204 g_mime_multipart_get_part (multipart, i));
209 if (GMIME_IS_MESSAGE_PART (part)) {
210 GMimeMessage *mime_message;
212 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
214 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
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)));
225 disposition = g_mime_object_get_content_disposition (part);
227 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
229 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
231 _notmuch_message_add_term (message, "tag", "attachment");
232 _notmuch_message_gen_terms (message, "attachment", filename);
234 /* XXX: Would be nice to call out to something here to parse
235 * the attachment into text and then index that. */
239 byte_array = g_byte_array_new ();
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));
245 g_mime_data_wrapper_write_to_stream (wrapper, stream);
247 g_object_unref (stream);
249 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
250 body = (char *) g_byte_array_free (byte_array, FALSE);
252 _index_body_text (message, body);
258 _notmuch_message_index_file (notmuch_message_t *message,
259 const char *filename)
261 GMimeStream *stream = NULL;
262 GMimeParser *parser = NULL;
263 GMimeMessage *mime_message = NULL;
264 InternetAddressList *addresses;
266 const char *from, *subject;
267 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
268 static int initialized = 0;
275 file = fopen (filename, "r");
277 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
278 ret = NOTMUCH_STATUS_FILE_ERROR;
282 /* Evil GMime steals my FILE* here so I won't fclose it. */
283 stream = g_mime_stream_file_new (file);
285 parser = g_mime_parser_new_with_stream (stream);
287 mime_message = g_mime_parser_construct_message (parser);
289 from = g_mime_message_get_sender (mime_message);
290 addresses = internet_address_list_parse_string (from);
292 _index_address_list (message, "from", addresses);
294 addresses = g_mime_message_get_all_recipients (mime_message);
295 _index_address_list (message, "to", addresses);
297 subject = g_mime_message_get_subject (mime_message);
298 subject = skip_re_in_subject (subject);
299 _notmuch_message_gen_terms (message, "subject", subject);
301 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
305 g_object_unref (mime_message);
308 g_object_unref (parser);
311 g_object_unref (stream);