]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
notmuch: Break notmuch.c up into several smaller files.
[notmuch] / notmuch.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 static int
24 notmuch_help_command (void *ctx, int argc, char *argv[]);
25
26 command_t commands[] = {
27     { "setup", notmuch_setup_command,
28       "Interactively setup notmuch for first use.",
29       "\t\tThe setup command is the first command you will run in order\n"
30       "\t\tto start using notmuch. It will prompt you for the directory\n"
31       "\t\tcontaining your email archives, and will then proceed to build\n"
32       "\t\ta database to allow fast searching of that mail.\n\n"
33       "\t\tInvoking notmuch with no command argument will run setup if\n"
34       "\t\tthe setup command has not previously been completed." },
35     { "new", notmuch_new_command,
36       "Find and import any new messages.",
37       "\t\tScans all sub-directories of the database, adding new messages\n"
38       "\t\tthat are found. Each new message will be tagged as both\n"
39       "\t\t\"inbox\" and \"unread\".\n"
40       "\n"
41       "\t\tNote: \"notmuch new\" will skip any read-only directories,\n"
42       "\t\tso you can use that to mark directories that will not\n"
43       "\t\treceive any new mail (and make \"notmuch new\" faster)." },
44     { "search", notmuch_search_command,
45       "<search-term> [...]\n\n"
46       "\t\tSearch for threads matching the given search terms.",
47       "\t\tNote that the individual mail messages will be matched\n"
48       "\t\tagainst the search terms, but the results will be the\n"
49       "\t\tthreads containing the matched messages.\n\n"
50       "\t\tCurrently, in addition to free text (and quoted phrases)\n"
51       "\t\twhich match terms appearing anywhere within an email,\n"
52       "\t\tthe following prefixes can be used to search specific\n"
53       "\t\tportions of an email, (where <brackets> indicate user-\n"
54       "\t\tsupplied values):\n\n"
55       "\t\t\tfrom:<name-or-address>\n"
56       "\t\t\tto:<name-or-address>\n"
57       "\t\t\tsubject:<word-or-quoted-phrase>\n"
58       "\t\t\ttag:<tag>\n"
59       "\t\t\tid:<message-id>\n"
60       "\t\t\tthread:<thread-id>\n\n"
61       "\t\tThe from: prefix is used to match the name or address of\n"
62       "\t\tthe sender of an email message.\n\n"
63       "\t\tThe to: prefix is used to match the names or addresses of\n"
64       "\t\tany recipient of an email message, (whether To, Cc, or Bcc).\n\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\n"
68       "\t\tValid tag values include \"inbox\" and \"unread\" by default\n"
69       "\t\tfor new messages added by \"notmuch new\" as well as any other\n"
70       "\t\ttag values added manually with \"notmuch tag\".\n\n"
71       "\t\tMessage ID values are the literal contents of the Message-ID:\n"
72       "\t\theader of email messages, but without the '<','>' delimiters.\n\n"
73       "\t\tThread ID values are generated internally by notmuch but can\n"
74       "\t\tbe seen in the output of \"notmuch search\" for example.\n\n"
75       "\t\tIn addition to individual terms, multiple terms can be\n"
76       "\t\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
77       "\t\tEach term in the query will be implicitly connected by a\n"
78       "\t\tlogical AND if no explicit operator is provided, (except\n"
79       "\t\tthat terms with a common prefix will be implicitly combined\n"
80       "\t\twith OR until we get Xapian defect #402 fixed).\n\n"
81       "\t\tParentheses can also be used to control the combination of\n"
82       "\t\tthe Boolean operators, but will have to be protected from\n"
83       "\t\tinterpretation by the shell, (such as by putting quotation\n"
84       "\t\tmarks around any parenthesized expression)." },
85     { "show", notmuch_show_command,
86       "<search-terms> [...]\n\n"
87       "\t\tShows all messages matching the search terms.",
88       "\t\tSee the documentation of \"notmuch search\" for details\n"
89       "\t\tof the supported syntax of search terms.\n\n"
90       "\t\tA common use of \"notmuch show\" is to display a single\n"
91       "\t\tthread of email messages. For this, use a search term of\n"
92       "\t\t\"thread:<thread-id>\" as can be seen in the first column\n"
93       "\t\tof output from the \"notmuch search\" command.\n\n"
94       "\t\tAll messages will be displayed in date order. The output\n"
95       "\t\tformat is plain-text, with all text-content MIME parts\n"
96       "\t\tdecoded. Various components in the output, ('message',\n"
97       "\t\t'header', 'body', 'attachment', and MIME 'part') will be\n"
98       "\t\tdelimited by easily-parsed markers. Each marker consists\n"
99       "\t\tof a Control-L character (ASCII decimal 12), the name of\n"
100       "\t\tthe marker, and then either an opening or closing brace,\n"
101       "\t\t'{' or '}' to either open or close the component."},
102     { "tag", notmuch_tag_command,
103       "+<tag>|-<tag> [...] [--] <search-term> [...]\n\n"
104       "\t\tAdd/remove tags for all messages matching the search terms.",
105       "\t\tThe search terms are handled exactly as in 'search' so one\n"
106       "\t\tcan use that command first to see what will be modified.\n\n"
107       "\t\tTags prefixed by '+' are added while those prefixed by '-' are\n"
108       "\t\tremoved. For each message, tag removal is before tag addition.\n\n"
109       "\t\tThe beginning of <search-terms> is recognized by the first\n"
110       "\t\targument that begins with neither '+' nor '-'. Support for\n"
111       "\t\tan initial search term beginning with '+' or '-' is provided\n"
112       "\t\tby allowing the user to specify a \"--\" argument to separate\n"
113       "\t\tthe tags from the search terms.\n\n"
114       "\t\tNote: If you run \"notmuch new\" between reading a thread with\n"
115       "\t\t\"notmuch show\" and removing the \"inbox\" tag for that thread\n"
116       "\t\twith \"notmuch tag\" then you create the possibility of moving\n"
117       "\t\tsome messages from that thread out of your inbox without ever\n"
118       "\t\treading them. The easiest way to avoid this problem is to not\n"
119       "\t\trun \"notmuch new\" between reading and removing tags." },
120     { "dump", notmuch_dump_command,
121       "[<filename>]\n\n"
122       "\t\tCreate a plain-text dump of the tags for each message.",
123       "\t\tOutput is to the given filename, if any, or to stdout.\n"
124       "\t\tThese tags are the only data in the notmuch database\n"
125       "\t\tthat can't be recreated from the messages themselves.\n"
126       "\t\tThe output of notmuch dump is therefore the only\n"
127       "\t\tcritical thing to backup (and much more friendly to\n"
128       "\t\tincremental backup than the native database files.)" },
129     { "restore", notmuch_restore_command,
130       "<filename>\n\n"
131       "\t\tRestore the tags from the given dump file (see 'dump').",
132       "\t\tNote: The dump file format is specifically chosen to be\n"
133       "\t\tcompatible with the format of files produced by sup-dump.\n"
134       "\t\tSo if you've previously been using sup for mail, then the\n"
135       "\t\t\"notmuch restore\" command provides you a way to import\n"
136       "\t\tall of your tags (or labels as sup calls them)." },
137     { "help", notmuch_help_command,
138       "[<command>]\n\n"
139       "\t\tThis message, or more detailed help for the named command.",
140       "\t\tExcept in this case, where there's not much more detailed\n"
141       "\t\thelp available." }
142 };
143
144 static void
145 usage (void)
146 {
147     command_t *command;
148     unsigned int i;
149
150     fprintf (stderr, "Usage: notmuch <command> [args...]\n");
151     fprintf (stderr, "\n");
152     fprintf (stderr, "Where <command> and [args...] are as follows:\n");
153     fprintf (stderr, "\n");
154
155     for (i = 0; i < ARRAY_SIZE (commands); i++) {
156         command = &commands[i];
157
158         fprintf (stderr, "\t%s\t%s\n\n", command->name, command->summary);
159     }
160
161     fprintf (stderr, "Use \"notmuch help <command>\" for more details on each command.\n\n");
162 }
163
164 static int
165 notmuch_help_command (unused (void *ctx), int argc, char *argv[])
166 {
167     command_t *command;
168     unsigned int i;
169
170     if (argc == 0) {
171         fprintf (stderr, "The notmuch mail system.\n\n");
172         usage ();
173         return 0;
174     }
175
176     for (i = 0; i < ARRAY_SIZE (commands); i++) {
177         command = &commands[i];
178
179         if (strcmp (argv[0], command->name) == 0) {
180             fprintf (stderr, "Help for \"notmuch %s\":\n\n", argv[0]);
181             fprintf (stderr, "\t%s\t%s\n\n%s\n\n", command->name,
182                      command->summary, command->documentation);
183             return 0;
184         }
185     }
186
187     fprintf (stderr,
188              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
189              argv[0]);
190     return 1;
191 }
192     
193 int
194 main (int argc, char *argv[])
195 {
196     void *local = talloc_new (NULL);
197     command_t *command;
198     unsigned int i;
199
200     if (argc == 1)
201         return notmuch_setup_command (local, 0, NULL);
202
203     for (i = 0; i < ARRAY_SIZE (commands); i++) {
204         command = &commands[i];
205
206         if (strcmp (argv[1], command->name) == 0)
207             return (command->function) (local, argc - 2, &argv[2]);
208     }
209
210     /* Don't complain about "help" being an unknown command when we're
211        about to provide exactly what's wanted anyway. */
212     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
213              argv[1]);
214
215     talloc_free (local);
216
217     return 1;
218 }