]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
notmuch: Add a configuration system.
[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 static int
26 notmuch_help_command (void *ctx, int argc, char *argv[]);
27
28 command_t commands[] = {
29     { "setup", notmuch_setup_command,
30       "Interactively setup notmuch for first use.",
31       "\t\tThe setup command is the first command you will run in order\n"
32       "\t\tto start using notmuch. It will prompt you for the directory\n"
33       "\t\tcontaining your email archives, and will then proceed to build\n"
34       "\t\ta database to allow fast searching of that mail.\n\n"
35       "\t\tInvoking notmuch with no command argument will run setup if\n"
36       "\t\tthe setup command has not previously been completed." },
37     { "new", notmuch_new_command,
38       "Find and import any new messages.",
39       "\t\tScans all sub-directories of the database, adding new messages\n"
40       "\t\tthat are found. Each new message will be tagged as both\n"
41       "\t\t\"inbox\" and \"unread\".\n"
42       "\n"
43       "\t\tNote: \"notmuch new\" will skip any read-only directories,\n"
44       "\t\tso you can use that to mark directories that will not\n"
45       "\t\treceive any new mail (and make \"notmuch new\" faster)." },
46     { "search", notmuch_search_command,
47       "<search-term> [...]\n\n"
48       "\t\tSearch for threads matching the given search terms.",
49       "\t\tNote that the individual mail messages will be matched\n"
50       "\t\tagainst the search terms, but the results will be the\n"
51       "\t\tthreads containing the matched messages.\n\n"
52       "\t\tCurrently, in addition to free text (and quoted phrases)\n"
53       "\t\twhich match terms appearing anywhere within an email,\n"
54       "\t\tthe following prefixes can be used to search specific\n"
55       "\t\tportions of an email, (where <brackets> indicate user-\n"
56       "\t\tsupplied values):\n\n"
57       "\t\t\tfrom:<name-or-address>\n"
58       "\t\t\tto:<name-or-address>\n"
59       "\t\t\tsubject:<word-or-quoted-phrase>\n"
60       "\t\t\ttag:<tag>\n"
61       "\t\t\tid:<message-id>\n"
62       "\t\t\tthread:<thread-id>\n\n"
63       "\t\tThe from: prefix is used to match the name or address of\n"
64       "\t\tthe sender of an email message.\n\n"
65       "\t\tThe to: prefix is used to match the names or addresses of\n"
66       "\t\tany recipient of an email message, (whether To, Cc, or Bcc).\n\n"
67       "\t\tAny term prefixed with subject: will match only text from\n"
68       "\t\tthe subject of an email. Quoted phrases are supported when\n"
69       "\t\tsearching with: subject:\"this is a phrase\".\n\n"
70       "\t\tValid tag values include \"inbox\" and \"unread\" by default\n"
71       "\t\tfor new messages added by \"notmuch new\" as well as any other\n"
72       "\t\ttag values added manually with \"notmuch tag\".\n\n"
73       "\t\tMessage ID values are the literal contents of the Message-ID:\n"
74       "\t\theader of email messages, but without the '<','>' delimiters.\n\n"
75       "\t\tThread ID values are generated internally by notmuch but can\n"
76       "\t\tbe seen in the output of \"notmuch search\" for example.\n\n"
77       "\t\tIn addition to individual terms, multiple terms can be\n"
78       "\t\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
79       "\t\tEach term in the query will be implicitly connected by a\n"
80       "\t\tlogical AND if no explicit operator is provided, (except\n"
81       "\t\tthat terms with a common prefix will be implicitly combined\n"
82       "\t\twith OR until we get Xapian defect #402 fixed).\n\n"
83       "\t\tParentheses can also be used to control the combination of\n"
84       "\t\tthe Boolean operators, but will have to be protected from\n"
85       "\t\tinterpretation by the shell, (such as by putting quotation\n"
86       "\t\tmarks around any parenthesized expression)." },
87     { "reply", notmuch_reply_command,
88       "<search-terms> [...]\n\n"
89       "\t\tFormats a reply from a set of existing messages.",
90       "\t\tConstructs a new message as a reply to a set of existing\n"
91       "\t\tmessages. The From: address is used as a To: address\n"
92       "\t\talong with all old To: addresses. All of the Cc: addresses\n"
93       "\t\tare copied as new Cc: addresses. An In-Reply-To: header\n"
94       "\t\twill be constructed from the name and date of the original\n"
95       "\t\tmessage, and the original Message-ID will be added to the\n"
96       "\t\tlist of References in the new message. The text of each\n"
97       "\t\tmessage (as described in the \"show\" command) will be\n"
98       "\t\tpresented, each line prefixed with \"> \" The resulting\n"
99       "\t\tmessage will be dumped to stdout." },
100     { "show", notmuch_show_command,
101       "<search-terms> [...]\n\n"
102       "\t\tShows all messages matching the search terms.",
103       "\t\tSee the documentation of \"notmuch search\" for details\n"
104       "\t\tof the supported syntax of search terms.\n\n"
105       "\t\tA common use of \"notmuch show\" is to display a single\n"
106       "\t\tthread of email messages. For this, use a search term of\n"
107       "\t\t\"thread:<thread-id>\" as can be seen in the first column\n"
108       "\t\tof output from the \"notmuch search\" command.\n\n"
109       "\t\tAll messages will be displayed in date order. The output\n"
110       "\t\tformat is plain-text, with all text-content MIME parts\n"
111       "\t\tdecoded. Various components in the output, ('message',\n"
112       "\t\t'header', 'body', 'attachment', and MIME 'part') will be\n"
113       "\t\tdelimited by easily-parsed markers. Each marker consists\n"
114       "\t\tof a Control-L character (ASCII decimal 12), the name of\n"
115       "\t\tthe marker, and then either an opening or closing brace,\n"
116       "\t\t'{' or '}' to either open or close the component."},
117     { "tag", notmuch_tag_command,
118       "+<tag>|-<tag> [...] [--] <search-term> [...]\n\n"
119       "\t\tAdd/remove tags for all messages matching the search terms.",
120       "\t\tThe search terms are handled exactly as in 'search' so one\n"
121       "\t\tcan use that command first to see what will be modified.\n\n"
122       "\t\tTags prefixed by '+' are added while those prefixed by '-' are\n"
123       "\t\tremoved. For each message, tag removal is before tag addition.\n\n"
124       "\t\tThe beginning of <search-terms> is recognized by the first\n"
125       "\t\targument that begins with neither '+' nor '-'. Support for\n"
126       "\t\tan initial search term beginning with '+' or '-' is provided\n"
127       "\t\tby allowing the user to specify a \"--\" argument to separate\n"
128       "\t\tthe tags from the search terms.\n\n"
129       "\t\tNote: If you run \"notmuch new\" between reading a thread with\n"
130       "\t\t\"notmuch show\" and removing the \"inbox\" tag for that thread\n"
131       "\t\twith \"notmuch tag\" then you create the possibility of moving\n"
132       "\t\tsome messages from that thread out of your inbox without ever\n"
133       "\t\treading them. The easiest way to avoid this problem is to not\n"
134       "\t\trun \"notmuch new\" between reading and removing tags." },
135     { "dump", notmuch_dump_command,
136       "[<filename>]\n\n"
137       "\t\tCreate a plain-text dump of the tags for each message.",
138       "\t\tOutput is to the given filename, if any, or to stdout.\n"
139       "\t\tThese tags are the only data in the notmuch database\n"
140       "\t\tthat can't be recreated from the messages themselves.\n"
141       "\t\tThe output of notmuch dump is therefore the only\n"
142       "\t\tcritical thing to backup (and much more friendly to\n"
143       "\t\tincremental backup than the native database files.)" },
144     { "restore", notmuch_restore_command,
145       "<filename>\n\n"
146       "\t\tRestore the tags from the given dump file (see 'dump').",
147       "\t\tNote: The dump file format is specifically chosen to be\n"
148       "\t\tcompatible with the format of files produced by sup-dump.\n"
149       "\t\tSo if you've previously been using sup for mail, then the\n"
150       "\t\t\"notmuch restore\" command provides you a way to import\n"
151       "\t\tall of your tags (or labels as sup calls them)." },
152     { "help", notmuch_help_command,
153       "[<command>]\n\n"
154       "\t\tThis message, or more detailed help for the named command.",
155       "\t\tExcept in this case, where there's not much more detailed\n"
156       "\t\thelp available." }
157 };
158
159 static void
160 usage (void)
161 {
162     command_t *command;
163     unsigned int i;
164
165     fprintf (stderr, "Usage: notmuch <command> [args...]\n");
166     fprintf (stderr, "\n");
167     fprintf (stderr, "Where <command> and [args...] are as follows:\n");
168     fprintf (stderr, "\n");
169
170     for (i = 0; i < ARRAY_SIZE (commands); i++) {
171         command = &commands[i];
172
173         fprintf (stderr, "\t%s\t%s\n\n", command->name, command->summary);
174     }
175
176     fprintf (stderr, "Use \"notmuch help <command>\" for more details on each command.\n\n");
177 }
178
179 static int
180 notmuch_help_command (unused (void *ctx), int argc, char *argv[])
181 {
182     command_t *command;
183     unsigned int i;
184
185     if (argc == 0) {
186         fprintf (stderr, "The notmuch mail system.\n\n");
187         usage ();
188         return 0;
189     }
190
191     for (i = 0; i < ARRAY_SIZE (commands); i++) {
192         command = &commands[i];
193
194         if (strcmp (argv[0], command->name) == 0) {
195             fprintf (stderr, "Help for \"notmuch %s\":\n\n", argv[0]);
196             fprintf (stderr, "\t%s\t%s\n\n%s\n\n", command->name,
197                      command->summary, command->documentation);
198             return 0;
199         }
200     }
201
202     fprintf (stderr,
203              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
204              argv[0]);
205     return 1;
206 }
207
208 /* Handle the case of "notmuch" being invoked with no command
209  * argument. Print a welcome and explanatory message, then invoke
210  * notmuch_setup_command.
211  */
212 static int
213 notmuch (void *ctx)
214 {
215     int ret;
216
217     printf (
218 "Welcome to notmuch!\n\n"
219
220 "The goal of notmuch is to help you manage and search your collection of\n"
221 "email, and to efficiently keep up with the flow of email as it comes in.\n\n"
222
223 "Notmuch needs to know a few things about you such as your name and email\n"
224 "address, as well as the directory that contains your email. This is where\n"
225 "you already have mail stored and where messages will be delivered in the\n"
226 "future. This directory can contain any number of sub-directories. Regular\n"
227 "files in these directories should be individual email messages. If there\n"
228 "are other, non-email files (such as indexes maintained by other email\n"
229 "programs) then notmuch will do its best to detect those and ignore them.\n\n"
230
231 "If you already have your email being delivered to directories in either\n"
232 "maildir or mh format, then that's perfect. Mail storage that uses mbox\n"
233 "format, (where one mbox file contains many messages), will not work with\n"
234 "notmuch. If that's how your mail is currently stored, we recommend you\n"
235 "first convert it to maildir format with a utility such as mb2md. You can\n"
236 "continue configuring notmuch now, but be sure to complete the conversion\n"
237 "before you run \"notmuch new\" for the first time.\n\n");
238
239     ret = notmuch_setup_command (ctx, 0, NULL);
240
241     printf ("\n"
242 "Notmuch is now configured, and the configuration settings are saved in\n"
243 "a file in your home directory named .notmuch-config . If you'd like to\n"
244 "change the configuration in the future, you can either edit that file\n"
245 "directly or run \"notmuch setup\".\n\n"
246
247 "The next step is to run \"notmuch new\" which will create a database\n"
248 "that indexes all of your mail. Depending on the amount of mail you have\n"
249 "the initial indexing process can take a long time, so expect that.\n"
250 "Also, the resulting database will require roughly the same amount of\n"
251 "storage space as your current collection of email. So please ensure you\n"
252 "have sufficient storage space available now.\n\n");
253
254     return ret;
255 }
256
257 int
258 main (int argc, char *argv[])
259 {
260     void *local;
261     command_t *command;
262     unsigned int i;
263
264     local = talloc_new (NULL);
265
266     g_mime_init (0);
267
268     if (argc == 1)
269         return notmuch (local);
270
271     for (i = 0; i < ARRAY_SIZE (commands); i++) {
272         command = &commands[i];
273
274         if (strcmp (argv[1], command->name) == 0)
275             return (command->function) (local, argc - 2, &argv[2]);
276     }
277
278     /* Don't complain about "help" being an unknown command when we're
279        about to provide exactly what's wanted anyway. */
280     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
281              argv[1]);
282
283     talloc_free (local);
284
285     return 1;
286 }