2000-04-26 04:03:38 -04:00
|
|
|
/*
|
2000-08-26 11:39:44 -04:00
|
|
|
servers-reconnect.c : irssi
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
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 "commands.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "signals.h"
|
|
|
|
|
2001-02-18 21:15:15 -05:00
|
|
|
#include "chat-protocols.h"
|
2001-01-01 02:45:54 -05:00
|
|
|
#include "servers.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers-setup.h"
|
|
|
|
#include "servers-reconnect.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
GSList *reconnects;
|
|
|
|
static int last_reconnect_tag;
|
|
|
|
static int reconnect_timeout_tag;
|
|
|
|
static int reconnect_time;
|
2002-11-19 06:55:36 -05:00
|
|
|
static int connect_timeout;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-18 21:15:15 -05:00
|
|
|
void reconnect_save_status(SERVER_CONNECT_REC *conn, SERVER_REC *server)
|
|
|
|
{
|
2001-06-08 17:19:08 -04:00
|
|
|
g_free_not_null(conn->tag);
|
|
|
|
conn->tag = g_strdup(server->tag);
|
|
|
|
|
2001-02-18 21:15:15 -05:00
|
|
|
g_free_not_null(conn->away_reason);
|
|
|
|
conn->away_reason = !server->usermode_away ? NULL :
|
|
|
|
g_strdup(server->away_reason);
|
|
|
|
|
2002-09-30 17:43:46 -04:00
|
|
|
if (!server->connected) {
|
|
|
|
/* default to channels/usermode from connect record
|
|
|
|
since server isn't fully connected yet */
|
|
|
|
g_free_not_null(conn->channels);
|
|
|
|
conn->channels = server->connrec->no_autojoin_channels ? NULL :
|
|
|
|
g_strdup(server->connrec->channels);
|
|
|
|
|
|
|
|
g_free_not_null(conn->channels);
|
|
|
|
conn->channels = g_strdup(server->connrec->channels);
|
|
|
|
}
|
|
|
|
|
2001-02-18 21:15:15 -05:00
|
|
|
signal_emit("server reconnect save status", 2, conn, server);
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void server_reconnect_add(SERVER_CONNECT_REC *conn,
|
|
|
|
time_t next_connect)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
RECONNECT_REC *rec;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_if_fail(IS_SERVER_CONNECT(conn));
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
rec = g_new(RECONNECT_REC, 1);
|
|
|
|
rec->tag = ++last_reconnect_tag;
|
|
|
|
rec->next_connect = next_connect;
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
rec->conn = conn;
|
|
|
|
server_connect_ref(conn);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
reconnects = g_slist_append(reconnects, rec);
|
|
|
|
}
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
void server_reconnect_destroy(RECONNECT_REC *rec)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
reconnects = g_slist_remove(reconnects, rec);
|
|
|
|
|
|
|
|
signal_emit("server reconnect remove", 1, rec);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(rec->conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(rec);
|
|
|
|
|
|
|
|
if (reconnects == NULL)
|
|
|
|
last_reconnect_tag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int server_reconnect_timeout(void)
|
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_CONNECT_REC *conn;
|
2002-11-19 06:55:36 -05:00
|
|
|
GSList *list, *tmp, *next;
|
2000-04-26 04:03:38 -04:00
|
|
|
time_t now;
|
|
|
|
|
2002-11-19 06:55:36 -05:00
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
/* timeout any connections that haven't gotten to connected-stage */
|
|
|
|
for (tmp = servers; tmp != NULL; tmp = next) {
|
|
|
|
SERVER_REC *server = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (!server->connected &&
|
|
|
|
server->connect_time + connect_timeout < now &&
|
|
|
|
connect_timeout > 0) {
|
|
|
|
server->connection_lost = TRUE;
|
|
|
|
server_disconnect(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
/* If server_connect() removes the next reconnection in queue,
|
2000-08-14 17:42:37 -04:00
|
|
|
we're screwed. I don't think this should happen anymore, but just
|
|
|
|
to be sure we don't crash, do this safely. */
|
|
|
|
list = g_slist_copy(reconnects);
|
|
|
|
for (tmp = list; tmp != NULL; tmp = tmp->next) {
|
2000-04-26 04:03:38 -04:00
|
|
|
RECONNECT_REC *rec = tmp->data;
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
if (g_slist_find(reconnects, rec) == NULL)
|
|
|
|
continue;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (rec->next_connect <= now) {
|
|
|
|
conn = rec->conn;
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_ref(conn);
|
|
|
|
server_reconnect_destroy(rec);
|
2002-05-20 00:12:12 -04:00
|
|
|
server_connect(conn);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
g_slist_free(list);
|
2000-04-26 04:03:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void sserver_connect(SERVER_SETUP_REC *rec, SERVER_CONNECT_REC *conn)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-03-14 17:36:04 -05:00
|
|
|
conn->family = rec->family;
|
2000-05-04 06:32:42 -04:00
|
|
|
conn->address = g_strdup(rec->address);
|
2000-11-25 21:09:15 -05:00
|
|
|
if (conn->port == 0) conn->port = rec->port;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
server_setup_fill_reconn(conn, rec);
|
2002-01-22 16:04:04 -05:00
|
|
|
server_reconnect_add(conn, rec->last_connect+reconnect_time);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static SERVER_CONNECT_REC *
|
2001-02-18 21:27:10 -05:00
|
|
|
server_connect_copy_skeleton(SERVER_CONNECT_REC *src, int connect_info)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_CONNECT_REC *dest;
|
|
|
|
|
|
|
|
dest = NULL;
|
|
|
|
signal_emit("server connect copy", 2, &dest, src);
|
|
|
|
g_return_val_if_fail(dest != NULL, NULL);
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_ref(dest);
|
2000-09-02 14:53:58 -04:00
|
|
|
dest->type = module_get_uniq_id("SERVER CONNECT", 0);
|
2000-10-13 21:43:05 -04:00
|
|
|
dest->reconnection = src->reconnection;
|
2000-06-14 13:50:16 -04:00
|
|
|
dest->proxy = g_strdup(src->proxy);
|
2000-04-26 04:03:38 -04:00
|
|
|
dest->proxy_port = src->proxy_port;
|
2000-06-14 13:50:16 -04:00
|
|
|
dest->proxy_string = g_strdup(src->proxy_string);
|
2002-01-22 15:29:45 -05:00
|
|
|
dest->proxy_string_after = g_strdup(src->proxy_string_after);
|
2001-03-20 18:24:45 -05:00
|
|
|
dest->proxy_password = g_strdup(src->proxy_password);
|
2000-06-14 13:50:16 -04:00
|
|
|
|
2001-06-08 17:19:08 -04:00
|
|
|
dest->tag = g_strdup(src->tag);
|
|
|
|
|
2001-02-18 21:27:10 -05:00
|
|
|
if (connect_info) {
|
2001-03-14 17:36:04 -05:00
|
|
|
dest->family = src->family;
|
2001-02-18 21:27:10 -05:00
|
|
|
dest->address = g_strdup(src->address);
|
|
|
|
dest->port = src->port;
|
|
|
|
dest->password = g_strdup(src->password);
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
dest->chatnet = g_strdup(src->chatnet);
|
2000-06-14 13:50:16 -04:00
|
|
|
dest->nick = g_strdup(src->nick);
|
|
|
|
dest->username = g_strdup(src->username);
|
|
|
|
dest->realname = g_strdup(src->realname);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
if (src->own_ip4 != NULL) {
|
|
|
|
dest->own_ip4 = g_new(IPADDR, 1);
|
|
|
|
memcpy(dest->own_ip4, src->own_ip4, sizeof(IPADDR));
|
|
|
|
}
|
|
|
|
if (src->own_ip6 != NULL) {
|
|
|
|
dest->own_ip6 = g_new(IPADDR, 1);
|
|
|
|
memcpy(dest->own_ip6, src->own_ip6, sizeof(IPADDR));
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
dest->channels = g_strdup(src->channels);
|
|
|
|
dest->away_reason = g_strdup(src->away_reason);
|
2002-04-03 09:08:52 -05:00
|
|
|
dest->no_autojoin_channels = src->no_autojoin_channels;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2002-08-26 15:32:15 -04:00
|
|
|
dest->use_ssl = src->use_ssl;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
return dest;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-11 15:34:29 -04:00
|
|
|
#define server_should_reconnect(server) \
|
2001-12-06 18:05:54 -05:00
|
|
|
((server)->connection_lost && !(server)->no_reconnect && \
|
2001-12-01 15:23:48 -05:00
|
|
|
((server)->connrec->chatnet != NULL || \
|
|
|
|
(!(server)->banned && !(server)->dns_error)))
|
2001-03-07 18:57:19 -05:00
|
|
|
|
|
|
|
#define sserver_connect_ok(rec, net) \
|
|
|
|
(!(rec)->banned && !(rec)->dns_error && (rec)->chatnet != NULL && \
|
|
|
|
g_strcasecmp((rec)->chatnet, (net)) == 0)
|
2000-06-11 15:34:29 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static void sig_reconnect(SERVER_REC *server)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_CONNECT_REC *conn;
|
|
|
|
SERVER_SETUP_REC *sserver;
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *tmp;
|
2001-03-07 18:57:19 -05:00
|
|
|
int use_next, through;
|
2000-04-26 04:03:38 -04:00
|
|
|
time_t now;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_if_fail(IS_SERVER(server));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-06-11 15:34:29 -04:00
|
|
|
if (reconnect_time == -1 || !server_should_reconnect(server))
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
|
2001-02-18 21:27:10 -05:00
|
|
|
conn = server_connect_copy_skeleton(server->connrec, FALSE);
|
2000-09-27 19:47:51 -04:00
|
|
|
g_return_if_fail(conn != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* save the server status */
|
2000-08-26 11:39:44 -04:00
|
|
|
if (server->connected) {
|
2000-05-04 06:32:42 -04:00
|
|
|
conn->reconnection = TRUE;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2001-02-18 21:15:15 -05:00
|
|
|
reconnect_save_status(conn, server);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
sserver = server_setup_find(server->connrec->address,
|
2001-06-27 17:47:31 -04:00
|
|
|
server->connrec->port,
|
|
|
|
server->connrec->chatnet);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (sserver != NULL) {
|
|
|
|
/* save the last connection time/status */
|
|
|
|
sserver->last_connect = server->connect_time == 0 ?
|
|
|
|
time(NULL) : server->connect_time;
|
|
|
|
sserver->last_failed = !server->connected;
|
2002-09-07 13:13:00 -04:00
|
|
|
sserver->banned = server->banned;
|
|
|
|
sserver->dns_error = server->dns_error;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
if (sserver == NULL || conn->chatnet == NULL) {
|
|
|
|
/* not in any chatnet, just reconnect back to same server */
|
2001-03-14 17:36:04 -05:00
|
|
|
conn->family = server->connrec->family;
|
2000-04-26 04:03:38 -04:00
|
|
|
conn->address = g_strdup(server->connrec->address);
|
|
|
|
conn->port = server->connrec->port;
|
2001-02-18 21:27:10 -05:00
|
|
|
conn->password = g_strdup(server->connrec->password);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-01-22 16:04:04 -05:00
|
|
|
server_reconnect_add(conn, (server->connect_time == 0 ? time(NULL) :
|
|
|
|
server->connect_time) + reconnect_time);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* always try to first connect to the first on the list where we
|
2001-06-08 17:19:08 -04:00
|
|
|
haven't got unsuccessful connection attempts for the past half
|
2000-04-26 04:03:38 -04:00
|
|
|
an hour. */
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_SETUP_REC *rec = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-07 18:57:19 -05:00
|
|
|
if (sserver_connect_ok(rec, conn->chatnet) &&
|
|
|
|
(!rec->last_connect || !rec->last_failed ||
|
|
|
|
rec->last_connect < now-FAILED_RECONNECT_WAIT)) {
|
2000-11-25 21:09:15 -05:00
|
|
|
if (rec == sserver)
|
|
|
|
conn->port = server->connrec->port;
|
2000-04-26 04:03:38 -04:00
|
|
|
sserver_connect(rec, conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* just try the next server in list */
|
2001-03-07 18:57:19 -05:00
|
|
|
use_next = through = FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
for (tmp = setupservers; tmp != NULL; ) {
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_SETUP_REC *rec = tmp->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-07 18:57:19 -05:00
|
|
|
if (!use_next && server->connrec->port == rec->port &&
|
|
|
|
g_strcasecmp(rec->address, server->connrec->address) == 0)
|
|
|
|
use_next = TRUE;
|
|
|
|
else if (use_next && sserver_connect_ok(rec, conn->chatnet)) {
|
2000-11-25 21:09:15 -05:00
|
|
|
if (rec == sserver)
|
|
|
|
conn->port = server->connrec->port;
|
2000-04-26 04:03:38 -04:00
|
|
|
sserver_connect(rec, conn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->next != NULL) {
|
|
|
|
tmp = tmp->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (through) {
|
|
|
|
/* shouldn't happen unless there's no servers in
|
2000-08-26 11:39:44 -04:00
|
|
|
this chatnet in setup.. */
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = setupservers;
|
2001-03-07 18:57:19 -05:00
|
|
|
use_next = through = TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-30 18:49:48 -04:00
|
|
|
static void sig_connected(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail(IS_SERVER(server));
|
|
|
|
if (!server->connrec->reconnection)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (server->connrec->channels != NULL)
|
|
|
|
server->channels_join(server, server->connrec->channels, TRUE);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* Remove all servers from reconnect list */
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: RMRECONNS */
|
2000-04-26 04:03:38 -04:00
|
|
|
static void cmd_rmreconns(void)
|
|
|
|
{
|
|
|
|
while (reconnects != NULL)
|
2001-10-21 09:59:07 -04:00
|
|
|
server_reconnect_destroy(reconnects->data);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static RECONNECT_REC *reconnect_find_tag(int tag)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
for (tmp = reconnects; tmp != NULL; tmp = tmp->next) {
|
|
|
|
RECONNECT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->tag == tag)
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-02-18 23:50:58 -05:00
|
|
|
static void reconnect_all(void)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
SERVER_CONNECT_REC *conn;
|
|
|
|
RECONNECT_REC *rec;
|
|
|
|
|
|
|
|
/* first move reconnects to another list so if server_connect()
|
|
|
|
fails and goes to reconnection list again, we won't get stuck
|
|
|
|
here forever */
|
|
|
|
list = NULL;
|
|
|
|
while (reconnects != NULL) {
|
|
|
|
rec = reconnects->data;
|
|
|
|
|
|
|
|
list = g_slist_append(list, rec->conn);
|
2001-11-03 07:58:36 -05:00
|
|
|
server_connect_ref(rec->conn);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_reconnect_destroy(rec);
|
2001-02-18 23:50:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (list != NULL) {
|
|
|
|
conn = list->data;
|
|
|
|
|
2002-05-20 00:12:12 -04:00
|
|
|
server_connect(conn);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2001-02-18 23:50:58 -05:00
|
|
|
list = g_slist_remove(list, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-14 11:58:47 -04:00
|
|
|
/* SYNTAX: RECONNECT <tag> [<quit message>] */
|
2000-08-26 11:39:44 -04:00
|
|
|
static void cmd_reconnect(const char *data, SERVER_REC *server)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_CONNECT_REC *conn;
|
2000-04-26 04:03:38 -04:00
|
|
|
RECONNECT_REC *rec;
|
2002-10-14 11:58:22 -04:00
|
|
|
char *tag, *msg;
|
|
|
|
void *free_arg;
|
|
|
|
int tagnum;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-10-14 11:58:22 -04:00
|
|
|
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &tag, &msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (*tag != '\0' && strcmp(tag, "*") != 0)
|
|
|
|
server = server_find_tag(tag);
|
|
|
|
|
|
|
|
if (server != NULL) {
|
|
|
|
/* reconnect connected server */
|
2001-02-18 21:27:10 -05:00
|
|
|
conn = server_connect_copy_skeleton(server->connrec, TRUE);
|
|
|
|
|
2001-11-25 10:05:38 -05:00
|
|
|
if (server->connected)
|
2001-02-18 21:27:10 -05:00
|
|
|
reconnect_save_status(conn, server);
|
2002-10-14 11:58:22 -04:00
|
|
|
|
|
|
|
msg = g_strconcat("* ", *msg == '\0' ?
|
|
|
|
"Reconnecting" : msg, NULL);
|
|
|
|
signal_emit("command disconnect", 2, msg, server);
|
|
|
|
g_free(msg);
|
2001-02-18 21:27:10 -05:00
|
|
|
|
|
|
|
conn->reconnection = TRUE;
|
2002-05-20 00:12:12 -04:00
|
|
|
server_connect(conn);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(conn);
|
2002-10-14 11:58:22 -04:00
|
|
|
cmd_params_free(free_arg);
|
2001-02-18 21:27:10 -05:00
|
|
|
return;
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
|
|
|
|
2002-10-14 11:58:22 -04:00
|
|
|
if (g_strcasecmp(tag, "all") == 0) {
|
2001-02-18 23:50:58 -05:00
|
|
|
/* reconnect all servers in reconnect queue */
|
|
|
|
reconnect_all();
|
2002-10-14 11:58:22 -04:00
|
|
|
cmd_params_free(free_arg);
|
2001-02-18 23:50:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-02 23:31:17 -05:00
|
|
|
if (*data == '\0') {
|
|
|
|
/* reconnect to first server in reconnection list */
|
|
|
|
if (reconnects == NULL)
|
|
|
|
cmd_return_error(CMDERR_NOT_CONNECTED);
|
|
|
|
rec = reconnects->data;
|
|
|
|
} else {
|
|
|
|
if (g_strncasecmp(data, "RECON-", 6) == 0)
|
|
|
|
data += 6;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-10-14 11:58:22 -04:00
|
|
|
tagnum = atoi(tag);
|
|
|
|
rec = tagnum <= 0 ? NULL : reconnect_find_tag(tagnum);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-10-14 11:58:22 -04:00
|
|
|
if (rec == NULL) {
|
|
|
|
signal_emit("server reconnect not found", 1, data);
|
|
|
|
} else {
|
|
|
|
conn = rec->conn;
|
|
|
|
server_connect_ref(conn);
|
|
|
|
server_reconnect_destroy(rec);
|
|
|
|
server_connect(conn);
|
|
|
|
server_connect_unref(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2000-12-02 23:31:17 -05:00
|
|
|
|
2002-10-14 11:58:22 -04:00
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_disconnect(const char *data, SERVER_REC *server)
|
|
|
|
{
|
|
|
|
RECONNECT_REC *rec;
|
|
|
|
|
|
|
|
if (g_strncasecmp(data, "RECON-", 6) != 0)
|
|
|
|
return; /* handle only reconnection removing */
|
|
|
|
|
2001-06-08 17:19:08 -04:00
|
|
|
rec = reconnect_find_tag(atoi(data+6));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (rec == NULL)
|
|
|
|
signal_emit("server reconnect not found", 1, data);
|
|
|
|
else
|
2001-10-21 09:59:07 -04:00
|
|
|
server_reconnect_destroy(rec);
|
2000-08-26 11:39:44 -04:00
|
|
|
signal_stop();
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-03-03 22:25:21 -05:00
|
|
|
static void sig_chat_protocol_deinit(CHAT_PROTOCOL_REC *proto)
|
|
|
|
{
|
|
|
|
GSList *tmp, *next;
|
|
|
|
|
|
|
|
for (tmp = reconnects; tmp != NULL; tmp = next) {
|
|
|
|
RECONNECT_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (rec->conn->chat_type == proto->id)
|
2001-10-21 09:59:07 -04:00
|
|
|
server_reconnect_destroy(rec);
|
2001-03-03 22:25:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void read_settings(void)
|
|
|
|
{
|
|
|
|
reconnect_time = settings_get_int("server_reconnect_time");
|
2002-11-19 06:55:36 -05:00
|
|
|
connect_timeout = settings_get_int("server_connect_timeout");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void servers_reconnect_init(void)
|
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
settings_add_int("server", "server_reconnect_time", 300);
|
2002-11-19 06:55:36 -05:00
|
|
|
settings_add_int("server", "server_connect_timeout", 300);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
reconnects = NULL;
|
|
|
|
last_reconnect_tag = 0;
|
|
|
|
|
|
|
|
reconnect_timeout_tag = g_timeout_add(1000, (GSourceFunc) server_reconnect_timeout, NULL);
|
|
|
|
read_settings();
|
|
|
|
|
|
|
|
signal_add("server connect failed", (SIGNAL_FUNC) sig_reconnect);
|
|
|
|
signal_add("server disconnected", (SIGNAL_FUNC) sig_reconnect);
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_add("event connected", (SIGNAL_FUNC) sig_connected);
|
2001-03-03 22:25:21 -05:00
|
|
|
signal_add("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
2001-03-03 22:25:21 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_bind("rmreconns", NULL, (SIGNAL_FUNC) cmd_rmreconns);
|
|
|
|
command_bind("reconnect", NULL, (SIGNAL_FUNC) cmd_reconnect);
|
2000-08-26 11:39:44 -04:00
|
|
|
command_bind_first("disconnect", NULL, (SIGNAL_FUNC) cmd_disconnect);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void servers_reconnect_deinit(void)
|
|
|
|
{
|
|
|
|
g_source_remove(reconnect_timeout_tag);
|
|
|
|
|
|
|
|
signal_remove("server connect failed", (SIGNAL_FUNC) sig_reconnect);
|
|
|
|
signal_remove("server disconnected", (SIGNAL_FUNC) sig_reconnect);
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
|
2001-03-03 22:25:21 -05:00
|
|
|
signal_remove("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
|
2000-09-30 18:49:48 -04:00
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
2001-03-03 22:25:21 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
command_unbind("rmreconns", (SIGNAL_FUNC) cmd_rmreconns);
|
|
|
|
command_unbind("reconnect", (SIGNAL_FUNC) cmd_reconnect);
|
|
|
|
command_unbind("disconnect", (SIGNAL_FUNC) cmd_disconnect);
|
|
|
|
}
|