1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
24 #define _GNU_SOURCE /* for getline */
31 #include <sys/types.h>
40 #include <glib.h> /* g_strdup_printf */
42 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
44 typedef int (*command_function_t) (int argc, char *argv[]);
46 typedef struct command {
48 command_function_t function;
55 struct timeval tv_start;
59 chomp_newline (char *str)
61 if (str && str[strlen(str)-1] == '\n')
62 str[strlen(str)-1] = '\0';
65 /* Compute the number of seconds elapsed from start to end. */
67 tv_elapsed (struct timeval start, struct timeval end)
69 return ((end.tv_sec - start.tv_sec) +
70 (end.tv_usec - start.tv_usec) / 1e6);
74 print_formatted_seconds (double seconds)
80 hours = (int) seconds / 3600;
81 printf ("%dh ", hours);
82 seconds -= hours * 3600;
86 minutes = (int) seconds / 60;
87 printf ("%dm ", minutes);
88 seconds -= minutes * 60;
91 printf ("%02ds", (int) seconds);
95 add_files_print_progress (add_files_state_t *state)
97 struct timeval tv_now;
98 double elapsed_overall, rate_overall;
100 gettimeofday (&tv_now, NULL);
102 elapsed_overall = tv_elapsed (state->tv_start, tv_now);
103 rate_overall = (state->count) / elapsed_overall;
105 printf ("Added %d of %d messages (",
106 state->count, state->total_messages);
107 print_formatted_seconds ((state->total_messages - state->count) /
109 printf (" remaining). \r");
114 /* Recursively find all regular files in 'path' and add them to the
117 add_files (notmuch_database_t *notmuch, const char *path,
118 add_files_state_t *state)
121 struct dirent *entry, *e;
126 notmuch_status_t status;
128 dir = opendir (path);
131 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
132 path, strerror (errno));
136 entry_length = offsetof (struct dirent, d_name) +
137 pathconf (path, _PC_NAME_MAX) + 1;
138 entry = malloc (entry_length);
141 err = readdir_r (dir, entry, &e);
143 fprintf (stderr, "Error reading directory: %s\n",
152 /* Ignore special directories to avoid infinite recursion.
153 * Also ignore the .notmuch directory.
155 /* XXX: Eventually we'll want more sophistication to let the
156 * user specify files to be ignored. */
157 if (strcmp (entry->d_name, ".") == 0 ||
158 strcmp (entry->d_name, "..") == 0 ||
159 strcmp (entry->d_name, ".notmuch") ==0)
164 next = g_strdup_printf ("%s/%s", path, entry->d_name);
168 if (S_ISREG (st.st_mode)) {
169 status = notmuch_database_add_message (notmuch, next);
170 if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {
171 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
176 if (state->count % 1000 == 0)
177 add_files_print_progress (state);
178 } else if (S_ISDIR (st.st_mode)) {
179 add_files (notmuch, next, state);
190 /* Recursively count all regular files in path and all sub-direcotries
191 * of path. The result is added to *count (which should be
192 * initialized to zero by the top-level caller before calling
195 count_files (const char *path, int *count)
198 struct dirent *entry, *e;
204 dir = opendir (path);
207 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
208 path, strerror (errno));
212 entry_length = offsetof (struct dirent, d_name) +
213 pathconf (path, _PC_NAME_MAX) + 1;
214 entry = malloc (entry_length);
217 err = readdir_r (dir, entry, &e);
219 fprintf (stderr, "Error reading directory: %s\n",
228 /* Ignore special directories to avoid infinite recursion.
229 * Also ignore the .notmuch directory.
231 /* XXX: Eventually we'll want more sophistication to let the
232 * user specify files to be ignored. */
233 if (strcmp (entry->d_name, ".") == 0 ||
234 strcmp (entry->d_name, "..") == 0 ||
235 strcmp (entry->d_name, ".notmuch") == 0)
240 next = g_strdup_printf ("%s/%s", path, entry->d_name);
244 if (S_ISREG (st.st_mode)) {
246 if (*count % 1000 == 0) {
247 printf ("Found %d files so far.\r", *count);
250 } else if (S_ISDIR (st.st_mode)) {
251 count_files (next, count);
263 setup_command (int argc, char *argv[])
265 notmuch_database_t *notmuch;
266 char *mail_directory, *default_path;
269 add_files_state_t add_files_state;
271 struct timeval tv_now;
273 printf ("Welcome to notmuch!\n\n");
275 printf ("The goal of notmuch is to help you manage and search your collection of\n"
276 "email, and to efficiently keep up with the flow of email as it comes in.\n\n");
278 printf ("Notmuch needs to know the top-level directory of your email archive,\n"
279 "(where you already have mail stored and where messages will be delivered\n"
280 "in the future). This directory can contain any number of sub-directories\n"
281 "and primarily just files with indvidual email messages (eg. maildir or mh\n"
282 "archives are perfect). If there are other, non-email files (such as\n"
283 "indexes maintained by other email programs) then notmuch will do its\n"
284 "best to detect those and ignore them.\n\n");
286 printf ("Mail storage that uses mbox format, (where one mbox file contains many\n"
287 "messages), will not work with notmuch. If that's how your mail is currently\n"
288 "stored, we recommend you first convert it to maildir format with a utility\n"
289 "such as mb2md. In that case, press Control-C now and run notmuch again\n"
290 "once the conversion is complete.\n\n");
293 default_path = notmuch_database_default_path ();
294 printf ("Top-level mail directory [%s]: ", default_path);
297 mail_directory = NULL;
298 getline (&mail_directory, &line_size, stdin);
299 chomp_newline (mail_directory);
303 if (mail_directory == NULL || strlen (mail_directory) == 0) {
305 free (mail_directory);
306 mail_directory = default_path;
308 /* XXX: Instead of telling the user to use an environment
309 * variable here, we should really be writing out a configuration
310 * file and loading that on the next run. */
311 if (strcmp (mail_directory, default_path)) {
312 printf ("Note: Since you are not using the default path, you will want to set\n"
313 "the NOTMUCH_BASE environment variable to %s so that\n"
314 "future calls to notmuch commands will know where to find your mail.\n",
316 printf ("For example, if you are using bash for your shell, add:\n\n");
317 printf ("\texport NOTMUCH_BASE=%s\n\n", mail_directory);
318 printf ("to your ~/.bashrc file.\n\n");
323 notmuch = notmuch_database_create (mail_directory);
324 if (notmuch == NULL) {
325 fprintf (stderr, "Failed to create new notmuch database at %s\n",
327 free (mail_directory);
331 printf ("OK. Let's take a look at the mail we can find in the directory\n");
332 printf ("%s ...\n", mail_directory);
335 count_files (mail_directory, &count);
337 printf ("Found %d total files. That's not much mail.\n\n", count);
339 printf ("Next, we'll inspect the messages and create a database of threads:\n");
341 add_files_state.total_messages = count;
342 add_files_state.count = 0;
343 gettimeofday (&add_files_state.tv_start, NULL);
345 add_files (notmuch, mail_directory, &add_files_state);
347 gettimeofday (&tv_now, NULL);
348 elapsed = tv_elapsed (add_files_state.tv_start,
350 printf ("Added %d total messages in ", add_files_state.count);
351 print_formatted_seconds (elapsed);
352 printf (" (%d messages/sec.). \n", (int) (add_files_state.count / elapsed));
354 notmuch_database_close (notmuch);
356 free (mail_directory);
362 search_command (int argc, char *argv[])
364 fprintf (stderr, "Error: search is not implemented yet.\n");
369 show_command (int argc, char *argv[])
371 fprintf (stderr, "Error: show is not implemented yet.\n");
376 dump_command (int argc, char *argv[])
379 notmuch_database_t *notmuch = NULL;
380 notmuch_query_t *query;
381 notmuch_results_t *results;
382 notmuch_message_t *message;
383 notmuch_tags_t *tags;
387 output = fopen (argv[0], "w");
388 if (output == NULL) {
389 fprintf (stderr, "Error opening %s for writing: %s\n",
390 argv[0], strerror (errno));
398 notmuch = notmuch_database_open (NULL);
399 if (notmuch == NULL) {
404 query = notmuch_query_create (notmuch, "");
406 fprintf (stderr, "Out of memory\n");
411 notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
413 for (results = notmuch_query_search (query);
414 notmuch_results_has_more (results);
415 notmuch_results_advance (results))
418 message = notmuch_results_get (results);
421 "%s (", notmuch_message_get_message_id (message));
423 for (tags = notmuch_message_get_tags (message);
424 notmuch_tags_has_more (tags);
425 notmuch_tags_advance (tags))
428 fprintf (output, " ");
430 fprintf (output, "%s", notmuch_tags_get (tags));
435 fprintf (output, ")\n");
437 notmuch_message_destroy (message);
440 notmuch_query_destroy (query);
444 notmuch_database_close (notmuch);
445 if (output != stdout)
452 restore_command (int argc, char *argv[])
454 fprintf (stderr, "Error: restore is not implemented yet.\n");
458 command_t commands[] = {
459 { "setup", setup_command,
460 "Interactively setup notmuch for first use.\n"
461 "\t\tInvoking notmuch with no command argument will run setup if\n"
462 "\t\tthe setup command has not previously been completed." },
463 { "search", search_command,
464 "<search-term> [...]\n\n"
465 "\t\tSearch for threads matching the given search terms.\n"
466 "\t\tOnce we actually implement search we'll document the\n"
467 "\t\tsyntax here." },
468 { "show", show_command,
470 "\t\tShow the thread with the given thread ID (see 'search')." },
471 { "dump", dump_command,
473 "\t\tCreate a plain-text dump of the tags for each message\n"
474 "\t\twriting to the given filename, if any, or to stdout.\n"
475 "\t\tThese tags are the only data in the notmuch database\n"
476 "\t\tthat can't be recreated from the messages themselves.\n"
477 "\t\tThe output of notmuch dump is therefore the only\n"
478 "\t\tcritical thing to backup (and much more friendly to\n"
479 "\t\tincremental backup than the native database files." },
480 { "restore", restore_command,
482 "\t\tRestore the tags from the given dump file (see 'dump')." }
491 fprintf (stderr, "Usage: notmuch <command> [args...]\n");
492 fprintf (stderr, "\n");
493 fprintf (stderr, "Where <command> and [args...] are as follows:\n");
494 fprintf (stderr, "\n");
496 for (i = 0; i < ARRAY_SIZE (commands); i++) {
497 command = &commands[i];
499 fprintf (stderr, "\t%s\t%s\n\n", command->name, command->usage);
504 main (int argc, char *argv[])
510 return setup_command (0, NULL);
512 for (i = 0; i < ARRAY_SIZE (commands); i++) {
513 command = &commands[i];
515 if (strcmp (argv[1], command->name) == 0)
516 return (command->function) (argc - 2, &argv[2]);
519 /* Don't complain about "help" being an unknown command when we're
520 about to provide exactly what's wanted anyway. */
521 if (strcmp (argv[1], "help") == 0 ||
522 strcmp (argv[1], "--help") == 0)
524 fprintf (stderr, "The notmuch mail system.\n\n");
526 fprintf (stderr, "Error: Unknown command '%s'\n\n", argv[1]);