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>
28 #include <gmime/gmime.h>
34 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
36 /* Xapian complains if we provide a term longer than this. */
37 #define NOTMUCH_MAX_TERM 245
39 /* These prefix values are specifically chosen to be compatible
40 * with sup, (http://sup.rubyforge.org), written by
41 * William Morgan <wmorgan-sup@masanjin.net>, and released
42 * under the GNU GPL v2.
50 prefix_t NORMAL_PREFIX[] = {
53 { "from_name", "FN" },
59 prefix_t BOOLEAN_PREFIX[] = {
61 { "from_email", "FE" },
67 { "attachment_extension", "O" },
73 /* Similarly, these value numbers are also chosen to be sup
77 NOTMUCH_VALUE_MESSAGE_ID = 0,
78 NOTMUCH_VALUE_THREAD = 1,
79 NOTMUCH_VALUE_DATE = 2
83 find_prefix (const char *name)
87 for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
88 if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
89 return NORMAL_PREFIX[i].prefix;
91 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
92 if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
93 return BOOLEAN_PREFIX[i].prefix;
98 int TERM_COMBINED = 0;
101 add_term (Xapian::Document doc,
102 const char *prefix_name,
111 prefix = find_prefix (prefix_name);
113 term = g_strdup_printf ("%s%s", prefix, value);
115 if (strlen (term) <= NOTMUCH_MAX_TERM)
122 gen_terms (Xapian::TermGenerator term_gen,
123 const char *prefix_name,
131 prefix = find_prefix (prefix_name);
133 term_gen.index_text (text, 1, prefix);
137 gen_terms_address_name (Xapian::TermGenerator term_gen,
138 InternetAddress *address,
139 const char *prefix_name)
143 name = internet_address_get_name (address);
146 gen_terms (term_gen, prefix_name, name);
150 gen_terms_address_names (Xapian::TermGenerator term_gen,
151 InternetAddressList *addresses,
152 const char *address_type)
155 InternetAddress *address;
157 for (i = 0; i < internet_address_list_length (addresses); i++) {
158 address = internet_address_list_get_address (addresses, i);
159 gen_terms_address_name (term_gen, address, address_type);
160 gen_terms_address_name (term_gen, address, "name");
161 gen_terms_address_name (term_gen, address, "body");
166 add_term_address_addr (Xapian::Document doc,
167 InternetAddress *address,
168 const char *prefix_name)
170 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
173 addr = internet_address_mailbox_get_addr (mailbox);
176 add_term (doc, prefix_name, addr);
180 add_terms_address_addrs (Xapian::Document doc,
181 InternetAddressList *addresses,
182 const char *address_type)
185 InternetAddress *address;
187 for (i = 0; i < internet_address_list_length (addresses); i++) {
188 address = internet_address_list_get_address (addresses, i);
189 add_term_address_addr (doc, address, address_type);
190 add_term_address_addr (doc, address, "email");
195 skip_re_in_subject (const char *subject)
197 const char *s = subject;
200 while (*s && isspace (*s))
202 if (strncasecmp (s, "re:", 3) == 0)
212 find_message (Xapian::Database db, const char *message_id)
214 Xapian::PostingIterator i;
217 term = g_strdup_printf ("%s%s", find_prefix ("msgid"), message_id);
218 i = db.postlist_begin (term);
219 if (i != db.postlist_end (term))
220 return db.get_document (*i);
222 return Xapian::Document ();
226 find_thread_id (Xapian::Database db, GPtrArray *parents)
228 Xapian::Document doc;
229 GHashTable *thread_ids;
231 GString *result = NULL;
236 thread_ids = g_hash_table_new (g_str_hash, g_str_equal);
238 for (i = 0; i < parents->len; i++) {
239 doc = find_message (db, (char *) g_ptr_array_index (parents, i));
240 value_string = doc.get_value (NOTMUCH_VALUE_THREAD);
241 value = value_string.c_str();
243 g_hash_table_insert (thread_ids, strdup (value), NULL);
246 keys = g_hash_table_get_keys (thread_ids);
247 for (l = keys; l; l = l->next) {
248 char *id = (char *) l->data;
249 if (result == NULL) {
250 result = g_string_new (id);
252 g_string_append_printf (result, ",%s", id);
258 return g_string_free (result, FALSE);
263 /* Add a term for each message-id in the References header of the
266 parse_references (GPtrArray *array,
269 const char *end, *next;
275 while (*refs && isspace (*refs))
280 while (*end && !isspace (*end))
284 if (end > refs && *end == '>')
287 g_ptr_array_add (array, g_strndup (refs, end - refs + 1));
293 /* Generate terms for the body of a message, given the filename of the
294 * message and the offset at which the headers of the message end,
295 * (and hence the body begins). */
297 gen_terms_body (Xapian::TermGenerator term_gen,
298 const char * filename,
302 GIOStatus gio_status;
303 GError *error = NULL;
304 char *p, *body_line = NULL, *prev_line = NULL;
306 channel = g_io_channel_new_file (filename, "r", &error);
307 if (channel == NULL) {
308 fprintf (stderr, "Error: %s\n", error->message);
312 gio_status = g_io_channel_seek_position (channel, body_offset,
314 if (gio_status != G_IO_STATUS_NORMAL) {
315 fprintf (stderr, "Error: %s\n", error->message);
323 gio_status = g_io_channel_read_line (channel, &body_line,
325 if (gio_status == G_IO_STATUS_EOF)
327 if (gio_status != G_IO_STATUS_NORMAL) {
328 fprintf (stderr, "Error: %s\n", error->message);
332 if (strlen (body_line) == 0)
335 /* If the line looks like it might be introducing a quote,
336 * save it until we see if the next line begins a quote. */
337 p = body_line + strlen (body_line) - 1;
338 while (p > body_line and isspace (*p))
341 prev_line = body_line;
346 /* Skip quoted lines, (and previous lines that introduced them) */
347 if (body_line[0] == '>') {
355 /* Now that we're not looking at a quote we can add the prev_line */
357 gen_terms (term_gen, "body", prev_line);
362 /* Skip signatures */
363 /* XXX: Should only do this if "near" the end of the message. */
364 if (strncmp (body_line, "-- ", 3) == 0 ||
365 strncmp (body_line, "----------", 10) == 0 ||
366 strncmp (body_line, "__________", 10) == 0)
369 gen_terms (term_gen, "body", body_line);
375 g_io_channel_close (channel);
379 index_file (Xapian::WritableDatabase db,
380 Xapian::TermGenerator term_gen,
381 const char *filename)
383 Xapian::Document doc;
387 GMimeMessage *message;
388 InternetAddressList *addresses;
393 const char *subject, *refs, *in_reply_to, *from;
394 const char *message_id, *thread_id;
397 struct tm gm_time_tm;
398 char date_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */
401 file = fopen (filename, "r");
403 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
407 stream = g_mime_stream_file_new (file);
409 parser = g_mime_parser_new_with_stream (stream);
411 message = g_mime_parser_construct_message (parser);
413 doc = Xapian::Document ();
415 doc.set_data (filename);
417 term_gen.set_stemmer (Xapian::Stem ("english"));
419 term_gen.set_document (doc);
421 from = g_mime_message_get_sender (message);
422 addresses = internet_address_list_parse_string (from);
424 gen_terms_address_names (term_gen, addresses, "from_name");
426 addresses = g_mime_message_get_all_recipients (message);
427 gen_terms_address_names (term_gen, addresses, "to_name");
429 subject = g_mime_message_get_subject (message);
430 subject = skip_re_in_subject (subject);
431 gen_terms (term_gen, "subject", subject);
432 gen_terms (term_gen, "body", subject);
434 gen_terms_body (term_gen, filename,
435 g_mime_parser_get_headers_end (parser));
437 parents = g_ptr_array_new ();
439 refs = g_mime_object_get_header (GMIME_OBJECT (message), "references");
440 parse_references (parents, refs);
442 in_reply_to = g_mime_object_get_header (GMIME_OBJECT (message),
444 parse_references (parents, in_reply_to);
446 for (i = 0; i < parents->len; i++)
447 add_term (doc, "ref", (char *) g_ptr_array_index (parents, i));
449 thread_id = find_thread_id (db, parents);
451 for (i = 0; i < parents->len; i++)
452 g_free (g_ptr_array_index (parents, i));
453 g_ptr_array_free (parents, TRUE);
455 from = g_mime_message_get_sender (message);
456 addresses = internet_address_list_parse_string (from);
458 add_terms_address_addrs (doc, addresses, "from_email");
460 add_terms_address_addrs (doc,
461 g_mime_message_get_all_recipients (message),
464 g_mime_message_get_date (message, &time, NULL);
466 gmtime_r (&time, &gm_time_tm);
468 if (strftime (date_str, sizeof (date_str),
469 "%Y%m%d%H%M%S", &gm_time_tm) == 0) {
470 fprintf (stderr, "Internal error formatting time\n");
474 add_term (doc, "date", date_str);
476 add_term (doc, "label", "inbox");
477 add_term (doc, "label", "unread");
478 add_term (doc, "type", "mail");
479 add_term (doc, "source_id", "1");
481 message_id = g_mime_message_get_message_id (message);
482 add_term (doc, "msgid", message_id);
483 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
486 add_term (doc, "thread", thread_id);
487 doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
488 free ((void *) thread_id);
490 /* If not referenced thread, use the message ID */
491 add_term (doc, "thread", message_id);
492 doc.add_value (NOTMUCH_VALUE_THREAD, message_id);
495 doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time));
497 db.add_document (doc);
499 g_object_unref (message);
500 g_object_unref (parser);
501 g_object_unref (stream);
505 usage (const char *argv0)
507 fprintf (stderr, "Usage: %s <path-to-xapian-database>\n", argv0);
508 fprintf (stderr, "\n");
509 fprintf (stderr, "Messages to be indexed are read from stdnin as absolute filenames\n");
510 fprintf (stderr, "one file per line.");
514 main (int argc, char **argv)
516 const char *database_path;
519 GIOStatus gio_status;
520 GError *error = NULL;
527 database_path = argv[1];
532 Xapian::WritableDatabase db;
533 Xapian::TermGenerator term_gen;
535 db = Xapian::WritableDatabase (database_path,
536 Xapian::DB_CREATE_OR_OPEN);
538 term_gen = Xapian::TermGenerator ();
540 channel = g_io_channel_unix_new (fileno (stdin));
543 gio_status = g_io_channel_read_line (channel, &filename,
545 if (gio_status == G_IO_STATUS_EOF)
547 if (gio_status != G_IO_STATUS_NORMAL) {
548 fprintf (stderr, "An error occurred reading from stdin: %s\n",
553 g_strchomp (filename);
554 index_file (db, term_gen, filename);
559 } catch (const Xapian::Error &error) {
560 cerr << "A Xapian exception occurred: " << error.get_msg () << endl;