]> git.notmuchmail.org Git - notmuch/blob - notmuch-index-message.cc
Minor code re-ordering for clarity.
[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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <time.h>
25
26 #include <iostream>
27
28 #include <gmime/gmime.h>
29
30 #include <xapian.h>
31
32 using namespace std;
33
34 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
35
36 /* These prefix values are specifically chosen to be compatible
37  * with sup, (http://sup.rubyforge.org), written by
38  * William Morgan <wmorgan-sup@masanjin.net>, and released
39  * under the GNU GPL v2.
40  */
41
42 typedef struct {
43     const char *name;
44     const char *prefix;
45 } prefix_t;
46
47 prefix_t NORMAL_PREFIX[] = {
48     { "subject", "S" },
49     { "body", "B" },
50     { "from_name", "FN" },
51     { "to_name", "TN" },
52     { "name", "N" },
53     { "attachment", "A" }
54 };
55
56 prefix_t BOOLEAN_PREFIX[] = {
57     { "type", "K" },
58     { "from_email", "FE" },
59     { "to_email", "TE" },
60     { "email", "E" },
61     { "date", "D" },
62     { "label", "L" },
63     { "source_id", "I" },
64     { "attachment_extension", "O" },
65     { "msgid", "Q" },
66     { "thread", "H" },
67     { "ref", "R" }
68 };
69
70 /* Similarly, these value numbers are also chosen to be sup
71  * compatible. */
72
73 typedef enum {
74     NOTMUCH_VALUE_MESSAGE_ID = 0,
75     NOTMUCH_VALUE_THREAD = 1,
76     NOTMUCH_VALUE_DATE = 2
77 } notmuch_value_t;
78
79 static const char *
80 find_prefix (const char *name)
81 {
82     unsigned int i;
83
84     for (i = 0; i < ARRAY_SIZE (NORMAL_PREFIX); i++)
85         if (strcmp (name, NORMAL_PREFIX[i].name) == 0)
86             return NORMAL_PREFIX[i].prefix;
87
88     for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX); i++)
89         if (strcmp (name, BOOLEAN_PREFIX[i].name) == 0)
90             return BOOLEAN_PREFIX[i].prefix;
91
92     return "";
93 }
94
95 int TERM_COMBINED = 0;
96
97 static void
98 add_term (Xapian::Document doc,
99           const char *prefix_name,
100           const char *value)
101 {
102     const char *prefix;
103     char *term;
104
105     if (value == NULL)
106         return;
107
108     prefix = find_prefix (prefix_name);
109
110     term = g_strdup_printf ("%s%s", prefix, value);
111
112     doc.add_term (term);
113
114     g_free (term);
115 }
116
117 static void
118 gen_terms (Xapian::TermGenerator term_gen,
119            const char *prefix_name,
120            const char *text)
121 {
122     const char *prefix;
123
124     if (text == NULL)
125         return;
126
127     prefix = find_prefix (prefix_name);
128
129     term_gen.index_text (text, 1, prefix);
130 }
131
132 static void
133 gen_terms_address_name (Xapian::TermGenerator term_gen,
134                         InternetAddress *address,
135                         const char *prefix_name)
136 {
137     const char *name;
138
139     name = internet_address_get_name (address);
140
141     if (name)
142         gen_terms (term_gen, prefix_name, name);
143 }
144
145 static void
146 gen_terms_address_names (Xapian::TermGenerator term_gen,
147                          InternetAddressList *addresses,
148                          const char *address_type)
149 {
150     int i;
151     InternetAddress *address;
152
153     for (i = 0; i < internet_address_list_length (addresses); i++) {
154         address = internet_address_list_get_address (addresses, i);
155         gen_terms_address_name (term_gen, address, address_type);
156         gen_terms_address_name (term_gen, address, "name");
157         gen_terms_address_name (term_gen, address, "body");
158     }
159 }
160
161 static void
162 add_term_address_addr (Xapian::Document doc,
163                        InternetAddress *address,
164                        const char *prefix_name)
165 {
166     InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
167     const char *addr;
168
169     addr = internet_address_mailbox_get_addr (mailbox);
170
171     if (addr)
172         add_term (doc, prefix_name, addr);
173 }
174
175 static void
176 add_terms_address_addrs (Xapian::Document doc,
177                          InternetAddressList *addresses,
178                          const char *address_type)
179 {
180     int i;
181     InternetAddress *address;
182
183     for (i = 0; i < internet_address_list_length (addresses); i++) {
184         address = internet_address_list_get_address (addresses, i);
185         add_term_address_addr (doc, address, address_type);
186         add_term_address_addr (doc, address, "email");
187     }
188 }
189
190 static const char *
191 skip_re_in_subject (const char *subject)
192 {
193     const char *s = subject;
194
195     while (*s) {
196         while (*s && isspace (*s))
197             s++;
198         if (strncasecmp (s, "re:", 3) == 0)
199             s += 3;
200         else
201             break;
202     }
203
204     return s;
205 }
206
207 /* Generate terms for the body of a message, given the filename of the
208  * message and the offset at which the headers of the message end,
209  * (and hence the body begins). */
210 static void
211 gen_terms_body (Xapian::TermGenerator term_gen,
212                 const char * filename,
213                 gint64 body_offset)
214 {
215     GIOChannel *channel;
216     GIOStatus gio_status;
217     GError *error = NULL;
218     char *p, *body_line = NULL, *prev_line = NULL;
219
220     channel = g_io_channel_new_file (filename, "r", &error);
221     if (channel == NULL) {
222         fprintf (stderr, "Error: %s\n", error->message);
223         exit (1);
224     }
225
226     gio_status = g_io_channel_seek_position (channel, body_offset,
227                                              G_SEEK_SET, &error);
228     if (gio_status != G_IO_STATUS_NORMAL) {
229         fprintf (stderr, "Error: %s\n", error->message);
230         exit (1);
231     }
232
233     while (1) {
234         if (body_line)
235             g_free (body_line);
236
237         gio_status = g_io_channel_read_line (channel, &body_line,
238                                              NULL, NULL, &error);
239         if (gio_status == G_IO_STATUS_EOF)
240             break;
241         if (gio_status != G_IO_STATUS_NORMAL) {
242             fprintf (stderr, "Error: %s\n", error->message);
243             exit (1);
244         }
245
246         if (strlen (body_line) == 0)
247             continue;
248
249         /* If the line looks like it might be introducing a quote,
250          * save it until we see if the next line begins a quote. */
251         p = body_line + strlen (body_line) - 1;
252         while (p > body_line and isspace (*p))
253             p--;
254         if (*p == ':') {
255             prev_line = body_line;
256             body_line = NULL;
257             continue;
258         }
259
260         /* Skip quoted lines, (and previous lines that introduced them) */
261         if (body_line[0] == '>') {
262             if (prev_line) {
263                 g_free (prev_line);
264                 prev_line = NULL;
265             }
266             continue;
267         }
268
269         /* Now that we're not looking at a quote we can add the prev_line */
270         if (prev_line) {
271             gen_terms (term_gen, "body", prev_line);
272             g_free (prev_line);
273             prev_line = NULL;
274         }
275
276         /* Skip signatures */
277         /* XXX: Should only do this if "near" the end of the message. */
278         if (strncmp (body_line, "-- ", 3) == 0)
279             break;
280
281         gen_terms (term_gen, "body", body_line);
282     }
283
284     if (body_line)
285         g_free (body_line);
286
287     g_io_channel_close (channel);
288 }
289
290
291 int
292 main (int argc, char **argv)
293 {
294     GMimeStream *stream;
295     GMimeParser *parser;
296     GMimeMessage *message;
297     InternetAddressList *addresses;
298
299     const char *database_path, *filename;
300     FILE *file;
301
302     const char *value, *from;
303
304     time_t time;
305     struct tm gm_time_tm;
306     char date_str[16]; /* YYYYMMDDHHMMSS + 1 for Y100k compatibility ;-) */
307
308     if (argc < 3) {
309         fprintf (stderr, "Usage: %s <path-to-xapian-database> <mail-message>\n",
310                  argv[0]);
311         exit (1);
312     }
313
314     database_path = argv[1];
315     filename = argv[2];
316
317     file = fopen (filename, "r");
318     if (! file) {
319         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
320         exit (1);
321     }
322
323     g_mime_init (0);
324
325     stream = g_mime_stream_file_new (file);
326
327     parser = g_mime_parser_new_with_stream (stream);
328
329     message = g_mime_parser_construct_message (parser);
330
331     try {
332         Xapian::WritableDatabase db;
333         Xapian::TermGenerator term_gen;
334         Xapian::Document doc;
335
336         doc = Xapian::Document ();
337
338         doc.set_data (filename);
339
340         db = Xapian::WritableDatabase (database_path,
341                                        Xapian::DB_CREATE_OR_OPEN);
342
343         term_gen = Xapian::TermGenerator ();
344         term_gen.set_stemmer (Xapian::Stem ("english"));
345
346         term_gen.set_document (doc);
347
348         from = g_mime_message_get_sender (message);
349         addresses = internet_address_list_parse_string (from);
350
351         gen_terms_address_names (term_gen, addresses, "from_name");
352
353         addresses = g_mime_message_get_all_recipients (message);
354         gen_terms_address_names (term_gen, addresses, "to_name");
355
356         value = g_mime_message_get_subject (message);
357         value = skip_re_in_subject (value);
358         gen_terms (term_gen, "subject", value);
359         gen_terms (term_gen, "body", value);
360
361         gen_terms_body (term_gen, filename,
362                         g_mime_parser_get_headers_end (parser));
363
364         from = g_mime_message_get_sender (message);
365         addresses = internet_address_list_parse_string (from);
366
367         add_terms_address_addrs (doc, addresses, "from_email");
368
369         add_terms_address_addrs (doc,
370                                  g_mime_message_get_all_recipients (message),
371                                  "to_email");
372
373         g_mime_message_get_date (message, &time, NULL);
374
375         gmtime_r (&time, &gm_time_tm);
376
377         if (strftime (date_str, sizeof (date_str),
378                       "%Y%m%d%H%M%S", &gm_time_tm) == 0) {
379             fprintf (stderr, "Internal error formatting time\n");
380             exit (1);
381         }
382
383         add_term (doc, "date", date_str);
384
385         add_term (doc, "label", "inbox");
386         add_term (doc, "label", "unread");
387         add_term (doc, "type", "mail");
388         add_term (doc, "source_id", "1");
389
390         value = g_mime_message_get_message_id (message);
391         add_term (doc, "msgid", value);
392         add_term (doc, "thread", value);
393
394         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, value);
395         doc.add_value (NOTMUCH_VALUE_THREAD, value);
396
397         doc.add_value (NOTMUCH_VALUE_DATE, Xapian::sortable_serialise (time));
398
399         db.add_document (doc);
400
401     } catch (const Xapian::Error &error) {
402         cerr << "A Xapian exception occurred: " << error.get_msg () << endl;
403         exit (1);
404     }
405
406     g_object_unref (message);
407     g_object_unref (parser);
408     g_object_unref (stream);
409
410     return 0;
411 }