1 /* vim: set cino= fo=croql sw=8 ts=8 sts=0 noet cin fdm=syntax : */
4 * Copyright (c) 2010 Ali Polatel <alip@exherbo.org>
5 * Based in part upon deliverquota of maildrop which is:
6 * Copyright 1998 - 2009 Double Precision, Inc.
8 * This file is part of the notmuch-deliver. notmuch-deliver is free software;
9 * you can redistribute it and/or modify it under the terms of the GNU General
10 * Public License version 2, as published by the Free Software Foundation.
12 * notmuch-deliver is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
36 #ifdef HAVE_SYSEXITS_H
43 #include "maildircreate.h"
44 #include "maildirmisc.h"
51 #define EX_SOFTWARE 70
63 #define EX_TEMPFAIL 75
74 static gboolean opt_create, opt_fatal, opt_folder, opt_version;
75 static gboolean opt_verbose = FALSE;
76 static gchar **opt_tags = NULL;
77 static gchar **opt_rtags = NULL;
79 static GOptionEntry options[] = {
80 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
81 "Display version", NULL},
82 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
83 "Be verbose (useful for debugging)", NULL},
84 {"create", 'c', 0, G_OPTION_ARG_NONE, &opt_create,
85 "Create the maildir if it doesn't exist", NULL},
86 {"folder", 'f', 0, G_OPTION_ARG_NONE, &opt_folder,
87 "Add a dot before FOLDER, e.g: Local => $MAILDIR/.Local", NULL},
88 {"tag", 't', 0, G_OPTION_ARG_STRING_ARRAY, &opt_tags,
89 "Add a tag to the message, may be specified multiple times", "TAG"},
90 {"remove-tag", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &opt_rtags,
91 "Remove a tag from the message, may be specified multiple times", "TAG"},
92 {"fatal-add", 0, 0, G_OPTION_ARG_NONE, &opt_fatal,
93 "If adding the mail to the database fails, unlink it and return non-zero", NULL},
94 {NULL, 0, 0, 0, NULL, NULL, NULL},
100 printf(PACKAGE"-"VERSION GITHEAD "\n");
104 log_handler(G_GNUC_UNUSED const gchar *domain, GLogLevelFlags level,
105 const gchar *message, G_GNUC_UNUSED gpointer user_data)
107 g_return_if_fail(message != NULL && message[0] != '\0');
109 if (!opt_verbose && (level & G_LOG_LEVEL_DEBUG))
112 g_printerr(PACKAGE": %s\n", message);
116 load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)
121 fd = g_key_file_new();
123 if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE, &error)) {
124 g_printerr("Failed to parse `%s': %s", path, error->message);
130 *db_path = g_key_file_get_string(fd, "database", "path", &error);
131 if (*db_path == NULL) {
132 g_critical("Failed to parse database.path from `%s': %s", path, error->message);
138 *tags = g_key_file_get_string_list(fd, "new", "tags", NULL, NULL);
146 save_splice(int fdin, int fdout)
148 int ret, written, pfd[2];
151 g_critical("Failed to create pipe: %s", g_strerror(errno));
156 ret = splice(fdin, NULL, pfd[1], NULL, 4096, 0);
160 g_critical("Splicing data from standard input failed: %s",
168 written = splice(pfd[0], NULL, fdout, NULL, ret, 0);
170 g_critical("Splicing data to temporary file failed: %s",
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;
363 ctx = g_option_context_new("[FOLDER]");
364 g_option_context_add_main_entries(ctx, options, PACKAGE);
365 g_option_context_set_summary(ctx, PACKAGE"-"VERSION GITHEAD" - notmuch delivery tool");
366 g_option_context_set_description(ctx,
368 " "PACKAGE" uses notmuch's configuration file to determine database path and\n"
369 " initial tags to add to new messages. You may set NOTMUCH_CONFIG environment\n"
370 " variable to specify an alternative configuration file.\n"
372 " NOTMUCH_CONFIG: Path to notmuch configuration file\n"
373 " NOTMUCH_DELIVER_NO_SPLICE: Don't use splice() even if it's available\n"
375 " 0 => Successful run\n"
376 " 64 => Usage error\n"
377 " 70 => Failed to open the database\n"
378 " (or to add to the database if --fatal-add is specified)\n"
379 " 71 => Input output errors\n"
380 " (failed to read from standard input)\n"
381 " (failed to write to temporary file)\n"
382 " 76 => Failed to open/create maildir\n"
383 " 78 => Configuration error (wrt .notmuch-config)\n");
385 g_log_set_default_handler(log_handler, NULL);
387 if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
388 g_critical("Option parsing failed: %s", error->message);
389 g_option_context_free(ctx);
393 g_option_context_free(ctx);
400 if (g_getenv("NOTMUCH_CONFIG"))
401 conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
402 else if (g_getenv("HOME"))
403 conf_path = g_build_filename(g_getenv("HOME"), ".notmuch-config", NULL);
405 g_critical("Neither NOTMUCH_CONFIG nor HOME set");
411 g_debug("Parsing configuration from `%s'", conf_path);
412 if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
419 folder = g_strdup_printf("%s%s", opt_folder ? "." : "", argv[1]);
420 maildir = g_build_filename(db_path, folder, NULL);
424 maildir = g_strdup(db_path);
426 g_debug("Opening notmuch database `%s'", db_path);
427 db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);
431 if (notmuch_database_needs_upgrade(db)) {
432 g_message("Upgrading database");
433 notmuch_database_upgrade(db, NULL, NULL);
436 g_debug("Opening maildir `%s'", maildir);
437 if ((ret = save_maildir(STDIN_FILENO, maildir, opt_create, &mail)) != 0) {
443 if ((ret = save_database(db, mail, conf_tags)) != 0 && opt_fatal) {
444 g_warning("Unlinking `%s'", mail);
448 g_strfreev(conf_tags);
449 g_strfreev(opt_tags);
450 g_strfreev(opt_rtags);
453 notmuch_database_close(db);