aboutsummaryrefslogtreecommitdiff
path: root/contrib/notmuch-deliver/src
diff options
context:
space:
mode:
authorAli Polatel <alip@exherbo.org>2010-05-26 10:09:47 +0300
committerAli Polatel <alip@exherbo.org>2011-11-05 01:12:34 +0200
commitaf863f8c7f1f1571df4f1172ec4e963fa654945e (patch)
tree518717c794680a0e136b112f4cc8121f689dca69 /contrib/notmuch-deliver/src
parent6a280088e6769015ade7758b9790384997a21ff3 (diff)
notmuch-deliver: Initial import
Diffstat (limited to 'contrib/notmuch-deliver/src')
-rw-r--r--contrib/notmuch-deliver/src/Makefile.am18
-rw-r--r--contrib/notmuch-deliver/src/maildircreate.c241
-rw-r--r--contrib/notmuch-deliver/src/maildircreate.h52
-rw-r--r--contrib/notmuch-deliver/src/maildirmisc.h202
-rw-r--r--contrib/notmuch-deliver/src/maildirmkdir.c78
-rw-r--r--contrib/notmuch-deliver/src/maildiropen.c141
-rw-r--r--contrib/notmuch-deliver/src/main.c378
7 files changed, 1110 insertions, 0 deletions
diff --git a/contrib/notmuch-deliver/src/Makefile.am b/contrib/notmuch-deliver/src/Makefile.am
new file mode 100644
index 0000000..939dc85
--- /dev/null
+++ b/contrib/notmuch-deliver/src/Makefile.am
@@ -0,0 +1,18 @@
+DEFS+= -DGITHEAD=\"$(GITHEAD)\"
+AM_CFLAGS= @NOTMUCH_DELIVER_CFLAGS@ $(glib_CFLAGS)
+
+noinst_HEADERS=\
+ maildircreate.h \
+ maildirmisc.h
+
+bin_PROGRAMS=\
+ notmuch-deliver
+
+notmuch_deliver_SOURCES=\
+ maildircreate.c \
+ maildiropen.c \
+ maildirmkdir.c \
+ main.c
+notmuch_deliver_LDADD=\
+ $(top_builddir)/numlib/libnumlib.la \
+ $(glib_LIBS)
diff --git a/contrib/notmuch-deliver/src/maildircreate.c b/contrib/notmuch-deliver/src/maildircreate.c
new file mode 100644
index 0000000..74030f4
--- /dev/null
+++ b/contrib/notmuch-deliver/src/maildircreate.c
@@ -0,0 +1,241 @@
+/*
+** Copyright 1998 - 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#include "maildircreate.h"
+#include "maildirmisc.h"
+#include <sys/types.h>
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+#endif
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include "numlib/numlib.h"
+
+
+static const char rcsid[]="$Id: maildircreate.c,v 1.6 2003/01/26 04:07:03 mrsam Exp $";
+
+FILE *maildir_tmpcreate_fp(struct maildir_tmpcreate_info *info)
+{
+ int fd=maildir_tmpcreate_fd(info);
+ FILE *fp;
+
+ if (fd < 0)
+ return NULL;
+
+ fp=fdopen(fd, "w+");
+
+ if (fp == NULL)
+ {
+ close(fd);
+ return NULL;
+ }
+
+ return fp;
+}
+
+static int maildir_tmpcreate_fd_do(struct maildir_tmpcreate_info *info);
+
+#define KEEPTRYING (60 * 60)
+#define SLEEPFOR 3
+
+int maildir_tmpcreate_fd(struct maildir_tmpcreate_info *info)
+{
+ int i;
+
+ if (!info->doordie)
+ return (maildir_tmpcreate_fd_do(info));
+
+ for (i=0; i<KEEPTRYING / SLEEPFOR; i++)
+ {
+ int fd=maildir_tmpcreate_fd_do(info);
+
+ if (fd >= 0 || errno != EAGAIN)
+ return fd;
+
+ sleep(SLEEPFOR);
+ }
+
+ return -1;
+}
+
+static int maildir_tmpcreate_fd_do(struct maildir_tmpcreate_info *info)
+{
+ const char *maildir=info->maildir;
+ const char *uniq=info->uniq;
+ const char *hostname=info->hostname;
+
+ char hostname_buf[256];
+ char time_buf[NUMBUFSIZE];
+ char usec_buf[NUMBUFSIZE];
+ char pid_buf[NUMBUFSIZE];
+ char len_buf[NUMBUFSIZE+3];
+ char dev_buf[NUMBUFSIZE];
+ char ino_buf[NUMBUFSIZE];
+ struct timeval tv;
+
+ struct stat stat_buf;
+ int fd;
+
+ if (!maildir)
+ maildir=".";
+ if (!uniq)
+ uniq="";
+
+ if (!hostname || !*hostname)
+ {
+ hostname_buf[sizeof(hostname_buf)-1]=0;
+ if (gethostname(hostname_buf, sizeof(hostname_buf)-1) < 0)
+ strcpy(hostname_buf, "localhost");
+ hostname=hostname_buf;
+ }
+
+ gettimeofday(&tv, NULL);
+
+ libmail_str_time_t(tv.tv_sec, time_buf);
+ libmail_str_time_t(tv.tv_usec, usec_buf);
+ libmail_str_pid_t(getpid(), pid_buf);
+ len_buf[0]=0;
+ if (info->msgsize > 0)
+ {
+ strcpy(len_buf, ",S=");
+ libmail_str_size_t(info->msgsize, len_buf+3);
+ }
+
+ if (info->tmpname)
+ free(info->tmpname);
+
+ info->tmpname=malloc(strlen(maildir)+strlen(uniq)+
+ strlen(hostname)+strlen(time_buf)+
+ strlen(usec_buf)+
+ strlen(pid_buf)+strlen(len_buf)+100);
+
+ if (!info->tmpname)
+ {
+ maildir_tmpcreate_free(info);
+ return -1;
+ }
+
+ strcpy(info->tmpname, maildir);
+ strcat(info->tmpname, "/tmp/");
+ strcat(info->tmpname, time_buf);
+ strcat(info->tmpname, ".M");
+ strcat(info->tmpname, usec_buf);
+ strcat(info->tmpname, "P");
+ strcat(info->tmpname, pid_buf);
+
+ if (*uniq)
+ strcat(strcat(info->tmpname, "_"), uniq);
+ strcat(info->tmpname, ".");
+ strcat(info->tmpname, hostname);
+ strcat(info->tmpname, len_buf);
+
+ if (stat( info->tmpname, &stat_buf) == 0)
+ {
+ maildir_tmpcreate_free(info);
+ errno=EAGAIN;
+ return -1;
+ }
+
+ if (errno != ENOENT)
+ {
+ maildir_tmpcreate_free(info);
+ if (errno == EAGAIN)
+ errno=EIO;
+ return -1;
+ }
+
+ if ((fd=maildir_safeopen_stat(info->tmpname, O_CREAT|O_RDWR|O_TRUNC,
+ info->openmode, &stat_buf)) < 0)
+ {
+ maildir_tmpcreate_free(info);
+ return -1;
+ }
+
+ libmail_strh_dev_t(stat_buf.st_dev, dev_buf);
+ libmail_strh_ino_t(stat_buf.st_ino, ino_buf);
+
+ if (info->newname)
+ free(info->newname);
+
+ info->newname=malloc(strlen(info->tmpname)+strlen(ino_buf)+
+ strlen(dev_buf)+3);
+
+ if (!info->newname)
+ {
+ maildir_tmpcreate_free(info);
+ unlink(info->tmpname);
+ close(fd);
+ if (errno == EAGAIN)
+ errno=EIO;
+ return -1;
+ }
+
+ strcpy(info->newname, maildir);
+ strcat(info->newname, "/new/");
+ strcat(info->newname, time_buf);
+ strcat(info->newname, ".M");
+ strcat(info->newname, usec_buf);
+ strcat(info->newname, "P");
+ strcat(info->newname, pid_buf);
+ strcat(info->newname, "V");
+ strcat(info->newname, dev_buf);
+ strcat(info->newname, "I");
+ strcat(info->newname, ino_buf);
+ if (*uniq)
+ strcat(strcat(info->newname, "_"), uniq);
+ strcat(info->newname, ".");
+ strcat(info->newname, hostname);
+ strcat(info->newname, len_buf);
+
+ return fd;
+}
+
+void maildir_tmpcreate_free(struct maildir_tmpcreate_info *info)
+{
+ if (info->tmpname)
+ free(info->tmpname);
+ info->tmpname=NULL;
+
+ if (info->newname)
+ free(info->newname);
+ info->newname=NULL;
+}
+
+int maildir_movetmpnew(const char *tmpname, const char *newname)
+{
+ if (link(tmpname, newname) == 0)
+ {
+ unlink(tmpname);
+ return 0;
+ }
+
+ if (errno != EXDEV)
+ return -1;
+
+ /* AFS? */
+
+ return rename(tmpname, newname);
+}
diff --git a/contrib/notmuch-deliver/src/maildircreate.h b/contrib/notmuch-deliver/src/maildircreate.h
new file mode 100644
index 0000000..ea1c71a
--- /dev/null
+++ b/contrib/notmuch-deliver/src/maildircreate.h
@@ -0,0 +1,52 @@
+#ifndef maildircreate_h
+#define maildircreate_h
+
+/*
+** Copyright 1998 - 2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static const char maildircreate_h_rcsid[]="$Id: maildircreate.h,v 1.10 2006/10/29 00:03:53 mrsam Exp $";
+
+ /* Create messages in maildirs */
+
+struct maildir_tmpcreate_info {
+ const char *maildir;
+ unsigned long msgsize; /* If known, 0 otherwise (must use requota later)*/
+ const char *uniq; /* You need when creating multiple msgs */
+ const char *hostname; /* If known, NULL otherwise */
+ int openmode; /* Default open mode */
+ int doordie; /* Loop until we get it right. */
+ char *tmpname; /* On exit, filename in tmp */
+ char *newname; /* On exit, filename in new */
+};
+
+#define maildir_tmpcreate_init(i) \
+ do \
+ { \
+ memset( (i), 0, sizeof(*(i))); \
+ (i)->openmode=0644; \
+ } while(0)
+
+int maildir_tmpcreate_fd(struct maildir_tmpcreate_info *);
+FILE *maildir_tmpcreate_fp(struct maildir_tmpcreate_info *);
+void maildir_tmpcreate_free(struct maildir_tmpcreate_info *);
+
+ /* Move created message from tmp to new */
+int maildir_movetmpnew(const char *tmpname, const char *newname);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/contrib/notmuch-deliver/src/maildirmisc.h b/contrib/notmuch-deliver/src/maildirmisc.h
new file mode 100644
index 0000000..545d11e
--- /dev/null
+++ b/contrib/notmuch-deliver/src/maildirmisc.h
@@ -0,0 +1,202 @@
+#ifndef maildirmisc_h
+#define maildirmisc_h
+
+/*
+** Copyright 2000-2003 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static const char maildirmisc_h_rcsid[]="$Id: maildirmisc.h,v 1.18 2006/07/22 02:48:15 mrsam Exp $";
+
+/*
+**
+** Miscellaneous maildir-related code
+**
+*/
+
+/* Some special folders */
+
+#define INBOX "INBOX"
+#define DRAFTS "Drafts"
+#define SENT "Sent"
+#define TRASH "Trash"
+#define SHARED "shared"
+
+#define SHAREDSUBDIR "shared-folders"
+
+#define NEWSHAREDSP "#shared"
+#define NEWSHARED "#shared."
+
+#define PUBLIC "public" /* SMAP */
+
+int maildir_make(const char *maildir, int perm, int subdirperm,
+ int folder);
+
+int maildir_del(const char *maildir);
+
+int maildir_del_content(const char *maildir);
+
+char *maildir_name2dir(const char *maildir, /* DIR location */
+ const char *foldername); /* INBOX.name */
+
+char *maildir_location(const char *homedir,
+ const char *maildir);
+/*
+** Homedir is the account's home directory, "maildir" is where the account's
+** default Maildir is configured to be (usually "./Maildir"). Combine the
+** two to produce an absolute pathname.
+*/
+
+
+char *maildir_folderdir(const char *, /* maildir */
+ const char *); /* folder name */
+ /* Returns the directory corresponding to foldername (foldername is
+ ** checked to make sure that it's a valid name, else we set errno
+ ** to EINVAL, and return (0).
+ */
+
+char *maildir_filename(const char *, /* maildir */
+ const char *, /* folder */
+ const char *); /* filename */
+ /*
+ ** Builds the filename to this message, suitable for opening.
+ ** If the file doesn't appear to be there, search the maildir to
+ ** see if someone changed the flags, and return the current filename.
+ */
+
+int maildir_safeopen(const char *, /* filename */
+ int, /* mode */
+ int); /* perm */
+
+/*
+** Same arguments as open(). When we're accessing a shared maildir,
+** prevent someone from playing cute and dumping a bunch of symlinks
+** in there. This function will open the indicate file only if the
+** last component is not a symlink.
+** This is implemented by opening the file with O_NONBLOCK (to prevent
+** a DOS attack of someone pointing the symlink to a pipe, causing
+** the open to hang), clearing O_NONBLOCK, then stat-int the file
+** descriptor, lstating the filename, and making sure that dev/ino
+** match.
+*/
+
+int maildir_semisafeopen(const char *, /* filename */
+ int, /* mode */
+ int); /* perm */
+
+/*
+** Same thing, except that we allow ONE level of soft link indirection,
+** because we're reading from our own maildir, which points to the
+** message in the sharable maildir.
+*/
+
+int maildir_safeopen_stat(const char *path, int mode, int perm,
+ struct stat *stat1);
+ /* Sane as maildir_safeopen(), except that we also initialize a
+ ** struct stat, saving an extra syscall to the caller.
+ */
+
+int maildir_mkdir(const char *); /* directory */
+/*
+** Create maildir including all subdirectories in the path (like mkdir -p)
+*/
+
+void maildir_purgetmp(const char *); /* maildir */
+ /* purges old stuff out of tmp */
+
+void maildir_purge(const char *, /* directory */
+ unsigned); /* time_t to purge */
+
+void maildir_getnew(const char *, /* maildir */
+ const char *, /* folder */
+ void (*)(const char *, void *), /* Callback function for
+ ** every moved msg.
+ */
+ void *arg); /* Passthrough callback arg */
+
+ /* move messages from new to cur */
+
+int maildir_deletefolder(const char *, /* maildir */
+ const char *); /* folder */
+ /* deletes a folder */
+
+void maildir_list(const char *maildir,
+ void (*func)(const char *, void *),
+ void *voidp);
+
+void maildir_list_sharable(const char *, /* maildir */
+ void (*)(const char *, void *), /* callback function */
+ void *); /* 2nd arg to callback func */
+ /* list sharable folders */
+
+int maildir_shared_subscribe(const char *, /* maildir */
+ const char *); /* folder */
+ /* subscribe to a shared folder */
+
+void maildir_list_shared(const char *, /* maildir */
+ void (*)(const char *, void *), /* callback function */
+ void *); /* 2nd arg to the callback func */
+ /* list subscribed folders */
+
+int maildir_shared_unsubscribe(const char *, /* maildir */
+ const char *); /* folder */
+ /* unsubscribe from a shared folder */
+
+char *maildir_shareddir(const char *, /* maildir */
+ const char *); /* folder */
+ /*
+ ** Validate and return a path to a shared folder. folderdir must be
+ ** a name of a valid shared folder.
+ */
+
+void maildir_shared_sync(const char *); /* maildir */
+ /* "sync" the shared folder */
+
+int maildir_sharedisro(const char *); /* maildir */
+ /* maildir is a shared read-only folder */
+
+int maildir_unlinksharedmsg(const char *); /* filename */
+ /* Remove a message from a shared folder */
+
+/* Internal function that reads a symlink */
+
+char *maildir_getlink(const char *);
+
+ /* Determine whether the maildir filename has a certain flag */
+
+int maildir_hasflag(const char *filename, char);
+
+#define MAILDIR_DELETED(f) maildir_hasflag((f), 'T')
+
+ /*
+ ** Hierarchical maildir rename.
+ */
+
+#define MAILDIR_RENAME_FOLDER 1
+#define MAILDIR_RENAME_SUBFOLDERS 2
+
+int maildir_rename(const char *maildir, /* Path to the maildir */
+ const char *oldname, /* .foldername */
+ const char *newname, /* .foldername */
+ int flags, /* See above */
+ void (*callback_func)(const char *old_path,
+ const char *new_path)
+ );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/contrib/notmuch-deliver/src/maildirmkdir.c b/contrib/notmuch-deliver/src/maildirmkdir.c
new file mode 100644
index 0000000..754b2c7
--- /dev/null
+++ b/contrib/notmuch-deliver/src/maildirmkdir.c
@@ -0,0 +1,78 @@
+/*
+** Copyright 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <errno.h>
+
+#include "maildirmisc.h"
+
+static const char rcsid[]="$Id: maildirmkdir.c,v 1.2 2002/03/15 03:09:21 mrsam Exp $";
+
+int maildir_mkdir(const char *dir)
+{
+char *buf, *p;
+size_t l;
+
+ if (dir == 0 || dir[0] == 0)
+ {
+ errno = EINVAL;
+ return (-1);
+ }
+ l = strlen(dir);
+ if ((buf = malloc(l + sizeof("/tmp"))) == 0)
+ {
+ errno = ENOMEM;
+ return (-1);
+ }
+ strcpy(buf, dir);
+ strcpy(buf+l, "/cur");
+
+ /* We do mkdir -p here */
+
+ p = buf+1;
+ while ((p = strchr(p, '/')) != 0)
+ {
+ *p = '\0';
+ if (mkdir(buf, 0700) < 0 && errno != EEXIST)
+ {
+ free(buf);
+ return (-1);
+ }
+ *p++ = '/';
+ }
+
+ if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
+ free(buf);
+ return (-1);
+ }
+ strcpy(buf+l, "/new");
+ if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
+ free(buf);
+ return (-1);
+ }
+ /*
+ * make /tmp last because this is the one we open first -
+ * the existence of this directory implies the whole
+ * Maildir structure is complete
+ */
+ strcpy(buf+l, "/tmp");
+ if (mkdir(buf, 0700) < 0 && errno != EEXIST) {
+ free(buf);
+ return (-1);
+ }
+ free(buf);
+ return (0);
+}
+
diff --git a/contrib/notmuch-deliver/src/maildiropen.c b/contrib/notmuch-deliver/src/maildiropen.c
new file mode 100644
index 0000000..5071df7
--- /dev/null
+++ b/contrib/notmuch-deliver/src/maildiropen.c
@@ -0,0 +1,141 @@
+/*
+** Copyright 2000 Double Precision, Inc.
+** See COPYING for distribution information.
+*/
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "maildirmisc.h"
+
+static const char rcsid[]="$Id: maildiropen.c,v 1.8 2003/01/19 16:39:52 mrsam Exp $";
+
+char *maildir_getlink(const char *filename)
+{
+#if HAVE_READLINK
+size_t bufsiz;
+char *buf;
+
+ bufsiz=0;
+ buf=0;
+
+ for (;;)
+ {
+ int n;
+
+ if (buf) free(buf);
+ bufsiz += 256;
+ if ((buf=malloc(bufsiz)) == 0)
+ {
+ perror("malloc");
+ return (0);
+ }
+ if ((n=readlink(filename, buf, bufsiz)) < 0)
+ {
+ free(buf);
+ return (0);
+ }
+ if (n < bufsiz)
+ {
+ buf[n]=0;
+ break;
+ }
+ }
+ return (buf);
+#else
+ return (0);
+#endif
+}
+
+int maildir_semisafeopen(const char *path, int mode, int perm)
+{
+
+#if HAVE_READLINK
+
+char *l=maildir_getlink(path);
+
+ if (l)
+ {
+ int f;
+
+ if (*l != '/')
+ {
+ char *q=malloc(strlen(path)+strlen(l)+2);
+ char *s;
+
+ if (!q)
+ {
+ free(l);
+ return (-1);
+ }
+
+ strcpy(q, path);
+ if ((s=strchr(q, '/')) != 0)
+ s[1]=0;
+ else *q=0;
+ strcat(q, l);
+ free(l);
+ l=q;
+ }
+
+ f=maildir_safeopen(l, mode, perm);
+
+ free(l);
+ return (f);
+ }
+#endif
+
+ return (maildir_safeopen(path, mode, perm));
+}
+
+int maildir_safeopen(const char *path, int mode, int perm)
+{
+ struct stat stat1;
+
+ return maildir_safeopen_stat(path, mode, perm, &stat1);
+}
+
+int maildir_safeopen_stat(const char *path, int mode, int perm,
+ struct stat *stat1)
+{
+ struct stat stat2;
+
+ int fd=open(path, mode
+#ifdef O_NONBLOCK
+ | O_NONBLOCK
+#else
+ | O_NDELAY
+#endif
+ , perm);
+
+ if (fd < 0) return (fd);
+ if (fcntl(fd, F_SETFL, (mode & O_APPEND)) || fstat(fd, stat1)
+ || lstat(path, &stat2))
+ {
+ close(fd);
+ return (-1);
+ }
+
+ if (stat1->st_dev != stat2.st_dev || stat1->st_ino != stat2.st_ino)
+ {
+ close(fd);
+ errno=ENOENT;
+ return (-1);
+ }
+
+ return (fd);
+}
diff --git a/contrib/notmuch-deliver/src/main.c b/contrib/notmuch-deliver/src/main.c
new file mode 100644
index 0000000..1fece76
--- /dev/null
+++ b/contrib/notmuch-deliver/src/main.c
@@ -0,0 +1,378 @@
+/* vim: set cino= fo=croql sw=8 ts=8 sts=0 noet cin fdm=syntax : */
+
+/*
+ * Copyright (c) 2010 Ali Polatel <alip@exherbo.org>
+ * Based in part upon deliverquota of maildrop which is:
+ * Copyright 1998 - 2009 Double Precision, Inc.
+ *
+ * This file is part of the notmuch-deliver. notmuch-deliver is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * notmuch-deliver 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, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#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;
+}
+
+static int
+save_maildir(int fdin, const char *dir, int auto_create, char **path)
+{
+ int fd, ret, written;
+ char buf[4096], *p;
+ struct maildir_tmpcreate_info info;
+
+ maildir_tmpcreate_init(&info);
+ info.openmode = 0666;
+ info.maildir = dir;
+ info.doordie = 1;
+
+ while ((fd = 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);
+ 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));
+ goto fail;
+ }
+ p = buf;
+ do {
+ written = write(fd, p, ret);
+ if (!written)
+ goto fail;
+ if (written < 0) {
+ if (errno == EINTR)
+ continue;
+ g_critical("Writing to temporary file `%s' failed: %s",
+ info.tmpname, g_strerror(errno));
+ goto fail;
+ }
+ p += written;
+ ret -= written;
+ } while (ret);
+ }
+
+ close(fd);
+ 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:
+ 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"
+ "\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) {
+ 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) {
+ 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;
+}