]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
notmuch reply: Process headers a bit more accurately.
[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 int
209 main (int argc, char *argv[])
210 {
211     void *local = talloc_new (NULL);
212     command_t *command;
213     unsigned int i;
214
215     if (argc == 1)
216         return notmuch_setup_command (local, 0, NULL);
217
218     for (i = 0; i < ARRAY_SIZE (commands); i++) {
219         command = &commands[i];
220
221         if (strcmp (argv[1], command->name) == 0)
222             return (command->function) (local, argc - 2, &argv[2]);
223     }
224
225     /* Don't complain about "help" being an unknown command when we're
226        about to provide exactly what's wanted anyway. */
227     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
228              argv[1]);
229
230     talloc_free (local);
231
232     return 1;
233 }