2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
server-reconnect.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 "modules.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "signals.h"
|
|
|
|
|
|
|
|
#include "irc.h"
|
|
|
|
#include "modes.h"
|
|
|
|
#include "irc-server.h"
|
|
|
|
#include "server-setup.h"
|
|
|
|
#include "server-reconnect.h"
|
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
GSList *reconnects;
|
|
|
|
static int last_reconnect_tag;
|
|
|
|
static int reconnect_timeout_tag;
|
|
|
|
static int reconnect_time;
|
|
|
|
|
|
|
|
static void server_reconnect_add(IRC_SERVER_CONNECT_REC *conn, time_t next_connect)
|
|
|
|
{
|
|
|
|
RECONNECT_REC *rec;
|
|
|
|
|
|
|
|
rec = g_new(RECONNECT_REC, 1);
|
|
|
|
rec->tag = ++last_reconnect_tag;
|
|
|
|
rec->conn = conn;
|
|
|
|
rec->next_connect = next_connect;
|
|
|
|
|
|
|
|
reconnects = g_slist_append(reconnects, rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_reconnect_destroy(RECONNECT_REC *rec, int free_conn)
|
|
|
|
{
|
|
|
|
reconnects = g_slist_remove(reconnects, rec);
|
|
|
|
|
|
|
|
signal_emit("server reconnect remove", 1, rec);
|
|
|
|
if (free_conn) irc_server_connect_free(rec->conn);
|
|
|
|
g_free(rec);
|
|
|
|
|
|
|
|
if (reconnects == NULL)
|
|
|
|
last_reconnect_tag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int server_reconnect_timeout(void)
|
|
|
|
{
|
|
|
|
IRC_SERVER_CONNECT_REC *conn;
|
2000-08-14 17:42:37 -04:00
|
|
|
GSList *list, *tmp;
|
2000-04-26 04:03:38 -04:00
|
|
|
time_t now;
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
/* If irc_server_connect() removes the next reconnection in queue,
|
|
|
|
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);
|
2000-04-26 04:03:38 -04:00
|
|
|
now = time(NULL);
|
2000-08-14 17:42:37 -04:00
|
|
|
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;
|
|
|
|
server_reconnect_destroy(rec, FALSE);
|
|
|
|
irc_server_connect(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
g_slist_free(list);
|
2000-04-26 04:03:38 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sserver_connect(SETUP_SERVER_REC *rec, IRC_SERVER_CONNECT_REC *conn)
|
|
|
|
{
|
2000-05-04 06:32:42 -04:00
|
|
|
conn->address = g_strdup(rec->address);
|
2000-04-26 04:03:38 -04:00
|
|
|
conn->port = rec->port;
|
|
|
|
|
2000-06-14 13:50:16 -04:00
|
|
|
server_setup_fill_conn(conn, rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (rec->last_connect > time(NULL)-reconnect_time) {
|
|
|
|
/* can't reconnect this fast, wait.. */
|
|
|
|
server_reconnect_add(conn, rec->last_connect+reconnect_time);
|
|
|
|
} else {
|
|
|
|
/* connect to server.. */
|
|
|
|
irc_server_connect(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_connect_copy_skeleton(IRC_SERVER_CONNECT_REC *dest, IRC_SERVER_CONNECT_REC *src)
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
dest->ircnet = g_strdup(src->ircnet);
|
|
|
|
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
|
|
|
|
|
|
|
if (src->own_ip != NULL) {
|
|
|
|
dest->own_ip = g_new(IPADDR, 1);
|
|
|
|
memcpy(dest->own_ip, src->own_ip, sizeof(IPADDR));
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->cmd_queue_speed = src->cmd_queue_speed;
|
|
|
|
dest->max_kicks = src->max_kicks;
|
|
|
|
dest->max_modes = src->max_modes;
|
|
|
|
dest->max_msgs = src->max_msgs;
|
|
|
|
}
|
|
|
|
|
2000-06-11 15:34:29 -04:00
|
|
|
#define server_should_reconnect(server) \
|
|
|
|
(irc_server_check(server) && (server)->connection_lost && \
|
|
|
|
((server)->connrec->ircnet != NULL || !(server)->banned))
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void sig_reconnect(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
IRC_SERVER_CONNECT_REC *conn;
|
|
|
|
SETUP_SERVER_REC *sserver;
|
|
|
|
GSList *tmp;
|
|
|
|
int found, through;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
conn = g_new0(IRC_SERVER_CONNECT_REC, 1);
|
|
|
|
server_connect_copy_skeleton(conn, server->connrec);
|
|
|
|
|
|
|
|
/* save the server status */
|
|
|
|
if (!server->connected) {
|
|
|
|
conn->channels = g_strdup(server->connrec->channels);
|
|
|
|
conn->away_reason = g_strdup(server->connrec->away_reason);
|
|
|
|
conn->usermode = g_strdup(server->connrec->usermode);
|
|
|
|
} else {
|
2000-05-04 06:32:42 -04:00
|
|
|
conn->reconnection = TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
conn->channels = irc_server_get_channels(server);
|
|
|
|
conn->away_reason = !server->usermode_away ? NULL :
|
|
|
|
g_strdup(server->away_reason);
|
|
|
|
conn->usermode = g_strdup(server->usermode);
|
|
|
|
}
|
|
|
|
|
2000-08-14 17:42:37 -04:00
|
|
|
sserver = server_setup_find(server->connrec->address,
|
|
|
|
server->connrec->port);
|
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;
|
2000-06-11 15:34:29 -04:00
|
|
|
if (server->banned) sserver->banned = TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sserver == NULL || conn->ircnet == NULL) {
|
|
|
|
/* not in any ircnet, just reconnect back to same server */
|
|
|
|
conn->address = g_strdup(server->connrec->address);
|
|
|
|
conn->port = server->connrec->port;
|
|
|
|
conn->password = server->connrec->password == NULL ? NULL :
|
|
|
|
g_strdup(server->connrec->password);
|
|
|
|
|
|
|
|
if (server->connect_time != 0 &&
|
|
|
|
time(NULL)-server->connect_time > reconnect_time) {
|
|
|
|
/* there's been enough time since last connection,
|
|
|
|
reconnect back immediately */
|
|
|
|
irc_server_connect(conn);
|
|
|
|
} else {
|
|
|
|
/* reconnect later.. */
|
|
|
|
server_reconnect_add(conn, (server->connect_time == 0 ? time(NULL) :
|
|
|
|
server->connect_time) + reconnect_time);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* always try to first connect to the first on the list where we
|
|
|
|
haven't got unsuccessful connection attempts for the last half
|
|
|
|
an hour. */
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SETUP_SERVER_REC *rec = tmp->data;
|
|
|
|
|
2000-06-11 15:34:29 -04:00
|
|
|
if (rec->ircnet != NULL && g_strcasecmp(conn->ircnet, rec->ircnet) == 0 &&
|
|
|
|
!rec->banned && (!rec->last_connect || !rec->last_failed ||
|
|
|
|
rec->last_connect < now-FAILED_RECONNECT_WAIT)) {
|
2000-04-26 04:03:38 -04:00
|
|
|
sserver_connect(rec, conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* just try the next server in list */
|
|
|
|
found = through = FALSE;
|
|
|
|
for (tmp = setupservers; tmp != NULL; ) {
|
|
|
|
SETUP_SERVER_REC *rec = tmp->data;
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
if (!found && g_strcasecmp(rec->address, server->connrec->address) == 0 &&
|
2000-04-26 04:03:38 -04:00
|
|
|
server->connrec->port == rec->port)
|
|
|
|
found = TRUE;
|
2000-06-11 15:34:29 -04:00
|
|
|
else if (found && !rec->banned && rec->ircnet != NULL &&
|
|
|
|
g_strcasecmp(conn->ircnet, rec->ircnet) == 0) {
|
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
|
|
|
|
this ircnet in setup.. */
|
2000-06-11 15:34:29 -04:00
|
|
|
irc_server_connect_free(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = setupservers;
|
|
|
|
found = through = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
server_reconnect_destroy(reconnects->data, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to reconnect immediately */
|
2000-07-23 19:19:22 -04:00
|
|
|
/* SYNTAX: RECONNECT <tag> */
|
2000-05-04 06:32:42 -04:00
|
|
|
static void cmd_reconnect(const char *data, IRC_SERVER_REC *server)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
IRC_SERVER_CONNECT_REC *conn;
|
|
|
|
RECONNECT_REC *rec;
|
2000-05-04 06:32:42 -04:00
|
|
|
char *str;
|
2000-04-26 04:03:38 -04:00
|
|
|
int tag;
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
if (*data == '\0') {
|
|
|
|
/* reconnect back to same server */
|
|
|
|
if (server == NULL) cmd_return_error(CMDERR_NOT_CONNECTED);
|
|
|
|
str = g_strdup_printf("%s %d %s %s", server->connrec->address,
|
|
|
|
server->connrec->port, server->connrec->password,
|
|
|
|
server->connrec->nick);
|
|
|
|
signal_emit("command server", 2, str, server);
|
|
|
|
g_free(str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (g_strncasecmp(data, "RECON-", 6) == 0)
|
|
|
|
data += 6;
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
tag = atoi(data);
|
|
|
|
rec = tag <= 0 ? NULL : reconnect_find_tag(tag);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (rec == NULL)
|
|
|
|
signal_emit("server reconnect not found", 1, data);
|
|
|
|
else {
|
|
|
|
conn = rec->conn;
|
|
|
|
server_reconnect_destroy(rec, FALSE);
|
2000-05-04 06:32:42 -04:00
|
|
|
irc_server_connect(conn);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_disconnect(const char *data, SERVER_REC *server)
|
|
|
|
{
|
|
|
|
RECONNECT_REC *rec;
|
|
|
|
int tag;
|
|
|
|
|
|
|
|
if (g_strncasecmp(data, "RECON-", 6) != 0)
|
|
|
|
return; /* handle only reconnection removing */
|
|
|
|
|
|
|
|
rec = sscanf(data+6, "%d", &tag) == 1 && tag > 0 ?
|
|
|
|
reconnect_find_tag(tag) : NULL;
|
|
|
|
|
|
|
|
if (rec == NULL)
|
|
|
|
signal_emit("server reconnect not found", 1, data);
|
|
|
|
else
|
|
|
|
server_reconnect_destroy(rec, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sig_set_user_mode(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
const char *mode;
|
|
|
|
char *newmode;
|
|
|
|
|
|
|
|
if (g_slist_find(servers, server) == NULL)
|
|
|
|
return 0; /* got disconnected */
|
|
|
|
|
|
|
|
mode = server->connrec->usermode;
|
|
|
|
if (mode == NULL) return 0;
|
|
|
|
|
2000-04-26 06:19:57 -04:00
|
|
|
newmode = server->usermode == NULL ? NULL :
|
|
|
|
modes_join(server->usermode, mode);
|
2000-07-04 15:23:01 -04:00
|
|
|
|
|
|
|
if (server->usermode == NULL) {
|
|
|
|
/* server didn't set user mode, just set the new one */
|
|
|
|
irc_send_cmdv(server, "MODE %s %s", server->nick, mode);
|
|
|
|
} else {
|
|
|
|
if (strcmp(newmode, server->usermode) != 0)
|
|
|
|
irc_send_cmdv(server, "MODE %s -%s+%s", server->nick, server->usermode, mode);
|
|
|
|
}
|
|
|
|
|
2000-06-01 13:31:03 -04:00
|
|
|
g_free_not_null(newmode);
|
2000-04-26 04:03:38 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_connected(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
if (!server->connrec->reconnection)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (server->connrec->channels != NULL)
|
|
|
|
channels_join(server, server->connrec->channels, TRUE);
|
|
|
|
if (server->connrec->away_reason != NULL)
|
|
|
|
signal_emit("command away", 2, server->connrec->away_reason, server, NULL);
|
|
|
|
if (server->connrec->usermode != NULL) {
|
|
|
|
/* wait a second and then send the user mode */
|
|
|
|
g_timeout_add(1000, (GSourceFunc) sig_set_user_mode, server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_settings(void)
|
|
|
|
{
|
|
|
|
reconnect_time = settings_get_int("server_reconnect_time");
|
|
|
|
}
|
|
|
|
|
|
|
|
void servers_reconnect_init(void)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
signal_add("event connected", (SIGNAL_FUNC) sig_connected);
|
|
|
|
command_bind("rmreconns", NULL, (SIGNAL_FUNC) cmd_rmreconns);
|
|
|
|
command_bind("reconnect", NULL, (SIGNAL_FUNC) cmd_reconnect);
|
|
|
|
command_bind("disconnect", NULL, (SIGNAL_FUNC) cmd_disconnect);
|
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void servers_reconnect_deinit(void)
|
|
|
|
{
|
|
|
|
g_source_remove(reconnect_timeout_tag);
|
|
|
|
|
|
|
|
while (reconnects != NULL)
|
|
|
|
server_reconnect_destroy(reconnects->data, TRUE);
|
|
|
|
|
|
|
|
signal_remove("server connect failed", (SIGNAL_FUNC) sig_reconnect);
|
|
|
|
signal_remove("server disconnected", (SIGNAL_FUNC) sig_reconnect);
|
|
|
|
signal_remove("event connected", (SIGNAL_FUNC) sig_connected);
|
|
|
|
command_unbind("rmreconns", (SIGNAL_FUNC) cmd_rmreconns);
|
|
|
|
command_unbind("reconnect", (SIGNAL_FUNC) cmd_reconnect);
|
|
|
|
command_unbind("disconnect", (SIGNAL_FUNC) cmd_disconnect);
|
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
}
|