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;
58 /* Compute the number of seconds elapsed from start to end. */
60 tv_elapsed (struct timeval start, struct timeval end)
62 return ((end.tv_sec - start.tv_sec) +
63 (end.tv_usec - start.tv_usec) / 1e6);
67 print_formatted_seconds (double seconds)
73 hours = (int) seconds / 3600;
74 printf ("%dh ", hours);
75 seconds -= hours * 3600;
79 minutes = (int) seconds / 60;
80 printf ("%dm ", minutes);
81 seconds -= minutes * 60;
84 printf ("%02ds", (int) seconds);
88 add_files_print_progress (add_files_state_t *state)
90 struct timeval tv_now;
91 double elapsed_overall, rate_overall;
93 gettimeofday (&tv_now, NULL);
95 elapsed_overall = tv_elapsed (state->tv_start, tv_now);
96 rate_overall = (state->count) / elapsed_overall;
98 printf ("Added %d of %d messages (",
99 state->count, state->total_messages);
100 print_formatted_seconds ((state->total_messages - state->count) /
102 printf (" remaining). \r");
107 /* Recursively find all regular files in 'path' and add them to the
110 add_files (notmuch_database_t *notmuch, const char *path,
111 add_files_state_t *state)
114 struct dirent *entry, *e;
119 notmuch_status_t status;
121 dir = opendir (path);
124 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
125 path, strerror (errno));
129 entry_length = offsetof (struct dirent, d_name) +
130 pathconf (path, _PC_NAME_MAX) + 1;
131 entry = malloc (entry_length);
134 err = readdir_r (dir, entry, &e);
136 fprintf (stderr, "Error reading directory: %s\n",
145 /* Ignore special directories to avoid infinite recursion.
146 * Also ignore the .notmuch directory.
148 /* XXX: Eventually we'll want more sophistication to let the
149 * user specify files to be ignored. */
150 if (strcmp (entry->d_name, ".") == 0 ||
151 strcmp (entry->d_name, "..") == 0 ||
152 strcmp (entry->d_name, ".notmuch") ==0)
157 next = g_strdup_printf ("%s/%s", path, entry->d_name);
161 if (S_ISREG (st.st_mode)) {
162 status = notmuch_database_add_message (notmuch, next);
163 if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) {
164 fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
169 if (state->count % 1000 == 0)
170 add_files_print_progress (state);
171 } else if (S_ISDIR (st.st_mode)) {
172 add_files (notmuch, next, state);
183 /* Recursively count all regular files in path and all sub-direcotries
184 * of path. The result is added to *count (which should be
185 * initialized to zero by the top-level caller before calling
188 count_files (const char *path, int *count)
191 struct dirent *entry, *e;
197 dir = opendir (path);
200 fprintf (stderr, "Warning: failed to open directory %s: %s\n",
201 path, strerror (errno));
205 entry_length = offsetof (struct dirent, d_name) +
206 pathconf (path, _PC_NAME_MAX) + 1;
207 entry = malloc (entry_length);
210 err = readdir_r (dir, entry, &e);
212 fprintf (stderr, "Error reading directory: %s\n",
221 /* Ignore special directories to avoid infinite recursion.
222 * Also ignore the .notmuch directory.
224 /* XXX: Eventually we'll want more sophistication to let the
225 * user specify files to be ignored. */
226 if (strcmp (entry->d_name, ".") == 0 ||
227 strcmp (entry->d_name, "..") == 0 ||
228 strcmp (entry->d_name, ".notmuch") == 0)
233 next = g_strdup_printf ("%s/%s", path, entry->d_name);
237 if (S_ISREG (st.st_mode)) {
239 if (*count % 1000 == 0) {
240 printf ("Found %d files so far.\r", *count);
243 } else if (S_ISDIR (st.st_mode)) {
244 count_files (next, count);
256 setup_command (int argc, char *argv[])
258 notmuch_database_t *notmuch;
259 char *mail_directory, *default_path;
262 add_files_state_t add_files_state;
264 struct timeval tv_now;
266 printf ("Welcome to notmuch!\n\n");
268 printf ("The goal of notmuch is to help you manage and search your collection of\n"
269 "email, and to efficiently keep up with the flow of email as it comes in.\n\n");
271 printf ("Notmuch needs to know the top-level directory of your email archive,\n"
272 "(where you already have mail stored and where messages will be delivered\n"
273 "in the future). This directory can contain any number of sub-directories\n"
274 "and primarily just files with indvidual email messages (eg. maildir or mh\n"
275 "archives are perfect). If there are other, non-email files (such as\n"
276 "indexes maintained by other email programs) then notmuch will do its\n"
277 "best to detect those and ignore them.\n\n");
279 printf ("Mail storage that uses mbox format, (where one mbox file contains many\n"
280 "messages), will not work with notmuch. If that's how your mail is currently\n"
281 "stored, we recommend you first convert it to maildir format with a utility\n"
282 "such as mb2md. In that case, press Control-C now and run notmuch again\n"
283 "once the conversion is complete.\n\n");
286 default_path = notmuch_database_default_path ();
287 printf ("Top-level mail directory [%s]: ", default_path);
290 mail_directory = NULL;
291 getline (&mail_directory, &line_size, stdin);
294 if (mail_directory &&
295 mail_directory[strlen(mail_directory)-1] == '\n')
297 mail_directory[strlen(mail_directory)-1] = '\0';
300 if (mail_directory == NULL || strlen (mail_directory) == 0) {
302 free (mail_directory);
303 mail_directory = default_path;
305 /* XXX: Instead of telling the user to use an environment
306 * variable here, we should really be writing out a configuration
307 * file and loading that on the next run. */
308 if (strcmp (mail_directory, default_path)) {
309 printf ("Note: Since you are not using the default path, you will want to set\n"
310 "the NOTMUCH_BASE environment variable to %s so that\n"
311 "future calls to notmuch commands will know where to find your mail.\n",
313 printf ("For example, if you are using bash for your shell, add:\n\n");
314 printf ("\texport NOTMUCH_BASE=%s\n\n", mail_directory);
315 printf ("to your ~/.bashrc file.\n\n");
320 notmuch = notmuch_database_create (mail_directory);
321 if (notmuch == NULL) {
322 fprintf (stderr, "Failed to create new notmuch database at %s\n",
324 free (mail_directory);
328 printf ("OK. Let's take a look at the mail we can find in the directory\n");
329 printf ("%s ...\n", mail_directory);
332 count_files (mail_directory, &count);
334 printf ("Found %d total files. That's not much mail.\n\n", count);
336 printf ("Next, we'll inspect the messages and create a database of threads:\n");
338 add_files_state.total_messages = count;
339 add_files_state.count = 0;
340 gettimeofday (&add_files_state.tv_start, NULL);
342 add_files (notmuch, mail_directory, &add_files_state);
344 gettimeofday (&tv_now, NULL);
345 elapsed = tv_elapsed (add_files_state.tv_start,
347 printf ("Added %d total messages in ", add_files_state.count);
348 print_formatted_seconds (elapsed);
349 printf (" (%d messages/sec.). \n", (int) (add_files_state.count / elapsed));
351 notmuch_database_close (notmuch);
353 free (mail_directory);
359 search_command (int argc, char *argv[])
361 fprintf (stderr, "Error: search is not implemented yet.\n");
366 show_command (int argc, char *argv[])
368 fprintf (stderr, "Error: show is not implemented yet.\n");
373 dump_command (int argc, char *argv[])
376 notmuch_database_t *notmuch = NULL;
377 notmuch_query_t *query;
378 notmuch_results_t *results;
379 notmuch_message_t *message;
380 notmuch_tags_t *tags;
384 output = fopen (argv[0], "w");
385 if (output == NULL) {
386 fprintf (stderr, "Error opening %s for writing: %s\n",
387 argv[0], strerror (errno));
395 notmuch = notmuch_database_open (NULL);
396 if (notmuch == NULL) {
401 query = notmuch_query_create (notmuch, "");
403 fprintf (stderr, "Out of memory\n");
408 notmuch_query_set_sort (query, NOTMUCH_SORT_MESSAGE_ID);
410 for (results = notmuch_query_search (query);
411 notmuch_results_has_more (results);
412 notmuch_results_advance (results))
415 message = notmuch_results_get (results);
418 "%s (", notmuch_message_get_message_id (message));
420 for (tags = notmuch_message_get_tags (message);
421 notmuch_tags_has_more (tags);
422 notmuch_tags_advance (tags))
425 fprintf (output, " ");
427 fprintf (output, "%s", notmuch_tags_get (tags));
432 fprintf (output, ")\n");
434 notmuch_message_destroy (message);
437 notmuch_query_destroy (query);
441 notmuch_database_close (notmuch);
442 if (output != stdout)
449 restore_command (int argc, char *argv[])
451 fprintf (stderr, "Error: restore is not implemented yet.\n");
455 command_t commands[] = {
456 { "setup", setup_command,
457 "Interactively setup notmuch for first use.\n"
458 "\t\tInvoking notmuch with no command argument will run setup if\n"
459 "\t\tthe setup command has not previously been completed." },
460 { "search", search_command,
461 "<search-term> [...]\n\n"
462 "\t\tSearch for threads matching the given search terms.\n"
463 "\t\tOnce we actually implement search we'll document the\n"
464 "\t\tsyntax here." },
465 { "show", show_command,
467 "\t\tShow the thread with the given thread ID (see 'search')." },
468 { "dump", dump_command,
470 "\t\tCreate a plain-text dump of the tags for each message\n"
471 "\t\twriting to the given filename, if any, or to stdout.\n"
472 "\t\tThese tags are the only data in the notmuch database\n"
473 "\t\tthat can't be recreated from the messages themselves.\n"
474 "\t\tThe output of notmuch dump is therefore the only\n"
475 "\t\tcritical thing to backup (and much more friendly to\n"
476 "\t\tincremental backup than the native database files." },
477 { "restore", restore_command,
479 "\t\tRestore the tags from the given dump file (see 'dump')." }
488 fprintf (stderr, "Usage: notmuch <command> [args...]\n");
489 fprintf (stderr, "\n");
490 fprintf (stderr, "Where <command> and [args...] are as follows:\n");
491 fprintf (stderr, "\n");
493 for (i = 0; i < ARRAY_SIZE (commands); i++) {
494 command = &commands[i];
496 fprintf (stderr, "\t%s\t%s\n\n", command->name, command->usage);
501 main (int argc, char *argv[])
507 return setup_command (0, NULL);
509 for (i = 0; i < ARRAY_SIZE (commands); i++) {
510 command = &commands[i];
512 if (strcmp (argv[1], command->name) == 0)
513 return (command->function) (argc - 2, &argv[2]);
516 /* Don't complain about "help" being an unknown command when we're
517 about to provide exactly what's wanted anyway. */
518 if (strcmp (argv[1], "help") == 0 ||
519 strcmp (argv[1], "--help") == 0)
521 fprintf (stderr, "The notmuch mail system.\n\n");
523 fprintf (stderr, "Error: Unknown command '%s'\n\n", argv[1]);