diff options
Diffstat (limited to 'src/irc/dcc')
-rw-r--r-- | src/irc/dcc/Makefile.am | 14 | ||||
-rw-r--r-- | src/irc/dcc/dcc-chat.c | 371 | ||||
-rw-r--r-- | src/irc/dcc/dcc-chat.h | 7 | ||||
-rw-r--r-- | src/irc/dcc/dcc-files.c | 577 | ||||
-rw-r--r-- | src/irc/dcc/dcc-files.h | 7 | ||||
-rw-r--r-- | src/irc/dcc/dcc.c | 550 | ||||
-rw-r--r-- | src/irc/dcc/dcc.h | 91 | ||||
-rw-r--r-- | src/irc/dcc/module.h | 3 |
8 files changed, 1620 insertions, 0 deletions
diff --git a/src/irc/dcc/Makefile.am b/src/irc/dcc/Makefile.am new file mode 100644 index 00000000..b7cd30e9 --- /dev/null +++ b/src/irc/dcc/Makefile.am @@ -0,0 +1,14 @@ +noinst_LTLIBRARIES = libirc_dcc.la + +INCLUDES = $(GLIB_CFLAGS) \ + -I$(top_srcdir)/src -I$(top_srcdir)/src/core/ -I$(top_srcdir)/src/irc/core/ + +libirc_dcc_la_SOURCES = \ + dcc.c \ + dcc-chat.c \ + dcc-files.c + +noinst_HEADERS = \ + dcc.h \ + dcc-chat.h \ + dcc-files.h diff --git a/src/irc/dcc/dcc-chat.c b/src/irc/dcc/dcc-chat.c new file mode 100644 index 00000000..5cddddfa --- /dev/null +++ b/src/irc/dcc/dcc-chat.c @@ -0,0 +1,371 @@ +/* + dcc-chat.c : irssi + + Copyright (C) 1999-2000 Timo Sirainen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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 "network.h" +#include "net-nonblock.h" +#include "line-split.h" +#include "settings.h" + +#include "masks.h" +#include "irc.h" +#include "server-setup.h" + +#include "dcc.h" + +/* Send text to DCC chat */ +static void dcc_chat_write(gchar *data) +{ + DCC_REC *dcc; + gchar *params, *text, *target; + gint len; + + g_return_if_fail(text != NULL); + + params = cmd_get_params(data, 2 | PARAM_FLAG_GETREST, &target, &text); + + if (*target == '=') + { + /* dcc msg */ + dcc = dcc_find_item(DCC_TYPE_CHAT, ++target, NULL); + if (dcc != NULL) + { + len = strlen(text); + /* FIXME: we need output queue! */ + if (net_transmit(dcc->handle, text, len) != len) + g_warning("dcc_chat_write() : could not send all data!"); + net_transmit(dcc->handle, "\n", 1); + } + } + + g_free(params); +} + +static void dcc_chat_me(const char *data, IRC_SERVER_REC *server, WI_IRC_REC *item) +{ + DCC_REC *dcc; + char *str; + + g_return_if_fail(data != NULL); + + dcc = irc_item_dcc_chat(item); + if (dcc == NULL) return; + + str = g_strdup_printf("ACTION %s", data); + dcc_ctcp_message(dcc->nick, NULL, dcc, FALSE, str); + g_free(str); +} + +static void dcc_chat_action(const char *data, IRC_SERVER_REC *server) +{ + char *params, *target, *text; + DCC_REC *dcc; + char *str; + + g_return_if_fail(data != NULL); + + if (*data != '=') { + /* handle only DCC actions */ + return; + } + + params = cmd_get_params(data, 3 | PARAM_FLAG_GETREST, &target, &text); + if (*target == '\0' || *text == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + dcc = dcc_find_item(DCC_TYPE_CHAT, target+1, NULL); + if (dcc != NULL) { + str = g_strdup_printf("ACTION %s", data); + dcc_ctcp_message(dcc->nick, NULL, dcc, FALSE, str); + g_free(str); + } + g_free(params); +} + +static void dcc_chat_ctcp(const char *data, IRC_SERVER_REC *server) +{ + char *params, *target, *ctcpcmd, *ctcpdata; + DCC_REC *dcc; + char *str; + + g_return_if_fail(data != NULL); + if (server == NULL || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED); + + params = cmd_get_params(data, 3 | PARAM_FLAG_GETREST, &target, &ctcpcmd, &ctcpdata); + if (*target == '\0' || *ctcpcmd == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + if (*target != '=') { + /* handle only DCC CTCPs */ + g_free(params); + return; + } + + dcc = dcc_find_item(DCC_TYPE_CHAT, target+1, NULL); + if (dcc != NULL) { + g_strup(ctcpcmd); + + str = g_strdup_printf("%s %s", ctcpcmd, ctcpdata); + dcc_ctcp_message(dcc->nick, NULL, dcc, FALSE, str); + g_free(str); + } + + g_free(params); +} + +/* DCC CHAT: text received */ +static void dcc_chat_msg(DCC_REC *dcc, gchar *msg) +{ + gchar *cmd, *ptr; + gboolean reply; + + g_return_if_fail(dcc != NULL); + g_return_if_fail(msg != NULL); + + reply = FALSE; + if (g_strncasecmp(msg, "CTCP_MESSAGE ", 13) != 0) + { + if (g_strncasecmp(msg, "CTCP_REPLY ", 11) != 0) + { + /* Use the mirc style CTCPing from now on.. */ + dcc->mirc_ctcp = TRUE; + } + else + { + /* bitchx (and ircii?) sends this */ + msg += 11; + reply = TRUE; + dcc->mirc_ctcp = FALSE; + } + } + else + { + /* bitchx (and ircii?) sends this */ + msg += 13; + dcc->mirc_ctcp = FALSE; + } + + /* Handle only DCC CTCPs */ + if (*msg != 1) + return; + + msg = g_strdup(msg+1); + /* remove the later \001 */ + ptr = strrchr(msg, 1); + if (ptr != NULL) *ptr = '\0'; + + /* get ctcp command */ + cmd = g_strconcat(reply ? "dcc reply " : "dcc ctcp ", msg, NULL); + ptr = strchr(cmd+9, ' '); + if (ptr != NULL) *ptr++ = '\0'; else ptr = ""; + + g_strdown(cmd+9); + if (!signal_emit(cmd, 2, ptr, dcc)) + signal_emit(reply ? "default dcc reply" : "default dcc ctcp", 2, msg, dcc); + + g_free(cmd); + g_free(msg); + + signal_stop(); +} + +/* input function: DCC CHAT received some data.. */ +static void dcc_chat_input(DCC_REC *dcc) +{ + char tmpbuf[512], *str; + int recvlen, ret; + + g_return_if_fail(dcc != NULL); + + do { + recvlen = net_receive(dcc->handle, tmpbuf, sizeof(tmpbuf)); + + ret = line_split(tmpbuf, recvlen, &str, (LINEBUF_REC **) &dcc->databuf); + if (ret == -1) { + /* connection lost */ + dcc->destroyed = TRUE; + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + break; + } + + if (ret > 0) { + dcc->transfd += ret; + signal_emit("dcc chat message", 2, dcc, str); + } + } while (ret > 0); +} + +/* input function: DCC CHAT - someone tried to connect to our socket */ +static void dcc_chat_listen(DCC_REC *dcc) +{ + IPADDR ip; + gint handle, port; + + g_return_if_fail(dcc != NULL); + + /* accept connection */ + handle = net_accept(dcc->handle, &ip, &port); + if (handle == -1) + return; + + /* FIXME: add paranoia checking, check if host ip is the same as to who + we sent the DCC CHAT request.. */ + + g_source_remove(dcc->tagread); + close(dcc->handle); + + dcc->starttime = time(NULL); + dcc->handle = handle; + memcpy(&dcc->addr, &ip, sizeof(IPADDR)); + net_ip2host(&dcc->addr, dcc->addrstr); + dcc->port = port; + dcc->tagread = g_input_add(handle, G_INPUT_READ, + (GInputFunction) dcc_chat_input, dcc); + + signal_emit("dcc connected", 1, dcc); +} + +/* callback: DCC CHAT - net_connect_nonblock() finished */ +static void dcc_chat_connect(DCC_REC *dcc) +{ + g_return_if_fail(dcc != NULL); + + g_source_remove(dcc->tagread); + if (net_geterror(dcc->handle) != 0) + { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); + return; + } + + /* connect ok. */ + dcc->starttime = time(NULL); + dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ, + (GInputFunction) dcc_chat_input, dcc); + + signal_emit("dcc connected", 1, dcc); +} + +/* command: DCC CHAT */ +static void cmd_dcc_chat(gchar *data, IRC_SERVER_REC *server) +{ + DCC_REC *dcc; + IPADDR addr; + gchar *str; + gint port, handle; + + g_return_if_fail(data != NULL); + if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); + + dcc = dcc_find_item(DCC_TYPE_CHAT, data, NULL); + if (dcc != NULL) + { + if (dcc->addrstr[0] == '\0' || dcc->starttime != 0) + { + /* already sent a chat request / already chatting */ + return; + } + + /* found from dcc list - so we're the connecting side.. */ + dcc->handle = net_connect_ip(&dcc->addr, dcc->port, + source_host_ok ? source_host_ip : NULL); + if (dcc->handle != -1) + { + dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE, + (GInputFunction) dcc_chat_connect, dcc); + } + else + { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); + } + + return; + } + + /* send dcc chat request */ + if (server == NULL || !server->connected) + cmd_return_error(CMDERR_NOT_CONNECTED); + + if (!net_getsockname(server->handle, &addr, NULL)) + cmd_return_error(CMDERR_GETSOCKNAME); + + port = settings_get_int("dcc_port"); + handle = net_listen(&addr, &port); + if (handle == -1) + cmd_return_error(CMDERR_LISTEN); + + dcc = dcc_create(DCC_TYPE_CHAT, handle, data, "chat", server, NULL); + dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ, + (GInputFunction) dcc_chat_listen, dcc); + + /* send the request */ + str = g_strdup_printf("PRIVMSG %s :\001DCC CHAT CHAT %s %d\001", + data, dcc_make_address(&addr), port); + irc_send_cmd(server, str); + g_free(str); +} + +static void cmd_mircdcc(gchar *data, IRC_SERVER_REC *server, WI_IRC_REC *item) +{ + DCC_REC *dcc; + + g_return_if_fail(data != NULL); + + dcc = irc_item_dcc_chat(item); + if (dcc == NULL) return; + + dcc->mirc_ctcp = toupper(*data) == 'N' ? FALSE : TRUE; +} + +static void dcc_ctcp_redirect(gchar *msg, DCC_REC *dcc) +{ + g_return_if_fail(msg != NULL); + g_return_if_fail(dcc != NULL); + + signal_emit("ctcp msg dcc", 6, msg, dcc->server, dcc->nick, "dcc", dcc->mynick, dcc); +} + +void dcc_chat_init(void) +{ + command_bind("msg", NULL, (SIGNAL_FUNC) dcc_chat_write); + command_bind("me", NULL, (SIGNAL_FUNC) dcc_chat_me); + command_bind("action", NULL, (SIGNAL_FUNC) dcc_chat_action); + command_bind("ctcp", NULL, (SIGNAL_FUNC) dcc_chat_ctcp); + command_bind("dcc chat", NULL, (SIGNAL_FUNC) cmd_dcc_chat); + signal_add_first("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg); + command_bind("mircdcc", NULL, (SIGNAL_FUNC) cmd_mircdcc); + signal_add("dcc ctcp dcc", (SIGNAL_FUNC) dcc_ctcp_redirect); +} + +void dcc_chat_deinit(void) +{ + command_unbind("msg", (SIGNAL_FUNC) dcc_chat_write); + command_unbind("me", (SIGNAL_FUNC) dcc_chat_me); + command_unbind("action", (SIGNAL_FUNC) dcc_chat_action); + command_unbind("ctcp", (SIGNAL_FUNC) dcc_chat_ctcp); + command_unbind("dcc chat", (SIGNAL_FUNC) cmd_dcc_chat); + signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg); + command_unbind("mircdcc", (SIGNAL_FUNC) cmd_mircdcc); + signal_remove("dcc ctcp dcc", (SIGNAL_FUNC) dcc_ctcp_redirect); +} diff --git a/src/irc/dcc/dcc-chat.h b/src/irc/dcc/dcc-chat.h new file mode 100644 index 00000000..9ae9503f --- /dev/null +++ b/src/irc/dcc/dcc-chat.h @@ -0,0 +1,7 @@ +#ifndef __DCC_CHAT_H +#define __DCC_CHAT_H + +void dcc_chat_init(void); +void dcc_chat_deinit(void); + +#endif diff --git a/src/irc/dcc/dcc-files.c b/src/irc/dcc/dcc-files.c new file mode 100644 index 00000000..23b1cdce --- /dev/null +++ b/src/irc/dcc/dcc-files.c @@ -0,0 +1,577 @@ +/* + dcc-files.c : irssi + + Copyright (C) 1999-2000 Timo Sirainen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + 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 "network.h" +#include "line-split.h" +#include "misc.h" +#include "settings.h" + +#include "masks.h" +#include "irc.h" +#include "server-setup.h" + +#include "dcc.h" + +static gint dcc_file_create_mode; + +static gchar *dcc_prepare_path(gchar *fname) +{ + gchar *str, *ptr, *downpath; + + /* strip all paths from file. */ + ptr = strrchr(fname, '/'); + if (ptr == NULL) ptr = fname; else ptr++; + + downpath = convert_home(settings_get_str("dcc_download_path")); + str = g_strdup_printf("%s/%s", downpath, ptr); + g_free(downpath); + + return str; +} + +/* input function: DCC GET received data */ +static void dcc_receive(DCC_REC *dcc) +{ + guint32 recd; + gint len, ret; + + g_return_if_fail(dcc != NULL); + + for (;;) + { + len = net_receive(dcc->handle, dcc->databuf, dcc->databufsize); + if (len == 0) break; + if (len < 0) + { + /* socket closed - transmit complete (or other side died..) */ + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + return; + } + + write(dcc->fhandle, dcc->databuf, len); + dcc->transfd += len; + } + + /* send number of total bytes received - if for some reason we couldn't + send the 4 characters last time, try to somehow fix it this time by + sending missing amount of 0 characters.. */ + if (dcc->trans_bytes != 0) + { + recd = (guint32) htonl(0); + dcc->trans_bytes += net_transmit(dcc->handle, ((gchar *) &recd)+dcc->trans_bytes, 4-dcc->trans_bytes); + if (dcc->trans_bytes == 4) dcc->trans_bytes = 0; + } + + if (dcc->trans_bytes == 0) + { + recd = (guint32) htonl(dcc->transfd); + ret = net_transmit(dcc->handle, ((gchar *) &recd), 4); + if (ret > 0 && ret < 4) dcc->trans_bytes = ret; + } + signal_emit("dcc transfer update", 1, dcc); +} + +/* callback: net_connect() finished for DCC GET */ +static void dcc_get_connect(DCC_REC *dcc) +{ + struct stat statbuf; + + g_return_if_fail(dcc != NULL); + + g_source_remove(dcc->tagread); + if (net_geterror(dcc->handle) != 0) + { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); + return; + } + dcc->file = dcc_prepare_path(dcc->arg); + + /* if some plugin wants to change the file name/path here.. */ + signal_emit("dcc get receive", 1, dcc); + + if (stat(dcc->file, &statbuf) == 0 && + (dcc->get_type == DCC_GET_RENAME || dcc->get_type == DCC_GET_DEFAULT)) + { + /* file exists, rename.. */ + GString *newname; + gint num; + + newname = g_string_new(NULL); + for (num = 1; ; num++) + { + g_string_sprintf(newname, "%s.%d", dcc->file, num); + if (stat(newname->str, &statbuf) != 0) break; + } + g_free(dcc->file); + dcc->file = newname->str; + g_string_free(newname, FALSE); + } + + if (dcc->get_type != DCC_GET_RESUME) + { + dcc->fhandle = open(dcc->file, O_WRONLY | O_TRUNC | O_CREAT, dcc_file_create_mode); + if (dcc->fhandle == -1) + { + signal_emit("dcc error file create", 2, dcc, dcc->file); + dcc_destroy(dcc); + return; + } + } + + dcc->databufsize = settings_get_int("dcc_block_size") > 0 ? settings_get_int("dcc_block_size") : 2048; + dcc->databuf = g_malloc(dcc->databufsize); + + dcc->starttime = time(NULL); + dcc->tagread = g_input_add(dcc->handle, G_INPUT_READ, + (GInputFunction) dcc_receive, dcc); + signal_emit("dcc connected", 1, dcc); +} + +/* command: DCC GET */ +static void cmd_dcc_get(gchar *data) +{ + DCC_REC *dcc; + GSList *tmp, *next; + gchar *params, *nick, *fname; + gboolean found; + + g_return_if_fail(data != NULL); + + params = cmd_get_params(data, 2 | PARAM_FLAG_GETREST, &nick, &fname); + if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + dcc = NULL; found = FALSE; + for (tmp = dcc_conns; tmp != NULL; tmp = next) + { + dcc = tmp->data; + next = tmp->next; + + if (dcc->dcc_type == DCC_TYPE_GET && dcc->handle == -1 && g_strcasecmp(dcc->nick, nick) == 0 && + (*fname == '\0' || strcmp(dcc->arg, fname) == 0)) + { + /* found! */ + found = TRUE; + dcc->handle = net_connect_ip(&dcc->addr, dcc->port, + source_host_ok ? source_host_ip : NULL); + if (dcc->handle != -1) + { + dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE, + (GInputFunction) dcc_get_connect, dcc); + } + else + { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); + } + } + } + + if (!found) + signal_emit("dcc error get not found", 1, nick); + + g_free(params); +} + +/* resume setup: DCC SEND - we either said resume on get, or when we sent, + someone chose resume */ +static void dcc_resume_setup(DCC_REC *dcc, gint port) +{ + gchar *str; + + /* Check for DCC_SEND_RESUME */ + if (dcc->dcc_type == DCC_TYPE_SEND) + { + if (lseek(dcc->fhandle, dcc->transfd, SEEK_SET) == -1) + { + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + return; + } + else + { + str = g_strdup_printf("DCC ACCEPT %s %d %lu", + dcc->arg, port, dcc->transfd); + dcc_ctcp_message(dcc->nick, dcc->server, dcc->chat, FALSE, str); + g_free(str); + } + } + + /* Check for DCC_GET_RESUME */ + if (dcc->dcc_type == DCC_TYPE_GET && dcc->get_type == DCC_GET_RESUME) + { + dcc->handle = net_connect_ip(&dcc->addr, dcc->port, + source_host_ok ? source_host_ip : NULL); + if (dcc->handle != -1) + { + dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE, + (GInputFunction) dcc_get_connect, dcc); + } + else + { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); + } + } +} + +static void dcc_ctcp_msg(gchar *data, IRC_SERVER_REC *server, gchar *sender, gchar *sendaddr, gchar *target, DCC_REC *chat) +{ + gchar *params, *type, *arg, *portstr, *sizestr; + gulong size; + gint port; + DCC_REC *dcc; + + g_return_if_fail(data != NULL); + g_return_if_fail(sender != NULL); + + params = cmd_get_params(data, 4, &type, &arg, &portstr, &sizestr); + if (g_strcasecmp(type, "RESUME") == 0 || g_strcasecmp(type, "ACCEPT") == 0) + { + if (sscanf(portstr, "%d", &port) != 1) port = 0; + if (sscanf(sizestr, "%lu", &size) != 1) size = 0; + + dcc = dcc_find_by_port(sender, port); + if (dcc != NULL && (dcc->dcc_type == DCC_TYPE_GET || dcc->transfd == 0)) + { + dcc->transfd = size; + dcc->skipped = size; + dcc_resume_setup(dcc, port); + } + } + + g_free(params); +} + +static void dcc_resume_rec(DCC_REC *dcc) +{ + gchar *str; + + dcc->file = dcc_prepare_path(dcc->arg); + + dcc->fhandle = open(dcc->file, O_WRONLY, dcc_file_create_mode); + if (dcc->fhandle == -1) + { + signal_emit("dcc error file not found", 2, dcc, dcc->file); + dcc_destroy(dcc); + } + else + { + dcc->transfd = lseek(dcc->fhandle, 0, SEEK_END); + if (dcc->transfd < 0) dcc->transfd = 0; + dcc->skipped = dcc->transfd; + + str = g_strdup_printf("DCC RESUME %s %d %lu", + dcc->arg, dcc->port, dcc->transfd); + dcc_ctcp_message(dcc->nick, dcc->server, dcc->chat, FALSE, str); + g_free(str); + } +} + +/* command: DCC RESUME */ +static void cmd_dcc_resume(gchar *data) +{ + DCC_REC *dcc; + GSList *tmp; + gchar *params, *nick, *fname; + gboolean found; + + g_return_if_fail(data != NULL); + + params = cmd_get_params(data, 2 | PARAM_FLAG_GETREST, &nick, &fname); + if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + dcc = NULL; found = FALSE; + for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) + { + dcc = tmp->data; + + if (dcc->dcc_type == DCC_TYPE_GET && dcc->handle == -1 && g_strcasecmp(dcc->nick, nick) == 0 && + (*fname == '\0' || strcmp(dcc->arg, fname) == 0)) + { + /* found! */ + dcc->get_type = DCC_GET_RESUME; + dcc_resume_rec(dcc); + found = TRUE; + } + } + + if (!found) + signal_emit("dcc error get not found", 1, nick); + + g_free(params); +} + +/* input function: DCC SEND send more data */ +static void dcc_send_data(DCC_REC *dcc) +{ + gint n; + + g_return_if_fail(dcc != NULL); + + if (!dcc->fastsend && !dcc->gotalldata) + { + /* haven't received everything we've send there yet.. */ + return; + } + + n = read(dcc->fhandle, dcc->databuf, dcc->databufsize); + if (n <= 0) + { + /* end of file .. or some error .. */ + if (dcc->fastsend) + { + /* 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; + } + + dcc->transfd += net_transmit(dcc->handle, dcc->databuf, n); + dcc->gotalldata = FALSE; + + lseek(dcc->fhandle, dcc->transfd, SEEK_SET); + + signal_emit("dcc transfer update", 1, dcc); +} + +/* input function: DCC SEND received some data */ +static void dcc_send_read_size(DCC_REC *dcc) +{ + guint32 bytes; + gint ret; + + g_return_if_fail(dcc != NULL); + + if (dcc->read_pos == 4) + return; + + /* we need to get 4 bytes.. */ + ret = net_receive(dcc->handle, dcc->read_buf+dcc->read_pos, 4-dcc->read_pos); + if (ret == -1) + { + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + return; + } + + dcc->read_pos += ret; + + if (dcc->read_pos == 4) + { + bytes = 0; memcpy(&bytes, dcc->read_buf, 4); + bytes = (guint32) ntohl(bytes); + + dcc->gotalldata = bytes == dcc->transfd; + dcc->read_pos = 0; + + if (!dcc->fastsend) + { + /* send more data.. */ + dcc_send_data(dcc); + } + + if (dcc->waitforend && dcc->gotalldata) + { + /* file is sent */ + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + return; + } + } +} + +/* input function: DCC SEND - someone tried to connect to our socket */ +static void dcc_send_init(DCC_REC *dcc) +{ + gint handle, port; + IPADDR addr; + + g_return_if_fail(dcc != NULL); + + /* accept connection */ + handle = net_accept(dcc->handle, &addr, &port); + if (handle == -1) + return; + + /* FIXME: add paranoia checking, check if host ip is the same as to who + we sent the DCC SEND request.. */ + + g_source_remove(dcc->tagread); + close(dcc->handle); + + dcc->fastsend = settings_get_bool("toggle_dcc_fast_send"); + dcc->handle = handle; + memcpy(&dcc->addr, &addr, sizeof(IPADDR)); + net_ip2host(&dcc->addr, dcc->addrstr); + dcc->port = port; + dcc->databufsize = settings_get_int("dcc_block_size") > 0 ? settings_get_int("dcc_block_size") : 2048; + dcc->databuf = g_malloc(dcc->databufsize); + dcc->starttime = time(NULL); + dcc->tagread = g_input_add(handle, G_INPUT_READ, + (GInputFunction) dcc_send_read_size, dcc); + dcc->tagwrite = !dcc->fastsend ? -1 : + g_input_add(handle, G_INPUT_WRITE, (GInputFunction) dcc_send_data, dcc); + + signal_emit("dcc connected", 1, dcc); + + if (!dcc->fastsend) + { + /* send first block */ + dcc->gotalldata = TRUE; + dcc_send_data(dcc); + } +} + +/* command: DCC SEND */ +static void cmd_dcc_send(gchar *data, IRC_SERVER_REC *server, WI_IRC_REC *item) +{ + gchar *params, *target, *fname, *str, *ptr; + gint fh, h, port; + glong fsize; + DCC_REC *dcc, *chat; + IPADDR addr; + + g_return_if_fail(data != NULL); + + params = cmd_get_params(data, 2 | PARAM_FLAG_GETREST, &target, &fname); + + /* if we're in dcc chat, send the request via it. */ + chat = irc_item_dcc_chat(item); + + if (chat != NULL && (chat->mirc_ctcp || g_strcasecmp(target, chat->nick) != 0)) + chat = NULL; + + if ((server == NULL || !server->connected) && chat == NULL) + cmd_param_error(CMDERR_NOT_CONNECTED); + + if (*target == '\0' || *fname == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + if (dcc_find_item(DCC_TYPE_SEND, target, fname)) + { + signal_emit("dcc error send exists", 2, target, fname); + g_free(params); + return; + } + + str = convert_home(fname); + if (*str != '/') + { + gchar *path; + + g_free(str); + path = convert_home(settings_get_str("dcc_upload_path")); + str = g_strconcat(path, "/", fname, NULL); + g_free(path); + } + + fh = open(str, O_RDONLY); + g_free(str); + + if (fh == -1) + { + signal_emit("dcc error file not found", 2, target, fname); + g_free(params); + return; + } + fsize = lseek(fh, 0, SEEK_END); + lseek(fh, 0, SEEK_SET); + + /* get the IP address we use with IRC server */ + if (!net_getsockname(chat != NULL ? chat->handle : server->handle, &addr, NULL)) + { + close(fh); + cmd_param_error(CMDERR_GETSOCKNAME); + } + + /* start listening */ + port = settings_get_int("dcc_port"); + h = net_listen(&addr, &port); + if (h == -1) + { + close(fh); + cmd_param_error(CMDERR_LISTEN); + } + + /* skip path */ + ptr = strrchr(fname, '/'); + if (ptr != NULL) fname = ptr+1; + + /* change all spaces to _ */ + fname = g_strdup(fname); + for (ptr = fname; *ptr != '\0'; ptr++) + if (*ptr == ' ') *ptr = '_'; + + dcc = dcc_create(DCC_TYPE_SEND, h, target, fname, server, chat); + dcc->port = port; + dcc->size = fsize; + dcc->fhandle = fh; + dcc->tagread = g_input_add(h, G_INPUT_READ, + (GInputFunction) dcc_send_init, dcc); + + /* send DCC request */ + str = g_strdup_printf("DCC SEND %s %s %d %lu", + fname, dcc_make_address(&addr), port, fsize); + dcc_ctcp_message(target, server, chat, FALSE, str); + g_free(str); + + g_free(fname); + g_free(params); +} + +static void read_settings(void) +{ + dcc_file_create_mode = octal2dec(settings_get_int("dcc_file_create_mode")); +} + +void dcc_files_init(void) +{ + signal_add("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg); + signal_add("setup changed", (SIGNAL_FUNC) read_settings); + signal_add("irssi init finished", (SIGNAL_FUNC) read_settings); + command_bind("dcc send", NULL, (SIGNAL_FUNC) cmd_dcc_send); + command_bind("dcc get", NULL, (SIGNAL_FUNC) cmd_dcc_get); + command_bind("dcc resume", NULL, (SIGNAL_FUNC) cmd_dcc_resume); +} + +void dcc_files_deinit(void) +{ + signal_remove("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg); + signal_remove("setup changed", (SIGNAL_FUNC) read_settings); + signal_remove("irssi init finished", (SIGNAL_FUNC) read_settings); + command_unbind("dcc send", (SIGNAL_FUNC) cmd_dcc_send); + command_unbind("dcc get", (SIGNAL_FUNC) cmd_dcc_get); + command_unbind("dcc resume", (SIGNAL_FUNC) cmd_dcc_resume); +} diff --git a/src/irc/dcc/dcc-files.h b/src/irc/dcc/dcc-files.h new file mode 100644 index 00000000..3d12ffc1 --- /dev/null +++ b/src/irc/dcc/dcc-files.h @@ -0,0 +1,7 @@ +#ifndef __DCC_FILES_H +#define __DCC_FILES_H + +void dcc_files_init(void); +void dcc_files_deinit(void); + +#endif diff --git a/src/irc/dcc/dcc.c b/src/irc/dcc/dcc.c new file mode 100644 index 00000000..41833744 --- /dev/null +++ b/src/irc/dcc/dcc.c @@ -0,0 +1,550 @@ +/* + dcc.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 "signals.h" +#include "commands.h" +#include "network.h" +#include "line-split.h" +#include "settings.h" + +#include "masks.h" +#include "irc.h" + +#include "dcc.h" + +#define DCC_TYPES 5 + +static gchar *dcc_types[] = +{ + "CHAT", + "SEND", + "GET", + "RESUME", + "ACCEPT" +}; + +GSList *dcc_conns; + +static gint dcc_timeouttag; + +/* Create new DCC record */ +DCC_REC *dcc_create(gint type, gint handle, gchar *nick, gchar *arg, IRC_SERVER_REC *server, DCC_REC *chat) +{ + DCC_REC *dcc; + + g_return_val_if_fail(nick != NULL, NULL); + g_return_val_if_fail(arg != NULL, NULL); + + dcc = g_new0(DCC_REC, 1); + dcc->type = type == DCC_TYPE_CHAT ? module_get_uniq_id("IRC", WI_IRC_DCC_CHAT) : -1; + dcc->mirc_ctcp = settings_get_bool("toggle_dcc_mirc_ctcp"); + dcc->created = time(NULL); + dcc->chat = chat; + dcc->dcc_type = type; + dcc->arg = g_strdup(arg); + dcc->nick = g_strdup(nick); + dcc->handle = handle; + dcc->fhandle = -1; + 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->ircnet == NULL ? NULL : g_strdup(server->connrec->ircnet); + dcc_conns = g_slist_append(dcc_conns, dcc); + + signal_emit("dcc created", 1, dcc); + return dcc; +} + +/* Destroy DCC record */ +void dcc_destroy(DCC_REC *dcc) +{ + GSList *tmp; + + g_return_if_fail(dcc != NULL); + + dcc_conns = g_slist_remove(dcc_conns, dcc); + + /* remove dcc chat references.. */ + for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) + { + DCC_REC *rec = tmp->data; + + if (rec->chat == dcc) + rec->chat = NULL; + } + + signal_emit("dcc destroyed", 1, dcc); + + if (dcc->fhandle != -1) close(dcc->fhandle); + if (dcc->handle != -1) net_disconnect(dcc->handle); + if (dcc->tagread != -1) g_source_remove(dcc->tagread); + if (dcc->tagwrite != -1) g_source_remove(dcc->tagwrite); + + if (dcc->dcc_type == DCC_TYPE_CHAT) + line_split_free((LINEBUF_REC *) dcc->databuf); + else if (dcc->databuf != NULL) g_free(dcc->databuf); + if (dcc->file != NULL) g_free(dcc->file); + if (dcc->ircnet != NULL) g_free(dcc->ircnet); + g_free(dcc->mynick); + g_free(dcc->nick); + g_free(dcc->arg); + g_free(dcc); +} + +gchar *dcc_make_address(IPADDR *ip) +{ + static gchar str[MAX_IP_LEN]; + gulong addr; + + if (is_ipv6_addr(ip)) + { + /* IPv6 */ + net_ip2host(ip, str); + } + else + { + memcpy(&addr, &ip->addr, 4); + sprintf(str, "%lu", (unsigned long) htonl(addr)); + } + + return str; +} + +/* Find DCC record, arg can be NULL */ +DCC_REC *dcc_find_item(gint type, gchar *nick, gchar *arg) +{ + DCC_REC *dcc; + GSList *tmp; + + g_return_val_if_fail(nick != NULL, NULL); + + for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) + { + dcc = tmp->data; + + if (dcc->dcc_type == type && g_strcasecmp(dcc->nick, nick) == 0 && + (arg == NULL || strcmp(dcc->arg, arg) == 0)) + return dcc; + } + + return NULL; +} + +/* Find DCC record by port # */ +DCC_REC *dcc_find_by_port(gchar *nick, gint port) +{ + DCC_REC *dcc; + GSList *tmp; + + for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) + { + dcc = tmp->data; + + if (dcc->port == port && ((dcc->dcc_type == DCC_TYPE_GET || dcc->dcc_type == DCC_TYPE_SEND) && g_strcasecmp(dcc->nick, nick) == 0)) + { + /* found! */ + return dcc; + } + } + + return NULL; +} + +gchar *dcc_type2str(gint type) +{ + g_return_val_if_fail(type >= 1 && type <= DCC_TYPES, NULL); + return dcc_types[type-1]; +} + +gint dcc_str2type(gchar *type) +{ + gint num; + + for (num = 0; num < DCC_TYPES; num++) + if (g_strcasecmp(dcc_types[num], type) == 0) return num+1; + + return 0; +} + +void dcc_ctcp_message(gchar *target, IRC_SERVER_REC *server, DCC_REC *chat, gboolean notice, gchar *msg) +{ + gchar *str; + + if (chat != NULL) + { + /* send it via open DCC chat */ + /* FIXME: we need output queue! */ + str = g_strdup_printf("%s\001%s\001\n", chat->mirc_ctcp ? "" : + notice ? "CTCP_REPLY " : "CTCP_MESSAGE ", msg); + net_transmit(chat->handle, str, strlen(str)); + } + else + { + str = g_strdup_printf("%s %s :\001%s\001", + notice ? "NOTICE" : "PRIVMSG", target, msg); + irc_send_cmd(server, str); + } + + g_free(str); +} + +/* Server connected, check if there's any open dcc sessions for this ircnet.. */ +static void dcc_server_connected(IRC_SERVER_REC *server) +{ + GSList *tmp; + + g_return_if_fail(server != NULL); + + if (server->connrec->ircnet == 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->ircnet) == 0) { + dcc->server = server; + g_free(dcc->mynick); + dcc->mynick = g_strdup(server->nick); + } + } +} + +/* Server disconnected, remove it from all dcc records */ +static void dcc_server_disconnected(IRC_SERVER_REC *server) +{ + GSList *tmp; + + g_return_if_fail(server != NULL); + + for (tmp = dcc_conns; tmp != NULL; tmp = tmp->next) + { + DCC_REC *dcc = tmp->data; + + if (dcc->server == server) + { + if (dcc->ircnet == NULL) + dcc->server = NULL; + else + { + dcc->server = (IRC_SERVER_REC *) server_find_ircnet(dcc->ircnet); + if (dcc->server != NULL) + { + g_free(dcc->mynick); + dcc->mynick = g_strdup(dcc->server->nick); + } + } + } + } +} + +static void dcc_get_address(gchar *str, IPADDR *ip) +{ + gulong addr; + + if (strchr(str, ':') == NULL) + { + /* normal IPv4 address */ + if (sscanf(str, "%lu", &addr)!=1) + addr = 0; + ip->family = AF_INET; + addr = (gulong) ntohl(addr); + memcpy(&ip->addr, &addr, 4); + } + else + { + /* IPv6 */ + net_host2ip(str, ip); + } +} + +/* Handle incoming DCC CTCP messages */ +static void dcc_ctcp_msg(gchar *data, IRC_SERVER_REC *server, gchar *sender, gchar *sendaddr, gchar *target, DCC_REC *chat) +{ + gchar *params, *type, *arg, *addrstr, *portstr, *sizestr, *str; + const char *cstr; + DCC_REC *dcc; + gulong size; + gint port; + + g_return_if_fail(data != NULL); + g_return_if_fail(sender != NULL); + + params = cmd_get_params(data, 5, &type, &arg, &addrstr, &portstr, &sizestr); + + if (sscanf(portstr, "%d", &port) != 1) port = 0; + if (sscanf(sizestr, "%lu", &size) != 1) size = 0; + + dcc = dcc_create(SWAP_SENDGET(dcc_str2type(type)), -1, sender, arg, server, chat); + dcc_get_address(addrstr, &dcc->addr); + net_ip2host(&dcc->addr, dcc->addrstr); + dcc->port = port; + dcc->size = size; + + switch (dcc->dcc_type) + { + case DCC_TYPE_GET: + cstr = settings_get_str("dcc_autoget_masks"); + /* check that autoget masks match */ + if (settings_get_bool("toggle_dcc_autoget") && (*cstr == '\0' || irc_masks_match(cstr, sender, sendaddr)) && + /* check file size limit, FIXME: it's possible to send a bogus file size and then just send what ever sized file.. */ + (settings_get_int("dcc_max_autoget_size") <= 0 || (settings_get_int("dcc_max_autoget_size") > 0 && size <= settings_get_int("dcc_max_autoget_size")*1024))) + { + /* automatically get */ + str = g_strdup_printf("GET %s %s", dcc->nick, dcc->arg); + signal_emit("command dcc", 2, str, server); + g_free(str); + } + else + { + /* send request */ + signal_emit("dcc request", 1, dcc); + } + break; + + case DCC_TYPE_CHAT: + cstr = settings_get_str("dcc_autochat_masks"); + if (*cstr != '\0' && irc_masks_match(cstr, sender, sendaddr)) + { + /* automatically accept chat */ + str = g_strdup_printf("CHAT %s", dcc->nick); + signal_emit("command dcc", 2, str, server); + g_free(str); + } + else + { + /* send request */ + signal_emit("dcc request", 1, dcc); + } + break; + + case DCC_TYPE_RESUME: + case DCC_TYPE_ACCEPT: + /* handle this in dcc-files.c */ + dcc_destroy(dcc); + break; + + default: + /* unknown DCC command */ + signal_emit("dcc unknown ctcp", 3, data, sender, sendaddr); + dcc_destroy(dcc); + break; + } + + g_free(params); +} + +/* Handle incoming DCC CTCP replies */ +static void dcc_ctcp_reply(gchar *data, IRC_SERVER_REC *server, gchar *sender, gchar *sendaddr) +{ + gchar *params, *cmd, *subcmd, *args; + gint type; + DCC_REC *dcc; + + g_return_if_fail(data != NULL); + g_return_if_fail(sender != NULL); + + params = cmd_get_params(data, 3 | PARAM_FLAG_GETREST, &cmd, &subcmd, &args); + + if (g_strcasecmp(cmd, "REJECT") == 0) + { + type = dcc_str2type(subcmd); + dcc = dcc_find_item(type, sender, type == DCC_TYPE_CHAT ? NULL : args); + if (dcc != NULL) + { + dcc->destroyed = TRUE; + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); + } + } + else + { + /* unknown dcc ctcp reply */ + signal_emit("dcc unknown reply", 3, data, sender, sendaddr); + } + + g_free(params); +} + +static void dcc_reject(DCC_REC *dcc, IRC_SERVER_REC *server) +{ + gchar *str; + + g_return_if_fail(dcc != NULL); + + if (dcc->server != NULL) server = dcc->server; + if (server != NULL && (dcc->dcc_type != DCC_TYPE_CHAT || dcc->starttime == 0)) + { + signal_emit("dcc rejected", 1, dcc); + str = g_strdup_printf("NOTICE %s :\001DCC REJECT %s %s\001", + dcc->nick, dcc_type2str(SWAP_SENDGET(dcc->dcc_type)), dcc->arg); + + irc_send_cmd(server, str); + g_free(str); + } + + dcc->destroyed = TRUE; + signal_emit("dcc closed", 1, dcc); + dcc_destroy(dcc); +} + +/* command: DCC CLOSE */ +static void cmd_dcc_close(gchar *data, IRC_SERVER_REC *server) +{ + DCC_REC *dcc; + GSList *tmp, *next; + gchar *params, *type, *nick, *arg; + gboolean found; + gint itype; + + g_return_if_fail(data != NULL); + + params = cmd_get_params(data, 3, &type, &nick, &arg); + + g_strup(type); + itype = dcc_str2type(type); + if (itype == 0) + { + signal_emit("dcc error unknown type", 1, type); + g_free(params); + return; + } + + dcc = NULL; found = FALSE; + for (tmp = dcc_conns; tmp != NULL; tmp = next) + { + dcc = tmp->data; + next = tmp->next; + + if (dcc->dcc_type == itype && g_strcasecmp(nick, dcc->nick) == 0) + { + dcc_reject(dcc, server); + found = TRUE; + } + } + + if (!found) + signal_emit("dcc error close not found", 3, type, nick, arg); + + g_free(params); +} + +static void cmd_dcc(const char *data, IRC_SERVER_REC *server, WI_IRC_REC *item) +{ + command_runsub("dcc", data, server, item); +} + +static int dcc_timeout_func(void) +{ + GSList *tmp, *next; + time_t now; + + now = time(NULL)-settings_get_int("dcc_timeout"); + for (tmp = dcc_conns; tmp != NULL; tmp = next) + { + DCC_REC *rec = tmp->data; + + next = tmp->next; + if (rec->tagread == -1 && now > rec->created) + { + /* timed out. */ + dcc_reject(rec, NULL); + } + } + return 1; +} + +static void event_no_such_nick(gchar *data, IRC_SERVER_REC *server) +{ + gchar *params, *nick; + GSList *tmp, *next; + + g_return_if_fail(data != NULL); + + params = event_get_params(data, 2, NULL, &nick); + + /* check if we've send any dcc requests to this nick.. */ + for (tmp = dcc_conns; tmp != NULL; tmp = next) + { + DCC_REC *rec = tmp->data; + + next = tmp->next; + if (g_strcasecmp(rec->nick, nick) == 0 && rec->starttime == 0) + { + /* timed out. */ + rec->destroyed = TRUE; + signal_emit("dcc closed", 1, rec); + dcc_destroy(rec); + } + } + + g_free(params); +} + +void dcc_init(void) +{ + dcc_conns = NULL; + dcc_timeouttag = g_timeout_add(1000, (GSourceFunc) dcc_timeout_func, NULL); + + settings_add_bool("dcc", "toggle_dcc_autorename", FALSE); + settings_add_bool("dcc", "toggle_dcc_autogete", FALSE); + settings_add_int("dcc", "dcc_max_autoget_size", 1000); + settings_add_str("dcc", "dcc_download_path", "~"); + settings_add_int("dcc", "dcc_file_create_mode", 644); + settings_add_str("dcc", "dcc_autoget_masks", ""); + settings_add_str("dcc", "dcc_autochat_masks", ""); + + settings_add_bool("dcc", "toggle_dcc_fast_send", TRUE); + settings_add_str("dcc", "dcc_upload_path", "~"); + + settings_add_bool("dcc", "toggle_dcc_mirc_ctcp", FALSE); + settings_add_bool("dcc", "toggle_dcc_autodisplay_dialog", TRUE); + settings_add_int("dcc", "dcc_block_size", 2048); + settings_add_int("dcc", "dcc_port", 0); + settings_add_int("dcc", "dcc_timeout", 300); + + signal_add("server connected", (SIGNAL_FUNC) dcc_server_connected); + signal_add("server disconnected", (SIGNAL_FUNC) dcc_server_disconnected); + signal_add("ctcp reply dcc", (SIGNAL_FUNC) dcc_ctcp_reply); + signal_add("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg); + 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); +} + +void dcc_deinit(void) +{ + signal_remove("server connected", (SIGNAL_FUNC) dcc_server_connected); + signal_remove("server disconnected", (SIGNAL_FUNC) dcc_server_disconnected); + signal_remove("ctcp reply dcc", (SIGNAL_FUNC) dcc_ctcp_reply); + signal_remove("ctcp msg dcc", (SIGNAL_FUNC) dcc_ctcp_msg); + 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); +} diff --git a/src/irc/dcc/dcc.h b/src/irc/dcc/dcc.h new file mode 100644 index 00000000..da640b41 --- /dev/null +++ b/src/irc/dcc/dcc.h @@ -0,0 +1,91 @@ +#ifndef __DCC_H +#define __DCC_H + +#include "network.h" + +enum +{ + DCC_TYPE_CHAT = 1, + DCC_TYPE_SEND, + DCC_TYPE_GET, + DCC_TYPE_RESUME, + DCC_TYPE_ACCEPT +}; + +enum +{ + DCC_GET_DEFAULT = 0, + DCC_GET_OVERWRITE, + DCC_GET_RENAME, + DCC_GET_RESUME +}; + +#define SWAP_SENDGET(a) ((a) == DCC_TYPE_SEND ? DCC_TYPE_GET : \ + (a) == DCC_TYPE_GET ? DCC_TYPE_SEND : (a)) + +typedef struct DCC_REC +{ + int type; + GHashTable *module_data; + + IRC_SERVER_REC *server; + gchar *nick; + + struct DCC_REC *chat; /* if the request came through DCC chat */ + + gchar *ircnet; + gchar *mynick; + + gchar *arg; + gchar *file; /* file name we're really moving, arg is just the reference.. */ + + time_t created; + gint dcc_type; + + IPADDR addr; /* address we're connected in */ + gchar addrstr[MAX_IP_LEN]; /* in readable form */ + gint port; /* port we're connected in */ + + glong size, transfd, skipped; /* file size / bytes transferred / skipped at start */ + gint handle; /* socket handle */ + gint tagread, tagwrite; + gint fhandle; /* file handle */ + time_t starttime; /* transfer start time */ + gint trans_bytes; + + gboolean fastsend; /* fastsending (just in case that global fastsend toggle changes while transferring..) */ + gboolean waitforend; /* DCC fast send: file is sent, just wait for the replies from the other side */ + gboolean gotalldata; /* DCC fast send: got all acks from the other end (needed to make sure the end of transfer works right) */ + gint get_type; /* DCC get: what to do if file exists? */ + + gboolean mirc_ctcp; /* DCC chat: Send CTCPs without the CTCP_MESSAGE prefix */ + gboolean destroyed; /* We're about to destroy this DCC recond */ + + /* read counter buffer */ + gchar read_buf[4]; + gint read_pos; + + gchar *databuf; /* buffer for receiving/transmitting data */ + gint databufsize; +} +DCC_REC; + +extern GSList *dcc_conns; + +void dcc_init(void); +void dcc_deinit(void); + +/* Find DCC record, arg can be NULL */ +DCC_REC *dcc_find_item(gint type, gchar *nick, gchar *arg); +DCC_REC *dcc_find_by_port(gchar *nick, gint port); + +gchar *dcc_type2str(gint type); +gint dcc_str2type(gchar *type); +gchar *dcc_make_address(IPADDR *ip); + +DCC_REC *dcc_create(gint type, gint handle, gchar *nick, gchar *arg, IRC_SERVER_REC *server, DCC_REC *chat); +void dcc_destroy(DCC_REC *dcc); + +void dcc_ctcp_message(gchar *target, IRC_SERVER_REC *server, DCC_REC *chat, gboolean notice, gchar *msg); + +#endif diff --git a/src/irc/dcc/module.h b/src/irc/dcc/module.h new file mode 100644 index 00000000..2557ed0f --- /dev/null +++ b/src/irc/dcc/module.h @@ -0,0 +1,3 @@ +#include "common.h" + +#define MODULE_NAME "irc/dcc" |