aboutsummaryrefslogtreecommitdiff
path: root/contrib/notmuch-deliver/src/main.c
blob: 6f32f73d68b97decbe382b3dc84cffc3e99f259d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* notmuch-deliver - If you make the user a promise... make sure you deliver it!
 *
 * Copyright © 2010 Ali Polatel
 * Based in part upon deliverquota of maildrop which is:
 *   Copyright 1998 - 2009 Double Precision, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/ .
 *
 * Author: Ali Polatel <polatel@gmail.com>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SPLICE
#include <fcntl.h>
#endif

#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#endif

#include <glib.h>
#include <notmuch.h>

#include "maildircreate.h"
#include "maildirmisc.h"

#ifndef EX_USAGE
#define EX_USAGE 64
#endif

#ifndef EX_SOFTWARE
#define EX_SOFTWARE 70
#endif

#ifndef EX_OSERR
#define EX_OSERR 71
#endif

#ifndef EX_IOERR
#define EX_IOERR 74
#endif

#ifndef EX_TEMPFAIL
#define EX_TEMPFAIL 75
#endif

#ifndef EX_NOPERM
#define EX_NOPERM 77
#endif

#ifndef EX_CONFIG
#define EX_CONFIG 78
#endif

static gboolean opt_create, opt_fatal, opt_folder, opt_version;
static gboolean opt_verbose = FALSE;
static gchar **opt_tags = NULL;
static gchar **opt_rtags = NULL;

static GOptionEntry options[] = {
	{"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
		"Display version", NULL},
	{"verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
		"Be verbose (useful for debugging)", NULL},
	{"create", 'c', 0, G_OPTION_ARG_NONE, &opt_create,
		"Create the maildir if it doesn't exist", NULL},
	{"folder", 'f', 0, G_OPTION_ARG_NONE, &opt_folder,
		"Add a dot before FOLDER, e.g: Local => $MAILDIR/.Local", NULL},
	{"tag", 't', 0, G_OPTION_ARG_STRING_ARRAY, &opt_tags,
		"Add a tag to the message, may be specified multiple times", "TAG"},
	{"remove-tag", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &opt_rtags,
		"Remove a tag from the message, may be specified multiple times", "TAG"},
	{"fatal-add", 0, 0, G_OPTION_ARG_NONE, &opt_fatal,
		"If adding the mail to the database fails, unlink it and return non-zero", NULL},
	{NULL, 0, 0, 0, NULL, NULL, NULL},
};

static void
about(void)
{
	printf(PACKAGE"-"VERSION GITHEAD "\n");
}

static void
log_handler(G_GNUC_UNUSED const gchar *domain, GLogLevelFlags level,
	const gchar *message, G_GNUC_UNUSED gpointer user_data)
{
	g_return_if_fail(message != NULL && message[0] != '\0');

	if (!opt_verbose && (level & G_LOG_LEVEL_DEBUG))
		return;

	g_printerr(PACKAGE": %s\n", message);
}

static gboolean
load_keyfile(const gchar *path, gchar **db_path, gchar ***tags)
{
	GKeyFile *fd;
	GError *error;

	fd = g_key_file_new();
	error = NULL;
	if (!g_key_file_load_from_file(fd, path, G_KEY_FILE_NONE, &error)) {
		g_printerr("Failed to parse `%s': %s", path, error->message);
		g_error_free(error);
		g_key_file_free(fd);
		return FALSE;
	}

	*db_path = g_key_file_get_string(fd, "database", "path", &error);
	if (*db_path == NULL) {
		g_critical("Failed to parse database.path from `%s': %s", path, error->message);
		g_error_free(error);
		g_key_file_free(fd);
		return FALSE;
	}

	*tags = g_key_file_get_string_list(fd, "new", "tags", NULL, NULL);

	g_key_file_free(fd);
	return TRUE;
}

#ifdef HAVE_SPLICE
static int
save_splice(int fdin, int fdout)
{
	int ret, written, pfd[2];

	if (pipe(pfd) < 0) {
		g_critical("Failed to create pipe: %s", g_strerror(errno));
		return EX_IOERR;
	}

	for (;;) {
		ret = splice(fdin, NULL, pfd[1], NULL, 4096, 0);
		if (!ret)
			break;
		if (ret < 0) {
			g_critical("Splicing data from standard input failed: %s",
				g_strerror(errno));
			close(pfd[0]);
			close(pfd[1]);
			return EX_IOERR;
		}

		do {
			written = splice(pfd[0], NULL, fdout, NULL, ret, 0);
			if (!written) {
				g_critical("Splicing data to temporary file failed: internal error");
				close(pfd[0]);
				close(pfd[1]);
				return EX_IOERR;
			}
			if (written < 0) {
				g_critical("Splicing data to temporary file failed: %s",
					g_strerror(errno));
				close(pfd[0]);
				close(pfd[1]);
				return EX_IOERR;
			}
			ret -= written;
		} while (ret);
	}

	close(pfd[0]);
	close(pfd[1]);
	return 0;
}
#endif /* HAVE_SPLICE */

static int
save_readwrite(int fdin, int fdout)
{
	int ret, written;
	char buf[4096], *p;

	for (;;) {
		ret = read(fdin, buf, 4096);
		if (!ret)
			break;
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			g_critical("Reading from standard input failed: %s",
				g_strerror(errno));
			return EX_IOERR;
		}
		p = buf;
		do {
			written = write(fdout, p, ret);
			if (!written)
				return EX_IOERR;
			if (written < 0) {
				if (errno == EINTR)
					continue;
				g_critical("Writing to temporary file failed: %s",
					g_strerror(errno));
				return EX_IOERR;
			}
			p += written;
			ret -= written;
		} while (ret);
	}

	return 0;
}

static int
save_maildir(int fdin, const char *dir, int auto_create, char **path)
{
	int fdout, ret;
	struct maildir_tmpcreate_info info;

	maildir_tmpcreate_init(&info);
	info.openmode = 0666;
	info.maildir = dir;
	info.doordie = 1;

	while ((fdout = maildir_tmpcreate_fd(&info)) < 0)
	{
		if (errno == ENOENT && auto_create && maildir_mkdir(dir) == 0)
		{
			auto_create = 0;
			continue;
		}

		g_critical("Failed to create temporary file `%s': %s",
			info.tmpname, g_strerror(errno));
		return EX_TEMPFAIL;
	}

	g_debug("Reading from standard input and writing to `%s'", info.tmpname);
#ifdef HAVE_SPLICE
	ret = g_getenv("NOTMUCH_DELIVER_NO_SPLICE")
		? save_readwrite(fdin, fdout)
		: save_splice(fdin, fdout);
#else
	ret = save_readwrite(fdin, fdout);
#endif /* HAVE_SPLICE */
	if (ret)
		goto fail;

	close(fdout);
	g_debug("Moving `%s' to `%s'", info.tmpname, info.newname);
	if (maildir_movetmpnew(info.tmpname, info.newname)) {
		g_critical("Moving `%s' to `%s' failed: %s",
			info.tmpname, info.newname, g_strerror(errno));
		unlink(info.tmpname);
		return EX_IOERR;
	}

	if (path)
		*path = g_strdup(info.newname);

	maildir_tmpcreate_free(&info);

	return 0;

fail:
	g_debug("Unlinking `%s'", info.tmpname);
	unlink(info.tmpname);
	return EX_IOERR;
}

static int
add_tags(notmuch_message_t *message, char **tags)
{
	unsigned i;
	notmuch_status_t ret;

	if (!tags)
		return 0;

	for (i = 0; tags[i]; i++) {
		ret = notmuch_message_add_tag(message, tags[i]);
		if (ret != NOTMUCH_STATUS_SUCCESS)
			g_warning("Failed to add tag `%s': %s",
				tags[i], notmuch_status_to_string(ret));
	}

	return i;
}

static int
rm_tags(notmuch_message_t *message, char **tags)
{
	unsigned i;
	notmuch_status_t ret;

	if (!tags)
		return 0;

	for (i = 0; tags[i]; i++) {
		ret = notmuch_message_remove_tag(message, tags[i]);
		if (ret != NOTMUCH_STATUS_SUCCESS)
			g_warning("Failed to remove tag `%s': %s",
				tags[i], notmuch_status_to_string(ret));
	}

	return i;
}

static int
save_database(notmuch_database_t *db, const char *path, char **default_tags)
{
	notmuch_status_t ret;
	notmuch_message_t *message;

	g_debug("Adding `%s' to notmuch database", path);
	ret = notmuch_database_add_message(db, path, &message);
	switch (ret) {
	case NOTMUCH_STATUS_SUCCESS:
		break;
	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
		g_debug("Message is a duplicate, not adding tags");
		return 0;
	default:
		g_warning("Failed to add `%s' to notmuch database: %s",
			path, notmuch_status_to_string(ret));
		return EX_SOFTWARE;
	}

	g_debug("Message isn't a duplicate, adding tags");
	add_tags(message, default_tags);
	add_tags(message, opt_tags);
	rm_tags(message, opt_rtags);

	return 0;
}

int
main(int argc, char **argv)
{
	int ret;
	gchar *conf_path, *db_path, *folder, *maildir, *mail;
	gchar **conf_tags;
	GOptionContext *ctx;
	GError *error = NULL;
	notmuch_database_t *db;

	ctx = g_option_context_new("[FOLDER]");
	g_option_context_add_main_entries(ctx, options, PACKAGE);
	g_option_context_set_summary(ctx, PACKAGE"-"VERSION GITHEAD" - notmuch delivery tool");
	g_option_context_set_description(ctx,
		"\nConfiguration:\n"
		"  "PACKAGE" uses notmuch's configuration file to determine database path and\n"
		"  initial tags to add to new messages. You may set NOTMUCH_CONFIG environment\n"
		"  variable to specify an alternative configuration file.\n"
		"\nEnvironment:\n"
		"  NOTMUCH_CONFIG: Path to notmuch configuration file\n"
		"  NOTMUCH_DELIVER_NO_SPLICE: Don't use splice() even if it's available\n"
		"\nExit codes:\n"
		"  0   => Successful run\n"
		"  64  => Usage error\n"
		"  70  => Failed to open the database\n"
		"         (or to add to the database if --fatal-add is specified)\n"
		"  71  => Input output errors\n"
		"         (failed to read from standard input)\n"
		"         (failed to write to temporary file)\n"
		"  76  => Failed to open/create maildir\n"
		"  78  => Configuration error (wrt .notmuch-config)\n");

	g_log_set_default_handler(log_handler, NULL);

	if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
		g_critical("Option parsing failed: %s", error->message);
		g_option_context_free(ctx);
		g_error_free(error);
		return EX_USAGE;
	}
	g_option_context_free(ctx);

	if (opt_version) {
		about();
		return 0;
	}

	if (g_getenv("NOTMUCH_CONFIG"))
		conf_path = g_strdup(g_getenv("NOTMUCH_CONFIG"));
	else if (g_getenv("HOME"))
		conf_path = g_build_filename(g_getenv("HOME"), ".notmuch-config", NULL);
	else {
		g_critical("Neither NOTMUCH_CONFIG nor HOME set");
		return EX_USAGE;
	}

	db_path = NULL;
	conf_tags = NULL;
	g_debug("Parsing configuration from `%s'", conf_path);
	if (!load_keyfile(conf_path, &db_path, &conf_tags)) {
		g_free(conf_path);
		return EX_CONFIG;
	}
	g_free(conf_path);

	if ((argc - 1) > 1) {
		g_critical("Won't deliver to %d folders", argc - 1);
		return EX_USAGE;
	}

	if (argc > 1) {
		folder = g_strdup_printf("%s%s", opt_folder ? "." : "", argv[1]);
		maildir = g_build_filename(db_path, folder, NULL);
		g_free(folder);
	}
	else
		maildir = g_strdup(db_path);

	g_debug("Opening notmuch database `%s'", db_path);
	db = notmuch_database_open(db_path, NOTMUCH_DATABASE_MODE_READ_WRITE);
	g_free(db_path);
	if (db == NULL)
		return EX_SOFTWARE;
	if (notmuch_database_needs_upgrade(db)) {
		g_message("Upgrading database");
		notmuch_database_upgrade(db, NULL, NULL);
	}

	g_debug("Opening maildir `%s'", maildir);
	if ((ret = save_maildir(STDIN_FILENO, maildir, opt_create, &mail)) != 0) {
		g_free(maildir);
		return ret;
	}
	g_free(maildir);

	if ((ret = save_database(db, mail, conf_tags)) != 0 && opt_fatal) {
		g_warning("Unlinking `%s'", mail);
		unlink(mail);
		return ret;
	}
	g_strfreev(conf_tags);
	g_strfreev(opt_tags);
	g_strfreev(opt_rtags);
	g_free(mail);

	notmuch_database_close(db);

	return 0;
}