1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
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/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
25 typedef int (*command_function_t) (void *ctx, int argc, char *argv[]);
27 typedef struct command {
29 command_function_t function;
30 const char *arguments;
32 const char *documentation;
36 notmuch_help_command (void *ctx, int argc, char *argv[]);
38 static const char search_terms_help[] =
39 "\t\tSeveral notmuch commands accept a comman syntax for search\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"
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"
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"
56 "\t\t\tid:<message-id>\n"
57 "\t\t\tthread:<thread-id>\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"
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"
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"
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"
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"
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"
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"
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\n";
94 command_t commands[] = {
95 { "setup", notmuch_setup_command,
97 "Interactively setup notmuch for first use.",
98 "\t\tThe setup command will prompt for your full name, your primary\n"
99 "\t\temail address, any alternate email addresses you use, and the\n"
100 "\t\tdirectory containing your email archives. Your answers will be\n"
101 "\t\twritten to a configuration file in ${HOME}/.notmuch-config .\n"
103 "\t\tThis configuration file will be created with descriptive\n"
104 "\t\tcomments, making it easy to edit by hand later to change the\n"
105 "\t\tconfiguration. Or you can run \"notmuch setup\" again.\n"
107 "\t\tInvoking notmuch with no command argument will run setup if\n"
108 "\t\tthe setup command has not previously been completed." },
109 { "new", notmuch_new_command,
111 "Find and import new messages to the notmuch database.",
112 "\t\tScans all sub-directories of the mail directory, performing\n"
113 "\t\tfull-text indexing on new messages that are found. Each new\n"
114 "\t\tmessage will be tagged as both \"inbox\" and \"unread\".\n"
116 "\t\tYou should run \"notmuch new\" once after first running\n"
117 "\t\t\"notmuch setup\" to create the initial database. The first\n"
118 "\t\trun may take a long time if you have a significant amount of\n"
119 "\t\tmail (several hundred thousand messages or more).\n"
121 "\t\tSubsequently, you should run \"notmuch new\" whenever new mail\n"
122 "\t\tis delivered and you wish to incorporate it into the database.\n"
123 "\t\tThese subsequent runs will be much quicker than the initial run.\n"
125 "\t\tNote: \"notmuch new\" runs (other than the first run) will\n"
126 "\t\tskip any read-only directories, so you can use that to mark\n"
127 "\t\tdirectories that will not receive any new mail (and make\n"
128 "\t\t\"notmuch new\" even faster).\n"
130 "\t\tInvoking notmuch with no command argument will run new if\n"
131 "\t\tthe setup command has previously been completed, but new has\n"
132 "\t\tnot previously been run." },
133 { "search", notmuch_search_command,
134 "[options...] <search-terms> [...]",
135 "\t\tSearch for messages matching the given search terms.",
136 "\t\tNote that the individual mail messages will be matched\n"
137 "\t\tagainst the search terms, but the results will be the\n"
138 "\t\tthreads (one per line) containing the matched messages.\n"
140 "\t\tSupported options for search include:\n"
142 "\t\t--max-threads=<value>\n"
144 "\t\t\tRestricts displayed search results to a subset\n"
145 "\t\t\tof the results that would match the terms.\n"
147 "\t\t--first=<value>\n"
149 "\t\t\tOmits the first <value> threads from the search\n"
150 "\t\t\tresults that would otherwise be displayed.\n"
152 "\t\t--sort=(newest-first|oldest-first)\n"
154 "\t\t\tPresent results in either chronological order\n"
155 "\t\t\t(oldest-first) or reverse chronological order\n"
156 "\t\t\t(newest-first), which is the default.\n"
158 "\t\tSee \"notmuch help search-terms\" for details of the search\n"
159 "\t\tterms syntax." },
160 { "show", notmuch_show_command,
161 "<search-terms> [...]",
162 "\t\tShow all messages matching the search terms.",
163 "\t\tThe messages are grouped and sorted based on the threading\n"
164 "\t\t(all replies to a particular message appear immediately\n"
165 "\t\tafter that message in date order).\n"
167 "\t\tThe output format is plain-text, with all text-content\n"
168 "\t\tMIME parts decoded. Various components in the output,\n"
169 "\t\t('message', 'header', 'body', 'attachment', and MIME 'part')\n"
170 "\t\tare delimited by easily-parsed markers. Each marker consists\n"
171 "\t\tof a Control-L character (ASCII decimal 12), the name of\n"
172 "\t\tthe marker, and then either an opening or closing brace,\n"
173 "\t\t'{' or '}' to either open or close the component.\n"
175 "\t\tA common use of \"notmuch show\" is to display a single\n"
176 "\t\tthread of email messages. For this, use a search term of\n"
177 "\t\t\"thread:<thread-id>\" as can be seen in the first column\n"
178 "\t\tof output from the \"notmuch search\" command.\n"
180 "\t\tSee \"notmuch help search-terms\" for details of the search\n"
181 "\t\tterms syntax." },
182 { "reply", notmuch_reply_command,
183 "<search-terms> [...]",
184 "\t\tConstruct a reply template for a set of messages.",
185 "\t\tConstructs a new message as a reply to a set of existing\n"
186 "\t\tmessages. The Reply-To: header (if any, otherwise From:) is\n"
187 "\t\tused for the To: address. The To: and Cc: headers are copied,\n"
188 "\t\tbut not including any of the user's configured addresses.\n"
190 "\t\tA suitable subject is constructed. The In-Reply-to: and\n"
191 "\t\tReferences: headers are set appropriately, and the content\n"
192 "\t\tof the original messages is quoted and included in the body.\n"
194 "\t\tThe resulting message template is output to stdout.\n"
196 "\t\tSee \"notmuch help search-terms\" for details of the search\n"
197 "\t\tterms syntax." },
198 { "tag", notmuch_tag_command,
199 "+<tag>|-<tag> [...] [--] <search-terms> [...]",
200 "\t\tAdd/remove tags for all messages matching the search terms.",
201 "\t\tThe search terms are handled exactly as in 'search' so one\n"
202 "\t\tcan use that command first to see what will be modified.\n"
204 "\t\tTags prefixed by '+' are added while those prefixed by\n"
205 "\t\t'-' are removed. For each message, tag removal is performed\n"
206 "\t\tbefore tag addition.\n"
208 "\t\tThe beginning of <search-terms> is recognized by the first\n"
209 "\t\targument that begins with neither '+' nor '-'. Support for\n"
210 "\t\tan initial search term beginning with '+' or '-' is provided\n"
211 "\t\tby allowing the user to specify a \"--\" argument to separate\n"
212 "\t\tthe tags from the search terms.\n"
214 "\t\tSee \"notmuch help search-terms\" for details of the search\n"
215 "\t\tterms syntax." },
216 { "dump", notmuch_dump_command,
218 "\t\tCreate a plain-text dump of the tags for each message.",
219 "\t\tOutput is to the given filename, if any, or to stdout.\n"
220 "\t\tThese tags are the only data in the notmuch database\n"
221 "\t\tthat can't be recreated from the messages themselves.\n"
222 "\t\tThe output of notmuch dump is therefore the only\n"
223 "\t\tcritical thing to backup (and much more friendly to\n"
224 "\t\tincremental backup than the native database files.)" },
225 { "restore", notmuch_restore_command,
227 "\t\tRestore the tags from the given dump file (see 'dump').",
228 "\t\tNote: The dump file format is specifically chosen to be\n"
229 "\t\tcompatible with the format of files produced by sup-dump.\n"
230 "\t\tSo if you've previously been using sup for mail, then the\n"
231 "\t\t\"notmuch restore\" command provides you a way to import\n"
232 "\t\tall of your tags (or labels as sup calls them)." },
233 { "help", notmuch_help_command,
235 "\t\tThis message, or more detailed help for the named command.",
236 "\t\tExcept in this case, where there's not much more detailed\n"
237 "\t\thelp available." }
246 fprintf (stderr, "Usage: notmuch <command> [args...]\n");
247 fprintf (stderr, "\n");
248 fprintf (stderr, "Where <command> and [args...] are as follows:\n");
249 fprintf (stderr, "\n");
251 for (i = 0; i < ARRAY_SIZE (commands); i++) {
252 command = &commands[i];
254 if (command->arguments)
255 fprintf (stderr, "\t%s\t%s\n\n%s\n\n",
256 command->name, command->arguments, command->summary);
258 fprintf (stderr, "\t%s\t%s\n\n",
259 command->name, command->summary);
263 "Use \"notmuch help <command>\" for more details on each command.\n"
264 "And \"notmuch help search-terms\" for the common search-terms syntax.\n\n");
268 notmuch_help_command (unused (void *ctx), int argc, char *argv[])
274 fprintf (stderr, "The notmuch mail system.\n\n");
279 for (i = 0; i < ARRAY_SIZE (commands); i++) {
280 command = &commands[i];
282 if (strcmp (argv[0], command->name) == 0) {
283 fprintf (stderr, "Help for \"notmuch %s\":\n\n", argv[0]);
284 if (command->arguments)
285 fprintf (stderr, "\t%s\t%s\n\n%s\n\n%s\n\n",
286 command->name, command->arguments,
287 command->summary, command->documentation);
289 fprintf (stderr, "\t%s\t%s\n\n%s\n\n", command->name,
290 command->summary, command->documentation);
295 if (strcmp (argv[0], "search-terms") == 0) {
296 fprintf (stderr, "Help for <%s>\n\n", argv[0]);
297 for (i = 0; i < ARRAY_SIZE (commands); i++) {
298 command = &commands[i];
300 if (command->arguments &&
301 strstr (command->arguments, "search-terms"))
303 fprintf (stderr, "\t%s\t%s\n",
304 command->name, command->arguments);
307 fprintf (stderr, "\n");
308 fprintf (stderr, search_terms_help);
313 "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
318 /* Handle the case of "notmuch" being invoked with no command
319 * argument. For now we just call notmuch_setup_command, but we plan
320 * to be more clever about this in the future.
325 notmuch_config_t *config;
326 notmuch_bool_t is_new;
330 config = notmuch_config_open (ctx, NULL, &is_new);
332 /* If the user has never configured notmuch, then run
333 * notmuch_setup_command which will give a nice welcome message,
334 * and interactively guide the user through the configuration. */
336 notmuch_config_close (config);
337 return notmuch_setup_command (ctx, 0, NULL);
340 /* Notmuch is already configured, but is there a database? */
341 db_path = talloc_asprintf (ctx, "%s/%s",
342 notmuch_config_get_database_path (config),
344 if (stat (db_path, &st)) {
345 notmuch_config_close (config);
346 if (errno != ENOENT) {
347 fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
348 db_path, strerror (errno));
351 printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
353 printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
354 "Note that the first run of \"notmuch new\" can take a very long time\n"
355 "and that the resulting database will use roughly the same amount of\n"
356 "storage space as the email being indexed.\n\n");
360 printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
361 "At this point you can start exploring the functionality of notmuch by\n"
362 "using commands such as:\n\n"
363 "\tnotmuch search tag:inbox\n\n"
364 "\tnotmuch search to:\"%s\"\n\n"
365 "\tnotmuch search from:\"%s\"\n\n"
366 "\tnotmuch search subject:\"my favorite things\"\n\n"
367 "See \"notmuch help search\" for more details.\n\n"
368 "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
369 "from a search. Finally, you may want to explore using a more sophisticated\n"
370 "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
371 "or any other interface described at http://notmuchmail.org\n\n"
372 "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
373 "Have fun, and may your inbox never have much mail.\n\n",
374 notmuch_config_get_user_name (config),
375 notmuch_config_get_user_primary_email (config));
377 notmuch_config_close (config);
383 main (int argc, char *argv[])
389 local = talloc_new (NULL);
394 return notmuch (local);
396 for (i = 0; i < ARRAY_SIZE (commands); i++) {
397 command = &commands[i];
399 if (strcmp (argv[1], command->name) == 0)
400 return (command->function) (local, argc - 2, &argv[2]);
403 /* Don't complain about "help" being an unknown command when we're
404 about to provide exactly what's wanted anyway. */
405 fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",