summaryrefslogtreecommitdiff
path: root/src/irc/core/lag.c
blob: 5b52fcce55acce15c1a84f3b42b4fbf36185072e (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 lag.c : irssi

    Copyright (C) 1999 Timo Sirainen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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
*/

#include "module.h"
#include "signals.h"
#include "misc.h"
#include "settings.h"

#include "irc.h"
#include "irc-server.h"

typedef struct {
	IRC_SERVER_REC *server;
	GTimeVal time;
} LAG_REC;

static gint timeout_tag;
static GSList *lags;

static LAG_REC *lag_find(IRC_SERVER_REC *server)
{
	GSList *tmp;

	for (tmp = lags; tmp != NULL; tmp = tmp->next) {
		LAG_REC *lag = tmp->data;

		if (lag->server == server)
			return lag;
	}

	return NULL;
}

static void lag_free(LAG_REC *rec)
{
	lags = g_slist_remove(lags, rec);
	g_free(rec);
}

static void lag_get(IRC_SERVER_REC *server)
{
	LAG_REC *lag;

	g_return_if_fail(server != NULL);

	lag = g_new0(LAG_REC, 1);
	lags = g_slist_append(lags, lag);
	lag->server = server;

	g_get_current_time(&lag->time);

	if (server->lag_sent == 0)
		server->lag_sent = time(NULL);
	server->lag_last_check = time(NULL);

	/* NOTE: this will fail if there's any nick changes in buffer -
	   that's why this function should be called only when the buffer
	   is empty */
	irc_send_cmdv(server, "NOTICE %s :\001IRSSILAG %ld %ld\001",
		      server->nick, lag->time.tv_sec, lag->time.tv_usec);
}

/* we use "ctcp reply" signal here, because "ctcp reply irssilag" can be
   ignored with /IGNORE * CTCPS */
static void sig_irssilag(const char *data, IRC_SERVER_REC *server, const char *nick, const char *addr, const char *target)
{
	GTimeVal now, sent;
	LAG_REC *lag;

	g_return_if_fail(data != NULL);

	if (strncmp(data, "IRSSILAG ", 9) != 0)
		return;
	data += 9;

	lag = lag_find(server);
	if (lag == NULL) {
		/* not expecting lag reply.. */
		return;
	}

	if (g_strcasecmp(nick, server->nick) != 0) {
		/* we didn't sent this - not a lag notice */
		return;
	}

	/* OK, it is a lag notice. */
	server->lag_sent = 0;

	if (sscanf(data, "%ld %ld", &sent.tv_sec, &sent.tv_usec) == 2) {
		g_get_current_time(&now);
		server->lag = (int) get_timeval_diff(&now, &sent);
		signal_emit("server lag", 1, server);
	}

	lag_free(lag);
}

static int sig_check_lag(void)
{
	GSList *tmp, *next;
	time_t now;
	int lag_check_time, max_lag;

	lag_check_time = settings_get_int("lag_check_time");
	max_lag = settings_get_int("lag_max_before_disconnect");

	if (lag_check_time <= 0)
		return 1;

	now = time(NULL);
	for (tmp = servers; tmp != NULL; tmp = next) {
		IRC_SERVER_REC *rec = tmp->data;

		next = tmp->next;
		if (!irc_server_check(rec))
			continue;

		if (rec->lag_sent != 0) {
			/* waiting for lag reply */
			if (max_lag > 1 && now-rec->lag_sent > max_lag) {
				/* too much lag, disconnect */
				signal_emit("server lag disconnect", 1, rec);
				rec->connection_lost = TRUE;
				server_disconnect((SERVER_REC *) rec);
			}
		}
		else if (rec->lag_last_check+lag_check_time < now &&
			 rec->cmdcount == 0 && rec->connected) {
			/* no commands in buffer - get the lag */
			lag_get(rec);
		}
	}

	return 1;
}

static void sig_empty(void)
{
	/* don't print the "CTCP IRSSILAG reply .." text */
}

void lag_init(void)
{
	settings_add_int("misc", "lag_check_time", 30);
	settings_add_int("misc", "lag_max_before_disconnect", 300);

	lags = NULL;
	timeout_tag = g_timeout_add(1000, (GSourceFunc) sig_check_lag, NULL);
	signal_add("ctcp reply", (SIGNAL_FUNC) sig_irssilag);
	signal_add("ctcp reply irssilag", (SIGNAL_FUNC) sig_empty);
}

void lag_deinit(void)
{
	g_source_remove(timeout_tag);
	while (lags != NULL)
		lag_free(lags->data);
	signal_remove("ctcp reply", (SIGNAL_FUNC) sig_irssilag);
	signal_remove("ctcp reply irssilag", (SIGNAL_FUNC) sig_empty);
}