dovecot-2.2: lda, lmtp: Added delivery_time and session_time var...

dovecot at dovecot.org dovecot at dovecot.org
Tue Apr 21 10:20:57 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/3ea5becbb56c
changeset: 18430:3ea5becbb56c
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Apr 21 13:19:24 2015 +0300
description:
lda, lmtp: Added delivery_time and session_time variables to deliver_log_format
session_time is meaningful only with LMTP. The delivery_time is separate for
each mail delivery. The results are in milliseconds now. If needed we could
add a new %modifier that converts it into secs.millisecs.

diffstat:

 src/lib-lda/mail-deliver.c |  44 ++++++++++++++++++++++++++++++++++++++------
 src/lib-lda/mail-deliver.h |   6 ++++++
 src/lmtp/client.h          |   2 ++
 src/lmtp/commands.c        |   9 +++++++++
 4 files changed, 55 insertions(+), 6 deletions(-)

diffs (167 lines):

diff -r 4f95a2b5a154 -r 3ea5becbb56c src/lib-lda/mail-deliver.c
--- a/src/lib-lda/mail-deliver.c	Tue Apr 21 11:13:51 2015 +0300
+++ b/src/lib-lda/mail-deliver.c	Tue Apr 21 13:19:24 2015 +0300
@@ -5,6 +5,7 @@
 #include "array.h"
 #include "str.h"
 #include "str-sanitize.h"
+#include "time-util.h"
 #include "unichar.h"
 #include "var-expand.h"
 #include "message-address.h"
@@ -38,8 +39,10 @@
 		NULL : t_strconcat(addr->mailbox, "@", addr->domain, NULL);
 }
 
-const struct var_expand_table *
-mail_deliver_get_log_var_expand_table(struct mail *mail, const char *message)
+static const struct var_expand_table *
+mail_deliver_get_log_var_expand_table_full(struct mail_deliver_context *ctx,
+					   struct mail *mail,
+					   const char *message)
 {
 	static struct var_expand_table static_tab[] = {
 		{ '$', NULL, NULL },
@@ -49,6 +52,8 @@
 		{ 'e', NULL, "from_envelope" },
 		{ 'p', NULL, "size" },
 		{ 'w', NULL, "vsize" },
+		{ '\0', NULL, "delivery_time" },
+		{ '\0', NULL, "session_time" },
 		{ '\0', NULL, NULL }
 	};
 	struct var_expand_table *tab;
@@ -75,20 +80,44 @@
 		tab[5].value = dec2str(size);
 	if (mail_get_virtual_size(mail, &size) == 0)
 		tab[6].value = dec2str(size);
+	if (ctx != NULL) {
+		int delivery_time_msecs;
+
+		io_loop_time_refresh();
+		delivery_time_msecs =
+			timeval_diff_msecs(&ioloop_timeval,
+					   &ctx->delivery_time_started);
+		tab[7].value = dec2str(delivery_time_msecs);
+		tab[8].value = dec2str(ctx->session_time_msecs);
+	}
 	return tab;
 }
 
+const struct var_expand_table *
+mail_deliver_get_log_var_expand_table(struct mail *mail, const char *message)
+{
+	return mail_deliver_get_log_var_expand_table_full(NULL, mail, message);
+}
+
+const struct var_expand_table *
+mail_deliver_ctx_get_log_var_expand_table(struct mail_deliver_context *ctx,
+					  const char *message)
+{
+	struct mail *mail;
+
+	mail = ctx->dest_mail != NULL ? ctx->dest_mail : ctx->src_mail;
+	return mail_deliver_get_log_var_expand_table_full(ctx, mail, message);
+}
+
 static void
 mail_deliver_log_cache_var_expand_table(struct mail_deliver_context *ctx)
 {
 	const struct var_expand_table *src;
 	struct var_expand_table *dest;
-	struct mail *mail;
 	unsigned int i, len;
 
-	mail = ctx->dest_mail != NULL ? ctx->dest_mail : ctx->src_mail;
-	src = mail_deliver_get_log_var_expand_table(mail, "");
-	for (len = 0; src[len].key != '\0'; len++) ;
+	src = mail_deliver_ctx_get_log_var_expand_table(ctx, "");
+	for (len = 0; src[len].key != '\0' || src[len].long_key != NULL; len++) ;
 
 	dest = p_new(ctx->pool, struct var_expand_table, len + 1);
 	for (i = 0; i < len; i++) {
@@ -392,6 +421,9 @@
 {
 	int ret;
 
+	io_loop_time_refresh();
+	ctx->delivery_time_started = ioloop_timeval;
+
 	*storage_r = NULL;
 	if (deliver_mail == NULL)
 		ret = -1;
diff -r 4f95a2b5a154 -r 3ea5becbb56c src/lib-lda/mail-deliver.h
--- a/src/lib-lda/mail-deliver.h	Tue Apr 21 11:13:51 2015 +0300
+++ b/src/lib-lda/mail-deliver.h	Tue Apr 21 13:19:24 2015 +0300
@@ -26,6 +26,9 @@
 	struct mail_deliver_session *session;
 	unsigned int timeout_secs;
 
+	unsigned int session_time_msecs;
+	struct timeval delivery_time_started;
+
 	struct duplicate_context *dup_ctx;
 
 	/* Session ID, used as log line prefix if non-NULL. */
@@ -78,6 +81,9 @@
 extern deliver_mail_func_t *deliver_mail;
 
 const struct var_expand_table *
+mail_deliver_ctx_get_log_var_expand_table(struct mail_deliver_context *ctx,
+					  const char *message);
+const struct var_expand_table *
 mail_deliver_get_log_var_expand_table(struct mail *mail, const char *message);
 void mail_deliver_log(struct mail_deliver_context *ctx, const char *fmt, ...)
 	ATTR_FORMAT(2, 3);
diff -r 4f95a2b5a154 -r 3ea5becbb56c src/lmtp/client.h
--- a/src/lmtp/client.h	Tue Apr 21 11:13:51 2015 +0300
+++ b/src/lmtp/client.h	Tue Apr 21 13:19:24 2015 +0300
@@ -37,6 +37,8 @@
 	struct ostream *mail_data_output;
 	const char *added_headers;
 
+	struct timeval mail_from_timeval, data_end_timeval;
+
 	struct mail *raw_mail;
 
 	struct mail_user *dest_user;
diff -r 4f95a2b5a154 -r 3ea5becbb56c src/lmtp/commands.c
--- a/src/lmtp/commands.c	Tue Apr 21 11:13:51 2015 +0300
+++ b/src/lmtp/commands.c	Tue Apr 21 13:19:24 2015 +0300
@@ -12,6 +12,7 @@
 #include "istream-dot.h"
 #include "safe-mkstemp.h"
 #include "hex-dec.h"
+#include "time-util.h"
 #include "var-expand.h"
 #include "restrict-access.h"
 #include "settings-parser.h"
@@ -212,6 +213,8 @@
 	p_array_init(&client->state.rcpt_to, client->state_pool, 64);
 	client_send_line(client, "250 2.1.0 OK");
 	client_state_set(client, "MAIL FROM", client->state.mail_from);
+
+	client->state.mail_from_timeval = ioloop_timeval;
 	return 0;
 }
 
@@ -809,6 +812,10 @@
 	dctx.src_mail = src_mail;
 	dctx.src_envelope_sender = client->state.mail_from;
 	dctx.dest_user = client->state.dest_user;
+	dctx.session_time_msecs =
+		timeval_diff_msecs(&client->state.data_end_timeval,
+				   &client->state.mail_from_timeval);
+
 	if (orcpt_get_valid_rfc822(rcpt->params.dsn_orcpt, &dctx.dest_addr)) {
 		/* used ORCPT */
 	} else if (*dctx.set->lda_original_recipient_header != '\0') {
@@ -1093,6 +1100,8 @@
 	io_remove(&client->io);
 	i_stream_destroy(&client->dot_input);
 
+	client->state.data_end_timeval = ioloop_timeval;
+
 	input = client_get_input(client);
 	if (array_count(&client->state.rcpt_to) != 0)
 		client_input_data_write_local(client, input);


More information about the dovecot-cvs mailing list