e51a3fa23f03393d6a46a05ada427c9c804307d9
[crypto.git] / apps / irssi / src / core / servers-reconnect.c
1 /*
2  servers-reconnect.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "commands.h"
23 #include "network.h"
24 #include "signals.h"
25
26 #include "chat-protocols.h"
27 #include "servers.h"
28 #include "servers-setup.h"
29 #include "servers-reconnect.h"
30
31 #include "settings.h"
32
33 GSList *reconnects;
34 static int last_reconnect_tag;
35 static int reconnect_timeout_tag;
36 static int reconnect_time;
37
38 void reconnect_save_status(SERVER_CONNECT_REC *conn, SERVER_REC *server)
39 {
40         g_free_not_null(conn->tag);
41         conn->tag = g_strdup(server->tag);
42
43         g_free_not_null(conn->away_reason);
44         conn->away_reason = !server->usermode_away ? NULL :
45                 g_strdup(server->away_reason);
46
47         signal_emit("server reconnect save status", 2, conn, server);
48 }
49
50 static void server_reconnect_add(SERVER_CONNECT_REC *conn,
51                                  time_t next_connect)
52 {
53         RECONNECT_REC *rec;
54
55         g_return_if_fail(IS_SERVER_CONNECT(conn));
56
57         rec = g_new(RECONNECT_REC, 1);
58         rec->tag = ++last_reconnect_tag;
59         rec->next_connect = next_connect;
60
61         rec->conn = conn;
62         server_connect_ref(conn);
63
64         reconnects = g_slist_append(reconnects, rec);
65 }
66
67 void server_reconnect_destroy(RECONNECT_REC *rec)
68 {
69         g_return_if_fail(rec != NULL);
70
71         reconnects = g_slist_remove(reconnects, rec);
72
73         signal_emit("server reconnect remove", 1, rec);
74         server_connect_unref(rec->conn);
75         g_free(rec);
76
77         if (reconnects == NULL)
78             last_reconnect_tag = 0;
79 }
80
81 static int server_reconnect_timeout(void)
82 {
83         SERVER_CONNECT_REC *conn;
84         GSList *list, *tmp;
85         time_t now;
86
87         /* If server_connect() removes the next reconnection in queue,
88            we're screwed. I don't think this should happen anymore, but just
89            to be sure we don't crash, do this safely. */
90         list = g_slist_copy(reconnects);
91         now = time(NULL);
92         for (tmp = list; tmp != NULL; tmp = tmp->next) {
93                 RECONNECT_REC *rec = tmp->data;
94
95                 if (g_slist_find(reconnects, rec) == NULL)
96                         continue;
97
98                 if (rec->next_connect <= now) {
99                         conn = rec->conn;
100                         server_connect_ref(conn);
101                         server_reconnect_destroy(rec);
102                         CHAT_PROTOCOL(conn)->server_connect(conn);
103                         server_connect_unref(conn);
104                 }
105         }
106
107         g_slist_free(list);
108         return 1;
109 }
110
111 static void sserver_connect(SERVER_SETUP_REC *rec, SERVER_CONNECT_REC *conn)
112 {
113         conn->family = rec->family;
114         conn->address = g_strdup(rec->address);
115         if (conn->port == 0) conn->port = rec->port;
116
117         server_setup_fill_reconn(conn, rec);
118         server_reconnect_add(conn, rec->last_connect+reconnect_time);
119         server_connect_unref(conn);
120 }
121
122 static SERVER_CONNECT_REC *
123 server_connect_copy_skeleton(SERVER_CONNECT_REC *src, int connect_info)
124 {
125         SERVER_CONNECT_REC *dest;
126
127         dest = NULL;
128         signal_emit("server connect copy", 2, &dest, src);
129         g_return_val_if_fail(dest != NULL, NULL);
130
131         server_connect_ref(dest);
132         dest->type = module_get_uniq_id("SERVER CONNECT", 0);
133         dest->reconnection = src->reconnection;
134         dest->proxy = g_strdup(src->proxy);
135         dest->proxy_port = src->proxy_port;
136         dest->proxy_string = g_strdup(src->proxy_string);
137         dest->proxy_string_after = g_strdup(src->proxy_string_after);
138         dest->proxy_password = g_strdup(src->proxy_password);
139
140         dest->tag = g_strdup(src->tag);
141
142         if (connect_info) {
143                 dest->family = src->family;
144                 dest->address = g_strdup(src->address);
145                 dest->port = src->port;
146                 dest->password = g_strdup(src->password);
147         }
148
149         dest->chatnet = g_strdup(src->chatnet);
150         dest->nick = g_strdup(src->nick);
151         dest->username = g_strdup(src->username);
152         dest->realname = g_strdup(src->realname);
153
154         if (src->own_ip4 != NULL) {
155                 dest->own_ip4 = g_new(IPADDR, 1);
156                 memcpy(dest->own_ip4, src->own_ip4, sizeof(IPADDR));
157         }
158         if (src->own_ip6 != NULL) {
159                 dest->own_ip6 = g_new(IPADDR, 1);
160                 memcpy(dest->own_ip6, src->own_ip6, sizeof(IPADDR));
161         }
162
163         dest->channels = g_strdup(src->channels);
164         dest->away_reason = g_strdup(src->away_reason);
165
166         return dest;
167 }
168
169 #define server_should_reconnect(server) \
170         ((server)->connection_lost && !(server)->no_reconnect && \
171         ((server)->connrec->chatnet != NULL || \
172                 (!(server)->banned && !(server)->dns_error)))
173
174 #define sserver_connect_ok(rec, net) \
175         (!(rec)->banned && !(rec)->dns_error && (rec)->chatnet != NULL && \
176         g_strcasecmp((rec)->chatnet, (net)) == 0)
177
178 static void sig_reconnect(SERVER_REC *server)
179 {
180         SERVER_CONNECT_REC *conn;
181         SERVER_SETUP_REC *sserver;
182         GSList *tmp;
183         int use_next, through;
184         time_t now;
185
186         g_return_if_fail(IS_SERVER(server));
187
188         if (reconnect_time == -1 || !server_should_reconnect(server))
189                 return;
190
191         conn = server_connect_copy_skeleton(server->connrec, FALSE);
192         g_return_if_fail(conn != NULL);
193
194         /* save the server status */
195         if (server->connected) {
196                 conn->reconnection = TRUE;
197
198                 reconnect_save_status(conn, server);
199         }
200
201         sserver = server_setup_find(server->connrec->address,
202                                     server->connrec->port,
203                                     server->connrec->chatnet);
204
205         if (sserver != NULL) {
206                 /* save the last connection time/status */
207                 sserver->last_connect = server->connect_time == 0 ?
208                         time(NULL) : server->connect_time;
209                 sserver->last_failed = !server->connected;
210                 if (server->banned) sserver->banned = TRUE;
211                 if (server->dns_error) sserver->dns_error = TRUE;
212         }
213
214         if (sserver == NULL || conn->chatnet == NULL) {
215                 /* not in any chatnet, just reconnect back to same server */
216                 conn->family = server->connrec->family;
217                 conn->address = g_strdup(server->connrec->address);
218                 conn->port = server->connrec->port;
219                 conn->password = g_strdup(server->connrec->password);
220
221                 server_reconnect_add(conn, (server->connect_time == 0 ? time(NULL) :
222                                             server->connect_time) + reconnect_time);
223                 server_connect_unref(conn);
224                 return;
225         }
226
227         /* always try to first connect to the first on the list where we
228            haven't got unsuccessful connection attempts for the past half
229            an hour. */
230
231         now = time(NULL);
232         for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
233                 SERVER_SETUP_REC *rec = tmp->data;
234
235                 if (sserver_connect_ok(rec, conn->chatnet) &&
236                     (!rec->last_connect || !rec->last_failed ||
237                      rec->last_connect < now-FAILED_RECONNECT_WAIT)) {
238                         if (rec == sserver)
239                                 conn->port = server->connrec->port;
240                         sserver_connect(rec, conn);
241                         return;
242                 }
243         }
244
245         /* just try the next server in list */
246         use_next = through = FALSE;
247         for (tmp = setupservers; tmp != NULL; ) {
248                 SERVER_SETUP_REC *rec = tmp->data;
249
250                 if (!use_next && server->connrec->port == rec->port &&
251                     g_strcasecmp(rec->address, server->connrec->address) == 0)
252                         use_next = TRUE;
253                 else if (use_next && sserver_connect_ok(rec, conn->chatnet)) {
254                         if (rec == sserver)
255                                 conn->port = server->connrec->port;
256                         sserver_connect(rec, conn);
257                         break;
258                 }
259
260                 if (tmp->next != NULL) {
261                         tmp = tmp->next;
262                         continue;
263                 }
264
265                 if (through) {
266                         /* shouldn't happen unless there's no servers in
267                            this chatnet in setup.. */
268                         server_connect_unref(conn);
269                         break;
270                 }
271
272                 tmp = setupservers;
273                 use_next = through = TRUE;
274         }
275 }
276
277 static void sig_connected(SERVER_REC *server)
278 {
279         g_return_if_fail(IS_SERVER(server));
280         if (!server->connrec->reconnection)
281                 return;
282
283         if (server->connrec->channels != NULL)
284                 server->channels_join(server, server->connrec->channels, TRUE);
285 }
286
287 /* Remove all servers from reconnect list */
288 /* SYNTAX: RMRECONNS */
289 static void cmd_rmreconns(void)
290 {
291         while (reconnects != NULL)
292                 server_reconnect_destroy(reconnects->data);
293 }
294
295 static RECONNECT_REC *reconnect_find_tag(int tag)
296 {
297         GSList *tmp;
298
299         for (tmp = reconnects; tmp != NULL; tmp = tmp->next) {
300                 RECONNECT_REC *rec = tmp->data;
301
302                 if (rec->tag == tag)
303                         return rec;
304         }
305
306         return NULL;
307 }
308
309 static void reconnect_all(void)
310 {
311         GSList *list;
312         SERVER_CONNECT_REC *conn;
313         RECONNECT_REC *rec;
314
315         /* first move reconnects to another list so if server_connect()
316            fails and goes to reconnection list again, we won't get stuck
317            here forever */
318         list = NULL;
319         while (reconnects != NULL) {
320                 rec = reconnects->data;
321
322                 list = g_slist_append(list, rec->conn);
323                 server_connect_ref(rec->conn);
324                 server_reconnect_destroy(rec);
325         }
326
327
328         while (list != NULL) {
329                 conn = list->data;
330
331                 CHAT_PROTOCOL(conn)->server_connect(conn);
332                 server_connect_unref(conn);
333                 list = g_slist_remove(list, conn);
334         }
335 }
336
337 /* SYNTAX: RECONNECT <tag> */
338 static void cmd_reconnect(const char *data, SERVER_REC *server)
339 {
340         SERVER_CONNECT_REC *conn;
341         RECONNECT_REC *rec;
342         int tag;
343
344         if (*data == '\0' && server != NULL) {
345                 /* reconnect back to same server */
346                 conn = server_connect_copy_skeleton(server->connrec, TRUE);
347
348                 if (server->connected)
349                         reconnect_save_status(conn, server);
350                 signal_emit("command disconnect", 2, "* Reconnecting", server);
351
352                 conn->reconnection = TRUE;
353                 CHAT_PROTOCOL(conn)->server_connect(conn);
354                 server_connect_unref(conn);
355                 return;
356         }
357
358         if (g_strcasecmp(data, "all") == 0) {
359                 /* reconnect all servers in reconnect queue */
360                 reconnect_all();
361                 return;
362         }
363
364         if (*data == '\0') {
365                 /* reconnect to first server in reconnection list */
366                 if (reconnects == NULL)
367                         cmd_return_error(CMDERR_NOT_CONNECTED);
368                 rec = reconnects->data;
369         } else {
370                 if (g_strncasecmp(data, "RECON-", 6) == 0)
371                         data += 6;
372
373                 tag = atoi(data);
374                 rec = tag <= 0 ? NULL : reconnect_find_tag(tag);
375
376                 if (rec == NULL) {
377                         signal_emit("server reconnect not found", 1, data);
378                         return;
379                 }
380         }
381
382         conn = rec->conn;
383         server_connect_ref(conn);
384         server_reconnect_destroy(rec);
385         CHAT_PROTOCOL(conn)->server_connect(conn);
386         server_connect_unref(conn);
387 }
388
389 static void cmd_disconnect(const char *data, SERVER_REC *server)
390 {
391         RECONNECT_REC *rec;
392
393         if (g_strncasecmp(data, "RECON-", 6) != 0)
394                 return; /* handle only reconnection removing */
395
396         rec = reconnect_find_tag(atoi(data+6));
397
398         if (rec == NULL)
399                 signal_emit("server reconnect not found", 1, data);
400         else
401                 server_reconnect_destroy(rec);
402         signal_stop();
403 }
404
405 static void sig_chat_protocol_deinit(CHAT_PROTOCOL_REC *proto)
406 {
407         GSList *tmp, *next;
408
409         for (tmp = reconnects; tmp != NULL; tmp = next) {
410                 RECONNECT_REC *rec = tmp->data;
411
412                 next = tmp->next;
413                 if (rec->conn->chat_type == proto->id)
414                         server_reconnect_destroy(rec);
415         }
416 }
417
418 static void read_settings(void)
419 {
420         reconnect_time = settings_get_int("server_reconnect_time");
421 }
422
423 void servers_reconnect_init(void)
424 {
425         settings_add_int("server", "server_reconnect_time", 300);
426
427         reconnects = NULL;
428         last_reconnect_tag = 0;
429
430         reconnect_timeout_tag = g_timeout_add(1000, (GSourceFunc) server_reconnect_timeout, NULL);
431         read_settings();
432
433         signal_add("server connect failed", (SIGNAL_FUNC) sig_reconnect);
434         signal_add("server disconnected", (SIGNAL_FUNC) sig_reconnect);
435         signal_add("event connected", (SIGNAL_FUNC) sig_connected);
436         signal_add("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
437         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
438
439         command_bind("rmreconns", NULL, (SIGNAL_FUNC) cmd_rmreconns);
440         command_bind("reconnect", NULL, (SIGNAL_FUNC) cmd_reconnect);
441         command_bind_first("disconnect", NULL, (SIGNAL_FUNC) cmd_disconnect);
442 }
443
444 void servers_reconnect_deinit(void)
445 {
446         g_source_remove(reconnect_timeout_tag);
447
448         signal_remove("server connect failed", (SIGNAL_FUNC) sig_reconnect);
449         signal_remove("server disconnected", (SIGNAL_FUNC) sig_reconnect);
450         signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
451         signal_remove("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
452         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
453
454         command_unbind("rmreconns", (SIGNAL_FUNC) cmd_rmreconns);
455         command_unbind("reconnect", (SIGNAL_FUNC) cmd_reconnect);
456         command_unbind("disconnect", (SIGNAL_FUNC) cmd_disconnect);
457 }