2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
server.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 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.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-04-26 04:03:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
2000-09-02 14:53:58 -04:00
|
|
|
#include "commands.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "line-split.h"
|
|
|
|
#include "net-nonblock.h"
|
2000-07-16 15:00:41 -04:00
|
|
|
#include "net-sendbuffer.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "misc.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "rawlog.h"
|
2001-03-03 20:47:13 -05:00
|
|
|
#include "settings.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 22:25:21 -05:00
|
|
|
#include "chat-protocols.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers.h"
|
2000-09-02 14:53:58 -04:00
|
|
|
#include "servers-reconnect.h"
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "servers-setup.h"
|
|
|
|
#include "channels.h"
|
|
|
|
#include "queries.h"
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
GSList *servers, *lookup_servers;
|
|
|
|
|
|
|
|
/* connection to server failed */
|
2000-09-27 19:47:51 -04:00
|
|
|
void server_connect_failed(SERVER_REC *server, const char *msg)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_if_fail(IS_SERVER(server));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
lookup_servers = g_slist_remove(lookup_servers, server);
|
|
|
|
|
|
|
|
signal_emit("server connect failed", 2, server, msg);
|
2001-12-13 20:54:12 -05:00
|
|
|
|
|
|
|
if (server->connect_tag != -1) {
|
2000-04-26 04:03:38 -04:00
|
|
|
g_source_remove(server->connect_tag);
|
2001-12-13 20:54:12 -05:00
|
|
|
server->connect_tag = -1;
|
|
|
|
}
|
|
|
|
if (server->handle != NULL) {
|
2000-07-31 16:40:24 -04:00
|
|
|
net_sendbuffer_destroy(server->handle, TRUE);
|
2001-12-13 20:54:12 -05:00
|
|
|
server->handle = NULL;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
if (server->connect_pipe[0] != NULL) {
|
|
|
|
g_io_channel_close(server->connect_pipe[0]);
|
|
|
|
g_io_channel_unref(server->connect_pipe[0]);
|
|
|
|
g_io_channel_close(server->connect_pipe[1]);
|
|
|
|
g_io_channel_unref(server->connect_pipe[1]);
|
2001-12-13 20:54:12 -05:00
|
|
|
server->connect_pipe[0] = NULL;
|
|
|
|
server->connect_pipe[1] = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-12-13 20:54:12 -05:00
|
|
|
server_unref(server);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generate tag from server's address */
|
|
|
|
static char *server_create_address_tag(const char *address)
|
|
|
|
{
|
|
|
|
const char *start, *end;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_val_if_fail(address != NULL, NULL);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* try to generate a reasonable server tag */
|
2000-05-04 06:32:42 -04:00
|
|
|
if (strchr(address, '.') == NULL) {
|
|
|
|
start = end = NULL;
|
|
|
|
} else if (g_strncasecmp(address, "irc", 3) == 0 ||
|
2000-04-26 04:03:38 -04:00
|
|
|
g_strncasecmp(address, "chat", 4) == 0) {
|
|
|
|
/* irc-2.cs.hut.fi -> hut, chat.bt.net -> bt */
|
|
|
|
end = strrchr(address, '.');
|
|
|
|
start = end-1;
|
|
|
|
while (start > address && *start != '.') start--;
|
|
|
|
} else {
|
|
|
|
/* efnet.cs.hut.fi -> efnet */
|
|
|
|
end = strchr(address, '.');
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start == end) start = address; else start++;
|
|
|
|
if (end == NULL) end = address + strlen(address);
|
|
|
|
|
|
|
|
return g_strndup(start, (int) (end-start));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create unique tag for server. prefer ircnet's name or
|
|
|
|
generate it from server's address */
|
|
|
|
static char *server_create_tag(SERVER_CONNECT_REC *conn)
|
|
|
|
{
|
|
|
|
GString *str;
|
|
|
|
char *tag;
|
|
|
|
int num;
|
|
|
|
|
2001-06-08 17:19:08 -04:00
|
|
|
g_return_val_if_fail(IS_SERVER_CONNECT(conn), NULL);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2001-03-03 17:06:45 -05:00
|
|
|
tag = conn->chatnet != NULL && *conn->chatnet != '\0' ?
|
|
|
|
g_strdup(conn->chatnet) :
|
2000-04-26 04:03:38 -04:00
|
|
|
server_create_address_tag(conn->address);
|
|
|
|
|
2001-06-08 17:19:08 -04:00
|
|
|
if (conn->tag != NULL && server_find_tag(conn->tag) == NULL &&
|
2002-09-14 18:46:18 -04:00
|
|
|
server_find_lookup_tag(conn->tag) == NULL &&
|
2001-06-08 17:19:08 -04:00
|
|
|
strncmp(conn->tag, tag, strlen(tag)) == 0) {
|
|
|
|
/* use the existing tag if it begins with the same ID -
|
|
|
|
this is useful when you have several connections to
|
|
|
|
same server and you want to keep the same tags with
|
|
|
|
the servers (or it would cause problems when rejoining
|
|
|
|
/LAYOUT SAVEd channels). */
|
2001-10-14 10:14:54 -04:00
|
|
|
g_free(tag);
|
2001-06-08 17:19:08 -04:00
|
|
|
return g_strdup(conn->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* then just append numbers after tag until unused is found.. */
|
|
|
|
str = g_string_new(tag);
|
2002-09-14 18:46:18 -04:00
|
|
|
|
|
|
|
num = 2;
|
2002-09-14 18:55:25 -04:00
|
|
|
while (server_find_tag(str->str) != NULL ||
|
2002-09-14 18:46:18 -04:00
|
|
|
server_find_lookup_tag(str->str) != NULL) {
|
2000-04-26 04:03:38 -04:00
|
|
|
g_string_sprintf(str, "%s%d", tag, num);
|
2002-09-14 18:46:18 -04:00
|
|
|
num++;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(tag);
|
|
|
|
|
|
|
|
tag = str->str;
|
|
|
|
g_string_free(str, FALSE);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
/* Connection to server finished, fill the rest of the fields */
|
|
|
|
void server_connect_finished(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
server->connect_time = time(NULL);
|
|
|
|
|
|
|
|
servers = g_slist_append(servers, server);
|
|
|
|
signal_emit("server connected", 1, server);
|
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
static void server_connect_callback_init(SERVER_REC *server, GIOChannel *handle)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_if_fail(IS_SERVER(server));
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
error = net_geterror(handle);
|
|
|
|
if (error != 0) {
|
|
|
|
server->connection_lost = TRUE;
|
2000-09-27 19:47:51 -04:00
|
|
|
server_connect_failed(server, g_strerror(error));
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup_servers = g_slist_remove(lookup_servers, server);
|
|
|
|
g_source_remove(server->connect_tag);
|
|
|
|
server->connect_tag = -1;
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
server_connect_finished(server);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2007-05-31 19:56:51 -04:00
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
static void server_connect_callback_init_ssl(SERVER_REC *server, GIOChannel *handle)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
g_return_if_fail(IS_SERVER(server));
|
|
|
|
|
|
|
|
error = irssi_ssl_handshake(handle);
|
|
|
|
if (error == -1) {
|
|
|
|
server->connection_lost = TRUE;
|
|
|
|
server_connect_failed(server, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (error & 1) {
|
|
|
|
if (server->connect_tag != -1)
|
|
|
|
g_source_remove(server->connect_tag);
|
|
|
|
server->connect_tag = g_input_add(handle, error == 1 ? G_INPUT_READ : G_INPUT_WRITE,
|
|
|
|
(GInputFunction)
|
|
|
|
server_connect_callback_init_ssl,
|
|
|
|
server);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup_servers = g_slist_remove(lookup_servers, server);
|
|
|
|
if (server->connect_tag != -1) {
|
|
|
|
g_source_remove(server->connect_tag);
|
|
|
|
server->connect_tag = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_connect_finished(server);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
static void server_real_connect(SERVER_REC *server, IPADDR *ip,
|
|
|
|
const char *unix_socket)
|
|
|
|
{
|
|
|
|
GIOChannel *handle;
|
2004-01-07 02:54:38 -05:00
|
|
|
IPADDR *own_ip = NULL;
|
|
|
|
const char *errmsg;
|
|
|
|
char *errmsg2;
|
|
|
|
char ipaddr[MAX_IP_LEN];
|
2002-08-26 15:05:14 -04:00
|
|
|
int port;
|
2002-05-19 10:43:16 -04:00
|
|
|
|
|
|
|
g_return_if_fail(ip != NULL || unix_socket != NULL);
|
|
|
|
|
|
|
|
signal_emit("server connecting", 2, server, ip);
|
|
|
|
|
2002-12-04 06:39:31 -05:00
|
|
|
if (server->connrec->no_connect)
|
|
|
|
return;
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
if (ip != NULL) {
|
|
|
|
own_ip = ip == NULL ? NULL :
|
|
|
|
(IPADDR_IS_V6(ip) ? server->connrec->own_ip6 :
|
|
|
|
server->connrec->own_ip4);
|
|
|
|
port = server->connrec->proxy != NULL ?
|
|
|
|
server->connrec->proxy_port : server->connrec->port;
|
2002-08-26 15:32:15 -04:00
|
|
|
handle = server->connrec->use_ssl ?
|
2003-11-16 12:53:55 -05:00
|
|
|
net_connect_ip_ssl(ip, port, own_ip, server->connrec->ssl_cert, server->connrec->ssl_pkey,
|
|
|
|
server->connrec->ssl_cafile, server->connrec->ssl_capath, server->connrec->ssl_verify) :
|
2002-08-26 15:32:15 -04:00
|
|
|
net_connect_ip(ip, port, own_ip);
|
2002-05-19 10:43:16 -04:00
|
|
|
} else {
|
2002-08-26 15:05:14 -04:00
|
|
|
handle = net_connect_unix(unix_socket);
|
2002-05-19 10:43:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
/* failed */
|
2004-01-07 02:54:38 -05:00
|
|
|
errmsg = g_strerror(errno);
|
|
|
|
errmsg2 = NULL;
|
|
|
|
if (errno == EADDRNOTAVAIL) {
|
|
|
|
if (own_ip != NULL) {
|
|
|
|
/* show the IP which is causing the error */
|
|
|
|
net_ip2host(own_ip, ipaddr);
|
|
|
|
errmsg2 = g_strconcat(errmsg, ": ", ipaddr, NULL);
|
|
|
|
}
|
|
|
|
server->no_reconnect = TRUE;
|
|
|
|
}
|
|
|
|
if (server->connrec->use_ssl && errno == ENOSYS)
|
2002-08-26 15:32:15 -04:00
|
|
|
server->no_reconnect = TRUE;
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
server->connection_lost = TRUE;
|
2004-01-07 02:54:38 -05:00
|
|
|
server_connect_failed(server, errmsg2 ? errmsg2 : errmsg);
|
|
|
|
g_free(errmsg2);
|
2002-05-19 10:43:16 -04:00
|
|
|
} else {
|
|
|
|
server->handle = net_sendbuffer_create(handle, 0);
|
2007-05-31 19:56:51 -04:00
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
if (server->connrec->use_ssl)
|
|
|
|
server_connect_callback_init_ssl(server, handle);
|
|
|
|
else
|
|
|
|
#endif
|
2002-05-19 10:43:16 -04:00
|
|
|
server->connect_tag =
|
|
|
|
g_input_add(handle, G_INPUT_WRITE | G_INPUT_READ,
|
|
|
|
(GInputFunction)
|
|
|
|
server_connect_callback_init,
|
|
|
|
server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-16 15:00:41 -04:00
|
|
|
static void server_connect_callback_readpipe(SERVER_REC *server)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
RESOLVED_IP_REC iprec;
|
2002-05-19 10:43:16 -04:00
|
|
|
IPADDR *ip;
|
2001-03-03 20:47:13 -05:00
|
|
|
const char *errormsg;
|
2002-11-28 10:39:39 -05:00
|
|
|
char *servername = NULL;
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
g_source_remove(server->connect_tag);
|
|
|
|
server->connect_tag = -1;
|
|
|
|
|
2000-07-16 15:00:41 -04:00
|
|
|
net_gethostbyname_return(server->connect_pipe[0], &iprec);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_close(server->connect_pipe[0]);
|
|
|
|
g_io_channel_unref(server->connect_pipe[0]);
|
|
|
|
g_io_channel_close(server->connect_pipe[1]);
|
|
|
|
g_io_channel_unref(server->connect_pipe[1]);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
server->connect_pipe[0] = NULL;
|
|
|
|
server->connect_pipe[1] = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
/* figure out if we should use IPv4 or v6 address */
|
2002-03-10 14:14:04 -05:00
|
|
|
if (iprec.error != 0) {
|
|
|
|
/* error */
|
|
|
|
ip = NULL;
|
|
|
|
} else if (server->connrec->family == AF_INET) {
|
|
|
|
/* force IPv4 connection */
|
|
|
|
ip = iprec.ip4.family == 0 ? NULL : &iprec.ip4;
|
2002-11-28 10:39:39 -05:00
|
|
|
servername = iprec.host4;
|
2002-03-10 14:14:04 -05:00
|
|
|
} else if (server->connrec->family == AF_INET6) {
|
|
|
|
/* force IPv6 connection */
|
|
|
|
ip = iprec.ip6.family == 0 ? NULL : &iprec.ip6;
|
2002-11-28 10:39:39 -05:00
|
|
|
servername = iprec.host6;
|
2002-03-10 14:14:04 -05:00
|
|
|
} else {
|
|
|
|
/* pick the one that was found, or if both do it like
|
|
|
|
/SET resolve_prefer_ipv6 says. */
|
2002-11-28 10:39:39 -05:00
|
|
|
if (iprec.ip4.family == 0 ||
|
|
|
|
(iprec.ip6.family != 0 &&
|
|
|
|
settings_get_bool("resolve_prefer_ipv6"))) {
|
|
|
|
ip = &iprec.ip6;
|
|
|
|
servername = iprec.host6;
|
|
|
|
} else {
|
|
|
|
ip = &iprec.ip4;
|
|
|
|
servername = iprec.host4;
|
|
|
|
}
|
2002-03-10 14:14:04 -05:00
|
|
|
}
|
2001-03-03 20:47:13 -05:00
|
|
|
|
2001-11-18 20:48:58 -05:00
|
|
|
if (ip != NULL) {
|
2002-05-19 10:43:16 -04:00
|
|
|
/* host lookup ok */
|
2002-11-28 10:39:39 -05:00
|
|
|
if (servername) {
|
|
|
|
g_free(server->connrec->address);
|
|
|
|
server->connrec->address = g_strdup(servername);
|
|
|
|
}
|
2002-05-19 10:43:16 -04:00
|
|
|
server_real_connect(server, ip, NULL);
|
|
|
|
errormsg = NULL;
|
|
|
|
} else {
|
|
|
|
if (iprec.error == 0 || net_hosterror_notfound(iprec.error)) {
|
|
|
|
/* IP wasn't found for the host, don't try to
|
|
|
|
reconnect back to this server */
|
2001-03-07 18:57:19 -05:00
|
|
|
server->dns_error = TRUE;
|
2000-05-25 09:09:22 -04:00
|
|
|
}
|
2000-07-16 16:18:05 -04:00
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
if (iprec.error == 0) {
|
2002-03-10 14:14:04 -05:00
|
|
|
/* forced IPv4 or IPv6 address but it wasn't found */
|
|
|
|
errormsg = server->connrec->family == AF_INET ?
|
|
|
|
"IPv4 address not found for host" :
|
|
|
|
"IPv6 address not found for host";
|
2000-07-16 16:18:05 -04:00
|
|
|
} else {
|
|
|
|
/* gethostbyname() failed */
|
|
|
|
errormsg = iprec.errorstr != NULL ? iprec.errorstr :
|
|
|
|
"Host lookup failed";
|
|
|
|
}
|
2002-05-19 10:43:16 -04:00
|
|
|
|
2001-03-07 18:57:19 -05:00
|
|
|
server->connection_lost = TRUE;
|
2000-09-27 19:47:51 -04:00
|
|
|
server_connect_failed(server, errormsg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
g_free(iprec.errorstr);
|
2002-11-28 10:39:39 -05:00
|
|
|
g_free(iprec.host4);
|
|
|
|
g_free(iprec.host6);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2002-05-20 00:12:12 -04:00
|
|
|
SERVER_REC *server_connect(SERVER_CONNECT_REC *conn)
|
|
|
|
{
|
|
|
|
CHAT_PROTOCOL_REC *proto;
|
|
|
|
SERVER_REC *server;
|
|
|
|
|
|
|
|
proto = CHAT_PROTOCOL(conn);
|
|
|
|
server = proto->server_init_connect(conn);
|
|
|
|
proto->server_connect(server);
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
/* initializes server record but doesn't start connecting */
|
|
|
|
void server_connect_init(SERVER_REC *server)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2002-04-09 22:53:06 -04:00
|
|
|
const char *str;
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
g_return_if_fail(server != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
MODULE_DATA_INIT(server);
|
2000-08-26 11:39:44 -04:00
|
|
|
server->type = module_get_uniq_id("SERVER", 0);
|
2001-12-13 20:54:12 -05:00
|
|
|
server_ref(server);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-27 19:07:01 -04:00
|
|
|
server->nick = g_strdup(server->connrec->nick);
|
|
|
|
if (server->connrec->username == NULL || *server->connrec->username == '\0') {
|
|
|
|
g_free_not_null(server->connrec->username);
|
|
|
|
|
2002-04-09 22:53:06 -04:00
|
|
|
str = g_get_user_name();
|
2002-07-16 12:12:16 -04:00
|
|
|
if (*str == '\0') str = "unknown";
|
2002-04-09 22:53:06 -04:00
|
|
|
server->connrec->username = g_strdup(str);
|
2000-08-27 19:07:01 -04:00
|
|
|
}
|
|
|
|
if (server->connrec->realname == NULL || *server->connrec->realname == '\0') {
|
|
|
|
g_free_not_null(server->connrec->realname);
|
|
|
|
|
2002-04-09 22:53:06 -04:00
|
|
|
str = g_get_real_name();
|
2002-07-16 12:12:16 -04:00
|
|
|
if (*str == '\0') str = server->connrec->username;
|
2002-04-09 22:53:06 -04:00
|
|
|
server->connrec->realname = g_strdup(str);
|
2000-08-27 19:07:01 -04:00
|
|
|
}
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
server->tag = server_create_tag(server->connrec);
|
2002-05-19 10:43:16 -04:00
|
|
|
server->connect_tag = -1;
|
2000-09-27 19:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* starts connecting to server */
|
|
|
|
int server_start_connect(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
const char *connect_address;
|
2000-12-04 17:57:18 -05:00
|
|
|
int fd[2];
|
2000-09-27 19:47:51 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(server != NULL, FALSE);
|
2002-05-19 10:43:16 -04:00
|
|
|
if (!server->connrec->unix_socket && server->connrec->port <= 0)
|
|
|
|
return FALSE;
|
2000-09-27 19:47:51 -04:00
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
server->rawlog = rawlog_create();
|
2000-09-27 19:47:51 -04:00
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
if (server->connrec->connect_handle != NULL) {
|
|
|
|
/* already connected */
|
|
|
|
GIOChannel *handle = server->connrec->connect_handle;
|
|
|
|
|
|
|
|
server->connrec->connect_handle = NULL;
|
|
|
|
server->handle = net_sendbuffer_create(handle, 0);
|
|
|
|
server_connect_finished(server);
|
|
|
|
} else if (server->connrec->unix_socket) {
|
|
|
|
/* connect with unix socket */
|
|
|
|
server_real_connect(server, NULL, server->connrec->address);
|
|
|
|
} else {
|
|
|
|
/* resolve host name */
|
|
|
|
if (pipe(fd) != 0) {
|
|
|
|
g_warning("server_connect(): pipe() failed.");
|
|
|
|
g_free(server->tag);
|
|
|
|
g_free(server->nick);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->connect_pipe[0] = g_io_channel_unix_new(fd[0]);
|
|
|
|
server->connect_pipe[1] = g_io_channel_unix_new(fd[1]);
|
|
|
|
|
|
|
|
connect_address = server->connrec->proxy != NULL ?
|
|
|
|
server->connrec->proxy : server->connrec->address;
|
|
|
|
server->connect_pid =
|
|
|
|
net_gethostbyname_nonblock(connect_address,
|
2002-11-28 10:39:39 -05:00
|
|
|
server->connect_pipe[1],
|
|
|
|
settings_get_bool("resolve_reverse_lookup"));
|
2002-05-19 10:43:16 -04:00
|
|
|
server->connect_tag =
|
|
|
|
g_input_add(server->connect_pipe[0], G_INPUT_READ,
|
|
|
|
(GInputFunction)
|
|
|
|
server_connect_callback_readpipe,
|
|
|
|
server);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
lookup_servers = g_slist_append(lookup_servers, server);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
signal_emit("server looking", 1, server);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
static int server_remove_channels(SERVER_REC *server)
|
|
|
|
{
|
2002-05-28 16:36:42 -04:00
|
|
|
GSList *tmp, *next;
|
2000-08-26 11:39:44 -04:00
|
|
|
int found;
|
|
|
|
|
|
|
|
g_return_val_if_fail(server != NULL, FALSE);
|
|
|
|
|
|
|
|
found = FALSE;
|
2002-05-28 16:36:42 -04:00
|
|
|
for (tmp = server->channels; tmp != NULL; tmp = next) {
|
2000-08-26 11:39:44 -04:00
|
|
|
CHANNEL_REC *channel = tmp->data;
|
|
|
|
|
2002-05-28 16:36:42 -04:00
|
|
|
next = tmp->next;
|
2000-08-26 11:39:44 -04:00
|
|
|
channel_destroy(channel);
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
|
2001-01-01 14:29:05 -05:00
|
|
|
while (server->queries != NULL)
|
|
|
|
query_change_server(server->queries->data, NULL);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
g_slist_free(server->channels);
|
|
|
|
g_slist_free(server->queries);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void server_disconnect(SERVER_REC *server)
|
|
|
|
{
|
2000-08-26 11:39:44 -04:00
|
|
|
int chans;
|
|
|
|
|
|
|
|
g_return_if_fail(IS_SERVER(server));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2002-01-02 16:21:49 -05:00
|
|
|
if (server->disconnected)
|
|
|
|
return;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (server->connect_tag != -1) {
|
|
|
|
/* still connecting to server.. */
|
|
|
|
if (server->connect_pid != -1)
|
|
|
|
net_disconnect_nonblock(server->connect_pid);
|
2000-09-27 19:47:51 -04:00
|
|
|
server_connect_failed(server, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
servers = g_slist_remove(servers, server);
|
|
|
|
|
2002-01-22 16:04:39 -05:00
|
|
|
server->disconnected = TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_emit("server disconnected", 1, server);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
/* close all channels */
|
|
|
|
chans = server_remove_channels(server);
|
|
|
|
|
|
|
|
if (server->handle != NULL) {
|
|
|
|
if (!chans || server->connection_lost)
|
|
|
|
net_sendbuffer_destroy(server->handle, TRUE);
|
|
|
|
else {
|
|
|
|
/* we were on some channels, try to let the server
|
|
|
|
disconnect so that our quit message is guaranteed
|
|
|
|
to get displayed */
|
|
|
|
net_disconnect_later(net_sendbuffer_handle(server->handle));
|
|
|
|
net_sendbuffer_destroy(server->handle, FALSE);
|
|
|
|
}
|
|
|
|
server->handle = NULL;
|
|
|
|
}
|
|
|
|
|
2001-12-13 20:54:12 -05:00
|
|
|
if (server->readtag > 0) {
|
2000-07-30 13:19:16 -04:00
|
|
|
g_source_remove(server->readtag);
|
2001-12-13 20:54:12 -05:00
|
|
|
server->readtag = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server_unref(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_ref(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail(IS_SERVER(server));
|
|
|
|
|
|
|
|
server->refcount++;
|
|
|
|
}
|
|
|
|
|
2002-01-22 16:04:39 -05:00
|
|
|
int server_unref(SERVER_REC *server)
|
2001-12-13 20:54:12 -05:00
|
|
|
{
|
2002-01-22 16:04:39 -05:00
|
|
|
g_return_val_if_fail(IS_SERVER(server), FALSE);
|
2001-12-13 20:54:12 -05:00
|
|
|
|
|
|
|
if (--server->refcount > 0)
|
2002-01-22 16:04:39 -05:00
|
|
|
return TRUE;
|
2001-12-13 20:54:12 -05:00
|
|
|
|
|
|
|
if (g_slist_find(servers, server) != NULL) {
|
|
|
|
g_warning("Non-referenced server wasn't disconnected");
|
|
|
|
server_disconnect(server);
|
2002-01-22 16:04:39 -05:00
|
|
|
return TRUE;
|
2001-12-13 20:54:12 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
MODULE_DATA_DEINIT(server);
|
2001-10-21 09:59:07 -04:00
|
|
|
server_connect_unref(server->connrec);
|
2001-12-13 20:54:12 -05:00
|
|
|
if (server->rawlog != NULL) rawlog_destroy(server->rawlog);
|
|
|
|
if (server->buffer != NULL) line_split_free(server->buffer);
|
|
|
|
g_free(server->version);
|
|
|
|
g_free(server->away_reason);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(server->nick);
|
2000-07-31 16:40:24 -04:00
|
|
|
g_free(server->tag);
|
2002-02-07 14:17:36 -05:00
|
|
|
|
|
|
|
server->type = 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(server);
|
2002-01-22 16:04:39 -05:00
|
|
|
return FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SERVER_REC *server_find_tag(const char *tag)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(tag != NULL, NULL);
|
|
|
|
if (*tag == '\0') return NULL;
|
|
|
|
|
|
|
|
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SERVER_REC *server = tmp->data;
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
if (g_strcasecmp(server->tag, tag) == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-08-09 07:17:31 -04:00
|
|
|
SERVER_REC *server_find_lookup_tag(const char *tag)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(tag != NULL, NULL);
|
|
|
|
if (*tag == '\0') return NULL;
|
|
|
|
|
|
|
|
for (tmp = lookup_servers; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SERVER_REC *server = tmp->data;
|
|
|
|
|
|
|
|
if (g_strcasecmp(server->tag, tag) == 0)
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
SERVER_REC *server_find_chatnet(const char *chatnet)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_return_val_if_fail(chatnet != NULL, NULL);
|
|
|
|
if (*chatnet == '\0') return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
for (tmp = servers; tmp != NULL; tmp = tmp->next) {
|
|
|
|
SERVER_REC *server = tmp->data;
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
if (server->connrec->chatnet != NULL &&
|
|
|
|
g_strcasecmp(server->connrec->chatnet, chatnet) == 0)
|
2000-08-20 03:17:13 -04:00
|
|
|
return server;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
void server_connect_ref(SERVER_CONNECT_REC *conn)
|
|
|
|
{
|
|
|
|
conn->refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void server_connect_unref(SERVER_CONNECT_REC *conn)
|
2000-08-26 11:39:44 -04:00
|
|
|
{
|
|
|
|
g_return_if_fail(IS_SERVER_CONNECT(conn));
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
if (--conn->refcount > 0)
|
|
|
|
return;
|
|
|
|
if (conn->refcount < 0) {
|
|
|
|
g_warning("Connection '%s' refcount = %d",
|
|
|
|
conn->tag, conn->refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHAT_PROTOCOL(conn)->destroy_server_connect(conn);
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
if (conn->connect_handle != NULL)
|
|
|
|
net_disconnect(conn->connect_handle);
|
|
|
|
|
2001-10-21 09:59:07 -04:00
|
|
|
g_free_not_null(conn->proxy);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(conn->proxy_string);
|
2002-01-22 15:29:45 -05:00
|
|
|
g_free_not_null(conn->proxy_string_after);
|
2001-03-20 18:24:45 -05:00
|
|
|
g_free_not_null(conn->proxy_password);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
2001-06-08 17:19:08 -04:00
|
|
|
g_free_not_null(conn->tag);
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(conn->address);
|
|
|
|
g_free_not_null(conn->chatnet);
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
g_free_not_null(conn->own_ip4);
|
|
|
|
g_free_not_null(conn->own_ip6);
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
g_free_not_null(conn->password);
|
|
|
|
g_free_not_null(conn->nick);
|
|
|
|
g_free_not_null(conn->username);
|
|
|
|
g_free_not_null(conn->realname);
|
|
|
|
|
2003-11-16 12:53:55 -05:00
|
|
|
g_free_not_null(conn->ssl_cert);
|
|
|
|
g_free_not_null(conn->ssl_pkey);
|
|
|
|
g_free_not_null(conn->ssl_cafile);
|
|
|
|
g_free_not_null(conn->ssl_capath);
|
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
g_free_not_null(conn->channels);
|
|
|
|
g_free_not_null(conn->away_reason);
|
2002-02-07 14:17:36 -05:00
|
|
|
|
|
|
|
conn->type = 0;
|
|
|
|
g_free(conn);
|
2000-08-26 11:39:44 -04:00
|
|
|
}
|
|
|
|
|
2001-03-04 05:30:41 -05:00
|
|
|
void server_change_nick(SERVER_REC *server, const char *nick)
|
|
|
|
{
|
|
|
|
g_free(server->nick);
|
|
|
|
server->nick = g_strdup(nick);
|
|
|
|
|
|
|
|
signal_emit("server nick changed", 1, server);
|
|
|
|
}
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
/* Update own IPv4 and IPv6 records */
|
|
|
|
void server_connect_own_ip_save(SERVER_CONNECT_REC *conn,
|
|
|
|
IPADDR *ip4, IPADDR *ip6)
|
|
|
|
{
|
|
|
|
if (ip4 == NULL || ip4->family == 0)
|
|
|
|
g_free_and_null(conn->own_ip4);
|
|
|
|
if (ip6 == NULL || ip6->family == 0)
|
|
|
|
g_free_and_null(conn->own_ip6);
|
|
|
|
|
|
|
|
if (ip4 != NULL && ip4->family != 0) {
|
|
|
|
/* IPv4 address was found */
|
2001-03-15 15:48:57 -05:00
|
|
|
if (conn->own_ip4 == NULL)
|
2001-03-03 20:47:13 -05:00
|
|
|
conn->own_ip4 = g_new0(IPADDR, 1);
|
|
|
|
memcpy(conn->own_ip4, ip4, sizeof(IPADDR));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ip6 != NULL && ip6->family != 0) {
|
|
|
|
/* IPv6 address was found */
|
2001-03-15 15:48:57 -05:00
|
|
|
if (conn->own_ip6 == NULL)
|
2001-03-03 20:47:13 -05:00
|
|
|
conn->own_ip6 = g_new0(IPADDR, 1);
|
|
|
|
memcpy(conn->own_ip6, ip6, sizeof(IPADDR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-29 16:56:25 -05:00
|
|
|
/* `optlist' should contain only one unknown key - the server tag.
|
2000-09-02 14:53:58 -04:00
|
|
|
returns NULL if there was unknown -option */
|
|
|
|
SERVER_REC *cmd_options_get_server(const char *cmd,
|
|
|
|
GHashTable *optlist,
|
|
|
|
SERVER_REC *defserver)
|
|
|
|
{
|
|
|
|
SERVER_REC *server;
|
|
|
|
GSList *list, *tmp, *next;
|
|
|
|
|
|
|
|
/* get all the options, then remove the known ones. there should
|
|
|
|
be only one left - the server tag. */
|
|
|
|
list = hashtable_get_keys(optlist);
|
2000-11-30 17:58:45 -05:00
|
|
|
if (cmd != NULL) {
|
|
|
|
for (tmp = list; tmp != NULL; tmp = next) {
|
|
|
|
char *option = tmp->data;
|
|
|
|
next = tmp->next;
|
2000-09-02 14:53:58 -04:00
|
|
|
|
2000-11-30 17:58:45 -05:00
|
|
|
if (command_have_option(cmd, option))
|
|
|
|
list = g_slist_remove(list, option);
|
|
|
|
}
|
2000-09-02 14:53:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return defserver;
|
|
|
|
|
|
|
|
server = server_find_tag(list->data);
|
|
|
|
if (server == NULL || list->next != NULL) {
|
|
|
|
/* unknown option (not server tag) */
|
|
|
|
signal_emit("error command", 2,
|
|
|
|
GINT_TO_POINTER(CMDERR_OPTION_UNKNOWN),
|
|
|
|
server == NULL ? list->data : list->next->data);
|
|
|
|
signal_stop();
|
|
|
|
|
|
|
|
server = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free(list);
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2001-03-03 22:25:21 -05:00
|
|
|
static void disconnect_servers(GSList *servers, int chat_type)
|
|
|
|
{
|
|
|
|
GSList *tmp, *next;
|
|
|
|
|
|
|
|
for (tmp = servers; tmp != NULL; tmp = next) {
|
|
|
|
SERVER_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (rec->chat_type == chat_type)
|
|
|
|
server_disconnect(rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_chat_protocol_deinit(CHAT_PROTOCOL_REC *proto)
|
|
|
|
{
|
|
|
|
disconnect_servers(servers, proto->id);
|
|
|
|
disconnect_servers(lookup_servers, proto->id);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void servers_init(void)
|
|
|
|
{
|
2001-03-03 20:47:13 -05:00
|
|
|
settings_add_bool("server", "resolve_prefer_ipv6", FALSE);
|
2002-11-28 10:39:39 -05:00
|
|
|
settings_add_bool("server", "resolve_reverse_lookup", FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
lookup_servers = servers = NULL;
|
|
|
|
|
2001-03-03 22:25:21 -05:00
|
|
|
signal_add("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
|
|
|
|
|
2000-09-02 14:53:58 -04:00
|
|
|
servers_reconnect_init();
|
2000-08-26 11:39:44 -04:00
|
|
|
servers_setup_init();
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void servers_deinit(void)
|
|
|
|
{
|
2001-03-03 22:25:21 -05:00
|
|
|
signal_remove("chat protocol deinit", (SIGNAL_FUNC) sig_chat_protocol_deinit);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
servers_setup_deinit();
|
2000-09-02 14:53:58 -04:00
|
|
|
servers_reconnect_deinit();
|
2000-08-26 11:39:44 -04:00
|
|
|
|
|
|
|
module_uniq_destroy("SERVER");
|
|
|
|
module_uniq_destroy("SERVER CONNECT");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|