changeset 19712:b2cde9a21ff8

stats: Added ADD-USER command to add stats to a user without having a session. This will be used by at least the auth process. Although the auth process does have a session, it's a bit too early to start using it yet. At least the PID should be possible to change when the session moves to imap process. Also the unsuccessful authentication sessions shouldn't really be added at all.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 05 Feb 2016 14:55:56 +0200
parents ce7e31d9ad08
children aad2bfc8cbd3
files src/stats/fifo-input-connection.c src/stats/mail-user.c src/stats/mail-user.h
diffstat 3 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/stats/fifo-input-connection.c	Fri Feb 05 14:35:48 2016 +0200
+++ b/src/stats/fifo-input-connection.c	Fri Feb 05 14:55:56 2016 +0200
@@ -6,6 +6,7 @@
 #include "ostream.h"
 #include "master-service.h"
 #include "mail-session.h"
+#include "mail-user.h"
 #include "mail-command.h"
 #include "fifo-input-connection.h"
 
@@ -36,6 +37,8 @@
 		return mail_session_disconnect_parse(args, error_r);
 	if (strcmp(cmd, "UPDATE-SESSION") == 0)
 		return mail_session_update_parse(args, error_r);
+	if (strcmp(cmd, "ADD-USER") == 0)
+		return mail_user_add_parse(args, error_r);
 	if (strcmp(cmd, "UPDATE-CMD") == 0)
 		return mail_command_update_parse(args, error_r);
 
--- a/src/stats/mail-user.c	Fri Feb 05 14:35:48 2016 +0200
+++ b/src/stats/mail-user.c	Fri Feb 05 14:55:56 2016 +0200
@@ -1,9 +1,11 @@
 /* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "buffer.h"
 #include "ioloop.h"
 #include "hash.h"
 #include "llist.h"
+#include "base64.h"
 #include "global-memory.h"
 #include "stats-settings.h"
 #include "mail-stats.h"
@@ -117,6 +119,38 @@
 	mail_domain_refresh(user->domain, diff_stats);
 }
 
+int mail_user_add_parse(const char *const *args, const char **error_r)
+{
+	struct mail_user *user;
+	struct stats *diff_stats;
+	buffer_t *buf;
+	const char *service, *error;
+
+	/* <user> <service> <diff stats> */
+	if (str_array_length(args) < 3) {
+		*error_r = "ADD-USER: Too few parameters";
+		return -1;
+	}
+
+	user = mail_user_login(args[0]);
+	service = args[1];
+
+	buf = buffer_create_dynamic(pool_datastack_create(), 256);
+	if (base64_decode(args[2], strlen(args[2]), NULL, buf) < 0) {
+		*error_r = t_strdup_printf("ADD-USER %s %s: Invalid base64 input",
+					   user->name, service);
+		return -1;
+	}
+	diff_stats = stats_alloc(pool_datastack_create());
+	if (!stats_import(buf->data, buf->used, user->stats, diff_stats, &error)) {
+		*error_r = t_strdup_printf("ADD-USER %s %s: %s",
+					   user->name, service, error);
+		return -1;
+	}
+	mail_user_refresh(user, diff_stats);
+	return 0;
+}
+
 void mail_users_free_memory(void)
 {
 	unsigned int diff;
--- a/src/stats/mail-user.h	Fri Feb 05 14:35:48 2016 +0200
+++ b/src/stats/mail-user.h	Fri Feb 05 14:55:56 2016 +0200
@@ -11,6 +11,7 @@
 
 void mail_user_refresh(struct mail_user *user,
 		       const struct stats *diff_stats) ATTR_NULL(2);
+int mail_user_add_parse(const char *const *args, const char **error_r);
 
 void mail_user_ref(struct mail_user *user);
 void mail_user_unref(struct mail_user **user);