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: internal error");
176 g_critical("Splicing data to temporary file failed: %s",
190 #endif /* HAVE_SPLICE */
193 save_readwrite(int fdin, int fdout)
199 ret = read(fdin, buf, 4096);
205 g_critical("Reading from standard input failed: %s",
211 written = write(fdout, p, ret);
217 g_critical("Writing to temporary file failed: %s",
230 save_maildir(int fdin, const char *dir, int auto_create, char **path)
233 struct maildir_tmpcreate_info info;
235 maildir_tmpcreate_init(&info);
236 info.openmode = 0666;
240 while ((fdout = maildir_tmpcreate_fd(&info)) < 0)
242 if (errno == ENOENT && auto_create && maildir_mkdir(dir) == 0)
248 g_critical("Failed to create temporary file `%s': %s",
249 info.tmpname, g_strerror(errno));
253 g_debug("Reading from standard input and writing to `%s'", info.tmpname);
255 ret = g_getenv("NOTMUCH_DELIVER_NO_SPLICE")
256 ? save_readwrite(fdin, fdout)
257 : save_splice(fdin, fdout);
259 ret = save_readwrite(fdin, fdout);
260 #endif /* HAVE_SPLICE */
265 g_debug("Moving `%s' to `%s'", info.tmpname, info.newname);
266 if (maildir_movetmpnew(info.tmpname, info.newname)) {
267 g_critical("Moving `%s' to `%s' failed: %s",
268 info.tmpname, info.newname, g_strerror(errno));
269 unlink(info.tmpname);
274 *path = g_strdup(info.newname);
276 maildir_tmpcreate_free(&info);
281 g_debug("Unlinking `%s'", info.tmpname);
282 unlink(info.tmpname);
287 add_tags(notmuch_message_t *message, char **tags)
290 notmuch_status_t ret;
295 for (i = 0; tags[i]; i++) {
296 ret = notmuch_message_add_tag(message, tags[i]);
297 if (ret != NOTMUCH_STATUS_SUCCESS)
298 g_warning("Failed to add tag `%s': %s",
299 tags[i], notmuch_status_to_string(ret));
306 rm_tags(notmuch_message_t *message, char **tags)
309 notmuch_status_t ret;
314 for (i = 0; tags[i]; i++) {
315 ret = notmuch_message_remove_tag(message, tags[i]);
316 if (ret != NOTMUCH_STATUS_SUCCESS)
317 g_warning("Failed to remove tag `%s': %s",
318 tags[i], notmuch_status_to_string(ret));
325 save_database(notmuch_database_t *db, const char *path, char **default_tags)
327 notmuch_status_t ret;
328 notmuch_message_t *message;
330 g_debug("Adding `%s' to notmuch database", path);
331 ret = notmuch_database_add_message(db, path, &message);
333 case NOTMUCH_STATUS_SUCCESS:
335 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
336 g_debug("Message is a duplicate, not adding tags");
339 g_warning("Failed to add `%s' to notmuch database: %s",
340 path, notmuch_status_to_string(ret));
344 g_debug("Message isn't a duplicate, adding tags");
345 add_tags(message, default_tags);
346 add_tags(message, opt_tags);
347 rm_tags(message, opt_rtags);
353 main(int argc, char **argv)
356 gchar *conf_path, *db_path, *folder, *maildir, *mail;
359 GError *error = NULL;
360 notmuch_database_t *db;
362 ctx = g_option_context_new("[FOLDER]");
363 g_option_context_add_main_entries(ctx, options, PACKAGE);
364 g_option_context_set_summary(ctx, PACKAGE"-"VERSION GITHEAD" - notmuch delivery tool");
365 g_option_context_set_description(ctx,
367 " "PACKAGE" uses notmuch's configuration file to determine database path and\n"
368 " initial tags to add to new messages. You may set NOTMUCH_CONFIG environment\n"
369 " variable to specify an alternative configuration file.\n"
371 " NOTMUCH_CONFIG: Path to notmuch configuration file\n"
372 " NOTMUCH_DELIVER_NO_SPLICE: Don't use splice() even if it's available\n"
374 " 0 => Successful run\n"
375 " 64 => Usage error\n"
376 " 70 => Failed to open the database\n"
377 " (or to add to the database if --fatal-add is specified)\n"
378 " 71 => Input output errors\n"
379 " (failed to read from standard input)\n"
380 " (failed to write to temporary file)\n"
381 " 76 => Failed to open/create maildir\n"
382 " 78 => Configuration error (wrt .notmuch-config)\n");
384 g_log_set_default_handler(log_handler, NULL);
386 if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
387 g_critical("Option parsing failed: %s", error->message);
388 g_option_context_free(ctx);
392 g_option_context_free(ctx);
399 if (g_getenv("NOTMUCH_CONFIG"))
400 conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
401 else if (g_getenv("HOME"))
402 conf_path = g_build_filename(g_getenv("HOME"), ".notmuch-config", NULL);
404 g_critical("Neither NOTMUCH_CONFIG nor HOME set");
410 g_debug("Parsing configuration from `%s'", conf_path);
411 if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
417 if ((argc - 1) > 1) {
418 g_critical("Won't deliver to %d folders", argc - 1);
423 folder = g_strdup_printf("%s%s", opt_folder ? "." : "", argv[1]);
424 maildir = g_build_filename(db_path, folder, NULL);
428 maildir = g_strdup(db_path);
430 g_debug("Opening notmuch database `%s'", db_path);
431 db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);
435 if (notmuch_database_needs_upgrade(db)) {
436 g_message("Upgrading database");
437 notmuch_database_upgrade(db, NULL, NULL);
440 g_debug("Opening maildir `%s'", maildir);
441 if ((ret = save_maildir(STDIN_FILENO, maildir, opt_create, &mail)) != 0) {
447 if ((ret = save_database(db, mail, conf_tags)) != 0 && opt_fatal) {
448 g_warning("Unlinking `%s'", mail);
452 g_strfreev(conf_tags);
453 g_strfreev(opt_tags);
454 g_strfreev(opt_rtags);
457 notmuch_database_close(db);