summaryrefslogtreecommitdiffstats
path: root/src/plugins/old-stats/mail-stats-connection.c
blob: ca20fcb51700183fd1f09a8bdeaac47ecb41e5e5 (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
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "base64.h"
#include "hostpid.h"
#include "net.h"
#include "str.h"
#include "strescape.h"
#include "mail-storage.h"
#include "stats.h"
#include "stats-plugin.h"
#include "mail-stats-connection.h"

int mail_stats_connection_connect(struct stats_connection *conn,
				  struct mail_user *user)
{
	struct stats_user *suser = STATS_USER_CONTEXT_REQUIRE(user);
	string_t *str = t_str_new(128);

	str_append(str, "CONNECT\t");
	/* required fields */
	str_append(str, suser->stats_session_id);
	str_append_c(str, '\t');
	str_append_tabescaped(str, user->username);
	str_append_c(str, '\t');
	str_append_tabescaped(str, user->service);
	str_printfa(str, "\t%s", my_pid);

	/* optional fields */
	if (user->conn.local_ip != NULL) {
		str_append(str, "\tlip=");
		str_append(str, net_ip2addr(user->conn.local_ip));
	}
	if (user->conn.remote_ip != NULL) {
		str_append(str, "\trip=");
		str_append(str, net_ip2addr(user->conn.remote_ip));
	}
	str_append_c(str, '\n');
	return stats_connection_send(conn, str);
}

void mail_stats_connection_disconnect(struct stats_connection *conn,
				      struct mail_user *user)
{
	struct stats_user *suser = STATS_USER_CONTEXT_REQUIRE(user);
	string_t *str = t_str_new(128);

	str_append(str, "DISCONNECT\t");
	str_append(str, suser->stats_session_id);
	str_append_c(str, '\n');
	if (stats_connection_send(conn, str) < 0) {
		/* we could retry this later, but stats process will forget it
		   anyway after 15 minutes. */
	}
}

void mail_stats_connection_send_session(struct stats_connection *conn,
					struct mail_user *user,
					const struct stats *stats)
{
	struct stats_user *suser = STATS_USER_CONTEXT_REQUIRE(user);
	string_t *str = t_str_new(256);
	buffer_t *buf;

	buf = t_buffer_create(128);
	stats_export(buf, stats);

	str_append(str, "UPDATE-SESSION\t");
	str_append(str, suser->stats_session_id);
	str_append_c(str, '\t');
	base64_encode(buf->data, buf->used, str);

	str_append_c(str, '\n');
	(void)stats_connection_send(conn, str);
}