]> git.notmuchmail.org Git - notmuch/blob - notmuch.c
use format=raw by default when requesting part with --part=
[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 typedef int (*command_function_t) (void *ctx, int argc, char *argv[]);
26
27 typedef struct command {
28     const char *name;
29     command_function_t function;
30     const char *arguments;
31     const char *summary;
32     const char *documentation;
33 } command_t;
34
35 #define MAX_ALIAS_SUBSTITUTIONS 2
36
37 typedef struct alias {
38     const char *name;
39     const char *substitutions[MAX_ALIAS_SUBSTITUTIONS];
40 } alias_t;
41
42 alias_t aliases[] = {
43     { "part", { "show", "--format=raw"}}
44 };
45
46 static int
47 notmuch_help_command (void *ctx, int argc, char *argv[]);
48
49 static const char search_terms_help[] =
50     "\tSeveral notmuch commands accept a comman syntax for search\n"
51     "\tterms.\n"
52     "\n"
53     "\tThe search terms can consist of free-form text (and quoted\n"
54     "\tphrases) which will match all messages that contain all of\n"
55     "\tthe given terms/phrases in the body, the subject, or any of\n"
56     "\tthe sender or recipient headers.\n"
57     "\n"
58     "\tAs a special case, a search string consisting of exactly a\n"
59     "\tsingle asterisk (\"*\") will match all messages.\n"
60     "\n"
61     "\tIn addition to free text, the following prefixes can be used\n"
62     "\tto force terms to match against specific portions of an email,\n"
63     "\t(where <brackets> indicate user-supplied values):\n"
64     "\n"
65     "\t\tfrom:<name-or-address>\n"
66     "\t\tto:<name-or-address>\n"
67     "\t\tsubject:<word-or-quoted-phrase>\n"
68     "\t\tattachment:<word>\n"
69     "\t\ttag:<tag> (or is:<tag>)\n"
70     "\t\tid:<message-id>\n"
71     "\t\tthread:<thread-id>\n"
72     "\t\tfolder:<directory-path>\n"
73     "\n"
74     "\tThe from: prefix is used to match the name or address of\n"
75     "\tthe sender of an email message.\n"
76     "\n"
77     "\tThe to: prefix is used to match the names or addresses of\n"
78     "\tany recipient of an email message, (whether To, Cc, or Bcc).\n"
79     "\n"
80     "\tAny term prefixed with subject: will match only text from\n"
81     "\tthe subject of an email. Quoted phrases are supported when\n"
82     "\tsearching with: subject:\"this is a phrase\".\n"
83     "\n"
84     "\tFor tag: and is:, valid tag values include \"inbox\" and \"unread\"\n"
85     "\tby default for new messages added by \"notmuch new\" as well\n"
86     "\tas any other tag values added manually with \"notmuch tag\".\n"
87     "\n"
88     "\tFor id:, message ID values are the literal contents of the\n"
89     "\tMessage-ID: header of email messages, but without the '<','>'\n"
90     "\tdelimiters.\n"
91     "\n"
92     "\tThe thread: prefix can be used with the thread ID values that\n"
93     "\tare generated internally by notmuch (and do not appear in email\n"
94     "\tmessages). These thread ID values can be seen in the first\n"
95     "\tcolumn of output from \"notmuch search\".\n"
96     "\n"
97     "\tThe folder: prefix can be used to search for email message\n"
98     "\tfiles that are contained within particular directories within\n"
99     "\tthe mail store. Only the directory components below the top-level\n"
100     "\tmail database path are available to be searched.\n"
101     "\n"
102     "\tIn addition to individual terms, multiple terms can be\n"
103     "\tcombined with Boolean operators (\"and\", \"or\", \"not\", etc.).\n"
104     "\tEach term in the query will be implicitly connected by a\n"
105     "\tlogical AND if no explicit operator is provided, (except\n"
106     "\tthat terms with a common prefix will be implicitly combined\n"
107     "\twith OR until we get Xapian defect #402 fixed).\n"
108     "\n"
109     "\tParentheses can also be used to control the combination of\n"
110     "\tthe Boolean operators, but will have to be protected from\n"
111     "\tinterpretation by the shell, (such as by putting quotation\n"
112     "\tmarks around any parenthesized expression).\n"
113     "\n"
114     "\tFinally, results can be restricted to only messages within a\n"
115     "\tparticular time range, (based on the Date: header) with:\n"
116     "\n"
117     "\t\t<intial-timestamp>..<final-timestamp>\n"
118     "\n"
119     "\tEach timestamp is a number representing the number of seconds\n"
120     "\tsince 1970-01-01 00:00:00 UTC. This is not the most convenient\n"
121     "\tmeans of expressing date ranges, but until notmuch is fixed to\n"
122     "\taccept a more convenient form, one can use the date program to\n"
123     "\tconstruct timestamps. For example, with the bash shell the\n"
124     "\tfollowing syntax would specify a date range to return messages\n"
125     "\tfrom 2009-10-01 until the current time:\n"
126     "\n"
127     "\t\t$(date +%%s -d 2009-10-01)..$(date +%%s)\n\n";
128
129 static command_t commands[] = {
130     { "setup", notmuch_setup_command,
131       NULL,
132       "Interactively setup notmuch for first use.",
133       "\tThe setup command will prompt for your full name, your primary\n"
134       "\temail address, any alternate email addresses you use, and the\n"
135       "\tdirectory containing your email archives. Your answers will be\n"
136       "\twritten to a configuration file in ${NOTMUCH_CONFIG} (if set)\n"
137       "\tor ${HOME}/.notmuch-config.\n"
138       "\n"
139       "\tThis configuration file will be created with descriptive\n"
140       "\tcomments, making it easy to edit by hand later to change the\n"
141       "\tconfiguration. Or you can run \"notmuch setup\" again.\n"
142       "\n"
143       "\tInvoking notmuch with no command argument will run setup if\n"
144       "\tthe setup command has not previously been completed." },
145     { "new", notmuch_new_command,
146       "[--verbose]",
147       "Find and import new messages to the notmuch database.",
148       "\tScans all sub-directories of the mail directory, performing\n"
149       "\tfull-text indexing on new messages that are found. Each new\n"
150       "\tmessage will be tagged as both \"inbox\" and \"unread\".\n"
151       "\n"
152       "\tYou should run \"notmuch new\" once after first running\n"
153       "\t\"notmuch setup\" to create the initial database. The first\n"
154       "\trun may take a long time if you have a significant amount of\n"
155       "\tmail (several hundred thousand messages or more).\n"
156       "\n"
157       "\tSubsequently, you should run \"notmuch new\" whenever new mail\n"
158       "\tis delivered and you wish to incorporate it into the database.\n"
159       "\tThese subsequent runs will be much quicker than the initial run.\n"
160       "\n"
161       "\tSupported options for new include:\n"
162       "\n"
163       "\t--verbose\n"
164       "\n"
165       "\t\tVerbose operation. Shows paths of message files as\n"
166       "\t\tthey are being indexed.\n"
167       "\n"
168       "\tInvoking notmuch with no command argument will run new if\n"
169       "\tthe setup command has previously been completed, but new has\n"
170       "\tnot previously been run." },
171     { "search", notmuch_search_command,
172       "[options...] <search-terms> [...]",
173       "Search for messages matching the given search terms.",
174       "\tNote that the individual mail messages will be matched\n"
175       "\tagainst the search terms, but the results will be the\n"
176       "\tthreads (one per line) containing the matched messages.\n"
177       "\n"
178       "\tSupported options for search include:\n"
179       "\n"
180       "\t--format=(json|text)\n"
181       "\n"
182       "\t\tPresents the results in either JSON or\n"
183       "\t\tplain-text (default)\n"
184       "\n"
185       "\t--output=(summary|threads|messages|files|tags)\n"
186       "\n"
187       "\t\tsummary (default)\n"
188       "\n"
189       "\t\tOutput a summary of each thread with any message matching the\n"
190       "\t\tsearch terms. The summary includes the thread ID, date, the\n"
191       "\t\tnumber of messages in the thread (both the number matched and\n"
192       "\t\tthe total number), the authors of the thread and the subject.\n"
193       "\n"
194       "\t\tthreads\n"
195       "\n"
196       "\t\tOutput the thread IDs of all threads with any message matching\n"
197       "\t\tthe search terms, either one per line (--format=text) or as a\n"
198       "\t\tJSON array (--format=json).\n"
199       "\n"
200       "\t\tmessages\n"
201       "\n"
202       "\t\tOutput the message IDs of all messages matching the search\n"
203       "\t\tterms, either one per line (--format=text) or as a JSON array\n"
204       "\t\t(--format=json).\n"
205       "\n"
206       "\t\tfiles\n"
207       "\n"
208       "\t\tOutput the filenames of all messages matching the search\n"
209       "\t\tterms, either one per line (--format=text) or as a JSON array\n"
210       "\t\t(--format=json).\n"
211       "\n"
212       "\t\ttags\n"
213       "\n"
214       "\t\tOutput all tags that appear on any message matching the search\n"
215       "\t\tterms, either one per line (--format=text) or as a JSON array\n"
216       "\t\t(--format=json).\n"
217       "\n"
218       "\t--sort=(newest-first|oldest-first)\n"
219       "\n"
220       "\t\tPresent results in either chronological order\n"
221       "\t\t(oldest-first) or reverse chronological order\n"
222       "\t\t(newest-first), which is the default.\n"
223       "\n"
224       "\tSee \"notmuch help search-terms\" for details of the search\n"
225       "\tterms syntax." },
226     { "show", notmuch_show_command,
227       "<search-terms> [...]",
228       "Show all messages matching the search terms.",
229       "\tThe messages are grouped and sorted based on the threading\n"
230       "\t(all replies to a particular message appear immediately\n"
231       "\tafter that message in date order).\n"
232       "\n"
233       "\tSupported options for show include:\n"
234       "\n"
235       "\t--entire-thread\n"
236       "\n"
237       "\t\tBy default only those messages that match the\n"
238       "\t\tsearch terms will be displayed. With this option,\n"
239       "\t\tall messages in the same thread as any matched\n"
240       "\t\tmessage will be displayed.\n"
241       "\n"
242       "\t--format=(text|json|mbox|raw)\n"
243       "\n"
244       "\t\ttext (default)\n"
245       "\n"
246       "\t\tThe default plain-text format has all text-content MIME parts\n"
247       "\t\tdecoded. Various components in the output, ('message', 'header',\n"
248       "\t\t'body', 'attachment', and MIME 'part') are delimited by\n"
249       "\t\teasily-parsed markers. Each marker consists of a Control-L\n"
250       "\t\tcharacter (ASCII decimal 12), the name of the marker, and\n"
251       "\t\tthen either an opening or closing brace, '{' or '}' to\n"
252       "\t\teither open or close the component. For a multipart MIME\n"
253       "\t\tmessage, these parts will be nested.\n"
254       "\n"
255       "\t\tjson\n"
256       "\n"
257       "\t\tThe output is formatted with Javascript Object Notation\n"
258       "\t\t(JSON). This format is more robust than the text format\n"
259       "\t\tfor automated processing. The nested structure of multipart\n"
260       "\t\tMIME messages is reflected in nested JSON output. JSON\n"
261       "\t\toutput always includes all messages in a matching thread;\n"
262       "\t\tin effect '--format=json' implies '--entire-thread'\n"
263       "\n"
264       "\t\tmbox\n"
265       "\n"
266       "\t\tAll matching messages are output in the traditional, Unix\n"
267       "\t\tmbox format with each message being prefixed by a line\n"
268       "\t\tbeginning with 'From ' and a blank line separating each\n"
269       "\t\tmessage. Lines in the message content beginning with 'From '\n"
270       "\t\t(preceded by zero or more '>' characters) have an additional\n"
271       "\t\t'>' character added. This reversible escaping is termed\n"
272       "\t\t\"mboxrd\" format and described in detail here:\n"
273       "\n"
274       "\t\thttp://homepage.ntlworld.com/jonathan.deboynepollard/FGA/mail-mbox-formats.html\n"
275       "\n"
276       "\t\traw\n"
277       "\n"
278       "\t\tThe original, raw content of the email message is displayed.\n"
279       "\t\tConsumers of this format should expect to implement MIME\n"
280       "\t\tdecoding and similar functions. This format must only\n"
281       "\t\tbe used with search terms matching a single message.\n"
282       "\n"
283       "\t--part=N\n"
284       "\n"
285       "\t\tOutput the single decoded MIME part N of a single message.\n"
286       "\t\tThe search terms must match only a single message.\n"
287       "\t\tMessage parts are numbered in a depth-first walk of the\n"
288       "\t\tmessage MIME structure, and are identified in the 'json' or\n"
289       "\t\t'text' output formats.\n"
290       "\n"
291       "\n"
292       "\tA common use of \"notmuch show\" is to display a single\n"
293       "\tthread of email messages. For this, use a search term of\n"
294       "\t\"thread:<thread-id>\" as can be seen in the first column\n"
295       "\tof output from the \"notmuch search\" command.\n"
296       "\n"
297       "\tSee \"notmuch help search-terms\" for details of the search\n"
298       "\tterms syntax." },
299     { "count", notmuch_count_command,
300       "<search-terms> [...]",
301       "Count messages matching the search terms.",
302       "\tThe number of matching messages is output to stdout.\n"
303       "\n"
304       "\tWith no search terms, a count of all messages in the database\n"
305       "\twill be displayed.\n"
306       "\n"
307       "\tSee \"notmuch help search-terms\" for details of the search\n"
308       "\tterms syntax." },
309     { "reply", notmuch_reply_command,
310       "[options...] <search-terms> [...]",
311       "Construct a reply template for a set of messages.",
312       "\tConstructs a new message as a reply to a set of existing\n"
313       "\tmessages. The Reply-To: header (if any, otherwise From:) is\n"
314       "\tused for the To: address. The To: and Cc: headers are copied,\n"
315       "\tbut not including any of the user's configured addresses.\n"
316       "\n"
317       "\tA suitable subject is constructed. The In-Reply-to: and\n"
318       "\tReferences: headers are set appropriately, and the content\n"
319       "\tof the original messages is quoted and included in the body\n"
320       "\t(unless --format=headers-only is given).\n"
321       "\n"
322       "\tThe resulting message template is output to stdout.\n"
323       "\n"
324       "\tSupported options for reply include:\n"
325       "\n"
326       "\t--format=(default|headers-only)\n"
327       "\n"
328       "\t\tdefault:\n"
329       "\t\t\tIncludes subject and quoted message body.\n"
330       "\n"
331       "\t\theaders-only:\n"
332       "\t\t\tOnly produces In-Reply-To, References, To\n"
333       "\t\t\tCc, and Bcc headers.\n"
334       "\n"
335       "\tSee \"notmuch help search-terms\" for details of the search\n"
336       "\tterms syntax." },
337     { "tag", notmuch_tag_command,
338       "+<tag>|-<tag> [...] [--] <search-terms> [...]",
339       "Add/remove tags for all messages matching the search terms.",
340       "\tThe search terms are handled exactly as in 'search' so one\n"
341       "\tcan use that command first to see what will be modified.\n"
342       "\n"
343       "\tTags prefixed by '+' are added while those prefixed by\n"
344       "\t'-' are removed. For each message, tag removal is performed\n"
345       "\tbefore tag addition.\n"
346       "\n"
347       "\tThe beginning of <search-terms> is recognized by the first\n"
348       "\targument that begins with neither '+' nor '-'. Support for\n"
349       "\tan initial search term beginning with '+' or '-' is provided\n"
350       "\tby allowing the user to specify a \"--\" argument to separate\n"
351       "\tthe tags from the search terms.\n"
352       "\n"
353       "\tSee \"notmuch help search-terms\" for details of the search\n"
354       "\tterms syntax." },
355     { "dump", notmuch_dump_command,
356       "[<filename>]",
357       "Create a plain-text dump of the tags for each message.",
358       "\tOutput is to the given filename, if any, or to stdout.\n"
359       "\tThese tags are the only data in the notmuch database\n"
360       "\tthat can't be recreated from the messages themselves.\n"
361       "\tThe output of notmuch dump is therefore the only\n"
362       "\tcritical thing to backup (and much more friendly to\n"
363       "\tincremental backup than the native database files.)" },
364     { "restore", notmuch_restore_command,
365       "<filename>",
366       "Restore the tags from the given dump file (see 'dump').",
367       "\tNote: The dump file format is specifically chosen to be\n"
368       "\tcompatible with the format of files produced by sup-dump.\n"
369       "\tSo if you've previously been using sup for mail, then the\n"
370       "\t\"notmuch restore\" command provides you a way to import\n"
371       "\tall of your tags (or labels as sup calls them)." },
372     { "search-tags", notmuch_search_tags_command,
373       "[<search-terms> [...] ]",
374       "List all tags found in the database or matching messages.",
375       "\tRun this command without any search-term(s) to obtain a list\n"
376       "\tof all tags found in the database. If you provide one or more\n"
377       "\tsearch-terms as argument(s) then the resulting list will\n"
378       "\tcontain tags only from messages that match the search-term(s).\n"
379       "\n"
380       "\tIn both cases the list will be alphabetically sorted." },
381     { "config", notmuch_config_command,
382       "[get|set] <section>.<item> [value ...]",
383       "Get or set settings in the notmuch configuration file.",
384       "    config get <section>.<item>\n"
385       "\n"
386       "\tThe value of the specified configuration item is printed\n"
387       "\tto stdout. If the item has multiple values, each value\n"
388       "\tis separated by a newline character.\n"
389       "\n"
390       "\tAvailable configuration items include at least\n"
391       "\n"
392       "\t\tdatabase.path\n"
393       "\t\tuser.name\n"
394       "\t\tuser.primary_email\n"
395       "\t\tuser.other_email\n"
396       "\t\tnew.tags\n"
397       "\n"
398       "    config set <section>.<item> [value ...]\n"
399       "\n"
400       "\tThe specified configuration item is set to the given value.\n"
401       "\tTo specify a multiple-value item, provide each value as\n"
402       "\ta separate command-line argument.\n"
403       "\n"
404       "\tIf no values are provided, the specified configuration item\n"
405       "\twill be removed from the configuration file." },
406     { "help", notmuch_help_command,
407       "[<command>]",
408       "This message, or more detailed help for the named command.",
409       "\tExcept in this case, where there's not much more detailed\n"
410       "\thelp available." }
411 };
412
413 static void
414 usage (FILE *out)
415 {
416     command_t *command;
417     unsigned int i;
418
419     fprintf (out,
420              "Usage: notmuch --help\n"
421              "       notmuch --version\n"
422              "       notmuch <command> [args...]\n");
423     fprintf (out, "\n");
424     fprintf (out, "The available commands are as follows:\n");
425     fprintf (out, "\n");
426
427     for (i = 0; i < ARRAY_SIZE (commands); i++) {
428         command = &commands[i];
429
430         fprintf (out, "  %-11s  %s\n",
431                  command->name, command->summary);
432     }
433
434     fprintf (out, "\n");
435     fprintf (out,
436     "Use \"notmuch help <command>\" for more details on each command\n"
437     "and \"notmuch help search-terms\" for the common search-terms syntax.\n\n");
438 }
439
440 static int
441 notmuch_help_command (unused (void *ctx), int argc, char *argv[])
442 {
443     command_t *command;
444     unsigned int i;
445
446     if (argc == 0) {
447         printf ("The notmuch mail system.\n\n");
448         usage (stdout);
449         return 0;
450     }
451
452     for (i = 0; i < ARRAY_SIZE (commands); i++) {
453         command = &commands[i];
454
455         if (strcmp (argv[0], command->name) == 0) {
456             printf ("Help for \"notmuch %s\":\n\n", argv[0]);
457             if (command->arguments)
458                 printf ("%s %s\n\n\t%s\n\n%s\n\n",
459                         command->name, command->arguments,
460                         command->summary, command->documentation);
461             else
462                 printf ("%s\t%s\n\n%s\n\n", command->name,
463                         command->summary, command->documentation);
464             return 0;
465         }
466     }
467
468     if (strcmp (argv[0], "search-terms") == 0) {
469         printf ("Help for <%s>\n\n", argv[0]);
470         for (i = 0; i < ARRAY_SIZE (commands); i++) {
471             command = &commands[i];
472
473             if (command->arguments &&
474                 strstr (command->arguments, "search-terms"))
475             {
476                 printf ("\t%s\t%s\n",
477                         command->name, command->arguments);
478             }
479         }
480         printf ("\n");
481         printf (search_terms_help);
482         return 0;
483     }
484
485     fprintf (stderr,
486              "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
487              argv[0]);
488     return 1;
489 }
490
491 /* Handle the case of "notmuch" being invoked with no command
492  * argument. For now we just call notmuch_setup_command, but we plan
493  * to be more clever about this in the future.
494  */
495 static int
496 notmuch (void *ctx)
497 {
498     notmuch_config_t *config;
499     notmuch_bool_t is_new;
500     char *db_path;
501     struct stat st;
502
503     config = notmuch_config_open (ctx, NULL, &is_new);
504
505     /* If the user has never configured notmuch, then run
506      * notmuch_setup_command which will give a nice welcome message,
507      * and interactively guide the user through the configuration. */
508     if (is_new) {
509         notmuch_config_close (config);
510         return notmuch_setup_command (ctx, 0, NULL);
511     }
512
513     /* Notmuch is already configured, but is there a database? */
514     db_path = talloc_asprintf (ctx, "%s/%s",
515                                notmuch_config_get_database_path (config),
516                                ".notmuch");
517     if (stat (db_path, &st)) {
518         notmuch_config_close (config);
519         if (errno != ENOENT) {
520             fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
521                      db_path, strerror (errno));
522             return 1;
523         }
524         printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
525                 db_path);
526         printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
527                 "Note that the first run of \"notmuch new\" can take a very long time\n"
528                 "and that the resulting database will use roughly the same amount of\n"
529                 "storage space as the email being indexed.\n\n");
530         return 0;
531     }
532
533     printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
534             "At this point you can start exploring the functionality of notmuch by\n"
535             "using commands such as:\n\n"
536             "\tnotmuch search tag:inbox\n\n"
537             "\tnotmuch search to:\"%s\"\n\n"
538             "\tnotmuch search from:\"%s\"\n\n"
539             "\tnotmuch search subject:\"my favorite things\"\n\n"
540             "See \"notmuch help search\" for more details.\n\n"
541             "You can also use \"notmuch show\" with any of the thread IDs resulting\n"
542             "from a search. Finally, you may want to explore using a more sophisticated\n"
543             "interface to notmuch such as the emacs interface implemented in notmuch.el\n"
544             "or any other interface described at http://notmuchmail.org\n\n"
545             "And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
546             "Have fun, and may your inbox never have much mail.\n\n",
547             notmuch_config_get_user_name (config),
548             notmuch_config_get_user_primary_email (config));
549
550     notmuch_config_close (config);
551
552     return 0;
553 }
554
555 int
556 main (int argc, char *argv[])
557 {
558     void *local;
559     command_t *command;
560     alias_t *alias;
561     unsigned int i, j;
562     const char **argv_local;
563
564     talloc_enable_null_tracking ();
565
566     local = talloc_new (NULL);
567
568     g_mime_init (0);
569
570     if (argc == 1)
571         return notmuch (local);
572
573     if (STRNCMP_LITERAL (argv[1], "--help") == 0)
574         return notmuch_help_command (NULL, 0, NULL);
575
576     if (STRNCMP_LITERAL (argv[1], "--version") == 0) {
577         printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n");
578         return 0;
579     }
580
581     for (i = 0; i < ARRAY_SIZE (aliases); i++) {
582         alias = &aliases[i];
583
584         if (strcmp (argv[1], alias->name) == 0)
585         {
586             int substitutions;
587
588             argv_local = talloc_size (local, sizeof (char *) *
589                                       (argc + MAX_ALIAS_SUBSTITUTIONS - 1));
590             if (argv_local == NULL) {
591                 fprintf (stderr, "Out of memory.\n");
592                 return 1;
593             }
594
595             /* Copy all substution arguments from the alias. */
596             argv_local[0] = argv[0];
597             for (j = 0; j < MAX_ALIAS_SUBSTITUTIONS; j++) {
598                 if (alias->substitutions[j] == NULL)
599                     break;
600                 argv_local[j+1] = alias->substitutions[j];
601             }
602             substitutions = j;
603
604             /* And copy all original arguments (skipping the argument
605              * that matched the alias of course. */
606             for (j = 2; j < (unsigned) argc; j++) {
607                 argv_local[substitutions+j-1] = argv[j];
608             }
609
610             argc += substitutions - 1;
611             argv = (char **) argv_local;
612         }
613     }
614
615     for (i = 0; i < ARRAY_SIZE (commands); i++) {
616         command = &commands[i];
617
618         if (strcmp (argv[1], command->name) == 0)
619             return (command->function) (local, argc - 2, &argv[2]);
620     }
621
622     fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
623              argv[1]);
624
625     talloc_free (local);
626
627     return 1;
628 }