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