]> git.notmuchmail.org Git - notmuch/blob - notmuch-index-message.cc
date.c: Add new file directly from gmime2.4-2.4.6/gmime/gmime-utils.c
[notmuch] / notmuch-index-message.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 /* This indexer creates a Xapian mail index that is remarkably similar
21  * to that created by sup. The big difference, (and the thing that
22  * will keep a notmuch index from being used by sup directly), is that
23  * sup expects a serialized ruby data structure in the document's data
24  * field, but notmuch just puts the mail's filename there (trusting
25  * that the email client can get the data in needs from the filename).
26  *
27  * Note: One bug here is that sup actually merges together fields such
28  * as To, CC, Bcc etc. when finding multiple emails with the same
29  * message ID. To support something similar, notmuch should list
30  * multiple files in the data field.
31  *
32  * Other differences between sup and notmuch-index identified so far:
33  *
34  *   o sup supports encrypted mime parts by prompting for a passphrase
35  *     to decrypt the message. So far, notmuch doesn't support this,
36  *     both because I'm lazy to code it, and I also think doing so
37  *     would present a security leak.
38  *
39  *   o sup and notmuch have different heuristics for identifying (and
40  *     thus ignoring) signatures. For example, sup considers a line
41  *     consisting of two hypens as a signature separator, while
42  *     notmuch expects those two hyphens to be followed by a space
43  *     character.
44  *
45  *   o sup as been seen to split some numbers before indexing
46  *     them. For example, the number 1754 in an email message was
47  *     indexed by sup as separate terms 17 and 54. I couldn't find any
48  *     explanation for this behavior and did not try to replicate it
49  *     in notmuch.
50  */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <errno.h>
56 #include <time.h>
57 #include <sys/time.h>
58
59 #include <iostream>
60
61 #include <gmime/gmime.h>
62
63 #include <xapian.h>
64
65 using namespace std;
66
67 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
68
69 /* Xapian complains if we provide a term longer than this. */
70 #define NOTMUCH_MAX_TERM 245
71
72 /* These prefix values are specifically chosen to be compatible
73  * with sup, (http://sup.rubyforge.org), written by
74  * William Morgan <wmorgan-sup@masanjin.net>, and released
75  * under the GNU GPL v2.
76  */
77
78 typedef struct {
79     const char *name;
80     const char *prefix;
81 } prefix_t;
82
83 prefix_t NORMAL_PREFIX[] = {
84     { "subject", "S" },
85     { "body", "B" },
86     { "from_name", "FN" },
87     { "to_name", "TN" },
88     { "name", "N" },
89     { "attachment", "A" }
90 };
91
92 prefix_t BOOLEAN_PREFIX[] = {
93     { "type", "K" },
94     { "from_email", "FE" },
95     { "to_email", "TE" },
96     { "email", "E" },
97     { "date", "D" },
98     { "label", "L" },
99     { "source_id", "I" },
100     { "attachment_extension", "O" },
101     { "msgid", "Q" },
102     { "thread", "H" },
103     { "ref", "R" }
104 };
105
106 /* Similarly, these value numbers are also chosen to be sup
107  * compatible. */
108
109 typedef enum {
110     NOTMUCH_VALUE_MESSAGE_ID = 0,
111     NOTMUCH_VALUE_THREAD = 1,
112     NOTMUCH_VALUE_DATE = 2
113 } notmuch_value_t;
114
115 static const char *
116 find_prefix (const char *name)
117 {
118     unsigned int i;
119
120     for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
121         if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
122             return NORMAL_PREFIX[i].prefix;
123
124     for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
125         if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
126             return BOOLEAN_PREFIX[i].prefix;
127
128     return "";
129 }
130
131 /* "128 bits of thread-id ought to be enough for anybody" */
132 #define NOTMUCH_THREAD_ID_BITS   128
133 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
134 typedef struct _thread_id {
135     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
136 } thread_id_t;
137
138 static void
139 thread_id_generate (thread_id_t *thread_id)
140 {
141     FILE *urandom;
142     uint32_t value;
143     char *s;
144     int i;
145
146     urandom = fopen ("/dev/urandom", "r");
147     if (urandom == NULL) {
148         fprintf (stderr, "Error opening /dev/urandom: %s\n",
149                  strerror (errno));
150         fprintf (stderr, "Perhaps notmuch needs some portability fixes for your platform?\n");
151         exit (1);
152     }
153
154     s = thread_id->str;
155     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
156         fread ((void *) &value, sizeof (value), 1, urandom);
157         sprintf (s, "%08x", value);
158         s += 8;
159     }
160
161     fclose (urandom);
162
163     printf ("Generated thread id: %s\n", thread_id->str);
164 }
165
166 static void
167 add_term (Xapian::Document doc,
168           const char *prefix_name,
169           const char *value)
170 {
171     const char *prefix;
172     char *term;
173
174     if (value == NULL)
175         return;
176
177     prefix = find_prefix (prefix_name);
178
179     term = g_strdup_printf ("%s%s", prefix, value);
180
181     if (strlen (term) <= NOTMUCH_MAX_TERM)
182         doc.add_term (term);
183
184     g_free (term);
185 }
186
187 static void
188 gen_terms (Xapian::TermGenerator term_gen,
189            const char *prefix_name,
190            const char *text)
191 {
192     const char *prefix;
193
194     if (text == NULL)
195         return;
196
197     prefix = find_prefix (prefix_name);
198
199     term_gen.index_text (text, 1, prefix);
200 }
201
202 static void
203 gen_terms_address_name (Xapian::TermGenerator term_gen,
204                         InternetAddress *address,
205                         const char *prefix_name)
206 {
207     if (INTERNET_ADDRESS_IS_MAILBOX(address)) {
208         const char *name;
209         int own_name = 0;
210
211         name = internet_address_get_name (address);
212
213         /* In the absence of a name, we'll strip the part before the @
214          * from the address. */
215         if (! name) {
216             InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
217             const char *addr = internet_address_mailbox_get_addr (mailbox);
218             const char *at;
219
220             at = strchr (addr, '@');
221             if (at) {
222                 name = strndup (addr, at - addr);
223                 own_name = 1;
224             }
225         }
226
227         if (name)
228             gen_terms (term_gen, prefix_name, name);
229
230         if (own_name)
231             free ((void *) name);
232     } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
233         InternetAddressGroup *group = INTERNET_ADDRESS_GROUP (address);
234         InternetAddressList *list = internet_address_group_get_members(group);
235         if (list) {
236             int length = internet_address_list_length(list);
237             int i;
238
239             for (i = 0; i < length; i++)
240                 gen_terms_address_name(term_gen,
241                                        internet_address_list_get_address(list, i),
242                                        prefix_name);
243         }
244     }
245 }
246
247 static void
248 gen_terms_address_names (Xapian::TermGenerator term_gen,
249                          InternetAddressList *addresses,
250                          const char *address_type)
251 {
252     int i;
253     InternetAddress *address;
254
255     if (addresses == NULL)
256         return;
257
258     for (i = 0; i < internet_address_list_length (addresses); i++) {
259         address = internet_address_list_get_address (addresses, i);
260         gen_terms_address_name (term_gen, address, address_type);
261         gen_terms_address_name (term_gen, address, "name");
262         gen_terms_address_name (term_gen, address, "body");
263     }
264 }
265
266 static void
267 add_term_address_addr (Xapian::Document doc,
268                        InternetAddress *address,
269                        const char *prefix_name)
270 {
271     if (INTERNET_ADDRESS_IS_MAILBOX(address)) {
272         InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
273         const char *addr;
274
275         addr = internet_address_mailbox_get_addr (mailbox);
276
277         if (addr)
278             add_term (doc, prefix_name, addr);
279     } else if (INTERNET_ADDRESS_IS_GROUP (address)) {
280         InternetAddressGroup *group = INTERNET_ADDRESS_GROUP (address);
281         InternetAddressList *list = internet_address_group_get_members(group);
282         if (list) {
283             int length = internet_address_list_length(list);
284             int i;
285
286             for (i = 0; i < length; i++)
287                 add_term_address_addr(doc,
288                                       internet_address_list_get_address(list, i),
289                                       prefix_name);
290         }
291     }
292 }
293
294 static void
295 add_terms_address_addrs (Xapian::Document doc,
296                          InternetAddressList *addresses,
297                          const char *address_type)
298 {
299     int i;
300     InternetAddress *address;
301
302     if (addresses == NULL)
303         return;
304
305     for (i = 0; i < internet_address_list_length (addresses); i++) {
306         address = internet_address_list_get_address (addresses, i);
307         add_term_address_addr (doc, address, address_type);
308         add_term_address_addr (doc, address, "email");
309     }
310 }
311
312 static const char *
313 skip_re_in_subject (const char *subject)
314 {
315     const char *s = subject;
316
317     if (subject == NULL)
318         return NULL;
319
320     while (*s) {
321         while (*s && isspace (*s))
322             s++;
323         if (strncasecmp (s, "re:", 3) == 0)
324             s += 3;
325         else
326             break;
327     }
328
329     return s;
330 }
331
332 static void
333 find_messages_by_term (Xapian::Database db,
334                        const char *prefix_name,
335                        const char *value,
336                        Xapian::PostingIterator *begin,
337                        Xapian::PostingIterator *end)
338 {
339     Xapian::PostingIterator i;
340     char *term;
341
342     term = g_strdup_printf ("%s%s", find_prefix (prefix_name), value);
343
344     *begin = db.postlist_begin (term);
345
346     if (end)
347         *end = db.postlist_end (term);
348
349     free (term);
350 }
351
352 Xapian::Document
353 find_message_by_docid (Xapian::Database db, Xapian::docid docid)
354 {
355     return db.get_document (docid);
356 }
357
358 Xapian::Document
359 find_message_by_message_id (Xapian::Database db, const char *message_id)
360 {
361     Xapian::PostingIterator i, end;
362
363     find_messages_by_term (db, "msgid", message_id, &i, &end);
364
365     if (i != end)
366         return find_message_by_docid (db, *i);
367     else
368         return Xapian::Document ();
369 }
370
371 static void
372 insert_thread_id (GHashTable *thread_ids, Xapian::Document doc)
373 {
374     string value_string;
375     const char *value, *id, *comma;
376
377     value_string = doc.get_value (NOTMUCH_VALUE_THREAD);
378     value = value_string.c_str();
379     if (strlen (value)) {
380         id = value;
381         while (*id) {
382             comma = strchr (id, ',');
383             if (comma == NULL)
384                 comma = id + strlen (id);
385             g_hash_table_insert (thread_ids,
386                                  strndup (id, comma - id), NULL);
387             id = comma;
388             if (*id)
389                 id++;
390         }
391     }
392 }
393
394 /* Return one or more thread_ids, (as a GPtrArray of strings), for the
395  * given message based on looking into the database for any messages
396  * referenced in parents, and also for any messages in the database
397  * referencing message_id.
398  *
399  * Caller should free all strings in the array and the array itself,
400  * (g_ptr_array_free) when done. */
401 static GPtrArray *
402 find_thread_ids (Xapian::Database db,
403                  GPtrArray *parents,
404                  const char *message_id)
405 {
406     Xapian::PostingIterator child, children_end;
407     Xapian::Document doc;
408     GHashTable *thread_ids;
409     GList *keys, *l;
410     unsigned int i;
411     const char *parent_message_id;
412     GPtrArray *result;
413
414     thread_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
415                                         free, NULL);
416
417     find_messages_by_term (db, "ref", message_id, &child, &children_end);
418     for ( ; child != children_end; child++) {
419         doc = find_message_by_docid (db, *child);
420         insert_thread_id (thread_ids, doc);
421     }
422
423     for (i = 0; i < parents->len; i++) {
424         parent_message_id = (char *) g_ptr_array_index (parents, i);
425         doc = find_message_by_message_id (db, parent_message_id);
426         insert_thread_id (thread_ids, doc);
427     }
428
429     result = g_ptr_array_new ();
430
431     keys = g_hash_table_get_keys (thread_ids);
432     for (l = keys; l; l = l->next) {
433         char *id = (char *) l->data;
434         g_ptr_array_add (result, id);
435     }
436     g_list_free (keys);
437
438     /* We're done with the hash table, but we've taken the pointers to
439      * the allocated strings and put them into our result array, so
440      * tell the hash not to free them on its way out. */
441     g_hash_table_steal_all (thread_ids);
442     g_hash_table_unref (thread_ids);
443
444     return result;
445 }
446
447 /* Add a term for each message-id in the References header of the
448  * message. */
449 static void
450 parse_references (GPtrArray *array,
451                   const char *refs_str)
452 {
453     GMimeReferences *refs, *r;
454     const char *message_id;
455
456     if (refs_str == NULL)
457         return;
458
459     refs = g_mime_references_decode (refs_str);
460
461     for (r = refs; r; r = r->next) {
462         message_id = g_mime_references_get_message_id (r);
463         g_ptr_array_add (array, g_strdup (message_id));
464     }
465
466     g_mime_references_free (refs);
467 }
468
469 /* Given a string representing the body of a message, generate terms
470  * for it, (skipping quoted portions and signatures). */
471 static void
472 gen_terms_body_str (Xapian::TermGenerator term_gen,
473                     char *body)
474 {
475     char *line, *line_end, *next_line;
476
477     if (body == NULL)
478         return;
479
480     next_line = body;
481
482     while (1) {
483         line = next_line;
484         if (*line == '\0')
485             break;
486
487         next_line = strchr (line, '\n');
488         if (next_line == NULL) {
489             next_line = line + strlen (line);
490         }
491         line_end = next_line - 1;
492
493         /* Get to the next non-blank line. */
494         while (*next_line == '\n')
495             next_line++;
496
497         /* Skip blank lines. */
498         if (line_end < line)
499             continue;
500
501         /* Skip lines that are quotes. */
502         if (*line == '>')
503             continue;
504
505         /* Also skip lines introducing a quote on the next line. */
506         if (*line_end == ':' && *next_line == '>')
507             continue;
508
509         /* Finally, bail as soon as we see a signature. */
510         /* XXX: Should only do this if "near" the end of the message. */
511         if (strncmp (line, "-- ", 3) == 0 ||
512             strncmp (line, "----------", 10) == 0 ||
513             strncmp (line, "__________", 10) == 0)
514             break;
515
516         *(line_end + 1) = '\0';
517         gen_terms (term_gen, "body", line);
518     }
519 }
520
521
522 /* Callback to generate terms for each mime part of a message. */
523 static void
524 gen_terms_part (Xapian::TermGenerator term_gen,
525                 GMimeObject *part)
526 {
527     GMimeStream *stream;
528     GMimeDataWrapper *wrapper;
529     GByteArray *byte_array;
530     GMimeContentDisposition *disposition;
531     char *body;
532
533     if (GMIME_IS_MULTIPART (part)) {
534         GMimeMultipart *multipart = GMIME_MULTIPART (part);
535         int i;
536
537         for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
538             if (GMIME_IS_MULTIPART_SIGNED (multipart)) {
539                 /* Don't index the signature. */
540                 if (i == 1)
541                     continue;
542                 if (i > 1)
543                     fprintf (stderr, "Warning: Unexpected extra parts of mutlipart/signed. Indexing anyway.\n");
544             }
545             gen_terms_part (term_gen,
546                             g_mime_multipart_get_part (multipart, i));
547         }
548         return;
549     }
550
551     if (GMIME_IS_MESSAGE_PART (part)) {
552         GMimeMessage *message;
553
554         message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
555
556         gen_terms_part (term_gen, g_mime_message_get_mime_part (message));
557
558         return;
559     }
560
561     if (! (GMIME_IS_PART (part))) {
562         fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",
563                  g_type_name (G_OBJECT_TYPE (part)));
564         return;
565     }
566
567     disposition = g_mime_object_get_content_disposition (part);
568     if (disposition &&
569         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
570     {
571         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
572         const char *extension;
573
574         add_term (term_gen.get_document (), "label", "attachment");
575         gen_terms (term_gen, "attachment", filename);
576
577         if (filename) {
578             extension = strchr (filename, '.');
579             if (extension) {
580                 add_term (term_gen.get_document (), "attachment_extension",
581                           extension + 1);
582             }
583         }
584
585         return;
586     }
587
588     byte_array = g_byte_array_new ();
589
590     stream = g_mime_stream_mem_new_with_byte_array (byte_array);
591     g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE);
592     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
593     if (wrapper)
594         g_mime_data_wrapper_write_to_stream (wrapper, stream);
595
596     g_object_unref (stream);
597
598     g_byte_array_append (byte_array, (guint8 *) "\0", 1);
599     body = (char *) g_byte_array_free (byte_array, FALSE);
600
601     gen_terms_body_str (term_gen, body);
602
603     free (body);
604 }
605
606 static void
607 index_file (Xapian::WritableDatabase db,
608             Xapian::TermGenerator term_gen,
609             const char *filename)
610 {
611     Xapian::Document doc;
612
613     GMimeStream *stream;
614     GMimeParser *parser;
615     GMimeMessage *message;
616     InternetAddressList *addresses;
617     GPtrArray *parents, *thread_ids;
618
619     FILE *file;
620
621     const char *subject, *refs, *in_reply_to, *from;
622     const char *message_id;
623
624     time_t time;
625     struct tm gm_time_tm;
626     char date_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */
627     unsigned int i;
628
629     file = fopen (filename, "r");
630     if (! file) {
631         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
632         exit (1);
633     }
634
635     stream = g_mime_stream_file_new (file);
636
637     parser = g_mime_parser_new_with_stream (stream);
638
639     message = g_mime_parser_construct_message (parser);
640
641     doc = Xapian::Document ();
642
643     doc.set_data (filename);
644
645     term_gen.set_stemmer (Xapian::Stem ("english"));
646
647     term_gen.set_document (doc);
648
649     from = g_mime_message_get_sender (message);
650     addresses = internet_address_list_parse_string (from);
651
652     gen_terms_address_names (term_gen, addresses, "from_name");
653
654     addresses = g_mime_message_get_all_recipients (message);
655     gen_terms_address_names (term_gen, addresses, "to_name");
656
657     subject = g_mime_message_get_subject (message);
658     subject = skip_re_in_subject (subject);
659     gen_terms (term_gen, "subject", subject);
660     gen_terms (term_gen, "body", subject);
661
662     gen_terms_part (term_gen, g_mime_message_get_mime_part (message));
663
664     parents = g_ptr_array_new ();
665
666     refs = g_mime_object_get_header (GMIME_OBJECT (message), "references");
667     parse_references (parents, refs);
668
669     in_reply_to = g_mime_object_get_header (GMIME_OBJECT (message),
670                                             "in-reply-to");
671     parse_references (parents, in_reply_to);
672
673     for (i = 0; i < parents->len; i++)
674         add_term (doc, "ref", (char *) g_ptr_array_index (parents, i));
675
676     message_id = g_mime_message_get_message_id (message);
677
678     thread_ids = find_thread_ids (db, parents, message_id);
679
680     for (i = 0; i < parents->len; i++)
681         g_free (g_ptr_array_index (parents, i));
682     g_ptr_array_free (parents, TRUE);
683
684     from = g_mime_message_get_sender (message);
685     addresses = internet_address_list_parse_string (from);
686
687     add_terms_address_addrs (doc, addresses, "from_email");
688
689     add_terms_address_addrs (doc,
690                              g_mime_message_get_all_recipients (message),
691                              "to_email");
692
693     g_mime_message_get_date (message, &time, NULL);
694
695     gmtime_r (&time, &gm_time_tm);
696
697     if (strftime (date_str, sizeof (date_str),
698                   "%Y%m%d%H%M%S", &gm_time_tm) == 0) {
699         fprintf (stderr, "Internal error formatting time\n");
700         exit (1);
701     }
702
703     add_term (doc, "date", date_str);
704
705     add_term (doc, "label", "inbox");
706     add_term (doc, "label", "unread");
707     add_term (doc, "type", "mail");
708     add_term (doc, "source_id", "1");
709
710     if (message_id) {
711         add_term (doc, "msgid", message_id);
712         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
713     }
714
715     if (thread_ids->len) {
716         unsigned int i;
717         GString *thread_id;
718         char *id;
719
720         for (i = 0; i < thread_ids->len; i++) {
721             id = (char *) thread_ids->pdata[i];
722
723             add_term (doc, "thread", id);
724
725             if (i == 0)
726                 thread_id = g_string_new (id);
727             else
728                 g_string_append_printf (thread_id, ",%s", id);
729
730             free (id);
731         }
732         g_ptr_array_free (thread_ids, TRUE);
733
734         doc.add_value (NOTMUCH_VALUE_THREAD, thread_id->str);
735
736         g_string_free (thread_id, TRUE);
737     } else if (message_id) {
738         /* If not part of any existing thread, generate a new thread_id. */
739         thread_id_t thread_id;
740
741         thread_id_generate (&thread_id);
742
743         add_term (doc, "thread", thread_id.str);
744         doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
745     }
746
747     doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time));
748
749     db.add_document (doc);
750
751     g_object_unref (message);
752     g_object_unref (parser);
753     g_object_unref (stream);
754 }
755
756 static void
757 usage (const char *argv0)
758 {
759     fprintf (stderr, "Usage: %s <path-to-xapian-database>\n", argv0);
760     fprintf (stderr, "\n");
761     fprintf (stderr, "Messages to be indexed are read from stdnin as absolute filenames\n");
762     fprintf (stderr, "one file per line.");
763 }
764
765 int
766 main (int argc, char **argv)
767 {
768     const char *database_path;
769     char *filename;
770     GIOChannel *channel;
771     GIOStatus gio_status;
772     GError *error = NULL;
773     int count;
774     struct timeval tv_start, tv_last, tv_now;
775     double elapsed;
776
777     if (argc < 2) {
778         usage (argv[0]);
779         exit (1);
780     }
781
782     database_path = argv[1];
783
784     g_mime_init (0);
785
786     try {
787         Xapian::WritableDatabase db;
788         Xapian::TermGenerator term_gen;
789
790         db = Xapian::WritableDatabase (database_path,
791                                        Xapian::DB_CREATE_OR_OPEN);
792
793         term_gen = Xapian::TermGenerator ();
794
795         channel = g_io_channel_unix_new (fileno (stdin));
796
797         count = 0;
798
799         gettimeofday (&tv_start, NULL);
800         tv_last = tv_start;
801
802         while (1) {
803             gio_status = g_io_channel_read_line (channel, &filename,
804                                                  NULL, NULL, &error);
805             if (gio_status == G_IO_STATUS_EOF)
806                 break;
807             if (gio_status != G_IO_STATUS_NORMAL) {
808                 fprintf (stderr, "An error occurred reading from stdin: %s\n",
809                          error->message);
810                 exit (1);
811             }
812
813             g_strchomp (filename);
814             index_file (db, term_gen, filename);
815
816             g_free (filename);
817
818             count++;
819             if (count % 1000 == 0) {
820                 gettimeofday (&tv_now, NULL);
821                 printf ("Indexed %d messages (%g messages/second)\n",
822                         count, 1000 / ((tv_now.tv_sec - tv_last.tv_sec) +
823                                        (tv_now.tv_usec - tv_last.tv_usec) / 1e6));
824                 tv_last = tv_now;
825             }
826         }
827
828         g_io_channel_unref (channel);
829
830         gettimeofday (&tv_now, NULL);
831         elapsed = (tv_now.tv_sec - tv_start.tv_sec +
832                    (tv_now.tv_usec - tv_start.tv_usec) / 1e6);
833         printf ("Completed indexing of %d messages in %g seconds (%g messages/second)\n",
834                 count, elapsed, count / elapsed);
835
836     } catch (const Xapian::Error &error) {
837         cerr << "A Xapian exception occurred: " << error.get_msg () << endl;
838         exit (1);
839     }
840
841     return 0;
842 }