dovecot: Added deliver_log_format setting to control what's logg...

dovecot at dovecot.org dovecot at dovecot.org
Sat Dec 8 14:59:09 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/6b5f89c41de5
changeset: 6948:6b5f89c41de5
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Dec 08 14:59:05 2007 +0200
description:
Added deliver_log_format setting to control what's logged when a mail is
delivered.

diffstat:

3 files changed, 158 insertions(+), 113 deletions(-)
dovecot-example.conf  |    7 +
src/deliver/deliver.c |  262 +++++++++++++++++++++++++++----------------------
src/deliver/deliver.h |    2 

diffs (truncated from 356 to 300 lines):

diff -r 0a7d53a1b8fe -r 6b5f89c41de5 dovecot-example.conf
--- a/dovecot-example.conf	Sat Dec 08 14:43:57 2007 +0200
+++ b/dovecot-example.conf	Sat Dec 08 14:59:05 2007 +0200
@@ -661,6 +661,13 @@ protocol lda {
   # If user is over quota, return with temporary failure instead of
   # bouncing the mail.
   #quota_full_tempfail = no
+
+  # Format to use for logging mail deliveries. You can use variables:
+  #  %$ - Delivery status message (e.g. "saved to INBOX")
+  #  %m - Message-ID
+  #  %s - Subject
+  #  %f - From address
+  #deliver_log_format = msgid=%m: %$
 
   # Binary to use for sending mails.
   #sendmail_path = /usr/lib/sendmail
diff -r 0a7d53a1b8fe -r 6b5f89c41de5 src/deliver/deliver.c
--- a/src/deliver/deliver.c	Sat Dec 08 14:43:57 2007 +0200
+++ b/src/deliver/deliver.c	Sat Dec 08 14:59:05 2007 +0200
@@ -48,6 +48,11 @@
    where to read the mail. */
 #define MAIL_MAX_MEMORY_BUFFER (1024*128)
 
+static const char *wanted_headers[] = {
+	"From", "Message-ID", "Subject", "Return-Path",
+	NULL
+};
+
 struct deliver_settings *deliver_set;
 deliver_mail_func_t *deliver_mail = NULL;
 
@@ -73,117 +78,7 @@ static void sig_die(int signo, void *con
 	io_loop_stop(ioloop);
 }
 
-static void
-deliver_log(struct mail *mail, const char *fmt, ...) ATTR_FORMAT(2, 3);
-
-static void deliver_log(struct mail *mail, const char *fmt, ...)
-{
-	va_list args;
-	string_t *str;
-	const char *msgid;
-
-	va_start(args, fmt);
-	str = t_str_new(256);
-
-	if (mail_get_first_header(mail, "Message-ID", &msgid) <= 0)
-		msgid = "";
-	str_printfa(str, "msgid=%s: ", str_sanitize(msgid, 80));
-
-	str_vprintfa(str, fmt, args);
-	i_info("%s", str_c(str));
-	va_end(args);
-}
-
-static struct mailbox *
-mailbox_open_or_create_synced(struct mail_namespace *namespaces,
-			      struct mail_storage **storage_r, const char *name)
-{
-	struct mail_namespace *ns;
-	struct mailbox *box;
-	enum mail_error error;
-
-	ns = mail_namespace_find(namespaces, &name);
-	if (ns == NULL) {
-		*storage_r = NULL;
-		return NULL;
-	}
-	*storage_r = ns->storage;
-
-	box = mailbox_open(ns->storage, name, NULL, MAILBOX_OPEN_FAST |
-			   MAILBOX_OPEN_KEEP_RECENT);
-	if (box != NULL || no_mailbox_autocreate)
-		return box;
-
-	(void)mail_storage_get_last_error(ns->storage, &error);
-	if (error != MAIL_ERROR_NOTFOUND)
-		return NULL;
-
-	/* try creating it. */
-	if (mail_storage_mailbox_create(ns->storage, name, FALSE) < 0)
-		return NULL;
-
-	/* and try opening again */
-	box = mailbox_open(ns->storage, name, NULL, MAILBOX_OPEN_FAST |
-			   MAILBOX_OPEN_KEEP_RECENT);
-	if (box == NULL)
-		return NULL;
-
-	if (mailbox_sync(box, 0, 0, NULL) < 0) {
-		mailbox_close(&box);
-		return NULL;
-	}
-	return box;
-}
-
-int deliver_save(struct mail_namespace *namespaces,
-		 struct mail_storage **storage_r, const char *mailbox,
-		 struct mail *mail, enum mail_flags flags,
-		 const char *const *keywords)
-{
-	struct mailbox *box;
-	struct mailbox_transaction_context *t;
-	struct mail_keywords *kw;
-	enum mail_error error;
-	const char *mailbox_name;
-	int ret = 0;
-
-	if (strcmp(mailbox, default_mailbox_name) == 0)
-		tried_default_save = TRUE;
-
-	mailbox_name = str_sanitize(mailbox, 80);
-	box = mailbox_open_or_create_synced(namespaces, storage_r, mailbox);
-	if (box == NULL) {
-		deliver_log(mail, "save failed to %s: %s", mailbox_name,
-			    mail_storage_get_last_error(*storage_r, &error));
-		return -1;
-	}
-
-	t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
-
-	kw = str_array_length(keywords) == 0 ? NULL :
-		mailbox_keywords_create_valid(box, keywords);
-	if (mailbox_copy(t, mail, flags, kw, NULL) < 0)
-		ret = -1;
-	mailbox_keywords_free(box, &kw);
-
-	if (ret < 0)
-		mailbox_transaction_rollback(&t);
-	else
-		ret = mailbox_transaction_commit(&t);
-
-	if (ret == 0) {
-		saved_mail = TRUE;
-		deliver_log(mail, "saved mail to %s", mailbox_name);
-	} else {
-		deliver_log(mail, "save failed to %s: %s", mailbox_name,
-			    mail_storage_get_last_error(*storage_r, &error));
-	}
-
-	mailbox_close(&box);
-	return ret;
-}
-
-const char *deliver_get_return_address(struct mail *mail)
+static const char *deliver_get_address(struct mail *mail, const char *header)
 {
 	struct message_address *addr;
 	const char *str;
@@ -191,7 +86,7 @@ const char *deliver_get_return_address(s
 	if (explicit_envelope_sender != NULL)
 		return explicit_envelope_sender;
 
-	if (mail_get_first_header(mail, "Return-Path", &str) <= 0)
+	if (mail_get_first_header(mail, header, &str) <= 0)
 		return NULL;
 	addr = message_address_parse(pool_datastack_create(),
 				     (const unsigned char *)str,
@@ -199,6 +94,141 @@ const char *deliver_get_return_address(s
 	return addr == NULL || addr->mailbox == NULL || addr->domain == NULL ||
 		*addr->mailbox == '\0' || *addr->domain == '\0' ?
 		NULL : t_strconcat(addr->mailbox, "@", addr->domain, NULL);
+}
+
+static const struct var_expand_table *
+get_log_var_expand_table(struct mail *mail, const char *message)
+{
+	static struct var_expand_table static_tab[] = {
+		{ '$', NULL },
+		{ 'm', NULL },
+		{ 's', NULL },
+		{ 'f', NULL },
+		{ '\0', NULL }
+	};
+	struct var_expand_table *tab;
+
+	tab = t_malloc(sizeof(static_tab));
+	memcpy(tab, static_tab, sizeof(static_tab));
+
+	tab[0].value = message;
+	(void)mail_get_first_header(mail, "Message-ID", &tab[1].value);
+	(void)mail_get_first_header(mail, "Subject", &tab[2].value);
+	tab[3].value = deliver_get_address(mail, "From");
+	return tab;
+}
+
+static void
+deliver_log(struct mail *mail, const char *fmt, ...) ATTR_FORMAT(2, 3);
+
+static void deliver_log(struct mail *mail, const char *fmt, ...)
+{
+	va_list args;
+	string_t *str;
+	const char *msg;
+
+	va_start(args, fmt);
+	msg = t_strdup_vprintf(fmt, args);
+
+	str = t_str_new(256);
+	var_expand(str, deliver_set->log_format,
+		   get_log_var_expand_table(mail, msg));
+	i_info("%s", str_c(str));
+	va_end(args);
+}
+
+static struct mailbox *
+mailbox_open_or_create_synced(struct mail_namespace *namespaces,
+			      struct mail_storage **storage_r, const char *name)
+{
+	struct mail_namespace *ns;
+	struct mailbox *box;
+	enum mail_error error;
+
+	ns = mail_namespace_find(namespaces, &name);
+	if (ns == NULL) {
+		*storage_r = NULL;
+		return NULL;
+	}
+	*storage_r = ns->storage;
+
+	box = mailbox_open(ns->storage, name, NULL, MAILBOX_OPEN_FAST |
+			   MAILBOX_OPEN_KEEP_RECENT);
+	if (box != NULL || no_mailbox_autocreate)
+		return box;
+
+	(void)mail_storage_get_last_error(ns->storage, &error);
+	if (error != MAIL_ERROR_NOTFOUND)
+		return NULL;
+
+	/* try creating it. */
+	if (mail_storage_mailbox_create(ns->storage, name, FALSE) < 0)
+		return NULL;
+
+	/* and try opening again */
+	box = mailbox_open(ns->storage, name, NULL, MAILBOX_OPEN_FAST |
+			   MAILBOX_OPEN_KEEP_RECENT);
+	if (box == NULL)
+		return NULL;
+
+	if (mailbox_sync(box, 0, 0, NULL) < 0) {
+		mailbox_close(&box);
+		return NULL;
+	}
+	return box;
+}
+
+int deliver_save(struct mail_namespace *namespaces,
+		 struct mail_storage **storage_r, const char *mailbox,
+		 struct mail *mail, enum mail_flags flags,
+		 const char *const *keywords)
+{
+	struct mailbox *box;
+	struct mailbox_transaction_context *t;
+	struct mail_keywords *kw;
+	enum mail_error error;
+	const char *mailbox_name;
+	int ret = 0;
+
+	if (strcmp(mailbox, default_mailbox_name) == 0)
+		tried_default_save = TRUE;
+
+	mailbox_name = str_sanitize(mailbox, 80);
+	box = mailbox_open_or_create_synced(namespaces, storage_r, mailbox);
+	if (box == NULL) {
+		deliver_log(mail, "save failed to %s: %s", mailbox_name,
+			    mail_storage_get_last_error(*storage_r, &error));
+		return -1;
+	}
+
+	t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
+
+	kw = str_array_length(keywords) == 0 ? NULL :
+		mailbox_keywords_create_valid(box, keywords);
+	if (mailbox_copy(t, mail, flags, kw, NULL) < 0)
+		ret = -1;
+	mailbox_keywords_free(box, &kw);
+
+	if (ret < 0)
+		mailbox_transaction_rollback(&t);
+	else
+		ret = mailbox_transaction_commit(&t);
+
+	if (ret == 0) {
+		saved_mail = TRUE;
+		deliver_log(mail, "saved mail to %s", mailbox_name);
+	} else {
+		deliver_log(mail, "save failed to %s: %s", mailbox_name,
+			    mail_storage_get_last_error(*storage_r, &error));
+	}
+
+	mailbox_close(&box);
+	return ret;
+}
+
+const char *deliver_get_return_address(struct mail *mail)
+{
+	return deliver_get_address(mail, "Return-Path");
 }


More information about the dovecot-cvs mailing list