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;
37 name = internet_address_get_name (address);
38 addr = internet_address_mailbox_get_addr (mailbox);
42 contact = talloc_asprintf (message, "\"%s\" <%s>",
44 _notmuch_message_add_term (message, "contact", contact);
45 talloc_free (contact);
47 _notmuch_message_add_term (message, "contact", addr);
51 /* In the absence of a name, we'll strip the part before the @
52 * from the address. */
56 at = strchr (addr, '@');
58 name = strndup (addr, at - addr);
64 _notmuch_message_gen_terms (message, prefix_name, name);
66 _notmuch_message_gen_terms (message, prefix_name, addr);
70 _index_address_list (notmuch_message_t *message,
71 const char *prefix_name,
72 InternetAddressList *addresses);
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.
79 _index_address_group (notmuch_message_t *message,
80 const char *prefix_name,
81 InternetAddress *address)
83 InternetAddressGroup *group;
84 InternetAddressList *list;
86 group = INTERNET_ADDRESS_GROUP (address);
87 list = internet_address_group_get_members (group);
92 _index_address_list (message, prefix_name, list);
96 _index_address_list (notmuch_message_t *message,
97 const char *prefix_name,
98 InternetAddressList *addresses)
101 InternetAddress *address;
103 if (addresses == NULL)
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);
113 INTERNAL_ERROR ("GMime InternetAddress is neither a mailbox nor a group.\n");
119 skip_re_in_subject (const char *subject)
121 const char *s = subject;
127 while (*s && isspace (*s))
129 if (strncasecmp (s, "re:", 3) == 0)
138 /* Given a string representing the body of a message, generate terms
139 * for it, (skipping quoted portions and signatures).
141 * This function is evil in that it modifies the string passed to it,
142 * (changing some newlines into '\0').
145 _index_body_text (notmuch_message_t *message, char *body)
147 char *line, *line_end, *next_line;
159 next_line = strchr (line, '\n');
160 if (next_line == NULL) {
161 next_line = line + strlen (line);
163 line_end = next_line - 1;
165 /* Get to the next non-blank line. */
166 while (*next_line == '\n')
169 /* Skip blank lines. */
173 /* Skip lines that are quotes. */
177 /* Also skip lines introducing a quote on the next line. */
178 if (*line_end == ':' && *next_line == '>')
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)
186 *(line_end + 1) = '\0';
188 _notmuch_message_gen_terms (message, NULL, line);
192 /* Callback to generate terms for each mime part of a message. */
194 _index_mime_part (notmuch_message_t *message,
198 GMimeDataWrapper *wrapper;
199 GByteArray *byte_array;
200 GMimeContentDisposition *disposition;
203 if (GMIME_IS_MULTIPART (part)) {
204 GMimeMultipart *multipart = GMIME_MULTIPART (part);
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. */
213 fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Indexing anyway.\n");
215 _index_mime_part (message,
216 g_mime_multipart_get_part (multipart, i));
221 if (GMIME_IS_MESSAGE_PART (part)) {
222 GMimeMessage *mime_message;
224 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
226 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
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)));
237 disposition = g_mime_object_get_content_disposition (part);
239 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
241 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
243 _notmuch_message_add_term (message, "tag", "attachment");
244 _notmuch_message_gen_terms (message, "attachment", filename);
246 /* XXX: Would be nice to call out to something here to parse
247 * the attachment into text and then index that. */
251 byte_array = g_byte_array_new ();
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));
257 g_mime_data_wrapper_write_to_stream (wrapper, stream);
259 g_object_unref (stream);
261 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
262 body = (char *) g_byte_array_free (byte_array, FALSE);
264 _index_body_text (message, body);
270 _notmuch_message_index_file (notmuch_message_t *message,
271 const char *filename)
273 GMimeStream *stream = NULL;
274 GMimeParser *parser = NULL;
275 GMimeMessage *mime_message = NULL;
276 InternetAddressList *addresses;
278 const char *from, *subject;
279 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
280 static int initialized = 0;
287 file = fopen (filename, "r");
289 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
290 ret = NOTMUCH_STATUS_FILE_ERROR;
294 /* Evil GMime steals my FILE* here so I won't fclose it. */
295 stream = g_mime_stream_file_new (file);
297 parser = g_mime_parser_new_with_stream (stream);
299 mime_message = g_mime_parser_construct_message (parser);
301 from = g_mime_message_get_sender (mime_message);
302 addresses = internet_address_list_parse_string (from);
304 _index_address_list (message, "from", addresses);
306 addresses = g_mime_message_get_all_recipients (mime_message);
307 _index_address_list (message, "to", addresses);
309 subject = g_mime_message_get_subject (mime_message);
310 subject = skip_re_in_subject (subject);
311 _notmuch_message_gen_terms (message, "subject", subject);
313 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
317 g_object_unref (mime_message);
320 g_object_unref (parser);
323 g_object_unref (stream);