2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
irc.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 "network.h"
|
2000-07-16 15:00:41 -04:00
|
|
|
#include "net-sendbuffer.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "line-split.h"
|
|
|
|
#include "rawlog.h"
|
2000-11-30 18:13:15 -05:00
|
|
|
#include "misc.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-26 11:39:44 -04:00
|
|
|
#include "irc-servers.h"
|
|
|
|
#include "irc-channels.h"
|
|
|
|
#include "servers-redirect.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
char *current_server_event;
|
|
|
|
static int signal_default_event;
|
|
|
|
static int signal_server_event;
|
|
|
|
static int signal_server_incoming;
|
|
|
|
|
2000-12-15 14:44:27 -05:00
|
|
|
#ifdef BLOCKING_SOCKETS
|
|
|
|
# define MAX_SOCKET_READS 1
|
|
|
|
#else
|
|
|
|
# define MAX_SOCKET_READS 5
|
|
|
|
#endif
|
|
|
|
|
2001-03-18 18:00:53 -05:00
|
|
|
/* The core of the irc_send_cmd* functions. If `raw' is TRUE, the `cmd'
|
|
|
|
won't be checked at all if it's 512 bytes or not, or if it contains
|
|
|
|
line feeds or not. Use with extreme caution! */
|
|
|
|
void irc_send_cmd_full(IRC_SERVER_REC *server, const char *cmd,
|
|
|
|
int send_now, int immediate, int raw)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-03-18 18:00:53 -05:00
|
|
|
char str[513];
|
2000-07-16 15:00:41 -04:00
|
|
|
int len;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-13 21:02:08 -05:00
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
g_return_if_fail(cmd != NULL);
|
|
|
|
if (server->disconnected) return;
|
|
|
|
|
2001-03-18 18:00:53 -05:00
|
|
|
len = strlen(cmd);
|
2000-04-26 04:03:38 -04:00
|
|
|
server->cmdcount++;
|
|
|
|
|
2001-03-18 18:00:53 -05:00
|
|
|
if (!raw) {
|
|
|
|
/* check that we don't send any longer commands
|
2001-07-14 14:52:53 -04:00
|
|
|
than 510 bytes (2 bytes for CR+LF) */
|
2001-03-18 18:00:53 -05:00
|
|
|
strncpy(str, cmd, 510);
|
|
|
|
if (len > 510) len = 510;
|
2001-07-14 14:52:53 -04:00
|
|
|
str[len] = '\0';
|
2001-03-18 18:00:53 -05:00
|
|
|
cmd = str;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-11 13:59:19 -05:00
|
|
|
if (send_now) {
|
2001-07-14 14:52:53 -04:00
|
|
|
rawlog_output(server->rawlog, cmd);
|
2001-11-11 15:20:43 -05:00
|
|
|
server_redirect_command(server, cmd, server->redirect_next);
|
|
|
|
server->redirect_next = NULL;
|
2001-11-11 13:59:19 -05:00
|
|
|
}
|
2001-07-14 14:52:53 -04:00
|
|
|
|
|
|
|
if (!raw) {
|
|
|
|
/* Add CR+LF to command */
|
|
|
|
str[len++] = 13;
|
|
|
|
str[len++] = 10;
|
|
|
|
str[len] = '\0';
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
if (send_now) {
|
2001-03-18 18:00:53 -05:00
|
|
|
if (net_sendbuffer_send(server->handle, cmd, len) == -1) {
|
2000-07-16 15:00:41 -04:00
|
|
|
/* something bad happened */
|
2000-09-06 18:00:28 -04:00
|
|
|
server->connection_lost = TRUE;
|
|
|
|
server_disconnect(SERVER(server));
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-07-16 15:00:41 -04:00
|
|
|
g_get_current_time(&server->last_cmd);
|
|
|
|
return;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add to queue */
|
2001-11-11 15:20:43 -05:00
|
|
|
if (immediate) {
|
|
|
|
server->cmdqueue = g_slist_prepend(server->cmdqueue,
|
|
|
|
server->redirect_next);
|
|
|
|
server->cmdqueue = g_slist_prepend(server->cmdqueue,
|
|
|
|
g_strdup(cmd));
|
|
|
|
} else {
|
|
|
|
server->cmdqueue = g_slist_append(server->cmdqueue,
|
|
|
|
g_strdup(cmd));
|
|
|
|
server->cmdqueue = g_slist_append(server->cmdqueue,
|
|
|
|
server->redirect_next);
|
|
|
|
}
|
|
|
|
server->redirect_next = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send command to IRC server */
|
|
|
|
void irc_send_cmd(IRC_SERVER_REC *server, const char *cmd)
|
|
|
|
{
|
2000-11-30 18:13:15 -05:00
|
|
|
GTimeVal now;
|
2000-04-26 04:03:38 -04:00
|
|
|
int send_now;
|
|
|
|
|
2000-11-30 18:13:15 -05:00
|
|
|
g_get_current_time(&now);
|
2000-12-28 12:23:06 -05:00
|
|
|
send_now = g_timeval_cmp(&now, &server->wait_cmd) >= 0 &&
|
2000-04-26 04:03:38 -04:00
|
|
|
(server->cmdcount < server->max_cmds_at_once ||
|
2000-12-28 12:23:06 -05:00
|
|
|
server->cmd_queue_speed <= 0);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-18 18:00:53 -05:00
|
|
|
irc_send_cmd_full(server, cmd, send_now, FALSE, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send command to IRC server */
|
|
|
|
void irc_send_cmdv(IRC_SERVER_REC *server, const char *cmd, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
va_start(args, cmd);
|
|
|
|
|
|
|
|
str = g_strdup_vprintf(cmd, args);
|
|
|
|
irc_send_cmd(server, str);
|
|
|
|
g_free(str);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send command to server immediately bypassing all flood protections
|
|
|
|
and queues. */
|
|
|
|
void irc_send_cmd_now(IRC_SERVER_REC *server, const char *cmd)
|
|
|
|
{
|
|
|
|
g_return_if_fail(cmd != NULL);
|
|
|
|
|
2001-03-18 18:00:53 -05:00
|
|
|
irc_send_cmd_full(server, cmd, TRUE, TRUE, FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *split_nicks(const char *cmd, char **pre, char **nicks, char **post, int arg)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
*pre = g_strdup(cmd);
|
|
|
|
*post = *nicks = NULL;
|
|
|
|
for (p = *pre; *p != '\0'; p++) {
|
|
|
|
if (!isspace(*p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (arg == 1) {
|
|
|
|
/* text after nicks */
|
|
|
|
*p++ = '\0';
|
|
|
|
while (isspace(*p)) p++;
|
|
|
|
*post = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find nicks */
|
|
|
|
while (isspace(p[1])) p++;
|
|
|
|
if (--arg == 1) {
|
|
|
|
*p = '\0';
|
|
|
|
*nicks = p+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irc_send_cmd_split(IRC_SERVER_REC *server, const char *cmd,
|
|
|
|
int nickarg, int max_nicks)
|
|
|
|
{
|
|
|
|
char *str, *pre, *post, *nicks;
|
|
|
|
char **nicklist, **tmp;
|
|
|
|
GString *nickstr;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
g_return_if_fail(cmd != NULL);
|
|
|
|
|
|
|
|
str = split_nicks(cmd, &pre, &nicks, &post, nickarg);
|
2001-11-19 22:26:36 -05:00
|
|
|
if (nicks == NULL) {
|
|
|
|
/* no nicks given? */
|
|
|
|
g_free(str);
|
|
|
|
return;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* split the nicks */
|
|
|
|
nickstr = g_string_new(NULL);
|
|
|
|
nicklist = g_strsplit(nicks, ",", -1); count = 0;
|
|
|
|
|
|
|
|
tmp = nicklist;
|
|
|
|
for (;; tmp++) {
|
|
|
|
if (*tmp != NULL) {
|
|
|
|
g_string_sprintfa(nickstr, "%s,", *tmp);
|
|
|
|
if (++count < max_nicks)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
g_string_truncate(nickstr, nickstr->len-1);
|
|
|
|
irc_send_cmdv(server, post == NULL ? "%s %s" : "%s %s %s",
|
|
|
|
pre, nickstr->str, post);
|
|
|
|
g_string_truncate(nickstr, 0);
|
|
|
|
|
|
|
|
if (*tmp == NULL || tmp[1] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_strfreev(nicklist);
|
|
|
|
g_string_free(nickstr, TRUE);
|
|
|
|
|
|
|
|
g_free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get next parameter */
|
|
|
|
char *event_get_param(char **data)
|
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
|
|
|
|
g_return_val_if_fail(data != NULL, NULL);
|
|
|
|
g_return_val_if_fail(*data != NULL, NULL);
|
|
|
|
|
|
|
|
if (**data == ':') {
|
|
|
|
/* last parameter */
|
|
|
|
pos = *data;
|
|
|
|
*data += strlen(*data);
|
|
|
|
return pos+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = *data;
|
|
|
|
while (**data != '\0' && **data != ' ') (*data)++;
|
|
|
|
if (**data == ' ') *(*data)++ = '\0';
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get count parameters from data */
|
|
|
|
char *event_get_params(const char *data, int count, ...)
|
|
|
|
{
|
|
|
|
char **str, *tmp, *duprec, *datad;
|
|
|
|
gboolean rest;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
g_return_val_if_fail(data != NULL, NULL);
|
|
|
|
|
|
|
|
va_start(args, count);
|
|
|
|
duprec = datad = g_strdup(data);
|
|
|
|
|
|
|
|
rest = count & PARAM_FLAG_GETREST;
|
|
|
|
count = PARAM_WITHOUT_FLAGS(count);
|
|
|
|
|
|
|
|
while (count-- > 0) {
|
|
|
|
str = (char **) va_arg(args, char **);
|
|
|
|
if (count == 0 && rest) {
|
|
|
|
/* put the rest to last parameter */
|
|
|
|
tmp = *datad == ':' ? datad+1 : datad;
|
|
|
|
} else {
|
|
|
|
tmp = event_get_param(&datad);
|
|
|
|
}
|
|
|
|
if (str != NULL) *str = tmp;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return duprec;
|
|
|
|
}
|
|
|
|
|
2001-11-11 13:59:19 -05:00
|
|
|
static void irc_server_event(IRC_SERVER_REC *server, const char *line,
|
|
|
|
const char *nick, const char *address)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-11-11 13:59:19 -05:00
|
|
|
const char *signal;
|
|
|
|
char *event, *args;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
2001-11-11 13:59:19 -05:00
|
|
|
/* split event / args */
|
2000-04-26 04:03:38 -04:00
|
|
|
event = g_strconcat("event ", line, NULL);
|
|
|
|
args = strchr(event+6, ' ');
|
|
|
|
if (args != NULL) *args++ = '\0'; else args = "";
|
|
|
|
while (*args == ' ') args++;
|
2001-11-11 13:59:19 -05:00
|
|
|
g_strdown(event);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-11 13:59:19 -05:00
|
|
|
/* check if event needs to be redirected */
|
|
|
|
signal = server_redirect_get_signal(server, event, args);
|
|
|
|
if (signal == NULL)
|
|
|
|
signal = event;
|
|
|
|
else
|
|
|
|
rawlog_redirect(server->rawlog, signal);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-11 13:59:19 -05:00
|
|
|
/* emit it */
|
2000-04-26 04:03:38 -04:00
|
|
|
current_server_event = event+6;
|
2001-11-11 13:59:19 -05:00
|
|
|
if (!signal_emit(signal, 4, server, args, nick, address))
|
2000-12-05 16:12:52 -05:00
|
|
|
signal_emit_id(signal_default_event, 4, server, line, nick, address);
|
2000-04-26 04:03:38 -04:00
|
|
|
current_server_event = NULL;
|
|
|
|
|
|
|
|
g_free(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read line from server */
|
2000-10-01 18:12:01 -04:00
|
|
|
static int irc_receive_line(SERVER_REC *server, char **str, int read_socket)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
char tmpbuf[512];
|
|
|
|
int recvlen, ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail(server != NULL, -1);
|
|
|
|
g_return_val_if_fail(str != NULL, -1);
|
|
|
|
|
2000-10-01 18:12:01 -04:00
|
|
|
recvlen = !read_socket ? 0 :
|
|
|
|
net_receive(net_sendbuffer_handle(server->handle),
|
|
|
|
tmpbuf, sizeof(tmpbuf));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-01 02:45:54 -05:00
|
|
|
ret = line_split(tmpbuf, recvlen, str, &server->buffer);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (ret == -1) {
|
|
|
|
/* connection lost */
|
|
|
|
server->connection_lost = TRUE;
|
|
|
|
server_disconnect(server);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *irc_parse_prefix(char *line, char **nick, char **address)
|
|
|
|
{
|
|
|
|
*nick = *address = NULL;
|
|
|
|
|
|
|
|
if (*line != ':')
|
|
|
|
return line;
|
|
|
|
|
|
|
|
*nick = ++line;
|
|
|
|
while (*line != '\0' && *line != ' ') {
|
|
|
|
if (*line == '!') {
|
2001-05-21 19:14:04 -04:00
|
|
|
*line++ = '\0';
|
|
|
|
*address = line;
|
|
|
|
while (*line != '\0' && *line != ' ')
|
|
|
|
line++;
|
|
|
|
break;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*line == ' ') {
|
|
|
|
*line++ = '\0';
|
|
|
|
while (*line == ' ') line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse command line sent by server */
|
|
|
|
static void irc_parse_incoming_line(IRC_SERVER_REC *server, char *line)
|
|
|
|
{
|
|
|
|
char *nick, *address;
|
|
|
|
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
|
|
|
line = irc_parse_prefix(line, &nick, &address);
|
|
|
|
if (*line != '\0')
|
2000-12-04 19:53:04 -05:00
|
|
|
signal_emit_id(signal_server_event, 4, server, line, nick, address);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* input function: handle incoming server messages */
|
|
|
|
static void irc_parse_incoming(SERVER_REC *server)
|
|
|
|
{
|
|
|
|
char *str;
|
2000-10-01 18:12:01 -04:00
|
|
|
int count;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
|
2000-10-01 18:12:01 -04:00
|
|
|
/* Some commands can send huge replies and irssi might handle them
|
2000-12-15 14:44:27 -05:00
|
|
|
too slowly, so read only a few times from the socket before
|
2000-10-01 18:12:01 -04:00
|
|
|
letting other tasks to run. */
|
|
|
|
count = 0;
|
2001-12-13 20:54:12 -05:00
|
|
|
server_ref(server);
|
|
|
|
while (!server->disconnected &&
|
|
|
|
irc_receive_line(server, &str, count < MAX_SOCKET_READS) > 0) {
|
2000-04-26 04:03:38 -04:00
|
|
|
rawlog_input(server->rawlog, str);
|
|
|
|
signal_emit_id(signal_server_incoming, 2, server, str);
|
2000-09-06 18:00:28 -04:00
|
|
|
|
2000-10-01 18:12:01 -04:00
|
|
|
count++;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void irc_init_server(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail(server != NULL);
|
|
|
|
|
2000-09-27 19:47:51 -04:00
|
|
|
if (!IS_IRC_SERVER(server))
|
|
|
|
return;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
server->readtag =
|
2000-07-16 15:00:41 -04:00
|
|
|
g_input_add(net_sendbuffer_handle(server->handle),
|
|
|
|
G_INPUT_READ,
|
2000-04-26 04:03:38 -04:00
|
|
|
(GInputFunction) irc_parse_incoming, server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void irc_irc_init(void)
|
|
|
|
{
|
|
|
|
signal_add("server event", (SIGNAL_FUNC) irc_server_event);
|
|
|
|
signal_add("server connected", (SIGNAL_FUNC) irc_init_server);
|
|
|
|
signal_add("server incoming", (SIGNAL_FUNC) irc_parse_incoming_line);
|
|
|
|
|
|
|
|
current_server_event = NULL;
|
2000-06-25 19:39:23 -04:00
|
|
|
signal_default_event = signal_get_uniq_id("default event");
|
|
|
|
signal_server_event = signal_get_uniq_id("server event");
|
|
|
|
signal_server_incoming = signal_get_uniq_id("server incoming");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void irc_irc_deinit(void)
|
|
|
|
{
|
|
|
|
signal_remove("server event", (SIGNAL_FUNC) irc_server_event);
|
|
|
|
signal_remove("server connected", (SIGNAL_FUNC) irc_init_server);
|
|
|
|
signal_remove("server incoming", (SIGNAL_FUNC) irc_parse_incoming_line);
|
|
|
|
}
|