From 6d9c9d8fcd8f4a6e436edee1cd420bf8269effee Mon Sep 17 00:00:00 2001 From: isundil Date: Tue, 3 Sep 2019 13:24:56 +0200 Subject: [PATCH] add /identify command --- src/core/chatnet-rec.h | 1 + src/core/chatnets.c | 3 ++ src/fe-common/irc/fe-irc-commands.c | 49 +++++++++++++++++++++++++++++ src/fe-common/irc/fe-ircnet.c | 11 +++++-- src/fe-common/irc/module-formats.c | 1 + src/fe-common/irc/module-formats.h | 3 +- 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/core/chatnet-rec.h b/src/core/chatnet-rec.h index e3ed8aa0..97e4e686 100644 --- a/src/core/chatnet-rec.h +++ b/src/core/chatnet-rec.h @@ -9,4 +9,5 @@ char *realname; char *own_host; /* address to use when connecting this server */ char *autosendcmd; /* command to send after connecting to this ircnet */ +char *identifycmd; /* command to use for identifying to this ircnet */ IPADDR *own_ip4, *own_ip6; /* resolved own_address if not NULL */ diff --git a/src/core/chatnets.c b/src/core/chatnets.c index 2d49a6df..41ef57dd 100644 --- a/src/core/chatnets.c +++ b/src/core/chatnets.c @@ -45,6 +45,7 @@ static void chatnet_config_save(CHATNET_REC *chatnet) iconfig_node_set_str(node, "realname", chatnet->realname); iconfig_node_set_str(node, "host", chatnet->own_host); iconfig_node_set_str(node, "autosendcmd", chatnet->autosendcmd); + iconfig_node_set_str(node, "identifycmd", chatnet->identifycmd); signal_emit("chatnet saved", 2, chatnet, node); } @@ -91,6 +92,7 @@ void chatnet_destroy(CHATNET_REC *chatnet) g_free_not_null(chatnet->realname); g_free_not_null(chatnet->own_host); g_free_not_null(chatnet->autosendcmd); + g_free_not_null(chatnet->identifycmd); g_free(chatnet->name); g_free(chatnet); } @@ -154,6 +156,7 @@ static void chatnet_read(CONFIG_NODE *node) rec->realname = g_strdup(config_node_get_str(node, "realname", NULL)); rec->own_host = g_strdup(config_node_get_str(node, "host", NULL)); rec->autosendcmd = g_strdup(config_node_get_str(node, "autosendcmd", NULL)); + rec->identifycmd = g_strdup(config_node_get_str(node, "identifycmd", NULL)); chatnets = g_slist_append(chatnets, rec); signal_emit("chatnet read", 2, rec, node); diff --git a/src/fe-common/irc/fe-irc-commands.c b/src/fe-common/irc/fe-irc-commands.c index 40ad36e0..036092cf 100644 --- a/src/fe-common/irc/fe-irc-commands.c +++ b/src/fe-common/irc/fe-irc-commands.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -393,6 +394,53 @@ static void cmd_oper(const char *data, IRC_SERVER_REC *server) cmd_params_free(free_arg); } +static void identify_with_pass(const char *password, SERVER_REC *server_rec) +{ + if (*password != '\0' && IS_IRC_SERVER(server_rec)) { + char *identify_cmd = "PRIVMSG NickServ IDENTIFY %s"; + CHATNET_REC *chatnet_rec = NULL; + + if (server_rec->connrec->chatnet) + chatnet_rec = chatnet_find(server_rec->connrec->chatnet); + if (chatnet_rec && chatnet_rec->identifycmd) + identify_cmd = chatnet_rec->identifycmd; + irc_send_cmdv((IRC_SERVER_REC *) server_rec, identify_cmd, password); + } +} + +static void cmd_identify_got_pass(const char *password, char *server_tag) +{ + identify_with_pass(password, server_find_tag(server_tag)); + g_free(server_tag); +} + +/* SYNTAX: IDENTIFY */ +static void cmd_identify(const char *data, IRC_SERVER_REC *server) +{ + char *password, *format; + void *free_arg; + + g_return_if_fail(data != NULL); + if (!IS_IRC_SERVER(server) || !server->connected) + cmd_return_error(CMDERR_NOT_CONNECTED); + + if (!cmd_get_params(data, &free_arg, 1, &password)) + return; + if (*password == '\0') { + format = format_get_text(MODULE_NAME, NULL, server, NULL, + IRCTXT_ASK_USER_PASS); + keyboard_entry_redirect((SIGNAL_FUNC) cmd_identify_got_pass, + format, + ENTRY_REDIRECT_FLAG_HIDDEN, g_strdup(server->tag)); + g_free(format); + signal_stop(); + } else { + identify_with_pass(password, (SERVER_REC*) server); + } + + cmd_params_free(free_arg); +} + /* SYNTAX: SETHOST (non-ircops) SETHOST (ircops) */ static void cmd_sethost(const char *data, IRC_SERVER_REC *server) @@ -428,6 +476,7 @@ void fe_irc_commands_init(void) command_bind_irc("topic", NULL, (SIGNAL_FUNC) cmd_topic); command_bind_irc("ts", NULL, (SIGNAL_FUNC) cmd_ts); command_bind_irc("oper", NULL, (SIGNAL_FUNC) cmd_oper); + command_bind_irc("identify", NULL, (SIGNAL_FUNC) cmd_identify); command_bind_irc("sethost", NULL, (SIGNAL_FUNC) cmd_sethost); } diff --git a/src/fe-common/irc/fe-ircnet.c b/src/fe-common/irc/fe-ircnet.c index 3484d923..f699434c 100644 --- a/src/fe-common/irc/fe-ircnet.c +++ b/src/fe-common/irc/fe-ircnet.c @@ -58,6 +58,8 @@ static void cmd_network_list(void) g_string_append_printf(str, "host: %s, ", rec->own_host); if (rec->autosendcmd != NULL) g_string_append_printf(str, "autosendcmd: %s, ", rec->autosendcmd); + if (rec->identifycmd != NULL) + g_string_append_printf(str, "identifycmd: %s, ", rec->identifycmd); if (rec->usermode != NULL) g_string_append_printf(str, "usermode: %s, ", rec->usermode); if (rec->sasl_mechanism != NULL) @@ -125,6 +127,7 @@ static void cmd_network_add_modify(const char *data, gboolean add) } if (g_hash_table_lookup(optlist, "usermode")) g_free_and_null(rec->usermode); if (g_hash_table_lookup(optlist, "autosendcmd")) g_free_and_null(rec->autosendcmd); + if (g_hash_table_lookup(optlist, "identifycmd")) g_free_and_null(rec->identifycmd); if (g_hash_table_lookup(optlist, "sasl_mechanism")) g_free_and_null(rec->sasl_mechanism); if (g_hash_table_lookup(optlist, "sasl_username")) g_free_and_null(rec->sasl_username); if (g_hash_table_lookup(optlist, "sasl_password")) g_free_and_null(rec->sasl_password); @@ -165,6 +168,8 @@ static void cmd_network_add_modify(const char *data, gboolean add) if (value != NULL && *value != '\0') rec->usermode = g_strdup(value); value = g_hash_table_lookup(optlist, "autosendcmd"); if (value != NULL && *value != '\0') rec->autosendcmd = g_strdup(value); + value = g_hash_table_lookup(optlist, "identifycmd"); + if (value != NULL && *value != '\0') rec->identifycmd = g_strdup(value); /* the validity of the parameters is checked in sig_server_setup_fill_chatnet */ value = g_hash_table_lookup(optlist, "sasl_mechanism"); @@ -186,7 +191,7 @@ static void cmd_network_add_modify(const char *data, gboolean add) [-kicks ] [-modes ] [-cmdspeed ] [-cmdmax ] [-sasl_mechanism ] [-sasl_username ] [-sasl_password ] - */ + [-identifycmd ] */ static void cmd_network_add(const char *data) { cmd_network_add_modify(data, TRUE); @@ -233,9 +238,9 @@ void fe_ircnet_init(void) command_bind("network remove", NULL, (SIGNAL_FUNC) cmd_network_remove); command_set_options("network add", "-kicks -msgs -modes -whois -cmdspeed " - "-cmdmax -nick -alternate_nick -user -realname -host -autosendcmd -querychans -usermode -sasl_mechanism -sasl_username -sasl_password"); + "-cmdmax -nick -alternate_nick -user -realname -host -autosendcmd -identifycmd -querychans -usermode -sasl_mechanism -sasl_username -sasl_password"); command_set_options("network modify", "-kicks -msgs -modes -whois -cmdspeed " - "-cmdmax -nick -alternate_nick -user -realname -host -autosendcmd -querychans -usermode -sasl_mechanism -sasl_username -sasl_password"); + "-cmdmax -nick -alternate_nick -user -realname -host -autosendcmd -identifycmd -querychans -usermode -sasl_mechanism -sasl_username -sasl_password"); } void fe_ircnet_deinit(void) diff --git a/src/fe-common/irc/module-formats.c b/src/fe-common/irc/module-formats.c index a0f6bd29..2466b969 100644 --- a/src/fe-common/irc/module-formats.c +++ b/src/fe-common/irc/module-formats.c @@ -170,6 +170,7 @@ FORMAT_REC fecommon_irc_formats[] = { { "silenced", "Silenced {nick $0}", 1, { 0 } }, { "unsilenced", "Unsilenced {nick $0}", 1, { 0 } }, { "silence_line", "{nick $0}: silence {ban $1}", 2, { 0, 0 } }, + { "ask_user_pass", "password:", 0 }, { "ask_oper_pass", "Operator password:", 0 }, { "accept_list", "Accepted users: {hilight $0}", 1, { 0 } }, diff --git a/src/fe-common/irc/module-formats.h b/src/fe-common/irc/module-formats.h index 66a9804f..46a347c0 100644 --- a/src/fe-common/irc/module-formats.h +++ b/src/fe-common/irc/module-formats.h @@ -140,7 +140,8 @@ enum { IRCTXT_SILENCED, IRCTXT_UNSILENCED, IRCTXT_SILENCE_LINE, - IRCTXT_ASK_OPER_PASS, + IRCTXT_ASK_USER_PASS, + IRCTXT_ASK_OPER_PASS, IRCTXT_ACCEPT_LIST };