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;
34 void *local = talloc_new (message);
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 = talloc_strndup (local, addr, at - addr);
50 _notmuch_message_gen_terms (message, prefix_name, name);
52 _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 /* Callback to generate terms for each mime part of a message. */
128 _index_mime_part (notmuch_message_t *message,
132 GMimeDataWrapper *wrapper;
133 GByteArray *byte_array;
134 GMimeContentDisposition *disposition;
137 if (GMIME_IS_MULTIPART (part)) {
138 GMimeMultipart *multipart = GMIME_MULTIPART (part);
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. */
147 fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
149 _index_mime_part (message,
150 g_mime_multipart_get_part (multipart, i));
155 if (GMIME_IS_MESSAGE_PART (part)) {
156 GMimeMessage *mime_message;
158 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
160 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
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)));
171 disposition = g_mime_object_get_content_disposition (part);
173 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
175 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
177 _notmuch_message_add_term (message, "tag", "attachment");
178 _notmuch_message_gen_terms (message, "attachment", filename);
180 /* XXX: Would be nice to call out to something here to parse
181 * the attachment into text and then index that. */
185 byte_array = g_byte_array_new ();
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));
191 g_mime_data_wrapper_write_to_stream (wrapper, stream);
193 g_object_unref (stream);
195 g_byte_array_append (byte_array, (guint8 *) "\0", 1);
196 body = (char *) g_byte_array_free (byte_array, FALSE);
199 _notmuch_message_gen_terms (message, NULL, body);
206 _notmuch_message_index_file (notmuch_message_t *message,
207 const char *filename)
209 GMimeStream *stream = NULL;
210 GMimeParser *parser = NULL;
211 GMimeMessage *mime_message = NULL;
212 InternetAddressList *addresses;
214 const char *from, *subject;
215 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
216 static int initialized = 0;
223 file = fopen (filename, "r");
225 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
226 ret = NOTMUCH_STATUS_FILE_ERROR;
230 /* Evil GMime steals my FILE* here so I won't fclose it. */
231 stream = g_mime_stream_file_new (file);
233 parser = g_mime_parser_new_with_stream (stream);
235 mime_message = g_mime_parser_construct_message (parser);
237 from = g_mime_message_get_sender (mime_message);
238 addresses = internet_address_list_parse_string (from);
240 _index_address_list (message, "from", addresses);
242 addresses = g_mime_message_get_all_recipients (mime_message);
243 _index_address_list (message, "to", addresses);
245 subject = g_mime_message_get_subject (mime_message);
246 subject = skip_re_in_subject (subject);
247 _notmuch_message_gen_terms (message, "subject", subject);
249 _index_mime_part (message, g_mime_message_get_mime_part (mime_message));
253 g_object_unref (mime_message);
256 g_object_unref (parser);
259 g_object_unref (stream);