1
0
mirror of https://github.com/irssi/irssi.git synced 2024-10-27 05:20:20 -04:00

DCC cleanups - split DCC_REC to CHAT|GET|SEND_DCC_RECs. Plugins should

now be able to add whatever new DCC types.

Nick changes affect DCC chats. /WHOIS without parameters works properly
in DCC CHAT queries.


git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1194 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2001-02-10 02:02:26 +00:00 committed by cras
parent 118f9a11eb
commit e4f7d55ce9
20 changed files with 1401 additions and 915 deletions

View File

@ -8,12 +8,16 @@ INCLUDES = \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/core/ \
-I$(top_srcdir)/src/irc/core/ \
-I$(top_srcdir)/src/irc/dcc/ \
-I$(top_srcdir)/src/fe-common/core/ \
-DHELPDIR=\""$(datadir)/irssi/help"\" \
-DSYSCONFDIR=\""$(sysconfdir)"\"
libfe_common_irc_dcc_la_SOURCES = \
fe-dcc.c \
fe-dcc-chat.c \
fe-dcc-get.c \
fe-dcc-send.c \
module-formats.c
noinst_HEADERS = \

View File

@ -0,0 +1,404 @@
/*
fe-dcc-chat.c : irssi
Copyright (C) 1999-2001 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 "signals.h"
#include "commands.h"
#include "levels.h"
#include "settings.h"
#include "irc.h"
#include "irc-queries.h"
#include "dcc-chat.h"
#include "module-formats.h"
#include "printtext.h"
#include "fe-messages.h"
#include "chat-completion.h"
static int autocreate_dccquery;
static void dcc_request(CHAT_DCC_REC *dcc)
{
if (!IS_DCC_CHAT(dcc)) return;
printformat(dcc->server, NULL, MSGLEVEL_DCC,
ischannel(*dcc->target) ? IRCTXT_DCC_CHAT_CHANNEL :
IRCTXT_DCC_CHAT, dcc->id, dcc->addrstr,
dcc->port, dcc->target);
}
static void dcc_connected(CHAT_DCC_REC *dcc)
{
char *sender;
if (!IS_DCC_CHAT(dcc)) return;
sender = g_strconcat("=", dcc->id, NULL);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_CONNECTED,
dcc->id, dcc->addrstr, dcc->port);
if (query_find(NULL, sender) == NULL) {
if (!autocreate_dccquery)
completion_last_message_add(sender);
else
irc_query_create(dcc->server == NULL ? NULL :
dcc->server->tag, sender, TRUE);
}
g_free(sender);
}
static void dcc_closed(CHAT_DCC_REC *dcc)
{
char *sender;
if (!IS_DCC_CHAT(dcc)) return;
sender = g_strconcat("=", dcc->id, NULL);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_DISCONNECTED, dcc->id);
g_free(sender);
}
static void dcc_chat_msg(CHAT_DCC_REC *dcc, const char *msg)
{
QUERY_REC *query;
char *sender, *freemsg;
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->id, NULL);
query = query_find(NULL, sender);
if (settings_get_bool("emphasis"))
msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
else
freemsg = NULL;
if (query_find(NULL, sender) == NULL)
completion_last_message_add(sender);
printformat(NULL, sender, MSGLEVEL_DCCMSGS,
query != NULL ? IRCTXT_DCC_MSG_QUERY :
IRCTXT_DCC_MSG, dcc->id, msg);
g_free_not_null(freemsg);
g_free(sender);
}
static void dcc_chat_action(const char *msg, CHAT_DCC_REC *dcc)
{
char *sender;
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->id, NULL);
if (query_find(NULL, sender) == NULL)
completion_last_message_add(sender);
printformat(NULL, sender, MSGLEVEL_DCCMSGS,
IRCTXT_ACTION_DCC, dcc->id, msg);
g_free(sender);
}
static void dcc_chat_ctcp(const char *msg, CHAT_DCC_REC *dcc)
{
char *sender;
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->id, NULL);
printformat(NULL, sender, MSGLEVEL_DCC,
IRCTXT_DCC_CTCP, dcc->id, msg);
g_free(sender);
}
static void dcc_error_ctcp(const char *type, const char *data,
const char *nick, const char *addr,
const char *target)
{
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_INVALID_CTCP, type, nick, addr, target);
}
static void dcc_unknown_ctcp(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, CHAT_DCC_REC *chat)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_CTCP,
type, nick, args);
cmd_params_free(free_arg);
}
static void dcc_unknown_reply(IRC_SERVER_REC *server, const char *data,
const char *nick)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_REPLY,
type, nick, args);
cmd_params_free(free_arg);
}
static void sig_dcc_destroyed(CHAT_DCC_REC *dcc)
{
QUERY_REC *query;
char *nick;
if (!IS_DCC_CHAT(dcc)) return;
nick = g_strconcat("=", dcc->id, NULL);
query = query_find(NULL, nick);
if (query != NULL) {
/* DCC chat closed, close the query with it. */
if (dcc->connection_lost) query->unwanted = TRUE;
query_destroy(query);
} else {
/* remove nick from msg completion
since it won't work anymore */
completion_last_message_remove(nick);
}
g_free(nick);
}
static void sig_query_destroyed(QUERY_REC *query)
{
CHAT_DCC_REC *dcc;
if (*query->name != '=')
return;
dcc = dcc_chat_find_id(query->name+1);
if (dcc != NULL && !dcc->destroyed) {
/* DCC query window closed, close the dcc chat too. */
dcc_close(DCC(dcc));
}
}
static void dcc_error_close_not_found(const char *type, const char *nick,
const char *fname)
{
g_return_if_fail(type != NULL);
g_return_if_fail(nick != NULL);
if (g_strcasecmp(type, "GET") != 0) return;
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_NOT_FOUND, nick);
}
static void sig_dcc_list_print(CHAT_DCC_REC *dcc)
{
if (!IS_DCC_CHAT(dcc)) return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_LIST_LINE_CHAT,
dcc->id, "CHAT");
}
static void cmd_msg(const char *data)
{
QUERY_REC *query;
CHAT_DCC_REC *dcc;
char *text, *target;
void *free_arg;
g_return_if_fail(data != NULL);
if (*data != '=') {
/* handle only DCC messages */
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
query = query_find(NULL, target);
printformat(NULL, target, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
query != NULL ? IRCTXT_OWN_DCC_QUERY :
IRCTXT_OWN_DCC, dcc->mynick, target+1, text);
if (query == NULL)
completion_last_message_add(target);
}
cmd_params_free(free_arg);
}
static void cmd_me(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
CHAT_DCC_REC *dcc;
g_return_if_fail(data != NULL);
dcc = item_get_dcc(item);
if (dcc == NULL) return;
printformat(NULL, item->name, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
IRCTXT_OWN_DCC_ACTION_QUERY, dcc->mynick, item->name, data);
}
static void cmd_action(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
QUERY_REC *query;
CHAT_DCC_REC *dcc;
char *target, *text;
void *free_arg;
g_return_if_fail(data != NULL);
if (*data != '=') {
/* handle only DCC actions */
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
if (*target == '\0' || *text == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
query = query_find(NULL, target);
printformat(NULL, target, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
query != NULL ? IRCTXT_OWN_DCC_ACTION_QUERY :
IRCTXT_OWN_DCC_ACTION, dcc->mynick, target, text);
if (query == NULL)
completion_last_message_add(target);
}
cmd_params_free(free_arg);
}
static void cmd_ctcp(const char *data, SERVER_REC *server)
{
CHAT_DCC_REC *dcc;
char *target, *ctcpcmd, *ctcpdata;
void *free_arg;
g_return_if_fail(data != NULL);
if (server == NULL || !server->connected)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
&target, &ctcpcmd, &ctcpdata))
return;
if (*target == '\0' || *ctcpcmd == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target != '=') {
/* handle only DCC CTCPs */
cmd_params_free(free_arg);
return;
}
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
g_strup(ctcpcmd);
printformat(server, target, MSGLEVEL_DCC, IRCTXT_OWN_DCC_CTCP,
target, ctcpcmd, ctcpdata);
}
cmd_params_free(free_arg);
}
static void read_settings(void)
{
int level;
level = level2bits(settings_get_str("autocreate_query_level"));
autocreate_dccquery = (level & MSGLEVEL_DCCMSGS) != 0;
}
void fe_dcc_chat_init(void)
{
read_settings();
signal_add("dcc request", (SIGNAL_FUNC) dcc_request);
signal_add("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_add("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_add("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_add("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_add("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
signal_add("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_add("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_add("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_add("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_add("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
command_bind("msg", NULL, (SIGNAL_FUNC) cmd_msg);
command_bind("me", NULL, (SIGNAL_FUNC) cmd_me);
command_bind("action", NULL, (SIGNAL_FUNC) cmd_action);
command_bind("ctcp", NULL, (SIGNAL_FUNC) cmd_ctcp);
signal_add("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
}
void fe_dcc_chat_deinit(void)
{
signal_remove("dcc request", (SIGNAL_FUNC) dcc_request);
signal_remove("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_remove("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_remove("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_remove("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
signal_remove("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_remove("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_remove("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_remove("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_remove("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
command_unbind("msg", (SIGNAL_FUNC) cmd_msg);
command_unbind("me", (SIGNAL_FUNC) cmd_me);
command_unbind("action", (SIGNAL_FUNC) cmd_action);
command_unbind("ctcp", (SIGNAL_FUNC) cmd_ctcp);
signal_remove("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}

View File

@ -0,0 +1,126 @@
/*
fe-dcc-get.c : irssi
Copyright (C) 1999-2001 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 "signals.h"
#include "levels.h"
#include "irc.h"
#include "dcc-file.h"
#include "dcc-get.h"
#include "module-formats.h"
#include "printtext.h"
#include "fe-dcc.h"
static void dcc_request(GET_DCC_REC *dcc)
{
if (!IS_DCC_GET(dcc)) return;
printformat(dcc->server, NULL, MSGLEVEL_DCC,
ischannel(*dcc->target) ? IRCTXT_DCC_SEND_CHANNEL :
IRCTXT_DCC_SEND, dcc->nick, dcc->addrstr,
dcc->port, dcc->arg, dcc->size, dcc->target);
}
static void dcc_connected(GET_DCC_REC *dcc)
{
if (!IS_DCC_GET(dcc)) return;
printformat(dcc->server, NULL, MSGLEVEL_DCC, IRCTXT_DCC_GET_CONNECTED,
dcc->arg, dcc->nick, dcc->addrstr, dcc->port);
}
static void dcc_closed(GET_DCC_REC *dcc)
{
double kbs;
time_t secs;
if (!IS_DCC_GET(dcc)) return;
secs = dcc->starttime == 0 ? -1 : time(NULL)-dcc->starttime;
kbs = (double) (dcc->transfd-dcc->skipped) /
(secs == 0 ? 1 : secs) / 1024.0;
if (secs == -1) {
/* aborted */
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_ABORTED, dcc->arg, dcc->nick);
} else {
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_COMPLETE, dcc->arg,
dcc->transfd/1024, dcc->nick, (long) secs, kbs);
}
}
static void dcc_error_file_create(GET_DCC_REC *dcc, const char *fname)
{
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_CANT_CREATE, fname);
}
static void dcc_error_get_not_found(const char *nick)
{
g_return_if_fail(nick != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_GET_NOT_FOUND, nick);
}
static void dcc_error_close_not_found(const char *type, const char *nick,
const char *fname)
{
g_return_if_fail(type != NULL);
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
if (g_strcasecmp(type, "GET") != 0) return;
if (fname == '\0') fname = "(ANY)";
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_NOT_FOUND, nick, fname);
}
static void sig_dcc_list_print(GET_DCC_REC *dcc)
{
if (IS_DCC_GET(dcc))
dcc_list_print_file((FILE_DCC_REC *) dcc);
}
void fe_dcc_get_init(void)
{
signal_add("dcc request", (SIGNAL_FUNC) dcc_request);
signal_add("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_add("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_add("dcc error file create", (SIGNAL_FUNC) dcc_error_file_create);
signal_add("dcc error get not found", (SIGNAL_FUNC) dcc_error_get_not_found);
signal_add("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_add("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
}
void fe_dcc_get_deinit(void)
{
signal_remove("dcc request", (SIGNAL_FUNC) dcc_request);
signal_remove("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_remove("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_remove("dcc error file create", (SIGNAL_FUNC) dcc_error_file_create);
signal_remove("dcc error get not found", (SIGNAL_FUNC) dcc_error_get_not_found);
signal_remove("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_remove("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
}

View File

@ -0,0 +1,143 @@
/*
fe-dcc-send.c : irssi
Copyright (C) 1999-2001 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 "signals.h"
#include "levels.h"
#include "dcc-file.h"
#include "dcc-send.h"
#include "module-formats.h"
#include "printtext.h"
#include "completion.h"
#include "fe-dcc.h"
static void dcc_connected(SEND_DCC_REC *dcc)
{
if (!IS_DCC_SEND(dcc)) return;
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_CONNECTED,
dcc->arg, dcc->nick, dcc->addrstr, dcc->port);
}
static void dcc_closed(SEND_DCC_REC *dcc)
{
double kbs;
time_t secs;
if (!IS_DCC_SEND(dcc)) return;
secs = dcc->starttime == 0 ? -1 : time(NULL)-dcc->starttime;
kbs = (double) (dcc->transfd-dcc->skipped) /
(secs == 0 ? 1 : secs) / 1024.0;
if (secs == -1) {
/* aborted */
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_ABORTED,
dcc->arg, dcc->nick);
} else {
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_COMPLETE,
dcc->arg, dcc->transfd/1024, dcc->nick,
(long) secs, kbs);
}
}
static void dcc_error_file_not_found(const char *nick, const char *fname)
{
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_FILE_NOT_FOUND, fname);
}
static void dcc_error_send_exists(const char *nick, const char *fname)
{
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_EXISTS, fname, nick);
}
static void dcc_error_close_not_found(const char *type, const char *nick,
const char *fname)
{
g_return_if_fail(type != NULL);
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
if (g_strcasecmp(type, "SEND") != 0) return;
if (fname == '\0') fname = "(ANY)";
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_NOT_FOUND, nick, fname);
}
static void sig_dcc_send_complete(GList **list, WINDOW_REC *window,
const char *word, const char *line,
int *want_space)
{
g_return_if_fail(list != NULL);
g_return_if_fail(word != NULL);
g_return_if_fail(line != NULL);
if (*line == '\0' || strchr(line, ' ') != NULL)
return;
/* completing filename parameter for /DCC SEND */
*list = filename_complete(word);
if (*list != NULL) {
*want_space = FALSE;
signal_stop();
}
}
static void sig_dcc_list_print(SEND_DCC_REC *dcc)
{
if (IS_DCC_SEND(dcc))
dcc_list_print_file((FILE_DCC_REC *) dcc);
}
void fe_dcc_send_init(void)
{
signal_add("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_add("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_add("dcc error file not found", (SIGNAL_FUNC) dcc_error_file_not_found);
signal_add("dcc error send exists", (SIGNAL_FUNC) dcc_error_send_exists);
signal_add("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_add("complete command dcc send", (SIGNAL_FUNC) sig_dcc_send_complete);
signal_add("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
}
void fe_dcc_send_deinit(void)
{
signal_remove("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_remove("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_remove("dcc error file not found", (SIGNAL_FUNC) dcc_error_file_not_found);
signal_remove("dcc error send exists", (SIGNAL_FUNC) dcc_error_send_exists);
signal_remove("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_remove("complete command dcc send", (SIGNAL_FUNC) sig_dcc_send_complete);
signal_remove("dcc list print", (SIGNAL_FUNC) sig_dcc_list_print);
}

View File

@ -1,7 +1,7 @@
/*
fe-dcc.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
Copyright (C) 1999-2001 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
@ -22,58 +22,39 @@
#include "signals.h"
#include "commands.h"
#include "network.h"
#include "settings.h"
#include "levels.h"
#include "irc.h"
#include "channels.h"
#include "irc-queries.h"
#include "irc/dcc/dcc-chat.h"
#include "completion.h"
#include "chat-completion.h"
#include "themes.h"
#include "fe-windows.h"
#include "dcc-chat.h"
#include "dcc-file.h"
#include "dcc-get.h"
#include "dcc-send.h"
#include "module-formats.h"
#include "printtext.h"
#include "fe-messages.h"
#include "themes.h"
static int autocreate_dccquery;
void fe_dcc_chat_init(void);
void fe_dcc_chat_deinit(void);
static void dcc_connected(DCC_REC *dcc)
void fe_dcc_get_init(void);
void fe_dcc_get_deinit(void);
void fe_dcc_send_init(void);
void fe_dcc_send_deinit(void);
static void dcc_request(DCC_REC *dcc)
{
char *sender;
char *service;
g_return_if_fail(dcc != NULL);
switch (dcc->type) {
case DCC_TYPE_CHAT:
sender = g_strconcat("=", dcc->chat_id, NULL);
if (dcc->port < 1024) {
/* warn about connecting to lowports */
service = net_getservbyport(dcc->port);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_CONNECTED,
dcc->chat_id, dcc->addrstr, dcc->port);
if (query_find(NULL, sender) == NULL) {
if (!autocreate_dccquery)
completion_last_message_add(sender);
else
irc_query_create(dcc->server == NULL ? NULL :
dcc->server->tag,
sender, TRUE);
}
g_free(sender);
break;
case DCC_TYPE_SEND:
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_CONNECTED,
dcc->arg, dcc->nick, dcc->addrstr, dcc->port);
break;
case DCC_TYPE_GET:
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_CONNECTED,
dcc->arg, dcc->nick, dcc->addrstr, dcc->port);
break;
IRCTXT_DCC_LOWPORT, dcc->port,
service != NULL ? service : "unknown");
}
}
@ -85,136 +66,6 @@ static void dcc_rejected(DCC_REC *dcc)
dcc_type2str(dcc->type), dcc->nick, dcc->arg);
}
static void dcc_closed(DCC_REC *dcc)
{
char *sender;
double kbs;
time_t secs;
g_return_if_fail(dcc != NULL);
secs = dcc->starttime == 0 ? -1 : time(NULL)-dcc->starttime;
kbs = (double) (dcc->transfd-dcc->skipped) /
(secs == 0 ? 1 : secs) / 1024.0;
switch (dcc->type) {
case DCC_TYPE_CHAT:
sender = g_strconcat("=", dcc->chat_id, NULL);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_DISCONNECTED, dcc->chat_id);
g_free(sender);
break;
case DCC_TYPE_SEND:
if (secs == -1) {
/* aborted */
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_ABORTED,
dcc->arg, dcc->nick);
} else {
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_COMPLETE,
dcc->arg, dcc->transfd/1024, dcc->nick,
(long) secs, kbs);
}
break;
case DCC_TYPE_GET:
if (secs == -1) {
/* aborted */
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_ABORTED,
dcc->arg, dcc->nick);
} else {
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_COMPLETE,
dcc->arg, dcc->transfd/1024, dcc->nick,
(long) secs, kbs);
}
break;
}
}
static void dcc_chat_action(const char *msg, DCC_REC *dcc)
{
char *sender;
g_return_if_fail(dcc != NULL);
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->chat_id, NULL);
if (query_find(NULL, sender) == NULL)
completion_last_message_add(sender);
printformat(NULL, sender, MSGLEVEL_DCCMSGS,
IRCTXT_ACTION_DCC, dcc->chat_id, msg);
g_free(sender);
}
static void dcc_chat_ctcp(const char *msg, DCC_REC *dcc)
{
char *sender;
g_return_if_fail(dcc != NULL);
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->chat_id, NULL);
printformat(NULL, sender, MSGLEVEL_DCC,
IRCTXT_DCC_CTCP, dcc->chat_id, msg);
g_free(sender);
}
static void dcc_chat_msg(DCC_REC *dcc, const char *msg)
{
QUERY_REC *query;
char *sender, *freemsg;
g_return_if_fail(dcc != NULL);
g_return_if_fail(msg != NULL);
sender = g_strconcat("=", dcc->chat_id, NULL);
query = query_find(NULL, sender);
if (settings_get_bool("emphasis"))
msg = freemsg = expand_emphasis((WI_ITEM_REC *) query, msg);
else
freemsg = NULL;
if (query_find(NULL, sender) == NULL)
completion_last_message_add(sender);
printformat(NULL, sender, MSGLEVEL_DCCMSGS,
query != NULL ? IRCTXT_DCC_MSG_QUERY :
IRCTXT_DCC_MSG, dcc->chat_id, msg);
g_free_not_null(freemsg);
g_free(sender);
}
static void dcc_request(DCC_REC *dcc)
{
g_return_if_fail(dcc != NULL);
switch (dcc->type) {
case DCC_TYPE_CHAT:
printformat(dcc->server, NULL, MSGLEVEL_DCC,
ischannel(*dcc->target) ? IRCTXT_DCC_CHAT_CHANNEL :
IRCTXT_DCC_CHAT, dcc->chat_id, dcc->addrstr,
dcc->port, dcc->target);
break;
case DCC_TYPE_GET:
printformat(dcc->server, NULL, MSGLEVEL_DCC,
ischannel(*dcc->target) ? IRCTXT_DCC_SEND_CHANNEL :
IRCTXT_DCC_SEND, dcc->nick, dcc->addrstr,
dcc->port, dcc->arg, dcc->size, dcc->target);
break;
}
if (dcc->port < 1024) {
char *service = net_getservbyport(dcc->port);
printformat(dcc->server, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_LOWPORT, dcc->port,
service != NULL ? service : "unknown");
}
}
static void dcc_error_connect(DCC_REC *dcc)
{
g_return_if_fail(dcc != NULL);
@ -223,38 +74,6 @@ static void dcc_error_connect(DCC_REC *dcc)
IRCTXT_DCC_CONNECT_ERROR, dcc->addrstr, dcc->port);
}
static void dcc_error_file_create(DCC_REC *dcc, const char *fname)
{
g_return_if_fail(dcc != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_CANT_CREATE, fname);
}
static void dcc_error_file_not_found(const char *nick, const char *fname)
{
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_FILE_NOT_FOUND, fname);
}
static void dcc_error_get_not_found(const char *nick)
{
g_return_if_fail(nick != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_GET_NOT_FOUND, nick);
}
static void dcc_error_send_exists(const char *nick, const char *fname)
{
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_EXISTS, fname, nick);
}
static void dcc_error_unknown_type(const char *type)
{
g_return_if_fail(type != NULL);
@ -262,258 +81,30 @@ static void dcc_error_unknown_type(const char *type)
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_TYPE, type);
}
static void dcc_error_close_not_found(const char *type, const char *nick,
const char *fname)
void dcc_list_print_file(FILE_DCC_REC *dcc)
{
g_return_if_fail(type != NULL);
g_return_if_fail(nick != NULL);
g_return_if_fail(fname != NULL);
time_t going;
if (fname == '\0') fname = "(ANY)";
switch (dcc_str2type(type)) {
case DCC_TYPE_CHAT:
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_CHAT_NOT_FOUND, nick);
break;
case DCC_TYPE_SEND:
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_SEND_NOT_FOUND, nick, fname);
break;
case DCC_TYPE_GET:
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_GET_NOT_FOUND, nick, fname);
break;
}
}
going = time(NULL) - dcc->starttime;
if (going == 0) going = 1; /* no division by zeros :) */
static void dcc_error_ctcp(const char *type, const char *data,
const char *nick, const char *addr,
const char *target)
{
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_INVALID_CTCP, type, nick, addr, target);
}
static void dcc_unknown_ctcp(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, DCC_REC *chat)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_CTCP,
type, nick, args);
cmd_params_free(free_arg);
}
static void dcc_unknown_reply(IRC_SERVER_REC *server, const char *data,
const char *nick)
{
char *type, *args;
void *free_arg;
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&type, &args))
return;
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_UNKNOWN_REPLY,
type, nick, args);
cmd_params_free(free_arg);
}
static void sig_dcc_destroyed(DCC_REC *dcc)
{
QUERY_REC *query;
char *nick;
if (dcc->type != DCC_TYPE_CHAT)
return;
nick = g_strconcat("=", dcc->chat_id, NULL);
query = query_find(NULL, nick);
if (query != NULL) {
/* DCC chat closed, close the query with it. */
if (dcc->connection_lost) query->unwanted = TRUE;
query_destroy(query);
} else {
/* remove nick from msg completion
since it won't work anymore */
completion_last_message_remove(nick);
}
g_free(nick);
}
static void sig_query_destroyed(QUERY_REC *query)
{
DCC_REC *dcc;
if (*query->name != '=')
return;
dcc = dcc_chat_find_id(query->name+1);
if (dcc != NULL && !dcc->destroyed) {
/* DCC query window closed, close the dcc chat too. */
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
}
}
static void cmd_msg(const char *data)
{
QUERY_REC *query;
DCC_REC *dcc;
char *text, *target;
void *free_arg;
g_return_if_fail(data != NULL);
if (*data != '=') {
/* handle only DCC messages */
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
query = query_find(NULL, target);
printformat(NULL, target, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
query != NULL ? IRCTXT_OWN_DCC_QUERY :
IRCTXT_OWN_DCC, dcc->mynick, target+1, text);
if (query == NULL)
completion_last_message_add(target);
}
cmd_params_free(free_arg);
}
static void cmd_me(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
DCC_REC *dcc;
g_return_if_fail(data != NULL);
dcc = item_get_dcc(item);
if (dcc == NULL) return;
printformat(NULL, item->name, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
IRCTXT_OWN_DCC_ACTION_QUERY, dcc->mynick, item->name, data);
}
static void cmd_action(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
QUERY_REC *query;
DCC_REC *dcc;
char *target, *text;
void *free_arg;
g_return_if_fail(data != NULL);
if (*data != '=') {
/* handle only DCC actions */
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
if (*target == '\0' || *text == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
query = query_find(NULL, target);
printformat(NULL, target, MSGLEVEL_DCCMSGS | MSGLEVEL_NOHILIGHT,
query != NULL ? IRCTXT_OWN_DCC_ACTION_QUERY :
IRCTXT_OWN_DCC_ACTION, dcc->mynick, target, text);
if (query == NULL)
completion_last_message_add(target);
}
cmd_params_free(free_arg);
}
static void cmd_ctcp(const char *data, SERVER_REC *server)
{
DCC_REC *dcc;
char *target, *ctcpcmd, *ctcpdata;
void *free_arg;
g_return_if_fail(data != NULL);
if (server == NULL || !server->connected)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
&target, &ctcpcmd, &ctcpdata))
return;
if (*target == '\0' || *ctcpcmd == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target != '=') {
/* handle only DCC CTCPs */
cmd_params_free(free_arg);
return;
}
dcc = dcc_chat_find_id(target+1);
if (dcc == NULL || dcc->sendbuf == NULL) {
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
IRCTXT_DCC_CHAT_NOT_FOUND, target+1);
} else {
g_strup(ctcpcmd);
printformat(server, target, MSGLEVEL_DCC, IRCTXT_OWN_DCC_CTCP,
target, ctcpcmd, ctcpdata);
}
cmd_params_free(free_arg);
IRCTXT_DCC_LIST_LINE_FILE,
dcc->nick, dcc_type2str(dcc->type),
dcc->transfd/1024, dcc->size/1024,
dcc->size == 0 ? 0 : (int)((double)dcc->transfd/(double)dcc->size*100.0),
(double) (dcc->transfd-dcc->skipped)/going/1024, dcc->arg);
}
static void cmd_dcc_list(const char *data)
{
GSList *tmp;
time_t going;
g_return_if_fail(data != NULL);
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_LIST_HEADER);
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
going = time(NULL) - dcc->starttime;
if (going == 0) going = 1; /* no division by zeros :) */
if (dcc->type == DCC_TYPE_CHAT) {
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_LIST_LINE_CHAT,
dcc->chat_id, dcc_type2str(dcc->type));
} else {
printformat(NULL, NULL, MSGLEVEL_DCC,
IRCTXT_DCC_LIST_LINE_FILE,
dcc->nick, dcc_type2str(dcc->type),
dcc->transfd/1024, dcc->size/1024,
dcc->size == 0 ? 0 : (int)((double)dcc->transfd/(double)dcc->size*100.0),
(double) (dcc->transfd-dcc->skipped)/going/1024, dcc->arg);
}
}
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next)
signal_emit("dcc list print", 1, tmp->data);
printformat(NULL, NULL, MSGLEVEL_DCC, IRCTXT_DCC_LIST_FOOTER);
}
@ -525,96 +116,34 @@ static void cmd_dcc(const char *data)
}
}
static void sig_dcc_send_complete(GList **list, WINDOW_REC *window,
const char *word, const char *line,
int *want_space)
{
g_return_if_fail(list != NULL);
g_return_if_fail(word != NULL);
g_return_if_fail(line != NULL);
if (*line == '\0' || strchr(line, ' ') != NULL)
return;
/* completing filename parameter for /DCC SEND */
*list = filename_complete(word);
if (*list != NULL) {
*want_space = FALSE;
signal_stop();
}
}
static void read_settings(void)
{
int level;
level = level2bits(settings_get_str("autocreate_query_level"));
autocreate_dccquery = (level & MSGLEVEL_DCCMSGS) != 0;
}
void fe_irc_dcc_init(void)
{
signal_add("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_add("dcc rejected", (SIGNAL_FUNC) dcc_rejected);
signal_add("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_add("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_add("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_add("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
fe_dcc_chat_init();
fe_dcc_get_init();
fe_dcc_send_init();
signal_add("dcc request", (SIGNAL_FUNC) dcc_request);
signal_add("dcc rejected", (SIGNAL_FUNC) dcc_rejected);
signal_add("dcc error connect", (SIGNAL_FUNC) dcc_error_connect);
signal_add("dcc error file create", (SIGNAL_FUNC) dcc_error_file_create);
signal_add("dcc error file not found", (SIGNAL_FUNC) dcc_error_file_not_found);
signal_add("dcc error get not found", (SIGNAL_FUNC) dcc_error_get_not_found);
signal_add("dcc error send exists", (SIGNAL_FUNC) dcc_error_send_exists);
signal_add("dcc error unknown type", (SIGNAL_FUNC) dcc_error_unknown_type);
signal_add("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_add("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_add("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_add("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_add("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_add("complete command dcc send", (SIGNAL_FUNC) sig_dcc_send_complete);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
command_bind("msg", NULL, (SIGNAL_FUNC) cmd_msg);
command_bind("me", NULL, (SIGNAL_FUNC) cmd_me);
command_bind("action", NULL, (SIGNAL_FUNC) cmd_action);
command_bind("ctcp", NULL, (SIGNAL_FUNC) cmd_ctcp);
command_bind("dcc", NULL, (SIGNAL_FUNC) cmd_dcc);
command_bind("dcc list", NULL, (SIGNAL_FUNC) cmd_dcc_list);
theme_register(fecommon_irc_dcc_formats);
read_settings();
}
void fe_irc_dcc_deinit(void)
{
fe_dcc_chat_deinit();
fe_dcc_get_deinit();
fe_dcc_send_deinit();
theme_unregister();
signal_remove("dcc connected", (SIGNAL_FUNC) dcc_connected);
signal_remove("dcc rejected", (SIGNAL_FUNC) dcc_rejected);
signal_remove("dcc closed", (SIGNAL_FUNC) dcc_closed);
signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_remove("dcc ctcp action", (SIGNAL_FUNC) dcc_chat_action);
signal_remove("default dcc ctcp", (SIGNAL_FUNC) dcc_chat_ctcp);
signal_remove("dcc request", (SIGNAL_FUNC) dcc_request);
signal_remove("dcc rejected", (SIGNAL_FUNC) dcc_rejected);
signal_remove("dcc error connect", (SIGNAL_FUNC) dcc_error_connect);
signal_remove("dcc error file create", (SIGNAL_FUNC) dcc_error_file_create);
signal_remove("dcc error file not found", (SIGNAL_FUNC) dcc_error_file_not_found);
signal_remove("dcc error get not found", (SIGNAL_FUNC) dcc_error_get_not_found);
signal_remove("dcc error send exists", (SIGNAL_FUNC) dcc_error_send_exists);
signal_remove("dcc error unknown type", (SIGNAL_FUNC) dcc_error_unknown_type);
signal_remove("dcc error close not found", (SIGNAL_FUNC) dcc_error_close_not_found);
signal_remove("dcc error ctcp", (SIGNAL_FUNC) dcc_error_ctcp);
signal_remove("default ctcp msg dcc", (SIGNAL_FUNC) dcc_unknown_ctcp);
signal_remove("default ctcp reply dcc", (SIGNAL_FUNC) dcc_unknown_reply);
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_remove("query destroyed", (SIGNAL_FUNC) sig_query_destroyed);
signal_remove("complete command dcc send", (SIGNAL_FUNC) sig_dcc_send_complete);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
command_unbind("msg", (SIGNAL_FUNC) cmd_msg);
command_unbind("me", (SIGNAL_FUNC) cmd_me);
command_unbind("action", (SIGNAL_FUNC) cmd_action);
command_unbind("ctcp", (SIGNAL_FUNC) cmd_ctcp);
command_unbind("dcc", (SIGNAL_FUNC) cmd_dcc);
command_unbind("dcc list", (SIGNAL_FUNC) cmd_dcc_list);
}

View File

@ -0,0 +1,6 @@
#ifndef __FE_DCC_H
#define __FE_DCC_H
void dcc_list_print_file(FILE_DCC_REC *dcc);
#endif

View File

@ -15,7 +15,11 @@ libirc_dcc_la_SOURCES = \
dcc-autoget.c
noinst_HEADERS = \
dcc-rec.h \
dcc-file-rec.h \
dcc.h \
dcc-file.h \
dcc-chat.h \
dcc-get.h \
dcc-send.h \
module.h

View File

@ -25,15 +25,14 @@
#include "dcc-get.h"
static void sig_dcc_request(DCC_REC *dcc, const char *nickaddr)
static void sig_dcc_request(GET_DCC_REC *dcc, const char *nickaddr)
{
struct stat statbuf;
const char *masks;
char *str, *file;
int max_size;
g_return_if_fail(dcc != NULL);
if (dcc->type != DCC_TYPE_GET) return;
if (!IS_DCC_GET(dcc)) return;
/* check if we want to autoget file offer */
if (!settings_get_bool("dcc_autoget") &&

View File

@ -1,7 +1,7 @@
/*
dcc-chat.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
Copyright (C) 1999-2001 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
@ -33,51 +33,108 @@
#include "servers-setup.h"
#include "irc-queries.h"
#include "dcc.h"
#include "dcc-chat.h"
DCC_REC *dcc_chat_find_id(const char *id)
static char *dcc_chat_get_new_id(const char *nick)
{
char *id;
int num;
g_return_val_if_fail(nick != NULL, NULL);
if (dcc_chat_find_id(nick) == NULL) {
/* same as nick, good */
return g_strdup(nick);
}
/* keep adding numbers after nick until some of them isn't found */
for (num = 2;; num++) {
id = g_strdup_printf("%s%d", nick, num);
if (dcc_chat_find_id(id) == NULL)
return id;
g_free(id);
}
}
static CHAT_DCC_REC *dcc_chat_create(IRC_SERVER_REC *server,
CHAT_DCC_REC *chat,
const char *nick, const char *arg)
{
CHAT_DCC_REC *dcc;
dcc = g_new0(CHAT_DCC_REC, 1);
dcc->orig_type = dcc->type = DCC_CHAT_TYPE;
dcc->mirc_ctcp = settings_get_bool("dcc_mirc_ctcp");
dcc->id = dcc_chat_get_new_id(nick);
dcc_init_rec(DCC(dcc), server, chat, nick, arg);
return dcc;
}
static void dcc_remove_chat_refs(CHAT_DCC_REC *dcc)
{
GSList *tmp;
g_return_if_fail(dcc != NULL);
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *rec = tmp->data;
if (rec->chat == dcc)
rec->chat = NULL;
}
}
static void sig_dcc_destroyed(CHAT_DCC_REC *dcc)
{
if (!IS_DCC_CHAT(dcc)) return;
dcc_remove_chat_refs(dcc);
if (dcc->sendbuf != NULL) net_sendbuffer_destroy(dcc->sendbuf, FALSE);
line_split_free((LINEBUF_REC *) dcc->databuf);
dcc->databuf = NULL;
g_free(dcc->id);
}
CHAT_DCC_REC *dcc_chat_find_id(const char *id)
{
GSList *tmp;
g_return_val_if_fail(id != NULL, NULL);
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
CHAT_DCC_REC *dcc = tmp->data;
if (dcc->type == DCC_TYPE_CHAT && dcc->chat_id != NULL &&
g_strcasecmp(dcc->chat_id, id) == 0)
if (IS_DCC_CHAT(dcc) && g_strcasecmp(dcc->id, id) == 0)
return dcc;
}
return NULL;
}
static void dcc_chat_set_id(DCC_REC *dcc)
static CHAT_DCC_REC *dcc_chat_find_nick(IRC_SERVER_REC *server,
const char *nick)
{
char *id;
int num;
GSList *tmp;
if (dcc_chat_find_id(dcc->nick) == NULL) {
/* same as nick, good */
dcc->chat_id = g_strdup(dcc->nick);
return;
g_return_val_if_fail(nick != NULL, NULL);
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
CHAT_DCC_REC *dcc = tmp->data;
if (IS_DCC_CHAT(dcc) && dcc->server == server &&
g_strcasecmp(dcc->nick, nick) == 0)
return dcc;
}
/* keep adding numbers after nick until some of them isn't found */
for (num = 2;; num++) {
id = g_strdup_printf("%s%d", dcc->nick, num);
if (dcc_chat_find_id(id) == NULL) {
dcc->chat_id = id;
break;
}
g_free(id);
}
return NULL;
}
/* Send `data' to dcc chat. */
void dcc_chat_send(DCC_REC *dcc, const char *data)
void dcc_chat_send(CHAT_DCC_REC *dcc, const char *data)
{
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(dcc->sendbuf != NULL);
g_return_if_fail(data != NULL);
@ -85,8 +142,28 @@ void dcc_chat_send(DCC_REC *dcc, const char *data)
net_sendbuffer_send(dcc->sendbuf, "\n", 1);
}
/* Send a CTCP message/notify to target.
Send the CTCP via DCC chat if `chat' is specified. */
void dcc_ctcp_message(IRC_SERVER_REC *server, const char *target,
CHAT_DCC_REC *chat, int notice, const char *msg)
{
char *str;
if (chat != NULL && chat->sendbuf != NULL) {
/* send it via open DCC chat */
str = g_strdup_printf("%s\001%s\001", chat->mirc_ctcp ? "" :
notice ? "CTCP_REPLY " :
"CTCP_MESSAGE ", msg);
dcc_chat_send(chat, str);
g_free(str);
} else {
irc_send_cmdv(server, "%s %s :\001%s\001",
notice ? "NOTICE" : "PRIVMSG", target, msg);
}
}
/* If `item' is a query of a =nick, return DCC chat record of nick */
DCC_REC *item_get_dcc(WI_ITEM_REC *item)
CHAT_DCC_REC *item_get_dcc(WI_ITEM_REC *item)
{
QUERY_REC *query;
@ -100,7 +177,7 @@ DCC_REC *item_get_dcc(WI_ITEM_REC *item)
/* Send text to DCC chat */
static void cmd_msg(const char *data)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
char *text, *target;
void *free_arg;
@ -111,10 +188,11 @@ static void cmd_msg(const char *data)
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &text))
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
dcc = dcc_chat_find_id(++target);
dcc = dcc_chat_find_id(target+1);
if (dcc != NULL && dcc->sendbuf != NULL)
dcc_chat_send(dcc, text);
@ -124,7 +202,7 @@ static void cmd_msg(const char *data)
static void cmd_me(const char *data, IRC_SERVER_REC *server, QUERY_REC *item)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
char *str;
g_return_if_fail(data != NULL);
@ -141,7 +219,7 @@ static void cmd_me(const char *data, IRC_SERVER_REC *server, QUERY_REC *item)
static void cmd_action(const char *data, IRC_SERVER_REC *server)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
char *target, *text, *str;
void *free_arg;
@ -152,9 +230,11 @@ static void cmd_action(const char *data, IRC_SERVER_REC *server)
return;
}
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST, &target, &text))
if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST,
&target, &text))
return;
if (*target == '\0' || *text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target == '\0' || *text == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
dcc = dcc_chat_find_id(target+1);
if (dcc != NULL) {
@ -169,16 +249,19 @@ static void cmd_action(const char *data, IRC_SERVER_REC *server)
static void cmd_ctcp(const char *data, IRC_SERVER_REC *server)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
char *target, *ctcpcmd, *ctcpdata, *str;
void *free_arg;
g_return_if_fail(data != NULL);
if (server == NULL || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED);
if (server == NULL || !server->connected)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST, &target, &ctcpcmd, &ctcpdata))
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
&target, &ctcpcmd, &ctcpdata))
return;
if (*target == '\0' || *ctcpcmd == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target == '\0' || *ctcpcmd == '\0')
cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
if (*target != '=') {
/* handle only DCC CTCPs */
@ -200,12 +283,12 @@ static void cmd_ctcp(const char *data, IRC_SERVER_REC *server)
}
/* input function: DCC CHAT received some data.. */
static void dcc_chat_input(DCC_REC *dcc)
static void dcc_chat_input(CHAT_DCC_REC *dcc)
{
char tmpbuf[512], *str;
int recvlen, ret;
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
do {
recvlen = net_receive(dcc->handle, tmpbuf, sizeof(tmpbuf));
@ -214,8 +297,7 @@ static void dcc_chat_input(DCC_REC *dcc)
if (ret == -1) {
/* connection lost */
dcc->connection_lost = TRUE;
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
dcc_close(DCC(dcc));
break;
}
@ -227,13 +309,13 @@ static void dcc_chat_input(DCC_REC *dcc)
}
/* input function: DCC CHAT - someone tried to connect to our socket */
static void dcc_chat_listen(DCC_REC *dcc)
static void dcc_chat_listen(CHAT_DCC_REC *dcc)
{
IPADDR ip;
GIOChannel *handle;
int port;
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
/* accept connection */
handle = net_accept(dcc->handle, &ip, &port);
@ -258,14 +340,14 @@ static void dcc_chat_listen(DCC_REC *dcc)
}
/* callback: DCC CHAT - net_connect_nonblock() finished */
static void sig_chat_connected(DCC_REC *dcc)
static void sig_chat_connected(CHAT_DCC_REC *dcc)
{
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
if (net_geterror(dcc->handle) != 0) {
/* error connecting */
signal_emit("dcc error connect", 1, dcc);
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
return;
}
@ -279,9 +361,9 @@ static void sig_chat_connected(DCC_REC *dcc)
signal_emit("dcc connected", 1, dcc);
}
static void dcc_chat_connect(DCC_REC *dcc)
static void dcc_chat_connect(CHAT_DCC_REC *dcc)
{
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
if (dcc->addrstr[0] == '\0' ||
dcc->starttime != 0 || dcc->handle != NULL) {
@ -298,7 +380,7 @@ static void dcc_chat_connect(DCC_REC *dcc)
} else {
/* error connecting */
signal_emit("dcc error connect", 1, dcc);
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
}
}
@ -306,7 +388,7 @@ static void dcc_chat_connect(DCC_REC *dcc)
static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server)
{
void *free_arg;
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
IPADDR own_ip;
GIOChannel *handle;
char *nick, host[MAX_IP_LEN];
@ -318,7 +400,7 @@ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server)
return;
if (*nick == '\0') {
dcc = dcc_find_request_latest(DCC_TYPE_CHAT);
dcc = DCC_CHAT(dcc_find_request_latest(DCC_CHAT_TYPE));
if (dcc != NULL)
dcc_chat_connect(dcc);
cmd_params_free(free_arg);
@ -338,7 +420,7 @@ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server)
dcc->server == server) {
/* sending request again even while old request is
still waiting, remove it. */
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
}
/* start listening */
@ -350,14 +432,13 @@ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server)
if (handle == NULL)
cmd_param_error(CMDERR_ERRNO);
dcc = dcc_create(DCC_TYPE_CHAT, nick, "chat", server, NULL);
dcc_chat_set_id(dcc);
dcc = dcc_chat_create(server, NULL, nick, "chat");
dcc->handle = handle;
dcc->tagconn = g_input_add(dcc->handle, G_INPUT_READ,
(GInputFunction) dcc_chat_listen, dcc);
/* send the chat request */
dcc_make_address(&own_ip, host);
dcc_ip2str(&own_ip, host);
irc_send_cmdv(server, "PRIVMSG %s :\001DCC CHAT CHAT %s %d\001",
nick, host, port);
@ -368,7 +449,7 @@ static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server)
static void cmd_mircdcc(const char *data, IRC_SERVER_REC *server,
QUERY_REC *item)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
g_return_if_fail(data != NULL);
@ -376,7 +457,65 @@ static void cmd_mircdcc(const char *data, IRC_SERVER_REC *server,
if (dcc == NULL) return;
dcc->mirc_ctcp = toupper(*data) != 'N' &&
g_strncasecmp(data, "OF", 3) != 0;
g_strncasecmp(data, "OF", 2) != 0;
}
/* DCC CLOSE CHAT <nick> - check only from chat_ids in open DCC chats,
the default handler will check from DCC chat requests */
static void cmd_dcc_close(char *data, IRC_SERVER_REC *server)
{
GSList *tmp, *next;
char *nick;
void *free_arg;
int found;
g_return_if_fail(data != NULL);
if (g_strncasecmp(data, "CHAT ", 5) != 0 ||
!cmd_get_params(data, &free_arg, 2, NULL, &nick))
return;
if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
found = FALSE;
for (tmp = dcc_conns; tmp != NULL; tmp = next) {
CHAT_DCC_REC *dcc = tmp->data;
next = tmp->next;
if (IS_DCC_CHAT(dcc) && dcc->id != NULL &&
g_strcasecmp(dcc->id, nick) == 0) {
found = TRUE;
if (!dcc_is_connected(dcc))
dcc_reject(DCC(dcc), server);
else {
/* don't send DCC REJECT after DCC chat
is already open */
dcc_close(DCC(dcc));
}
}
}
if (found) signal_stop();
cmd_params_free(free_arg);
}
static void cmd_whois(const char *data, IRC_SERVER_REC *server,
WI_ITEM_REC *item)
{
CHAT_DCC_REC *dcc;
g_return_if_fail(data != NULL);
/* /WHOIS without target in DCC CHAT query? */
if (*data == '\0') {
dcc = item_get_dcc(item);
if (dcc != NULL) {
signal_emit("command whois", 3,
dcc->nick, server, item);
signal_stop();
}
}
}
#define DCC_AUTOACCEPT_PORT(dcc) \
@ -391,9 +530,9 @@ static void cmd_mircdcc(const char *data, IRC_SERVER_REC *server,
/* CTCP: DCC CHAT */
static void ctcp_msg_dcc_chat(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, DCC_REC *chat)
const char *target, CHAT_DCC_REC *chat)
{
DCC_REC *dcc;
CHAT_DCC_REC *dcc;
char **params;
int paramcount;
int autoallow = FALSE;
@ -407,25 +546,24 @@ static void ctcp_msg_dcc_chat(IRC_SERVER_REC *server, const char *data,
return;
}
dcc = dcc_find_request(DCC_TYPE_CHAT, nick, NULL);
dcc = DCC_CHAT(dcc_find_request(DCC_CHAT_TYPE, nick, NULL));
if (dcc != NULL) {
if (dcc_is_listening(dcc)) {
/* we requested dcc chat, they requested
dcc chat from us .. allow it. */
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
autoallow = TRUE;
} else {
/* we already have one dcc chat request
from this nick, remove it. */
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
}
}
dcc = dcc_create(DCC_TYPE_CHAT, nick, params[0], server, chat);
dcc_chat_set_id(dcc);
dcc = dcc_chat_create(server, chat, nick, params[0]);
dcc->target = g_strdup(target);
dcc->port = atoi(params[2]);
dcc_get_address(params[1], &dcc->addr);
dcc_str2ip(params[1], &dcc->addr);
net_ip2host(&dcc->addr, dcc->addrstr);
signal_emit("dcc request", 2, dcc, addr);
@ -437,12 +575,12 @@ static void ctcp_msg_dcc_chat(IRC_SERVER_REC *server, const char *data,
}
/* DCC CHAT: text received */
static void dcc_chat_msg(DCC_REC *dcc, const char *msg)
static void dcc_chat_msg(CHAT_DCC_REC *dcc, const char *msg)
{
char *cmd, *ptr;
int reply;
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
g_return_if_fail(msg != NULL);
reply = FALSE;
@ -481,26 +619,84 @@ static void dcc_chat_msg(DCC_REC *dcc, const char *msg)
signal_stop();
}
static void dcc_ctcp_redirect(const char *msg, DCC_REC *dcc)
static void dcc_ctcp_redirect(const char *msg, CHAT_DCC_REC *dcc)
{
g_return_if_fail(msg != NULL);
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
signal_emit("ctcp msg dcc", 6, dcc->server, msg,
dcc->nick, "dcc", dcc->mynick, dcc);
}
static void dcc_ctcp_reply_redirect(const char *msg, DCC_REC *dcc)
static void dcc_ctcp_reply_redirect(const char *msg, CHAT_DCC_REC *dcc)
{
g_return_if_fail(msg != NULL);
g_return_if_fail(dcc != NULL);
g_return_if_fail(IS_DCC_CHAT(dcc));
signal_emit("ctcp reply dcc", 6, dcc->server, msg,
dcc->nick, "dcc", dcc->mynick, dcc);
}
/* CTCP REPLY: REJECT */
static void ctcp_reply_dcc_reject(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
DCC_REC *chat)
{
DCC_REC *dcc;
/* default REJECT handler checks args too -
we don't care about it in DCC chats. */
if (g_strncasecmp(data, "CHAT", 4) == 0 &&
(data[4] == '\0' || data[4] == ' ')) {
dcc = dcc_find_request(DCC_CHAT_TYPE, nick, NULL);
if (dcc != NULL) dcc_close(dcc);
signal_stop();
}
}
static void event_nick(IRC_SERVER_REC *server, const char *data,
const char *orignick)
{
QUERY_REC *query;
CHAT_DCC_REC *dcc;
char *params, *nick, *tag;
g_return_if_fail(data != NULL);
params = event_get_params(data, 1, &nick);
if (g_strcasecmp(nick, orignick) == 0) {
/* shouldn't happen, but just to be sure irssi doesn't
get into infinite loop */
g_free(params);
return;
}
while ((dcc = dcc_chat_find_nick(server, orignick)) != NULL) {
g_free(dcc->nick);
dcc->nick = g_strdup(nick);
tag = g_strconcat("=", dcc->id, NULL);
query = irc_query_find(server, tag);
g_free(tag);
/* change the id too */
g_free(dcc->id);
dcc->id = dcc_chat_get_new_id(nick);
if (query != NULL) {
tag = g_strconcat("=", dcc->id, NULL);
query_change_nick(query, tag);
g_free(tag);
}
}
g_free(params);
}
void dcc_chat_init(void)
{
dcc_register_type("CHAT");
settings_add_bool("dcc", "dcc_mirc_ctcp", FALSE);
settings_add_str("dcc", "dcc_autochat_masks", "");
command_bind("msg", NULL, (SIGNAL_FUNC) cmd_msg);
@ -509,22 +705,33 @@ void dcc_chat_init(void)
command_bind("ctcp", NULL, (SIGNAL_FUNC) cmd_ctcp);
command_bind("dcc chat", NULL, (SIGNAL_FUNC) cmd_dcc_chat);
command_bind("mircdcc", NULL, (SIGNAL_FUNC) cmd_mircdcc);
command_bind("dcc close", NULL, (SIGNAL_FUNC) cmd_dcc_close);
command_bind("whois", NULL, (SIGNAL_FUNC) cmd_whois);
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_add("ctcp msg dcc chat", (SIGNAL_FUNC) ctcp_msg_dcc_chat);
signal_add_first("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_add("dcc ctcp dcc", (SIGNAL_FUNC) dcc_ctcp_redirect);
signal_add("dcc reply dcc", (SIGNAL_FUNC) dcc_ctcp_reply_redirect);
signal_add("ctcp reply dcc reject", (SIGNAL_FUNC) ctcp_reply_dcc_reject);
signal_add("event nick", (SIGNAL_FUNC) event_nick);
}
void dcc_chat_deinit(void)
{
dcc_unregister_type("CHAT");
command_unbind("msg", (SIGNAL_FUNC) cmd_msg);
command_unbind("me", (SIGNAL_FUNC) cmd_me);
command_unbind("action", (SIGNAL_FUNC) cmd_action);
command_unbind("ctcp", (SIGNAL_FUNC) cmd_ctcp);
command_unbind("dcc chat", (SIGNAL_FUNC) cmd_dcc_chat);
command_unbind("mircdcc", (SIGNAL_FUNC) cmd_mircdcc);
command_unbind("dcc close", (SIGNAL_FUNC) cmd_dcc_close);
command_unbind("whois", (SIGNAL_FUNC) cmd_whois);
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_remove("ctcp msg dcc chat", (SIGNAL_FUNC) ctcp_msg_dcc_chat);
signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg);
signal_remove("dcc ctcp dcc", (SIGNAL_FUNC) dcc_ctcp_redirect);
signal_remove("dcc reply dcc", (SIGNAL_FUNC) dcc_ctcp_reply_redirect);
signal_remove("ctcp reply dcc reject", (SIGNAL_FUNC) ctcp_reply_dcc_reject);
signal_remove("event nick", (SIGNAL_FUNC) event_nick);
}

View File

@ -3,7 +3,36 @@
#include "dcc.h"
DCC_REC *dcc_chat_find_id(const char *id);
#define DCC_CHAT(dcc) \
MODULE_CHECK_CAST_MODULE(dcc, CHAT_DCC_REC, type, "DCC", "CHAT")
#define IS_DCC_CHAT(dcc) \
(DCC_CHAT(dcc) ? TRUE : FALSE)
struct CHAT_DCC_REC {
#include "dcc-rec.h"
char *id; /* unique identifier - usually same as nick. */
NET_SENDBUF_REC *sendbuf;
unsigned int mirc_ctcp:1; /* Send CTCPs without the CTCP_MESSAGE prefix */
unsigned int connection_lost:1; /* other side closed connection */
};
#define DCC_CHAT_TYPE module_get_uniq_id_str("DCC", "CHAT")
CHAT_DCC_REC *dcc_chat_find_id(const char *id);
/* Send `data' to dcc chat. */
void dcc_chat_send(CHAT_DCC_REC *dcc, const char *data);
/* Send a CTCP message/notify to target.
Send the CTCP via DCC chat if `chat' is specified. */
void dcc_ctcp_message(IRC_SERVER_REC *server, const char *target,
CHAT_DCC_REC *chat, int notice, const char *msg);
/* If `item' is a query of a =nick, return DCC chat record of nick */
CHAT_DCC_REC *item_get_dcc(WI_ITEM_REC *item);
void dcc_chat_init(void);
void dcc_chat_deinit(void);

View File

@ -0,0 +1,9 @@
#include "dcc-rec.h"
unsigned long size, skipped; /* file size / skipped at start */
int fhandle; /* file handle */
/* counter buffer */
char count_buf[4];
int count_pos;

10
src/irc/dcc/dcc-file.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __DCC_FILE_H
#define __DCC_FILE_H
#include "dcc.h"
typedef struct {
#include "dcc-file-rec.h"
} FILE_DCC_REC;
#endif

View File

@ -30,6 +30,28 @@
static int dcc_file_create_mode;
static GET_DCC_REC *dcc_get_create(IRC_SERVER_REC *server, CHAT_DCC_REC *chat,
const char *nick, const char *arg)
{
GET_DCC_REC *dcc;
dcc = g_new0(GET_DCC_REC, 1);
dcc->orig_type = module_get_uniq_id_str("DCC", "SEND");
dcc->type = module_get_uniq_id_str("DCC", "GET");
dcc->fhandle = -1;
dcc_init_rec(DCC(dcc), server, chat, nick, arg);
return dcc;
}
static void sig_dcc_destroyed(GET_DCC_REC *dcc)
{
if (!IS_DCC_GET(dcc)) return;
g_free_not_null(dcc->file);
if (dcc->fhandle != -1) close(dcc->fhandle);
}
char *dcc_get_download_path(const char *fname)
{
char *str, *downpath;
@ -60,9 +82,9 @@ static char *dcc_get_rename_file(const char *fname)
return ret;
}
static void sig_dccget_send(DCC_REC *dcc);
static void sig_dccget_send(GET_DCC_REC *dcc);
void dcc_get_send_received(DCC_REC *dcc)
void dcc_get_send_received(GET_DCC_REC *dcc)
{
guint32 recd;
@ -86,7 +108,7 @@ void dcc_get_send_received(DCC_REC *dcc)
}
/* input function: DCC GET is free to send data */
static void sig_dccget_send(DCC_REC *dcc)
static void sig_dccget_send(GET_DCC_REC *dcc)
{
guint32 recd;
int ret;
@ -115,12 +137,10 @@ static void sig_dccget_send(DCC_REC *dcc)
}
/* input function: DCC GET received data */
static void sig_dccget_receive(DCC_REC *dcc)
static void sig_dccget_receive(GET_DCC_REC *dcc)
{
int ret;
g_return_if_fail(dcc != NULL);
for (;;) {
ret = net_receive(dcc->handle, dcc->databuf, dcc->databufsize);
if (ret == 0) break;
@ -128,8 +148,7 @@ static void sig_dccget_receive(DCC_REC *dcc)
if (ret < 0) {
/* socket closed - transmit complete,
or other side died.. */
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
dcc_close(DCC(dcc));
return;
}
@ -145,17 +164,15 @@ static void sig_dccget_receive(DCC_REC *dcc)
}
/* callback: net_connect() finished for DCC GET */
static void sig_dccget_connected(DCC_REC *dcc)
static void sig_dccget_connected(GET_DCC_REC *dcc)
{
struct stat statbuf;
char *fname;
g_return_if_fail(dcc != NULL);
if (net_geterror(dcc->handle) != 0) {
/* error connecting */
signal_emit("dcc error connect", 1, dcc);
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
return;
}
@ -181,7 +198,7 @@ static void sig_dccget_connected(DCC_REC *dcc)
if (dcc->fhandle == -1) {
signal_emit("dcc error file create", 2,
dcc, dcc->file);
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
return;
}
}
@ -196,7 +213,7 @@ static void sig_dccget_connected(DCC_REC *dcc)
signal_emit("dcc connected", 1, dcc);
}
void dcc_get_connect(DCC_REC *dcc)
void dcc_get_connect(GET_DCC_REC *dcc)
{
if (dcc->get_type == DCC_GET_DEFAULT) {
dcc->get_type = settings_get_bool("dcc_autorename") ?
@ -215,7 +232,7 @@ void dcc_get_connect(DCC_REC *dcc)
} else {
/* error connecting */
signal_emit("dcc error connect", 1, dcc);
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
}
}
@ -258,9 +275,9 @@ int get_file_params_count(char **params, int paramcount)
/* CTCP: DCC SEND */
static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, DCC_REC *chat)
const char *target, CHAT_DCC_REC *chat)
{
DCC_REC *dcc;
GET_DCC_REC *dcc;
IPADDR ip;
char **params, *fname;
int paramcount, fileparams;
@ -280,7 +297,7 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
fileparams = get_file_params_count(params, paramcount);
dcc_get_address(params[fileparams], &ip);
dcc_str2ip(params[fileparams], &ip);
port = atoi(params[fileparams+1]);
size = atol(params[fileparams+2]);
@ -296,13 +313,13 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
quoted = TRUE;
}
dcc = dcc_find_request(DCC_TYPE_GET, nick, fname);
dcc = DCC_GET(dcc_find_request(DCC_GET_TYPE, nick, fname));
if (dcc != NULL) {
/* same DCC request offered again, remove the old one */
dcc_destroy(dcc);
dcc_destroy(DCC(dcc));
}
dcc = dcc_create(DCC_TYPE_GET, nick, fname, server, chat);
dcc = dcc_get_create(server, chat, nick, fname);
dcc->target = g_strdup(target);
memcpy(&dcc->addr, &ip, sizeof(ip));
net_ip2host(&dcc->addr, dcc->addrstr);
@ -316,9 +333,9 @@ static void ctcp_msg_dcc_send(IRC_SERVER_REC *server, const char *data,
}
/* handle receiving DCC - GET/RESUME. */
void cmd_dcc_receive(const char *data, DCC_GET_FUNC accept)
void cmd_dcc_receive(const char *data, DCC_GET_FUNC accept_func)
{
DCC_REC *dcc;
GET_DCC_REC *dcc;
GSList *tmp, *next;
char *nick, *fname;
void *free_arg;
@ -331,23 +348,23 @@ void cmd_dcc_receive(const char *data, DCC_GET_FUNC accept)
return;
if (*nick == '\0') {
dcc = dcc_find_request_latest(DCC_TYPE_GET);
dcc = DCC_GET(dcc_find_request_latest(DCC_GET_TYPE));
if (dcc != NULL)
accept(dcc);
accept_func(dcc);
cmd_params_free(free_arg);
return;
}
found = FALSE;
for (tmp = dcc_conns; tmp != NULL; tmp = next) {
DCC_REC *dcc = tmp->data;
GET_DCC_REC *dcc = tmp->data;
next = tmp->next;
if (dcc_is_waiting_get(dcc) &&
if (IS_DCC_GET(dcc) && dcc_is_waiting_user(dcc) &&
g_strcasecmp(dcc->nick, nick) == 0 &&
(*fname == '\0' || strcmp(dcc->arg, fname) == 0)) {
found = TRUE;
accept(dcc);
accept_func(dcc);
}
}
@ -371,11 +388,13 @@ static void read_settings(void)
void dcc_get_init(void)
{
dcc_register_type("GET");
settings_add_bool("dcc", "dcc_autorename", FALSE);
settings_add_str("dcc", "dcc_download_path", "~");
settings_add_int("dcc", "dcc_file_create_mode", 644);
read_settings();
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_add("ctcp msg dcc send", (SIGNAL_FUNC) ctcp_msg_dcc_send);
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
command_bind("dcc get", NULL, (SIGNAL_FUNC) cmd_dcc_get);
@ -383,6 +402,8 @@ void dcc_get_init(void)
void dcc_get_deinit(void)
{
dcc_unregister_type("GET");
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
signal_remove("ctcp msg dcc send", (SIGNAL_FUNC) ctcp_msg_dcc_send);
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
command_unbind("dcc get", (SIGNAL_FUNC) cmd_dcc_get);

View File

@ -3,6 +3,12 @@
#include "dcc.h"
#define DCC_GET(dcc) \
MODULE_CHECK_CAST_MODULE(dcc, GET_DCC_REC, type, "DCC", "GET")
#define IS_DCC_GET(dcc) \
(DCC_GET(dcc) ? TRUE : FALSE)
enum {
DCC_GET_DEFAULT,
@ -11,17 +17,25 @@ enum {
DCC_GET_RESUME
};
typedef void (*DCC_GET_FUNC) (DCC_REC *);
typedef struct {
#include "dcc-file-rec.h"
int get_type; /* what to do if file exists? */
char *file; /* file name we're really moving, arg is just the reference */
unsigned int file_quoted:1; /* file name was received quoted ("file name") */
} GET_DCC_REC;
#define DCC_GET_TYPE module_get_uniq_id_str("DCC", "GET")
typedef void (*DCC_GET_FUNC) (GET_DCC_REC *);
/* handle receiving DCC - GET/RESUME. */
void cmd_dcc_receive(const char *data, DCC_GET_FUNC accept);
void cmd_dcc_receive(const char *data, DCC_GET_FUNC accept_func);
void dcc_get_connect(DCC_REC *dcc);
void dcc_get_connect(GET_DCC_REC *dcc);
char *dcc_get_download_path(const char *fname);
#define dcc_is_waiting_get(dcc) \
((dcc)->type == DCC_TYPE_GET && dcc_is_waiting_user(dcc))
void dcc_get_init(void);
void dcc_get_deinit(void);

28
src/irc/dcc/dcc-rec.h Normal file
View File

@ -0,0 +1,28 @@
int type;
int orig_type; /* original DCC type that was sent to us - same as type except GET and SEND are swapped */
time_t created;
IRC_SERVER_REC *server;
char *servertag; /* for resetting server later if we get disconnected */
char *mynick; /* my current nick */
char *nick;
CHAT_DCC_REC *chat; /* if the request came through DCC chat */
char *target; /* who the request was sent to - your nick, channel or NULL if you sent the request */
char *arg;
IPADDR addr; /* address we're connected in */
char addrstr[MAX_IP_LEN]; /* in readable form */
int port; /* port we're connected in */
GIOChannel *handle; /* socket handle */
int tagconn, tagread, tagwrite;
time_t starttime; /* transfer start time */
unsigned long transfd; /* bytes transferred */
unsigned int destroyed:1; /* We're about to destroy this DCC recond */
char *databuf; /* buffer for receiving/transmitting data */
int databufsize;
GHashTable *module_data;

View File

@ -24,14 +24,17 @@
#include "network.h"
#include "misc.h"
#include "dcc-file.h"
#include "dcc-get.h"
#include "dcc-send.h"
#include "dcc-chat.h"
static DCC_REC *dcc_resume_find(int type, const char *nick, int port)
static FILE_DCC_REC *dcc_resume_find(int type, const char *nick, int port)
{
GSList *tmp;
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
FILE_DCC_REC *dcc = tmp->data;
if (dcc->type == type && !dcc_is_connected(dcc) &&
dcc->port == port && g_strcasecmp(dcc->nick, nick) == 0)
@ -42,7 +45,7 @@ static DCC_REC *dcc_resume_find(int type, const char *nick, int port)
}
static int dcc_ctcp_resume_parse(int type, const char *data, const char *nick,
DCC_REC **dcc, long *size)
FILE_DCC_REC **dcc, long *size)
{
char **params;
int paramcount;
@ -56,23 +59,22 @@ static int dcc_ctcp_resume_parse(int type, const char *data, const char *nick,
port = atoi(params[paramcount-2]);
*size = atol(params[paramcount-1]);
type = type == DCC_TYPE_RESUME ? DCC_TYPE_SEND : DCC_TYPE_GET;
*dcc = dcc_resume_find(type, nick, port);
}
g_strfreev(params);
return paramcount >= 3;
}
static int dcc_resume_file_check(DCC_REC *dcc, IRC_SERVER_REC *server,
static int dcc_resume_file_check(FILE_DCC_REC *dcc, IRC_SERVER_REC *server,
long size)
{
if (lseek(dcc->fhandle, 0, SEEK_END) == dcc->size) {
if (size >= dcc->size) {
/* whole file sent */
dcc->starttime = time(NULL);
dcc_reject(dcc, server);
dcc_reject(DCC(dcc), server);
} else if (lseek(dcc->fhandle, size, SEEK_SET) != size) {
/* error, or trying to seek after end of file */
dcc_reject(dcc, server);
dcc_reject(DCC(dcc), server);
} else {
dcc->transfd = dcc->skipped = size;
return TRUE;
@ -81,20 +83,20 @@ static int dcc_resume_file_check(DCC_REC *dcc, IRC_SERVER_REC *server,
return FALSE;
}
/* CTCP: DCC RESUME */
/* CTCP: DCC RESUME - requesting to resume DCC SEND */
static void ctcp_msg_dcc_resume(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, DCC_REC *chat)
{
DCC_REC *dcc;
FILE_DCC_REC *dcc;
char *str;
long size;
if (!dcc_ctcp_resume_parse(DCC_TYPE_RESUME, data, nick, &dcc, &size)) {
if (!dcc_ctcp_resume_parse(DCC_SEND_TYPE, data, nick, &dcc, &size)) {
signal_emit("dcc error ctcp", 5, "RESUME", data,
nick, addr, target);
} else if (dcc != NULL && dcc_resume_file_check(dcc, server, size)) {
str = g_strdup_printf(dcc->file_quoted ?
str = g_strdup_printf(DCC_SEND(dcc)->file_quoted ?
"DCC ACCEPT \"%s\" %d %lu" :
"DCC ACCEPT %s %d %lu",
dcc->arg, dcc->port, dcc->transfd);
@ -104,23 +106,24 @@ static void ctcp_msg_dcc_resume(IRC_SERVER_REC *server, const char *data,
}
}
/* CTCP: DCC ACCEPT */
/* CTCP: DCC ACCEPT - accept resuming DCC GET */
static void ctcp_msg_dcc_accept(IRC_SERVER_REC *server, const char *data,
const char *nick, const char *addr,
const char *target, DCC_REC *chat)
{
DCC_REC *dcc;
FILE_DCC_REC *dcc;
long size;
if (!dcc_ctcp_resume_parse(DCC_TYPE_ACCEPT, data, nick, &dcc, &size) ||
(dcc != NULL && dcc->get_type != DCC_GET_RESUME)) {
if (!dcc_ctcp_resume_parse(DCC_GET_TYPE, data, nick, &dcc, &size) ||
(dcc != NULL && DCC_GET(dcc)->get_type != DCC_GET_RESUME)) {
signal_emit("dcc error ctcp", 5, "ACCEPT", data,
nick, addr, target);
} else if (dcc != NULL && dcc_resume_file_check(dcc, server, size))
dcc_get_connect(dcc);
dcc_get_connect(DCC_GET(dcc));
}
static void dcc_send_resume(DCC_REC *dcc)
/* Resume a DCC GET */
static void dcc_send_resume(GET_DCC_REC *dcc)
{
char *str;
@ -142,7 +145,7 @@ static void dcc_send_resume(DCC_REC *dcc)
if (dcc->skipped == dcc->size) {
/* already received whole file */
dcc->starttime = time(NULL);
dcc_reject(dcc, NULL);
dcc_reject(DCC(dcc), NULL);
} else {
str = g_strdup_printf(dcc->file_quoted ?
"DCC RESUME \"%s\" %d %lu" :

View File

@ -26,15 +26,36 @@
#include "misc.h"
#include "settings.h"
#include "dcc.h"
#include "dcc-send.h"
#include "dcc-chat.h"
static SEND_DCC_REC *dcc_send_create(IRC_SERVER_REC *server,
CHAT_DCC_REC *chat,
const char *nick, const char *arg)
{
SEND_DCC_REC *dcc;
dcc = g_new0(SEND_DCC_REC, 1);
dcc->orig_type = module_get_uniq_id_str("DCC", "GET");
dcc->type = module_get_uniq_id_str("DCC", "SEND");
dcc->fhandle = -1;
dcc_init_rec(DCC(dcc), server, chat, nick, arg);
return dcc;
}
static void sig_dcc_destroyed(SEND_DCC_REC *dcc)
{
if (!IS_DCC_SEND(dcc)) return;
if (dcc->fhandle != -1) close(dcc->fhandle);
}
/* input function: DCC SEND - we're ready to send more data */
static void dcc_send_data(DCC_REC *dcc)
static void dcc_send_data(SEND_DCC_REC *dcc)
{
int ret;
g_return_if_fail(dcc != NULL);
if (!dcc->fastsend && !dcc->gotalldata) {
/* haven't received everything we've send there yet.. */
return;
@ -43,15 +64,14 @@ static void dcc_send_data(DCC_REC *dcc)
ret = read(dcc->fhandle, dcc->databuf, dcc->databufsize);
if (ret <= 0) {
/* end of file .. or some error .. */
if (dcc->fastsend) {
if (!dcc->fastsend)
dcc_close(DCC(dcc));
else {
/* no need to call this function anymore..
in fact it just eats all the cpu.. */
dcc->waitforend = TRUE;
g_source_remove(dcc->tagwrite);
dcc->tagwrite = -1;
} else {
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
}
return;
}
@ -66,13 +86,11 @@ static void dcc_send_data(DCC_REC *dcc)
}
/* input function: DCC SEND - received some data */
static void dcc_send_read_size(DCC_REC *dcc)
static void dcc_send_read_size(SEND_DCC_REC *dcc)
{
guint32 bytes;
int ret;
g_return_if_fail(dcc != NULL);
if (dcc->count_pos == 4)
return;
@ -80,8 +98,7 @@ static void dcc_send_read_size(DCC_REC *dcc)
ret = net_receive(dcc->handle, dcc->count_buf+dcc->count_pos,
4-dcc->count_pos);
if (ret == -1) {
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
dcc_close(DCC(dcc));
return;
}
@ -103,20 +120,17 @@ static void dcc_send_read_size(DCC_REC *dcc)
if (dcc->waitforend && dcc->gotalldata) {
/* file is sent */
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
dcc_close(DCC(dcc));
}
}
/* input function: DCC SEND - someone tried to connect to our socket */
static void dcc_send_connected(DCC_REC *dcc)
static void dcc_send_connected(SEND_DCC_REC *dcc)
{
GIOChannel *handle;
IPADDR addr;
int port;
g_return_if_fail(dcc != NULL);
/* accept connection */
handle = net_accept(dcc->handle, &addr, &port);
if (handle == NULL)
@ -181,7 +195,8 @@ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server,
char host[MAX_IP_LEN];
int hfile, port;
long fsize;
DCC_REC *dcc, *chat;
SEND_DCC_REC *dcc;
CHAT_DCC_REC *chat;
IPADDR own_ip;
GIOChannel *handle;
@ -203,7 +218,7 @@ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server,
if ((server == NULL || !server->connected) && chat == NULL)
cmd_param_error(CMDERR_NOT_CONNECTED);
if (dcc_find_request(DCC_TYPE_SEND, target, fname)) {
if (dcc_find_request(DCC_SEND_TYPE, target, fname)) {
signal_emit("dcc error send exists", 2, target, fname);
cmd_params_free(free_arg);
return;
@ -232,7 +247,7 @@ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server,
fname = g_basename(fname);
dcc = dcc_create(DCC_TYPE_SEND, target, fname, server, chat);
dcc = dcc_send_create(server, chat, target, fname);
dcc->handle = handle;
dcc->port = port;
dcc->size = fsize;
@ -242,7 +257,7 @@ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server,
(GInputFunction) dcc_send_connected, dcc);
/* send DCC request */
dcc_make_address(&own_ip, host);
dcc_ip2str(&own_ip, host);
str = g_strdup_printf(dcc->file_quoted ?
"DCC SEND \"%s\" %s %d %lu" :
"DCC SEND %s %s %d %lu",
@ -255,13 +270,17 @@ static void cmd_dcc_send(const char *data, IRC_SERVER_REC *server,
void dcc_send_init(void)
{
dcc_register_type("SEND");
settings_add_bool("dcc", "dcc_fast_send", TRUE);
settings_add_str("dcc", "dcc_upload_path", "~");
signal_add("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
command_bind("dcc send", NULL, (SIGNAL_FUNC) cmd_dcc_send);
}
void dcc_send_deinit(void)
{
dcc_unregister_type("SEND");
signal_remove("dcc destroyed", (SIGNAL_FUNC) sig_dcc_destroyed);
command_unbind("dcc send", (SIGNAL_FUNC) cmd_dcc_send);
}

28
src/irc/dcc/dcc-send.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __DCC_SEND_H
#define __DCC_SEND_H
#include "dcc.h"
#define DCC_SEND(dcc) \
MODULE_CHECK_CAST_MODULE(dcc, SEND_DCC_REC, type, "DCC", "SEND")
#define IS_DCC_SEND(dcc) \
(DCC_SEND(dcc) ? TRUE : FALSE)
typedef struct {
#include "dcc-file-rec.h"
unsigned int file_quoted:1; /* file name was received quoted ("file name") */
/* fastsending: */
unsigned int fastsend:1;
unsigned int waitforend:1; /* file is sent, just wait for the replies from the other side */
unsigned int gotalldata:1; /* got all acks from the other end (needed to make sure the end of transfer works right) */
} SEND_DCC_REC;
#define DCC_SEND_TYPE module_get_uniq_id_str("DCC", "SEND")
void dcc_send_init(void);
void dcc_send_deinit(void);
#endif

View File

@ -22,17 +22,15 @@
#include "signals.h"
#include "commands.h"
#include "network.h"
#include "net-sendbuffer.h"
#include "line-split.h"
#include "misc.h"
#include "settings.h"
#include "irc.h"
#include "dcc-chat.h"
#include "dcc-get.h"
void dcc_send_init(void);
void dcc_send_deinit(void);
#include "dcc-send.h"
void dcc_resume_init(void);
void dcc_resume_deinit(void);
@ -40,63 +38,58 @@ void dcc_resume_deinit(void);
void dcc_autoget_init(void);
void dcc_autoget_deinit(void);
#define DCC_TYPES 5
static const char *dcc_types[] = {
"CHAT",
"SEND",
"GET",
"RESUME",
"ACCEPT"
};
GSList *dcc_conns;
static GSList *dcc_types;
static int dcc_timeouttag;
/* Create new DCC record */
DCC_REC *dcc_create(int type, const char *nick, const char *arg,
IRC_SERVER_REC *server, DCC_REC *chat)
void dcc_register_type(const char *type)
{
DCC_REC *dcc;
dcc_types = g_slist_append(dcc_types, g_strdup(type));
}
g_return_val_if_fail(nick != NULL, NULL);
g_return_val_if_fail(arg != NULL, NULL);
void dcc_unregister_type(const char *type)
{
GSList *pos;
pos = gslist_find_string(dcc_types, type);
if (pos != NULL) {
g_free(pos->data);
dcc_types = g_slist_remove(dcc_types, pos->data);
}
}
int dcc_str2type(const char *str)
{
if (gslist_find_string(dcc_types, str) == NULL)
return -1;
return module_get_uniq_id_str("DCC", str);
}
/* Initialize DCC record */
void dcc_init_rec(DCC_REC *dcc, IRC_SERVER_REC *server, CHAT_DCC_REC *chat,
const char *nick, const char *arg)
{
g_return_if_fail(dcc != NULL);
g_return_if_fail(nick != NULL);
g_return_if_fail(arg != NULL);
g_return_if_fail(server != NULL || chat != NULL);
dcc = g_new0(DCC_REC, 1);
dcc->mirc_ctcp = settings_get_bool("dcc_mirc_ctcp");
dcc->created = time(NULL);
dcc->chat = chat;
dcc->type = type;
dcc->arg = g_strdup(arg);
dcc->nick = g_strdup(nick);
dcc->fhandle = -1;
dcc->tagconn = dcc->tagread = dcc->tagwrite = -1;
dcc->server = server;
dcc->mynick = g_strdup(server != NULL ? server->nick :
chat != NULL ? chat->nick : "??");
dcc->ircnet = server == NULL ?
(chat == NULL || chat->ircnet == NULL ? NULL : g_strdup(chat->ircnet)) :
(server->connrec->chatnet == NULL ? NULL : g_strdup(server->connrec->chatnet));
dcc->servertag = server != NULL ? g_strdup(server->tag) :
(chat == NULL ? NULL : g_strdup(chat->servertag));
dcc_conns = g_slist_append(dcc_conns, dcc);
signal_emit("dcc created", 1, dcc);
return dcc;
}
static void dcc_remove_chat_refs(DCC_REC *dcc)
{
GSList *tmp;
g_return_if_fail(dcc != NULL);
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *rec = tmp->data;
if (rec->chat == dcc)
rec->chat = NULL;
}
}
/* Destroy DCC record */
@ -106,47 +99,24 @@ void dcc_destroy(DCC_REC *dcc)
if (dcc->destroyed) return;
dcc_conns = g_slist_remove(dcc_conns, dcc);
dcc_remove_chat_refs(dcc);
dcc->destroyed = TRUE;
signal_emit("dcc destroyed", 1, dcc);
if (dcc->fhandle != -1) close(dcc->fhandle);
if (dcc->handle != NULL) net_disconnect(dcc->handle);
if (dcc->tagconn != -1) g_source_remove(dcc->tagconn);
if (dcc->tagread != -1) g_source_remove(dcc->tagread);
if (dcc->tagwrite != -1) g_source_remove(dcc->tagwrite);
if (dcc->sendbuf != NULL) net_sendbuffer_destroy(dcc->sendbuf, FALSE);
if (dcc->type == DCC_TYPE_CHAT)
line_split_free((LINEBUF_REC *) dcc->databuf);
else if (dcc->databuf != NULL)
g_free(dcc->databuf);
g_free_not_null(dcc->file);
g_free_not_null(dcc->ircnet);
g_free_not_null(dcc->chat_id);
g_free_not_null(dcc->servertag);
g_free_not_null(dcc->target);
g_free_not_null(dcc->databuf);
g_free(dcc->mynick);
g_free(dcc->nick);
g_free(dcc->arg);
g_free(dcc);
}
void dcc_make_address(IPADDR *ip, char *host)
{
unsigned long addr;
if (is_ipv6_addr(ip)) {
/* IPv6 */
net_ip2host(ip, host);
} else {
memcpy(&addr, &ip->addr, 4);
g_snprintf(host, MAX_IP_LEN, "%lu",
(unsigned long) htonl(addr));
}
}
DCC_REC *dcc_find_request_latest(int type)
{
DCC_REC *latest;
@ -181,35 +151,21 @@ DCC_REC *dcc_find_request(int type, const char *nick, const char *arg)
return NULL;
}
const char *dcc_type2str(int type)
void dcc_ip2str(IPADDR *ip, char *host)
{
g_return_val_if_fail(type >= 1 && type <= DCC_TYPES, NULL);
unsigned long addr;
return dcc_types[type-1];
}
int dcc_str2type(const char *type)
{
int num;
for (num = 0; num < DCC_TYPES; num++) {
if (g_strcasecmp(dcc_types[num], type) == 0)
return num+1;
if (is_ipv6_addr(ip)) {
/* IPv6 */
net_ip2host(ip, host);
} else {
memcpy(&addr, &ip->addr, 4);
g_snprintf(host, MAX_IP_LEN, "%lu",
(unsigned long) htonl(addr));
}
return 0;
}
GIOChannel *dcc_listen(GIOChannel *interface, IPADDR *ip, int *port)
{
if (net_getsockname(interface, ip, NULL) == -1)
return NULL;
*port = settings_get_int("dcc_port");
return net_listen(ip, port);
}
void dcc_get_address(const char *str, IPADDR *ip)
void dcc_str2ip(const char *str, IPADDR *ip)
{
unsigned long addr;
@ -225,39 +181,29 @@ void dcc_get_address(const char *str, IPADDR *ip)
}
}
void dcc_ctcp_message(IRC_SERVER_REC *server, const char *target,
DCC_REC *chat, int notice, const char *msg)
/* Start listening for incoming connections */
GIOChannel *dcc_listen(GIOChannel *interface, IPADDR *ip, int *port)
{
char *str;
if (net_getsockname(interface, ip, NULL) == -1)
return NULL;
if (chat != NULL && chat->sendbuf != NULL) {
/* send it via open DCC chat */
str = g_strdup_printf("%s\001%s\001", chat->mirc_ctcp ? "" :
notice ? "CTCP_REPLY " :
"CTCP_MESSAGE ", msg);
dcc_chat_send(chat, str);
g_free(str);
} else {
irc_send_cmdv(server, "%s %s :\001%s\001",
notice ? "NOTICE" : "PRIVMSG", target, msg);
}
*port = settings_get_int("dcc_port");
return net_listen(ip, port);
}
/* Server connected, check if there's any open dcc sessions for this ircnet.. */
static void dcc_server_connected(IRC_SERVER_REC *server)
/* Server connected - update server for DCC records that have
the same server tag */
static void sig_server_connected(IRC_SERVER_REC *server)
{
GSList *tmp;
g_return_if_fail(server != NULL);
if (server->connrec->chatnet == NULL)
return;
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
if (dcc->server == NULL && dcc->ircnet != NULL &&
g_strcasecmp(dcc->ircnet, server->connrec->chatnet) == 0) {
if (dcc->server == NULL && dcc->servertag != NULL &&
g_strcasecmp(dcc->servertag, server->tag) == 0) {
dcc->server = server;
g_free(dcc->mynick);
dcc->mynick = g_strdup(server->nick);
@ -265,8 +211,8 @@ static void dcc_server_connected(IRC_SERVER_REC *server)
}
}
/* Server disconnected, remove it from all dcc records */
static void dcc_server_disconnected(IRC_SERVER_REC *server)
/* Server disconnected, remove it from all DCC records */
static void sig_server_disconnected(IRC_SERVER_REC *server)
{
GSList *tmp;
@ -275,14 +221,24 @@ static void dcc_server_disconnected(IRC_SERVER_REC *server)
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
if (dcc->server != server)
continue;
if (dcc->server == server)
dcc->server = NULL;
}
}
dcc->server = dcc->ircnet == NULL ? NULL :
IRC_SERVER(server_find_chatnet(dcc->ircnet));
if (dcc->server != NULL) {
/* Your nick changed, change nick in all DCC records */
static void sig_server_nick_changed(IRC_SERVER_REC *server)
{
GSList *tmp;
if (!IS_IRC_SERVER(server)) return;
for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) {
DCC_REC *dcc = tmp->data;
if (dcc->server == server) {
g_free(dcc->mynick);
dcc->mynick = g_strdup(dcc->server->nick);
dcc->mynick = g_strdup(server->nick);
}
}
}
@ -331,22 +287,22 @@ static void ctcp_reply_dcc_reject(IRC_SERVER_REC *server, const char *data,
DCC_REC *chat)
{
DCC_REC *dcc;
char *typestr, *args;
int type;
char *type, *args;
typestr = g_strdup(data);
args = strchr(typestr, ' ');
type = g_strdup(data);
args = strchr(type, ' ');
if (args != NULL) *args++ = '\0'; else args = "";
type = dcc_str2type(typestr);
dcc = dcc_find_request(type, nick,
type == DCC_TYPE_CHAT ? NULL : args);
if (dcc != NULL) {
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
}
dcc = dcc_find_request(dcc_str2type(type), nick, args);
if (dcc != NULL) dcc_close(dcc);
g_free(typestr);
g_free(type);
}
void dcc_close(DCC_REC *dcc)
{
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
}
/* Reject a DCC request */
@ -355,16 +311,14 @@ void dcc_reject(DCC_REC *dcc, IRC_SERVER_REC *server)
g_return_if_fail(dcc != NULL);
if (dcc->server != NULL) server = dcc->server;
if (server != NULL && (dcc->type != DCC_TYPE_CHAT ||
dcc->starttime == 0)) {
if (server != NULL) {
signal_emit("dcc rejected", 1, dcc);
irc_send_cmdv(server, "NOTICE %s :\001DCC REJECT %s %s\001",
dcc->nick, dcc_type2str(SWAP_SENDGET(dcc->type)),
dcc->nick, dcc_type2str(dcc->orig_type),
dcc->arg);
}
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
dcc_close(dcc);
}
static int dcc_timeout_func(void)
@ -374,14 +328,18 @@ static int dcc_timeout_func(void)
now = time(NULL)-settings_get_int("dcc_timeout");
for (tmp = dcc_conns; tmp != NULL; tmp = next) {
DCC_REC *rec = tmp->data;
DCC_REC *dcc = tmp->data;
next = tmp->next;
if (rec->tagread == -1 && now > rec->created) {
/* timed out. */
dcc_reject(rec, NULL);
if (dcc->tagread == -1 && now > dcc->created) {
/* Timed out - don't send DCC REJECT CTCP so CTCP
flooders won't affect us and it really doesn't
matter that much anyway if the other side doen't
get it.. */
dcc_close(dcc);
}
}
return 1;
}
@ -400,10 +358,8 @@ static void event_no_such_nick(IRC_SERVER_REC *server, char *data)
next = tmp->next;
if (!dcc_is_connected(dcc) && dcc->server == server &&
g_strcasecmp(dcc->nick, nick) == 0) {
signal_emit("dcc closed", 1, dcc);
dcc_destroy(dcc);
}
g_strcasecmp(dcc->nick, nick) == 0)
dcc_close(dcc);
}
g_free(params);
@ -419,14 +375,15 @@ static void cmd_dcc_close(char *data, IRC_SERVER_REC *server)
g_return_if_fail(data != NULL);
if (!cmd_get_params(data, &free_arg, 3, &typestr, &nick, &arg))
if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST,
&typestr, &nick, &arg))
return;
if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
g_strup(typestr);
type = dcc_str2type(typestr);
if (type == 0) {
if (type == -1) {
signal_emit("dcc error unknown type", 1, typestr);
cmd_params_free(free_arg);
return;
@ -437,9 +394,7 @@ static void cmd_dcc_close(char *data, IRC_SERVER_REC *server)
DCC_REC *dcc = tmp->data;
next = tmp->next;
if (dcc->type == type &&
g_strcasecmp(dcc->chat_id != NULL ?
dcc->chat_id : dcc->nick, nick) == 0 &&
if (dcc->type == type && g_strcasecmp(dcc->nick, nick) == 0 &&
(*arg == '\0' || strcmp(dcc->arg, arg) == 0)) {
dcc_reject(dcc, server);
found = TRUE;
@ -464,19 +419,19 @@ void irc_dcc_init(void)
dcc_conns = NULL;
dcc_timeouttag = g_timeout_add(1000, (GSourceFunc) dcc_timeout_func, NULL);
settings_add_bool("dcc", "dcc_mirc_ctcp", FALSE);
settings_add_int("dcc", "dcc_port", 0);
settings_add_int("dcc", "dcc_timeout", 300);
settings_add_int("dcc", "dcc_block_size", 2048);
signal_add("server connected", (SIGNAL_FUNC) dcc_server_connected);
signal_add("server disconnected", (SIGNAL_FUNC) dcc_server_disconnected);
signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
signal_add("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
signal_add("server nick changed", (SIGNAL_FUNC) sig_server_nick_changed);
signal_add("ctcp msg dcc", (SIGNAL_FUNC) ctcp_msg_dcc);
signal_add("ctcp reply dcc", (SIGNAL_FUNC) ctcp_reply_dcc);
signal_add("ctcp reply dcc reject", (SIGNAL_FUNC) ctcp_reply_dcc_reject);
signal_add("event 401", (SIGNAL_FUNC) event_no_such_nick);
command_bind("dcc", NULL, (SIGNAL_FUNC) cmd_dcc);
command_bind("dcc close", NULL, (SIGNAL_FUNC) cmd_dcc_close);
signal_add("event 401", (SIGNAL_FUNC) event_no_such_nick);
dcc_chat_init();
dcc_get_init();
@ -487,23 +442,24 @@ void irc_dcc_init(void)
void irc_dcc_deinit(void)
{
while (dcc_conns != NULL)
dcc_destroy(dcc_conns->data);
dcc_chat_deinit();
dcc_get_deinit();
dcc_send_deinit();
dcc_resume_deinit();
dcc_autoget_deinit();
signal_remove("server connected", (SIGNAL_FUNC) dcc_server_connected);
signal_remove("server disconnected", (SIGNAL_FUNC) dcc_server_disconnected);
signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
signal_remove("server disconnected", (SIGNAL_FUNC) sig_server_disconnected);
signal_remove("server nick changed", (SIGNAL_FUNC) sig_server_nick_changed);
signal_remove("ctcp msg dcc", (SIGNAL_FUNC) ctcp_msg_dcc);
signal_remove("ctcp reply dcc", (SIGNAL_FUNC) ctcp_reply_dcc);
signal_remove("ctcp reply dcc reject", (SIGNAL_FUNC) ctcp_reply_dcc_reject);
signal_remove("event 401", (SIGNAL_FUNC) event_no_such_nick);
command_unbind("dcc", (SIGNAL_FUNC) cmd_dcc);
command_unbind("dcc close", (SIGNAL_FUNC) cmd_dcc_close);
signal_remove("event 401", (SIGNAL_FUNC) event_no_such_nick);
g_source_remove(dcc_timeouttag);
while (dcc_conns != NULL)
dcc_destroy(dcc_conns->data);
}

View File

@ -1,104 +1,18 @@
#ifndef __DCC_H
#define __DCC_H
#include "modules.h"
#include "network.h"
#include "irc-servers.h"
enum {
DCC_TYPE_CHAT = 1,
DCC_TYPE_SEND,
DCC_TYPE_GET,
DCC_TYPE_RESUME,
DCC_TYPE_ACCEPT
};
#define DCC(dcc) ((DCC_REC *) (dcc))
#define SWAP_SENDGET(a) ((a) == DCC_TYPE_SEND ? DCC_TYPE_GET : \
(a) == DCC_TYPE_GET ? DCC_TYPE_SEND : (a))
typedef struct CHAT_DCC_REC CHAT_DCC_REC;
typedef struct DCC_REC {
int type;
time_t created;
IRC_SERVER_REC *server;
char *chat_id; /* unique identifier for dcc chat. usually same as nick. */
char *nick;
char *target; /* who the request was sent to - your nick, channel or NULL if you sent the request */
struct DCC_REC *chat; /* if the request came through DCC chat */
char *ircnet;
char *mynick;
char *arg;
char *file; /* file name we're really moving, arg is just the reference.. */
IPADDR addr; /* address we're connected in */
char addrstr[MAX_IP_LEN]; /* in readable form */
int port; /* port we're connected in */
long size, transfd, skipped; /* file size / bytes transferred / skipped at start */
GIOChannel *handle; /* socket handle */
NET_SENDBUF_REC *sendbuf;
int tagconn, tagread, tagwrite;
int fhandle; /* file handle */
time_t starttime; /* transfer start time */
int trans_bytes;
int get_type; /* DCC get: what to do if file exists? */
unsigned int fastsend:1; /* fastsending (just in case that global fastsend toggle changes while transferring..) */
unsigned int waitforend:1; /* DCC fast send: file is sent, just wait for the replies from the other side */
unsigned int gotalldata:1; /* DCC fast send: got all acks from the other end (needed to make sure the end of transfer works right) */
unsigned int file_quoted:1; /* file name was received quoted ("file name") */
unsigned int mirc_ctcp:1; /* DCC chat: Send CTCPs without the CTCP_MESSAGE prefix */
unsigned int connection_lost:1; /* DCC chat: other side closed connection */
unsigned int destroyed:1; /* We're about to destroy this DCC recond */
/* read/write counter buffer */
char count_buf[4];
int count_pos;
char *databuf; /* buffer for receiving/transmitting data */
int databufsize;
GHashTable *module_data;
typedef struct {
#include "dcc-rec.h"
} DCC_REC;
extern GSList *dcc_conns;
void dcc_init(void);
void dcc_deinit(void);
/* Find waiting DCC requests (non-connected) */
DCC_REC *dcc_find_request_latest(int type);
DCC_REC *dcc_find_request(int type, const char *nick, const char *arg);
const char *dcc_type2str(int type);
int dcc_str2type(const char *type);
void dcc_make_address(IPADDR *ip, char *host);
DCC_REC *dcc_create(int type, const char *nick, const char *arg,
IRC_SERVER_REC *server, DCC_REC *chat);
void dcc_destroy(DCC_REC *dcc);
GIOChannel *dcc_listen(GIOChannel *interface, IPADDR *ip, int *port);
void dcc_get_address(const char *str, IPADDR *ip);
/* Send a CTCP message/notify to target. Send the CTCP via DCC chat if
`chat' is specified. */
void dcc_ctcp_message(IRC_SERVER_REC *server, const char *target,
DCC_REC *chat, int notice, const char *msg);
/* Send `data' to dcc chat. */
void dcc_chat_send(DCC_REC *dcc, const char *data);
/* If `item' is a query of a =nick, return DCC chat record of nick */
DCC_REC *item_get_dcc(WI_ITEM_REC *item);
/* Reject a DCC request */
void dcc_reject(DCC_REC *dcc, IRC_SERVER_REC *server);
/* fully connected? */
#define dcc_is_connected(dcc) \
((dcc)->starttime != 0)
@ -111,4 +25,37 @@ void dcc_reject(DCC_REC *dcc, IRC_SERVER_REC *server);
#define dcc_is_waiting_user(dcc) \
((dcc)->handle == NULL)
extern GSList *dcc_conns;
void dcc_register_type(const char *type);
void dcc_unregister_type(const char *type);
int dcc_str2type(const char *str);
#define dcc_type2str(type) (module_find_id_str("DCC", type))
/* Initialize DCC record */
void dcc_init_rec(DCC_REC *dcc, IRC_SERVER_REC *server, CHAT_DCC_REC *chat,
const char *nick, const char *arg);
void dcc_destroy(DCC_REC *dcc);
/* Find waiting DCC requests (non-connected) */
DCC_REC *dcc_find_request_latest(int type);
DCC_REC *dcc_find_request(int type, const char *nick, const char *arg);
/* IP <-> string for DCC CTCP messages.
`str' must be at least MAX_IP_LEN bytes */
void dcc_ip2str(IPADDR *ip, char *str);
void dcc_str2ip(const char *str, IPADDR *ip);
/* Start listening for incoming connections */
GIOChannel *dcc_listen(GIOChannel *interface, IPADDR *ip, int *port);
/* Close DCC - sends "dcc closed" signal and calls dcc_destroy() */
void dcc_close(DCC_REC *dcc);
/* Reject a DCC request */
void dcc_reject(DCC_REC *dcc, IRC_SERVER_REC *server);
void dcc_init(void);
void dcc_deinit(void);
#endif