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
31 #ifdef HAVE_SYSEXITS_H
38 #include "maildircreate.h"
39 #include "maildirmisc.h"
46 #define EX_SOFTWARE 70
58 #define EX_TEMPFAIL 75
69 static gboolean opt_create, opt_fatal, opt_folder, opt_version;
70 static gboolean opt_verbose = FALSE;
71 static gchar **opt_tags = NULL;
72 static gchar **opt_rtags = NULL;
74 static GOptionEntry options[] = {
75 {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
76 "Display version", NULL},
77 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
78 "Be verbose (useful for debugging)", NULL},
79 {"create", 'c', 0, G_OPTION_ARG_NONE, &opt_create,
80 "Create the maildir if it doesn't exist", NULL},
81 {"folder", 'f', 0, G_OPTION_ARG_NONE, &opt_folder,
82 "Add a dot before FOLDER, e.g: Local => $MAILDIR/.Local", NULL},
83 {"tag", 't', 0, G_OPTION_ARG_STRING_ARRAY, &opt_tags,
84 "Add a tag to the message, may be specified multiple times", "TAG"},
85 {"remove-tag", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &opt_rtags,
86 "Remove a tag from the message, may be specified multiple times", "TAG"},
87 {"fatal-add", 0, 0, G_OPTION_ARG_NONE, &opt_fatal,
88 "If adding the mail to the database fails, unlink it and return non-zero", NULL},
89 {NULL, 0, 0, 0, NULL, NULL, NULL},
95 printf(PACKAGE"-"VERSION GITHEAD "\n");
99 log_handler(G_GNUC_UNUSED const gchar *domain, GLogLevelFlags level,
100 const gchar *message, G_GNUC_UNUSED gpointer user_data)
102 g_return_if_fail(message != NULL && message[0] != '\0');
104 if (!opt_verbose && (level & G_LOG_LEVEL_DEBUG))
107 g_printerr(PACKAGE": %s\n", message);
111 load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)
116 fd = g_key_file_new();
118 if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE, &error)) {
119 g_printerr("Failed to parse `%s': %s", path, error->message);
125 *db_path = g_key_file_get_string(fd, "database", "path", &error);
126 if (*db_path == NULL) {
127 g_critical("Failed to parse database.path from `%s': %s", path, error->message);
133 *tags = g_key_file_get_string_list(fd, "new", "tags", NULL, NULL);
140 save_maildir(int fdin, const char *dir, int auto_create, char **path)
142 int fd, ret, written;
144 struct maildir_tmpcreate_info info;
146 maildir_tmpcreate_init(&info);
147 info.openmode = 0666;
151 while ((fd = maildir_tmpcreate_fd(&info)) < 0)
153 if (errno == ENOENT && auto_create && maildir_mkdir(dir) == 0)
159 g_critical("Failed to create temporary file `%s': %s",
160 info.tmpname, g_strerror(errno));
164 g_debug("Reading from standard input and writing to `%s'", info.tmpname);
166 ret = read(fdin, buf, 4096);
172 g_critical("Reading from standard input failed: %s", g_strerror(errno));
177 written = write(fd, p, ret);
183 g_critical("Writing to temporary file `%s' failed: %s",
184 info.tmpname, g_strerror(errno));
193 g_debug("Moving `%s' to `%s'", info.tmpname, info.newname);
194 if (maildir_movetmpnew(info.tmpname, info.newname)) {
195 g_critical("Moving `%s' to `%s' failed: %s",
196 info.tmpname, info.newname, g_strerror(errno));
197 unlink(info.tmpname);
202 *path = g_strdup(info.newname);
204 maildir_tmpcreate_free(&info);
209 g_debug("Unlinking `%s'", info.tmpname);
210 unlink(info.tmpname);
215 add_tags(notmuch_message_t *message, char **tags)
218 notmuch_status_t ret;
223 for (i = 0; tags[i]; i++) {
224 ret = notmuch_message_add_tag(message, tags[i]);
225 if (ret != NOTMUCH_STATUS_SUCCESS)
226 g_warning("Failed to add tag `%s': %s",
227 tags[i], notmuch_status_to_string(ret));
234 rm_tags(notmuch_message_t *message, char **tags)
237 notmuch_status_t ret;
242 for (i = 0; tags[i]; i++) {
243 ret = notmuch_message_remove_tag(message, tags[i]);
244 if (ret != NOTMUCH_STATUS_SUCCESS)
245 g_warning("Failed to remove tag `%s': %s",
246 tags[i], notmuch_status_to_string(ret));
253 save_database(notmuch_database_t *db, const char *path, char **default_tags)
255 notmuch_status_t ret;
256 notmuch_message_t *message;
258 g_debug("Adding `%s' to notmuch database", path);
259 ret = notmuch_database_add_message(db, path, &message);
261 case NOTMUCH_STATUS_SUCCESS:
263 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
264 g_debug("Message is a duplicate, not adding tags");
267 g_warning("Failed to add `%s' to notmuch database: %s",
268 path, notmuch_status_to_string(ret));
272 g_debug("Message isn't a duplicate, adding tags");
273 add_tags(message, default_tags);
274 add_tags(message, opt_tags);
275 rm_tags(message, opt_rtags);
281 main(int argc, char **argv)
284 gchar *conf_path, *db_path, *folder, *maildir, *mail;
287 GError *error = NULL;
288 notmuch_database_t *db;
290 ctx = g_option_context_new("[FOLDER]");
291 g_option_context_add_main_entries(ctx, options, PACKAGE);
292 g_option_context_set_summary(ctx, PACKAGE"-"VERSION GITHEAD" - notmuch delivery tool");
293 g_option_context_set_description(ctx,
295 " "PACKAGE" uses notmuch's configuration file to determine database path and\n"
296 " initial tags to add to new messages. You may set NOTMUCH_CONFIG environment\n"
297 " variable to specify an alternative configuration file.\n"
299 " 0 => Successful run\n"
300 " 64 => Usage error\n"
301 " 70 => Failed to open the database\n"
302 " (or to add to the database if --fatal-add is specified)\n"
303 " 71 => Input output errors\n"
304 " (failed to read from standard input)\n"
305 " (failed to write to temporary file)\n"
306 " 76 => Failed to open/create maildir\n"
307 " 78 => Configuration error (wrt .notmuch-config)\n");
309 g_log_set_default_handler(log_handler, NULL);
311 if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
312 g_critical("Option parsing failed: %s", error->message);
313 g_option_context_free(ctx);
317 g_option_context_free(ctx);
324 if (g_getenv("NOTMUCH_CONFIG"))
325 conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
326 else if (g_getenv("HOME"))
327 conf_path = g_build_filename(g_getenv("HOME"), ".notmuch-config", NULL);
329 g_critical("Neither NOTMUCH_CONFIG nor HOME set");
335 g_debug("Parsing configuration from `%s'", conf_path);
336 if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
343 folder = g_strdup_printf("%s%s", opt_folder ? "." : "", argv[1]);
344 maildir = g_build_filename(db_path, folder, NULL);
348 maildir = g_strdup(db_path);
350 g_debug("Opening notmuch database `%s'", db_path);
351 db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);
355 if (notmuch_database_needs_upgrade(db)) {
356 g_message("Upgrading database");
357 notmuch_database_upgrade(db, NULL, NULL);
360 g_debug("Opening maildir `%s'", maildir);
361 if ((ret = save_maildir(STDIN_FILENO, maildir, opt_create, &mail)) != 0) {
367 if ((ret = save_database(db, mail, conf_tags)) != 0 && opt_fatal) {
368 g_warning("Unlinking `%s'", mail);
372 g_strfreev(conf_tags);
373 g_strfreev(opt_tags);
374 g_strfreev(opt_rtags);
377 notmuch_database_close(db);