diff options
Diffstat (limited to 'src/irc/dcc/dcc-chat.c')
-rw-r--r-- | src/irc/dcc/dcc-chat.c | 347 |
1 files changed, 277 insertions, 70 deletions
diff --git a/src/irc/dcc/dcc-chat.c b/src/irc/dcc/dcc-chat.c index 6fa9b19e..eda25211 100644 --- a/src/irc/dcc/dcc-chat.c +++ b/src/irc/dcc/dcc-chat.c @@ -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); - /* 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); + 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; } + + 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); } |