]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
Include <stdint.h> to get uint32_t in C++ file with gcc 4.4
[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. For now we just call notmuch_setup_command, but we plan
210  * to be more clever about this in the future.
211  */
212 static int
213 notmuch (void *ctx)
214 {
215     notmuch_config_t *config;
216     notmuch_bool_t is_new;
217     char *db_path;
218     struct stat st;
219
220     config = notmuch_config_open (ctx, NULL, &is_new);
221
222     /* If the user has never configured notmuch, then run
223      * notmuch_setup_command which will give a nice welcome message,
224      * and interactively guide the user through the configuration. */
225     if (is_new) {
226         notmuch_config_close (config);
227         return notmuch_setup_command (ctx, 0, NULL);
228     }
229
230     /* Notmuch is already configured, but is there a database? */
231     db_path = talloc_asprintf (ctx, "%s/%s",
232                                notmuch_config_get_database_path (config),
233                                ".notmuch");
234     if (stat (db_path, &st)) {
235         notmuch_config_close (config);
236         if (errno != ENOENT) {
237             fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
238                      db_path, strerror (errno));
239             return 1;
240         }
241         printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
242                 db_path);
243         printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
244                 "Note that the first run of \"notmuch new\" can take a very long time\n"
245                 "and that the resulting database will use roughly the same amount of\n"
246                 "storage space as the email being indexed.\n\n");
247         return 0;
248     }
249
250     printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
251             "At this point you can start exploring the functionality of notmuch by\n"
252             "using commands such as:\n\n"
253             "\tnotmuch search tag:inbox\n\n"
254             "\tnotmuch search to:\"%s\"\n\n"
255             "\tnotmuch search from:\"%s\"\n\n"
256             "\tnotmuch search subject:\"my favorite things\"\n\n"
257             "See \"notmuch help search\" for more details.\n\n"
258             "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
259             "from a search. Finally, you may want to explore using a more sophisticated\n"
260             "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
261             "or any other interface described at http://notmuchmail.org\n\n"
262             "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
263             "Have fun, and may your inbox never have much mail.\n\n",
264             notmuch_config_get_user_name (config),
265             notmuch_config_get_user_primary_email (config));
266
267     notmuch_config_close (config);
268
269     return 0;
270 }
271
272 int
273 main (int argc, char *argv[])
274 {
275     void *local;
276     command_t *command;
277     unsigned int i;
278
279     local = talloc_new (NULL);
280
281     g_mime_init (0);
282
283     if (argc == 1)
284         return notmuch (local);
285
286     for (i = 0; i < ARRAY_SIZE (commands); i++) {
287         command = &commands[i];
288
289         if (strcmp (argv[1], command->name) == 0)
290             return (command->function) (local, argc - 2, &argv[2]);
291     }
292
293     /* Don't complain about "help" being an unknown command when we're
294        about to provide exactly what's wanted anyway. */
295     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
296              argv[1]);
297
298     talloc_free (local);
299
300     return 1;
301 }