diff options
author | Timo Sirainen <cras@irssi.org> | 2000-06-04 01:36:07 +0000 |
---|---|---|
committer | cras <cras@dbcabf3a-b0e7-0310-adc4-f8d773084564> | 2000-06-04 01:36:07 +0000 |
commit | 35fab0c9ef3ec129e2a753fd6a659124d0e168ae (patch) | |
tree | ce7d3d142161f11c7d7069bf29d59dba84cde075 /src/irc/dcc/dcc-chat.c | |
parent | 841736a7d359dd7f9ba0238589965ea205102415 (diff) | |
download | irssi-35fab0c9ef3ec129e2a753fd6a659124d0e168ae.zip |
Lots of DCC related fixes.
Added command_bind_first() and command_bind_last() functions.
git-svn-id: http://svn.irssi.org/repos/irssi/trunk@285 dbcabf3a-b0e7-0310-adc4-f8d773084564
Diffstat (limited to 'src/irc/dcc/dcc-chat.c')
-rw-r--r-- | src/irc/dcc/dcc-chat.c | 384 |
1 files changed, 196 insertions, 188 deletions
diff --git a/src/irc/dcc/dcc-chat.c b/src/irc/dcc/dcc-chat.c index 699af0d4..642cbb7e 100644 --- a/src/irc/dcc/dcc-chat.c +++ b/src/irc/dcc/dcc-chat.c @@ -29,53 +29,72 @@ #include "masks.h" #include "irc.h" #include "server-setup.h" +#include "query.h" #include "dcc.h" +/* Send `data' to dcc chat. */ +void dcc_chat_send(DCC_REC *dcc, const char *data) +{ + g_return_if_fail(dcc != NULL); + g_return_if_fail(data != NULL); + + /* FIXME: we need output queue! */ + net_transmit(dcc->handle, data, strlen(data)); + net_transmit(dcc->handle, "\n", 1); +} + +/* If `item' is a query of a =nick, return DCC chat record of nick */ +DCC_REC *item_get_dcc(void *item) +{ + QUERY_REC *query; + + query = irc_item_query(item); + if (query == NULL || *query->nick != '=') return NULL; + + return dcc_find_item(DCC_TYPE_CHAT, query->nick+1, NULL); +} + /* Send text to DCC chat */ -static void dcc_chat_write(gchar *data) +static void cmd_msg(const char *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); + DCC_REC *dcc; + char *params, *text, *target; + + g_return_if_fail(text != NULL); + + if (*data != '=') { + /* handle only DCC messages */ + return; } - } - g_free(params); + params = cmd_get_params(data, 2 | PARAM_FLAG_GETREST, &target, &text); + + dcc = dcc_find_item(DCC_TYPE_CHAT, ++target, NULL); + if (dcc != NULL) dcc_chat_send(dcc, text); + + g_free(params); + signal_stop(); } -static void dcc_chat_me(const char *data, IRC_SERVER_REC *server, WI_IRC_REC *item) +static void cmd_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); + dcc = item_get_dcc(item); if (dcc == NULL) return; str = g_strdup_printf("ACTION %s", data); dcc_ctcp_message(dcc->nick, NULL, dcc, FALSE, str); g_free(str); + + signal_stop(); } -static void dcc_chat_action(const char *data, IRC_SERVER_REC *server) +static void cmd_action(const char *data, IRC_SERVER_REC *server) { char *params, *target, *text; DCC_REC *dcc; @@ -93,14 +112,16 @@ static void dcc_chat_action(const char *data, IRC_SERVER_REC *server) dcc = dcc_find_item(DCC_TYPE_CHAT, target+1, NULL); if (dcc != NULL) { - str = g_strdup_printf("ACTION %s", data); + str = g_strdup_printf("ACTION %s", text); dcc_ctcp_message(dcc->nick, NULL, dcc, FALSE, str); g_free(str); } + g_free(params); + signal_stop(); } -static void dcc_chat_ctcp(const char *data, IRC_SERVER_REC *server) +static void cmd_ctcp(const char *data, IRC_SERVER_REC *server) { char *params, *target, *ctcpcmd, *ctcpdata; DCC_REC *dcc; @@ -128,62 +149,7 @@ static void dcc_chat_ctcp(const char *data, IRC_SERVER_REC *server) } 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(); + signal_stop(); } /* input function: DCC CHAT received some data.. */ @@ -200,7 +166,6 @@ static void dcc_chat_input(DCC_REC *dcc) 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; @@ -216,114 +181,114 @@ 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) { - IPADDR ip; - gint handle, port; + IPADDR ip; + int handle, port; - g_return_if_fail(dcc != NULL); + g_return_if_fail(dcc != NULL); - /* accept connection */ - handle = net_accept(dcc->handle, &ip, &port); - if (handle == -1) - return; + /* 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.. */ + /* TODO: add paranoia check - see dcc-files.c */ - g_source_remove(dcc->tagread); - close(dcc->handle); + 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); + 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); + signal_emit("dcc connected", 1, dcc); } /* callback: DCC CHAT - net_connect_nonblock() finished */ -static void dcc_chat_connect(DCC_REC *dcc) +static void sig_chat_connected(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); + 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) +static void dcc_chat_connect(DCC_REC *dcc) { - 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; + g_return_if_fail(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|G_INPUT_READ|G_INPUT_EXCEPTION, - (GInputFunction) dcc_chat_connect, dcc); - } - else - { - /* error connecting */ - signal_emit("dcc error connect", 1, dcc); - dcc_destroy(dcc); + if (dcc->handle != -1) { + dcc->tagread = g_input_add(dcc->handle, G_INPUT_WRITE|G_INPUT_READ|G_INPUT_EXCEPTION, + (GInputFunction) sig_chat_connected, dcc); + } else { + /* error connecting */ + signal_emit("dcc error connect", 1, dcc); + dcc_destroy(dcc); } +} - return; - } +/* command: DCC CHAT */ +static void cmd_dcc_chat(const char *data, IRC_SERVER_REC *server) +{ + DCC_REC *dcc; + IPADDR own_ip; + char *str, host[MAX_IP_LEN]; + int 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) { + /* found from dcc list - so we're the connecting side.. */ + dcc_chat_connect(dcc); + return; + } - /* send dcc chat request */ - if (server == NULL || !server->connected) - cmd_return_error(CMDERR_NOT_CONNECTED); + /* send dcc chat request */ + if (server == NULL || !server->connected) + cmd_return_error(CMDERR_NOT_CONNECTED); - if (net_getsockname(server->handle, &addr, NULL) == -1) - cmd_return_error(CMDERR_ERRNO); + if (net_getsockname(server->handle, &own_ip, NULL) == -1) + cmd_return_error(CMDERR_ERRNO); - port = settings_get_int("dcc_port"); - handle = net_listen(&addr, &port); - if (handle == -1) - cmd_return_error(CMDERR_ERRNO); + port = settings_get_int("dcc_port"); + handle = net_listen(&own_ip, &port); + if (handle == -1) + cmd_return_error(CMDERR_ERRNO); - 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); + 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); + /* send the request */ + dcc_make_address(&own_ip, host); + str = g_strdup_printf("PRIVMSG %s :\001DCC CHAT CHAT %s %d\001", + data, host, port); + irc_send_cmd(server, str); + g_free(str); } static void cmd_mircdcc(gchar *data, IRC_SERVER_REC *server, WI_IRC_REC *item) @@ -332,40 +297,83 @@ static void cmd_mircdcc(gchar *data, IRC_SERVER_REC *server, WI_IRC_REC *item) g_return_if_fail(data != NULL); - dcc = irc_item_dcc_chat(item); + dcc = item_get_dcc(item); if (dcc == NULL) return; dcc->mirc_ctcp = toupper(*data) == 'N' ? FALSE : TRUE; } +/* DCC CHAT: text received */ +static void dcc_chat_msg(DCC_REC *dcc, const char *msg) +{ + char *cmd, *ptr; + int reply; + + g_return_if_fail(dcc != NULL); + g_return_if_fail(msg != NULL); + + reply = FALSE; + if (g_strncasecmp(msg, "CTCP_MESSAGE ", 13) == 0) { + /* bitchx (and ircii?) sends this */ + msg += 13; + dcc->mirc_ctcp = FALSE; + } else if (g_strncasecmp(msg, "CTCP_REPLY ", 11) == 0) { + /* bitchx (and ircii?) sends this */ + msg += 11; + reply = TRUE; + dcc->mirc_ctcp = FALSE; + } else if (*msg == 1) { + /* Use the mirc style CTCPs from now on.. */ + dcc->mirc_ctcp = TRUE; + } + + /* Handle only DCC CTCPs */ + if (*msg != 1) + return; + + /* get ctcp command, remove \001 chars */ + cmd = g_strconcat(reply ? "dcc reply " : "dcc ctcp ", msg+1, NULL); + if (cmd[strlen(cmd)-1] == 1) cmd[strlen(cmd)-1] = '\0'; + + 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); + + signal_stop(); +} + static void dcc_ctcp_redirect(gchar *msg, DCC_REC *dcc) { - g_return_if_fail(msg != NULL); - g_return_if_fail(dcc != NULL); + 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); + 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); + 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 chat", NULL, (SIGNAL_FUNC) cmd_dcc_chat); + command_bind("mircdcc", NULL, (SIGNAL_FUNC) cmd_mircdcc); + signal_add_first("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg); + 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); + 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); + signal_remove("dcc chat message", (SIGNAL_FUNC) dcc_chat_msg); + signal_remove("dcc ctcp dcc", (SIGNAL_FUNC) dcc_ctcp_redirect); } |