]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
configure: Look for both Xapian 1.1 and 1.0 and allow user override.
[notmuch] / notmuch.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24
25 typedef int (*command_function_t) (void *ctx, int argc, char *argv[]);
26
27 typedef struct command {
28     const char *name;
29     command_function_t function;
30     const char *arguments;
31     const char *summary;
32     const char *documentation;
33 } command_t;
34
35 static int
36 notmuch_help_command (void *ctx, int argc, char *argv[]);
37
38 static const char search_terms_help[] =
39     "\t\tSeveral notmuch commands accept a comman syntax for search\n"
40     "\t\tterms.\n"
41     "\n"
42     "\t\tThe search terms can consist of free-form text (and quoted\n"
43     "\t\tphrases) which will match all messages that contain all of\n"
44     "\t\tthe given terms/phrases in the body, the subject, or any of\n"
45     "\t\tthe sender or recipient headers.\n"
46     "\n"
47     "\t\tIn addition to free text, the following prefixes can be used\n"
48     "\t\tto force terms to match against specific portions of an email,\n"
49     "\t\t(where <brackets> indicate user-supplied values):\n"
50     "\n"
51     "\t\t\tfrom:<name-or-address>\n"
52     "\t\t\tto:<name-or-address>\n"
53     "\t\t\tsubject:<word-or-quoted-phrase>\n"
54     "\t\t\tattachment:<word>\n"
55     "\t\t\ttag:<tag>\n"
56     "\t\t\tid:<message-id>\n"
57     "\t\t\tthread:<thread-id>\n"
58     "\n"
59     "\t\tThe from: prefix is used to match the name or address of\n"
60     "\t\tthe sender of an email message.\n"
61     "\n"
62     "\t\tThe to: prefix is used to match the names or addresses of\n"
63     "\t\tany recipient of an email message, (whether To, Cc, or Bcc).\n"
64     "\n"
65     "\t\tAny term prefixed with subject: will match only text from\n"
66     "\t\tthe subject of an email. Quoted phrases are supported when\n"
67     "\t\tsearching with: subject:\"this is a phrase\".\n"
68     "\n"
69     "\t\tFor tag:, valid tag values include \"inbox\" and \"unread\"\n"
70     "\t\tby default for new messages added by \"notmuch new\" as well\n"
71     "\t\tas any other tag values added manually with \"notmuch tag\".\n"
72     "\n"
73     "\t\tFor id:, message ID values are the literal contents of the\n"
74     "\t\tMessage-ID: header of email messages, but without the '<','>'\n"
75     "\t\tdelimiters.\n"
76     "\n"
77     "\t\tThe thread: prefix can be used with the thread ID values that\n"
78     "\t\tare generated internally by notmuch (and do not appear in email\n"
79     "\t\tmessages). These thread ID values can be seen in the first\n"
80     "\t\tcolumn of output from \"notmuch search\".\n"
81     "\n"
82     "\t\tIn addition to individual terms, multiple terms can be\n"
83     "\t\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
84     "\t\tEach term in the query will be implicitly connected by a\n"
85     "\t\tlogical AND if no explicit operator is provided, (except\n"
86     "\t\tthat terms with a common prefix will be implicitly combined\n"
87     "\t\twith OR until we get Xapian defect #402 fixed).\n"
88     "\n"
89     "\t\tParentheses can also be used to control the combination of\n"
90     "\t\tthe Boolean operators, but will have to be protected from\n"
91     "\t\tinterpretation by the shell, (such as by putting quotation\n"
92     "\t\tmarks around any parenthesized expression).\n"
93     "\n"
94     "\t\tFinally, results can be restricted to only messages within a\n"
95     "\t\tparticular time range, (based on the Date: header) with:\n"
96     "\n"
97     "\t\t\t<intial-timestamp>..<final-timestamp>\n"
98     "\n"
99     "\t\tEach timestamp is a number representing the number of seconds\n"
100     "\t\tsince 1970-01-01 00:00:00 UTC. This is not the most convenient\n"
101     "\t\tmeans of expressing date ranges, but until notmuch is fixed to\n"
102     "\t\taccept a more convenient form, one can use the date program to\n"
103     "\t\tconstruct timestamps. For example, with the bash shell the\n"
104     "\t\tfollowing syntax would specify a date range to return messages\n"
105     "\t\tfrom 2009-10-01 until the current time:\n"
106     "\n"
107     "\t\t\t$(date +%%s -d 2009-10-01)..$(date +%%s)\n\n";
108
109 command_t commands[] = {
110     { "setup", notmuch_setup_command,
111       NULL,
112       "Interactively setup notmuch for first use.",
113       "\t\tThe setup command will prompt for your full name, your primary\n"
114       "\t\temail address, any alternate email addresses you use, and the\n"
115       "\t\tdirectory containing your email archives. Your answers will be\n"
116       "\t\twritten to a configuration file in ${NOTMUCH_CONFIG} (if set)\n"
117       "\t\tor ${HOME}/.notmuch-config.\n"
118       "\n"
119       "\t\tThis configuration file will be created with descriptive\n"
120       "\t\tcomments, making it easy to edit by hand later to change the\n"
121       "\t\tconfiguration. Or you can run \"notmuch setup\" again.\n"
122       "\n"
123       "\t\tInvoking notmuch with no command argument will run setup if\n"
124       "\t\tthe setup command has not previously been completed." },
125     { "new", notmuch_new_command,
126       "[--verbose]",
127       "\t\tFind and import new messages to the notmuch database.",
128       "\t\tScans all sub-directories of the mail directory, performing\n"
129       "\t\tfull-text indexing on new messages that are found. Each new\n"
130       "\t\tmessage will be tagged as both \"inbox\" and \"unread\".\n"
131       "\n"
132       "\t\tYou should run \"notmuch new\" once after first running\n"
133       "\t\t\"notmuch setup\" to create the initial database. The first\n"
134       "\t\trun may take a long time if you have a significant amount of\n"
135       "\t\tmail (several hundred thousand messages or more).\n"
136       "\n"
137       "\t\tSubsequently, you should run \"notmuch new\" whenever new mail\n"
138       "\t\tis delivered and you wish to incorporate it into the database.\n"
139       "\t\tThese subsequent runs will be much quicker than the initial run.\n"
140       "\n"
141       "\t\tSupported options for new include:\n"
142       "\n"
143       "\t\t--verbose\n"
144       "\n"
145       "\t\t\tVerbose operation. Shows paths of message files as\n"
146       "\t\t\tthey are being indexed.\n"
147       "\n"
148       "\t\tNote: \"notmuch new\" runs (other than the first run) will\n"
149       "\t\tskip any read-only directories, so you can use that to mark\n"
150       "\t\tdirectories that will not receive any new mail (and make\n"
151       "\t\t\"notmuch new\" even faster).\n"
152       "\n"
153       "\t\tInvoking notmuch with no command argument will run new if\n"
154       "\t\tthe setup command has previously been completed, but new has\n"
155       "\t\tnot previously been run." },
156     { "search", notmuch_search_command,
157       "[options...] <search-terms> [...]",
158       "\t\tSearch for messages matching the given search terms.",
159       "\t\tNote that the individual mail messages will be matched\n"
160       "\t\tagainst the search terms, but the results will be the\n"
161       "\t\tthreads (one per line) containing the matched messages.\n"
162       "\n"
163       "\t\tSupported options for search include:\n"
164       "\n"
165       "\t\t--sort=(newest-first|oldest-first)\n"
166       "\n"
167       "\t\t\tPresent results in either chronological order\n"
168       "\t\t\t(oldest-first) or reverse chronological order\n"
169       "\t\t\t(newest-first), which is the default.\n"
170       "\n"
171       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
172       "\t\tterms syntax." },
173     { "show", notmuch_show_command,
174       "<search-terms> [...]",
175       "\t\tShow all messages matching the search terms.",
176       "\t\tThe messages are grouped and sorted based on the threading\n"
177       "\t\t(all replies to a particular message appear immediately\n"
178       "\t\tafter that message in date order).\n"
179       "\n"
180       "\t\tSupported options for show include:\n"
181       "\n"
182       "\t\t--entire-thread\n"
183       "\n"
184       "\t\t\tBy default only those messages that match the\n"
185       "\t\t\tsearch terms will be displayed. With this option,\n"
186       "\t\t\tall messages in the same thread as any matched\n"
187       "\t\t\tmessage will be displayed.\n"
188       "\n"
189       "\t\tThe output format is plain-text, with all text-content\n"
190       "\t\tMIME parts decoded. Various components in the output,\n"
191       "\t\t('message', 'header', 'body', 'attachment', and MIME 'part')\n"
192       "\t\tare delimited by easily-parsed markers. Each marker consists\n"
193       "\t\tof a Control-L character (ASCII decimal 12), the name of\n"
194       "\t\tthe marker, and then either an opening or closing brace,\n"
195       "\t\t'{' or '}' to either open or close the component.\n"
196       "\n"
197       "\t\tA common use of \"notmuch show\" is to display a single\n"
198       "\t\tthread of email messages. For this, use a search term of\n"
199       "\t\t\"thread:<thread-id>\" as can be seen in the first column\n"
200       "\t\tof output from the \"notmuch search\" command.\n"
201       "\n"
202       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
203       "\t\tterms syntax." },
204     { "count", notmuch_count_command,
205       "<search-terms> [...]",
206       "\t\tCount messages matching the search terms.",
207       "\t\tThe number of matching messages is output to stdout.\n"
208       "\n"
209       "\t\tA common use of \"notmuch count\" is to display the count\n"
210       "\t\tof messages matching both a specific tag and either inbox\n"
211       "\t\tor unread\n"
212       "\n"
213       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
214       "\t\tterms syntax." },
215     { "reply", notmuch_reply_command,
216       "[options...] <search-terms> [...]",
217       "\t\tConstruct a reply template for a set of messages.",
218       "\t\tConstructs a new message as a reply to a set of existing\n"
219       "\t\tmessages. The Reply-To: header (if any, otherwise From:) is\n"
220       "\t\tused for the To: address. The To: and Cc: headers are copied,\n"
221       "\t\tbut not including any of the user's configured addresses.\n"
222       "\n"
223       "\t\tA suitable subject is constructed. The In-Reply-to: and\n"
224       "\t\tReferences: headers are set appropriately, and the content\n"
225       "\t\tof the original messages is quoted and included in the body\n"
226       "\t\t(unless --format=headers-only is given).\n"
227       "\n"
228       "\t\tThe resulting message template is output to stdout.\n"
229       "\n"
230       "\t\tSupported options for reply include:\n"
231       "\n"
232       "\t\t--format=(default|headers-only)\n"
233       "\n"
234       "\t\t\tdefault:\n"
235       "\t\t\t\tIncludes subject and quoted message body.\n"
236       "\n"
237       "\t\t\theaders-only:\n"
238       "\t\t\t\tOnly produces In-Reply-To, References, To\n"
239       "\t\t\t\tCc, and Bcc headers.\n"
240       "\n"
241       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
242       "\t\tterms syntax." },
243     { "tag", notmuch_tag_command,
244       "+<tag>|-<tag> [...] [--] <search-terms> [...]",
245       "\t\tAdd/remove tags for all messages matching the search terms.",
246       "\t\tThe search terms are handled exactly as in 'search' so one\n"
247       "\t\tcan use that command first to see what will be modified.\n"
248       "\n"
249       "\t\tTags prefixed by '+' are added while those prefixed by\n"
250       "\t\t'-' are removed. For each message, tag removal is performed\n"
251       "\t\tbefore tag addition.\n"
252       "\n"
253       "\t\tThe beginning of <search-terms> is recognized by the first\n"
254       "\t\targument that begins with neither '+' nor '-'. Support for\n"
255       "\t\tan initial search term beginning with '+' or '-' is provided\n"
256       "\t\tby allowing the user to specify a \"--\" argument to separate\n"
257       "\t\tthe tags from the search terms.\n"
258       "\n"
259       "\t\tSee \"notmuch help search-terms\" for details of the search\n"
260       "\t\tterms syntax." },
261     { "dump", notmuch_dump_command,
262       "[<filename>]",
263       "\t\tCreate a plain-text dump of the tags for each message.",
264       "\t\tOutput is to the given filename, if any, or to stdout.\n"
265       "\t\tThese tags are the only data in the notmuch database\n"
266       "\t\tthat can't be recreated from the messages themselves.\n"
267       "\t\tThe output of notmuch dump is therefore the only\n"
268       "\t\tcritical thing to backup (and much more friendly to\n"
269       "\t\tincremental backup than the native database files.)" },
270     { "restore", notmuch_restore_command,
271       "<filename>",
272       "\t\tRestore the tags from the given dump file (see 'dump').",
273       "\t\tNote: The dump file format is specifically chosen to be\n"
274       "\t\tcompatible with the format of files produced by sup-dump.\n"
275       "\t\tSo if you've previously been using sup for mail, then the\n"
276       "\t\t\"notmuch restore\" command provides you a way to import\n"
277       "\t\tall of your tags (or labels as sup calls them)." },
278     { "search-tags", notmuch_search_tags_command,
279       "[<search-terms> [...] ]",
280       "\t\tList all tags found in the database or matching messages.",
281       "\t\tRun this command without any search-term(s) to obtain a list\n"
282       "\t\tof all tags found in the database. If you provide one or more\n"
283       "\t\tsearch-terms as argument(s) then the resulting list will\n"
284       "\t\tcontain tags only from messages that match the search-term(s).\n"
285       "\n"
286       "\t\tIn both cases the list will be alphabetically sorted." },
287     { "help", notmuch_help_command,
288       "[<command>]",
289       "\t\tThis message, or more detailed help for the named command.",
290       "\t\tExcept in this case, where there's not much more detailed\n"
291       "\t\thelp available." }
292 };
293
294 static void
295 usage (FILE *out)
296 {
297     command_t *command;
298     unsigned int i;
299
300     fprintf (out, "Usage: notmuch <command> [args...]\n");
301     fprintf (out, "\n");
302     fprintf (out, "Where <command> and [args...] are as follows:\n");
303     fprintf (out, "\n");
304
305     for (i = 0; i < ARRAY_SIZE (commands); i++) {
306         command = &commands[i];
307
308         if (command->arguments)
309             fprintf (out, "\t%s\t%s\n\n%s\n\n",
310                      command->name, command->arguments, command->summary);
311         else
312             fprintf (out, "\t%s\t%s\n\n",
313                      command->name, command->summary);
314     }
315
316     fprintf (out,
317     "Use \"notmuch help <command>\" for more details on each command.\n"
318     "And \"notmuch help search-terms\" for the common search-terms syntax.\n\n");
319 }
320
321 static int
322 notmuch_help_command (unused (void *ctx), int argc, char *argv[])
323 {
324     command_t *command;
325     unsigned int i;
326
327     if (argc == 0) {
328         printf ("The notmuch mail system.\n\n");
329         usage (stdout);
330         return 0;
331     }
332
333     for (i = 0; i < ARRAY_SIZE (commands); i++) {
334         command = &commands[i];
335
336         if (strcmp (argv[0], command->name) == 0) {
337             printf ("Help for \"notmuch %s\":\n\n", argv[0]);
338             if (command->arguments)
339                 printf ("\t%s\t%s\n\n%s\n\n%s\n\n",
340                         command->name, command->arguments,
341                         command->summary, command->documentation);
342             else
343                 printf ("\t%s\t%s\n\n%s\n\n", command->name,
344                         command->summary, command->documentation);
345             return 0;
346         }
347     }
348
349     if (strcmp (argv[0], "search-terms") == 0) {
350         printf ("Help for <%s>\n\n", argv[0]);
351         for (i = 0; i < ARRAY_SIZE (commands); i++) {
352             command = &commands[i];
353
354             if (command->arguments &&
355                 strstr (command->arguments, "search-terms"))
356             {
357                 printf ("\t%s\t%s\n",
358                         command->name, command->arguments);
359             }
360         }
361         printf ("\n");
362         printf (search_terms_help);
363         return 0;
364     }
365
366     fprintf (stderr,
367              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
368              argv[0]);
369     return 1;
370 }
371
372 /* Handle the case of "notmuch" being invoked with no command
373  * argument. For now we just call notmuch_setup_command, but we plan
374  * to be more clever about this in the future.
375  */
376 static int
377 notmuch (void *ctx)
378 {
379     notmuch_config_t *config;
380     notmuch_bool_t is_new;
381     char *db_path;
382     struct stat st;
383
384     config = notmuch_config_open (ctx, NULL, &is_new);
385
386     /* If the user has never configured notmuch, then run
387      * notmuch_setup_command which will give a nice welcome message,
388      * and interactively guide the user through the configuration. */
389     if (is_new) {
390         notmuch_config_close (config);
391         return notmuch_setup_command (ctx, 0, NULL);
392     }
393
394     /* Notmuch is already configured, but is there a database? */
395     db_path = talloc_asprintf (ctx, "%s/%s",
396                                notmuch_config_get_database_path (config),
397                                ".notmuch");
398     if (stat (db_path, &st)) {
399         notmuch_config_close (config);
400         if (errno != ENOENT) {
401             fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
402                      db_path, strerror (errno));
403             return 1;
404         }
405         printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
406                 db_path);
407         printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
408                 "Note that the first run of \"notmuch new\" can take a very long time\n"
409                 "and that the resulting database will use roughly the same amount of\n"
410                 "storage space as the email being indexed.\n\n");
411         return 0;
412     }
413
414     printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
415             "At this point you can start exploring the functionality of notmuch by\n"
416             "using commands such as:\n\n"
417             "\tnotmuch search tag:inbox\n\n"
418             "\tnotmuch search to:\"%s\"\n\n"
419             "\tnotmuch search from:\"%s\"\n\n"
420             "\tnotmuch search subject:\"my favorite things\"\n\n"
421             "See \"notmuch help search\" for more details.\n\n"
422             "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
423             "from a search. Finally, you may want to explore using a more sophisticated\n"
424             "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
425             "or any other interface described at http://notmuchmail.org\n\n"
426             "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
427             "Have fun, and may your inbox never have much mail.\n\n",
428             notmuch_config_get_user_name (config),
429             notmuch_config_get_user_primary_email (config));
430
431     notmuch_config_close (config);
432
433     return 0;
434 }
435
436 int
437 main (int argc, char *argv[])
438 {
439     void *local;
440     command_t *command;
441     unsigned int i;
442
443     local = talloc_new (NULL);
444
445     g_mime_init (0);
446
447     if (argc == 1)
448         return notmuch (local);
449
450     for (i = 0; i < ARRAY_SIZE (commands); i++) {
451         command = &commands[i];
452
453         if (strcmp (argv[1], command->name) == 0)
454             return (command->function) (local, argc - 2, &argv[2]);
455     }
456
457     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
458              argv[1]);
459
460     talloc_free (local);
461
462     return 1;
463 }