1 /* notmuch-deliver - If you make the user a promise... make sure you deliver it!
3 * Copyright © 2010 Ali Polatel
4 * Based in part upon deliverquota of maildrop which is:
5 * Copyright 1998 - 2009 Double Precision, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see http://www.gnu.org/licenses/ .
20 * Author: Ali Polatel <polatel@gmail.com>
37 #ifdef HAVE_SYSEXITS_H
44 #include "maildircreate.h"
45 #include "maildirmisc.h"
52 #define EX_SOFTWARE 70
64 #define EX_TEMPFAIL 75
75 static gboolean opt_create, opt_fatal, opt_folder, opt_version;
76 static gboolean opt_verbose = FALSE;
77 static gchar **opt_tags = NULL;
78 static gchar **opt_rtags = NULL;
80 static GOptionEntry options[] = {
81 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
82 "Display version", NULL},
83 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
84 "Be verbose (useful for debugging)", NULL},
85 {"create", 'c', 0, G_OPTION_ARG_NONE, &opt_create,
86 "Create the maildir if it doesn't exist", NULL},
87 {"folder", 'f', 0, G_OPTION_ARG_NONE, &opt_folder,
88 "Add a dot before FOLDER, e.g: Local => $MAILDIR/.Local", NULL},
89 {"tag", 't', 0, G_OPTION_ARG_STRING_ARRAY, &opt_tags,
90 "Add a tag to the message, may be specified multiple times", "TAG"},
91 {"remove-tag", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &opt_rtags,
92 "Remove a tag from the message, may be specified multiple times", "TAG"},
93 {"fatal-add", 0, 0, G_OPTION_ARG_NONE, &opt_fatal,
94 "If adding the mail to the database fails, unlink it and return non-zero", NULL},
95 {NULL, 0, 0, 0, NULL, NULL, NULL},
101 printf(PACKAGE"-"VERSION GITHEAD "\n");
105 log_handler(G_GNUC_UNUSED const gchar *domain, GLogLevelFlags level,
106 const gchar *message, G_GNUC_UNUSED gpointer user_data)
108 g_return_if_fail(message != NULL && message[0] != '\0');
110 if (!opt_verbose && (level & G_LOG_LEVEL_DEBUG))
113 g_printerr(PACKAGE": %s\n", message);
117 load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)
122 fd = g_key_file_new();
124 if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE, &error)) {
125 g_printerr("Failed to parse `%s': %s", path, error->message);
131 *db_path = g_key_file_get_string(fd, "database", "path", &error);
132 if (*db_path == NULL) {
133 g_critical("Failed to parse database.path from `%s': %s", path, error->message);
139 *tags = g_key_file_get_string_list(fd, "new", "tags", NULL, NULL);
147 save_splice(int fdin, int fdout)
149 int ret, written, pfd[2];
152 g_critical("Failed to create pipe: %s", g_strerror(errno));
157 ret = splice(fdin, NULL, pfd[1], NULL, 4096, 0);
161 g_critical("Splicing data from standard input failed: %s",
169 written = splice(pfd[0], NULL, fdout, NULL, ret, 0);
171 g_critical("Splicing data to temporary file failed: internal error");
177 g_critical("Splicing data to temporary file failed: %s",
191 #endif /* HAVE_SPLICE */
194 save_readwrite(int fdin, int fdout)
200 ret = read(fdin, buf, 4096);
206 g_critical("Reading from standard input failed: %s",
212 written = write(fdout, p, ret);
218 g_critical("Writing to temporary file failed: %s",
231 save_maildir(int fdin, const char *dir, int auto_create, char **path)
234 struct maildir_tmpcreate_info info;
236 maildir_tmpcreate_init(&info);
237 info.openmode = 0666;
241 while ((fdout = maildir_tmpcreate_fd(&info)) < 0)
243 if (errno == ENOENT && auto_create && maildir_mkdir(dir) == 0)
249 g_critical("Failed to create temporary file `%s': %s",
250 info.tmpname, g_strerror(errno));
254 g_debug("Reading from standard input and writing to `%s'", info.tmpname);
256 ret = g_getenv("NOTMUCH_DELIVER_NO_SPLICE")
257 ? save_readwrite(fdin, fdout)
258 : save_splice(fdin, fdout);
260 ret = save_readwrite(fdin, fdout);
261 #endif /* HAVE_SPLICE */
266 g_debug("Moving `%s' to `%s'", info.tmpname, info.newname);
267 if (maildir_movetmpnew(info.tmpname, info.newname)) {
268 g_critical("Moving `%s' to `%s' failed: %s",
269 info.tmpname, info.newname, g_strerror(errno));
270 unlink(info.tmpname);
275 *path = g_strdup(info.newname);
277 maildir_tmpcreate_free(&info);
282 g_debug("Unlinking `%s'", info.tmpname);
283 unlink(info.tmpname);
288 add_tags(notmuch_message_t *message, char **tags)
291 notmuch_status_t ret;
296 for (i = 0; tags[i]; i++) {
297 ret = notmuch_message_add_tag(message, tags[i]);
298 if (ret != NOTMUCH_STATUS_SUCCESS)
299 g_warning("Failed to add tag `%s': %s",
300 tags[i], notmuch_status_to_string(ret));
307 rm_tags(notmuch_message_t *message, char **tags)
310 notmuch_status_t ret;
315 for (i = 0; tags[i]; i++) {
316 ret = notmuch_message_remove_tag(message, tags[i]);
317 if (ret != NOTMUCH_STATUS_SUCCESS)
318 g_warning("Failed to remove tag `%s': %s",
319 tags[i], notmuch_status_to_string(ret));
326 save_database(notmuch_database_t *db, const char *path, char **default_tags)
328 notmuch_status_t ret;
329 notmuch_message_t *message;
331 g_debug("Adding `%s' to notmuch database", path);
332 ret = notmuch_database_add_message(db, path, &message);
334 case NOTMUCH_STATUS_SUCCESS:
336 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
337 g_debug("Message is a duplicate, not adding tags");
340 g_warning("Failed to add `%s' to notmuch database: %s",
341 path, notmuch_status_to_string(ret));
345 g_debug("Message isn't a duplicate, adding tags");
346 add_tags(message, default_tags);
347 add_tags(message, opt_tags);
348 rm_tags(message, opt_rtags);
354 main(int argc, char **argv)
357 gchar *conf_path, *db_path, *folder, *maildir, *mail;
360 GError *error = NULL;
361 notmuch_database_t *db;
362 notmuch_status_t status;
364 ctx = g_option_context_new("[FOLDER]");
365 g_option_context_add_main_entries(ctx, options, PACKAGE);
366 g_option_context_set_summary(ctx, PACKAGE"-"VERSION GITHEAD" - notmuch delivery tool");
367 g_option_context_set_description(ctx,
369 " "PACKAGE" uses notmuch's configuration file to determine database path and\n"
370 " initial tags to add to new messages. You may set NOTMUCH_CONFIG environment\n"
371 " variable to specify an alternative configuration file.\n"
373 " NOTMUCH_CONFIG: Path to notmuch configuration file\n"
374 " NOTMUCH_DELIVER_NO_SPLICE: Don't use splice() even if it's available\n"
376 " 0 => Successful run\n"
377 " 64 => Usage error\n"
378 " 70 => Failed to open the database\n"
379 " (or to add to the database if --fatal-add is specified)\n"
380 " 71 => Input output errors\n"
381 " (failed to read from standard input)\n"
382 " (failed to write to temporary file)\n"
383 " 76 => Failed to open/create maildir\n"
384 " 78 => Configuration error (wrt .notmuch-config)\n");
386 g_log_set_default_handler(log_handler, NULL);
388 if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
389 g_critical("Option parsing failed: %s", error->message);
390 g_option_context_free(ctx);
394 g_option_context_free(ctx);
401 if (g_getenv("NOTMUCH_CONFIG"))
402 conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
403 else if (g_getenv("HOME"))
404 conf_path = g_build_filename(g_getenv("HOME"), ".notmuch-config", NULL);
406 g_critical("Neither NOTMUCH_CONFIG nor HOME set");
412 g_debug("Parsing configuration from `%s'", conf_path);
413 if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
419 if ((argc - 1) > 1) {
420 g_critical("Won't deliver to %d folders", argc - 1);
425 folder = g_strdup_printf("%s%s", opt_folder ? "." : "", argv[1]);
426 maildir = g_build_filename(db_path, folder, NULL);
430 maildir = g_strdup(db_path);
432 g_debug("Opening notmuch database `%s'", db_path);
433 status = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
436 g_critical("Failed to open database `%s': %s",
437 db_path, notmuch_status_to_string(status));
444 if (notmuch_database_needs_upgrade(db)) {
445 g_message("Upgrading database");
446 notmuch_database_upgrade(db, NULL, NULL);
449 g_debug("Opening maildir `%s'", maildir);
450 if ((ret = save_maildir(STDIN_FILENO, maildir, opt_create, &mail)) != 0) {
456 if ((ret = save_database(db, mail, conf_tags)) != 0 && opt_fatal) {
457 g_warning("Unlinking `%s'", mail);
461 g_strfreev(conf_tags);
462 g_strfreev(opt_tags);
463 g_strfreev(opt_rtags);
466 notmuch_database_destroy(db);