X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=apps%2Firssi%2Fsrc%2Fsilc%2Fcore%2Fsilc-channels.c;h=4bd4e49ef556fb0d8a464fb930f8f014028eafa7;hb=HEAD;hp=afff90e60b28a94dbbe4f66297d476385b42e5cb;hpb=fbcba64240f0accf54c44249df6b03652bb86dfc;p=silc.git diff --git a/apps/irssi/src/silc/core/silc-channels.c b/apps/irssi/src/silc/core/silc-channels.c index afff90e6..4bd4e49e 100644 --- a/apps/irssi/src/silc/core/silc-channels.c +++ b/apps/irssi/src/silc/core/silc-channels.c @@ -1,19 +1,19 @@ /* silc-channels.c : irssi - Copyright (C) 2000 - 2001 Timo Sirainen - Pekka Riikonen + Copyright (C) 2000 - 2001, 2004, 2006, 2007 Timo Sirainen + Pekka Riikonen 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 @@ -38,6 +38,7 @@ #include "silc-channels.h" #include "silc-queries.h" #include "silc-nicklist.h" +#include "silc-cmdqueue.h" #include "window-item-def.h" #include "fe-common/core/printtext.h" @@ -45,8 +46,38 @@ #include "silc-commands.h" +void sig_mime(SILC_SERVER_REC *server, SILC_CHANNEL_REC *channel, + const char *blob, const char *nick, int verified) +{ + unsigned char *message; + SilcUInt32 message_len; + SilcMime mime; + + if (!(IS_SILC_SERVER(server))) + return; + + message = silc_unescape_data(blob, &message_len); + + mime = silc_mime_decode(NULL, message, message_len); + if (!mime) { + silc_free(message); + return; + } + + printformat_module("fe-common/silc", server, + channel == NULL ? NULL : channel->name, + MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA, + nick == NULL ? "[]" : nick, + silc_mime_get_field(mime, "Content-Type")); + + silc_free(message); + silc_mime_free(mime); +} + SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server, - const char *name, int automatic) + const char *name, + const char *visible_name, + int automatic) { SILC_CHANNEL_REC *rec; @@ -55,10 +86,8 @@ SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server, rec = g_new0(SILC_CHANNEL_REC, 1); rec->chat_type = SILC_PROTOCOL; - rec->name = g_strdup(name); - rec->server = server; - - channel_init((CHANNEL_REC *) rec, automatic); + channel_init((CHANNEL_REC *)rec, (SERVER_REC *)server, name, name, + automatic); return rec; } @@ -73,6 +102,8 @@ static void sig_channel_destroyed(SILC_CHANNEL_REC *channel) /* destroying channel record without actually having left the channel yet */ silc_command_exec(channel->server, "LEAVE", channel->name); + /* enable queueing because we destroy the channel immedially */ + silc_queue_enable(channel->server->conn); } } @@ -80,7 +111,10 @@ static void silc_channels_join(SILC_SERVER_REC *server, const char *channels, int automatic) { char **list, **tmp; + char *channel, *key; SILC_CHANNEL_REC *chanrec; + CHANNEL_SETUP_REC *schannel; + GString *tmpstr; list = g_strsplit(channels, ",", -1); for (tmp = list; *tmp != NULL; tmp++) { @@ -88,7 +122,25 @@ static void silc_channels_join(SILC_SERVER_REC *server, if (chanrec) continue; - silc_command_exec(server, "JOIN", *tmp); + channel = *tmp; + key = strchr(channel, ' '); + if (key != NULL) { + *key = '\0'; + key++; + } + tmpstr = g_string_new(NULL); + + schannel = channel_setup_find(channel, server->connrec->chatnet); + if (key && *key != '\0') + g_string_sprintfa(tmpstr, "%s %s", channel, key); + else if (schannel && schannel->password && schannel->password[0] != '\0') + g_string_sprintfa(tmpstr, "%s %s", channel, schannel->password); + else + g_string_sprintfa(tmpstr, "%s", channel); + + + silc_command_exec(server, "JOIN", tmpstr->str); + g_string_free(tmpstr, FALSE); } g_strfreev(list); @@ -105,7 +157,7 @@ static void sig_connected(SILC_SERVER_REC *server) static void sig_server_quit(SILC_SERVER_REC *server, const char *msg) { - if (IS_SILC_SERVER(server) && server->conn && server->conn->sock) + if (IS_SILC_SERVER(server) && server->conn) silc_command_exec(server, "QUIT", msg); } @@ -135,7 +187,7 @@ static void command_part(const char *data, SILC_SERVER_REC *server, { SILC_CHANNEL_REC *chanrec; char userhost[256]; - + CMD_SILC_SERVER(server); if (!IS_SILC_SERVER(server) || !server->connected) @@ -144,151 +196,135 @@ static void command_part(const char *data, SILC_SERVER_REC *server, if (!strcmp(data, "*") || *data == '\0') { if (!IS_SILC_CHANNEL(item)) cmd_return_error(CMDERR_NOT_JOINED); - data = item->name; + data = item->visible_name; } chanrec = silc_channel_find(server, data); - if (chanrec == NULL) + if (chanrec == NULL) cmd_return_error(CMDERR_CHAN_NOT_FOUND); memset(userhost, 0, sizeof(userhost)); snprintf(userhost, sizeof(userhost) - 1, "%s@%s", - server->conn->local_entry->username, + server->conn->local_entry->username, server->conn->local_entry->hostname); signal_emit("message part", 5, server, chanrec->name, server->nick, userhost, ""); - + chanrec->left = TRUE; silc_command_exec(server, "LEAVE", chanrec->name); + /* enable queueing because we destroy the channel immedially */ + silc_queue_enable(server->conn); signal_stop(); - + channel_destroy(CHANNEL(chanrec)); } -/* ME local command. */ -static void command_me(const char *data, SILC_SERVER_REC *server, - WI_ITEM_REC *item) +/* ACTION local command. */ + +static void command_action(const char *data, SILC_SERVER_REC *server, + WI_ITEM_REC *item) { - SILC_CHANNEL_REC *chanrec; - char *tmpcmd = "ME", *tmp; - SilcUInt32 argc = 0; - unsigned char *message = NULL; - unsigned char **argv; - SilcUInt32 *argv_lens, *argv_types; - int i; - - CMD_SILC_SERVER(server); + GHashTable *optlist; + char *target, *msg; + char *message = NULL; + int target_type; + void *free_arg; + SilcBool sign = FALSE; + CMD_SILC_SERVER(server); if (!IS_SILC_SERVER(server) || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED); - if (!IS_SILC_CHANNEL(item)) + if ((item != NULL) && (!IS_SILC_CHANNEL(item) && !IS_SILC_QUERY(item))) cmd_return_error(CMDERR_NOT_JOINED); /* Now parse all arguments */ - tmp = g_strconcat(tmpcmd, " ", data, NULL); - silc_parse_command_line(tmp, &argv, &argv_lens, - &argv_types, &argc, 2); - g_free(tmp); - - if (argc < 2) - cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); + if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | + PARAM_FLAG_GETREST, + "action", &optlist, &target, &msg)) + return; - chanrec = silc_channel_find(server, item->name); - if (chanrec == NULL) - cmd_return_error(CMDERR_CHAN_NOT_FOUND); + if (*target == '\0' || *msg == '\0') + cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + if (strcmp(target, "*") == 0) { + /* send to active channel/query */ + if (item == NULL) + cmd_param_error(CMDERR_NOT_JOINED); + + target_type = IS_SILC_CHANNEL(item) ? + SEND_TARGET_CHANNEL : SEND_TARGET_NICK; + target = (char *)window_item_get_target(item); + } else if (g_hash_table_lookup(optlist, "channel") != NULL) + target_type = SEND_TARGET_CHANNEL; + else { + target_type = SEND_TARGET_NICK; + } if (!silc_term_utf8()) { - int len = silc_utf8_encoded_len(argv[1], argv_lens[1], - SILC_STRING_LANGUAGE); + int len = silc_utf8_encoded_len(msg, strlen(msg), + SILC_STRING_LOCALE); message = silc_calloc(len + 1, sizeof(*message)); g_return_if_fail(message != NULL); - silc_utf8_encode(argv[1], argv_lens[1], SILC_STRING_LANGUAGE, + silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len); } - /* Send the action message */ - silc_client_send_channel_message(silc_client, server->conn, - chanrec->entry, NULL, - SILC_MESSAGE_FLAG_ACTION | - SILC_MESSAGE_FLAG_UTF8, - message ? message : argv[1], - message ? strlen(message) : argv_lens[1], - TRUE); - - printformat_module("fe-common/silc", server, chanrec->entry->channel_name, - MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_OWNACTION, - server->conn->local_entry->nickname, argv[1]); - - for (i = 0; i < argc; i++) - silc_free(argv[i]); - silc_free(argv_lens); - silc_free(argv_types); + if (target != NULL) { + if (target_type == SEND_TARGET_CHANNEL) { + sign = (g_hash_table_lookup(optlist, "sign") ? TRUE : + settings_get_bool("sign_channel_messages") ? TRUE : FALSE); + if (silc_send_channel(server, target, (message != NULL ? message : msg), + SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | + (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) { + if (g_hash_table_lookup(optlist, "sign")) + signal_emit("message silc signed_own_action", 3, server, msg, target); + else + signal_emit("message silc own_action", 3, server, msg, target); + } + } else { + sign = (g_hash_table_lookup(optlist, "sign") ? TRUE : + settings_get_bool("sign_private_messages") ? TRUE : FALSE); + if (silc_send_msg(server, target, (message != NULL ? message : msg), + (message != NULL ? strlen(message) : strlen(msg)), + SILC_MESSAGE_FLAG_ACTION | SILC_MESSAGE_FLAG_UTF8 | + (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) { + if (g_hash_table_lookup(optlist, "sign")) + signal_emit("message silc signed_own_private_action", 3, + server, msg, target); + else + signal_emit("message silc own_private_action", 3, + server, msg, target); + } + } + } + + cmd_params_free(free_arg); silc_free(message); } -/* ACTION local command. Same as ME but takes the channel as mandatory - argument. */ +/* ME local command. */ -static void command_action(const char *data, SILC_SERVER_REC *server, - WI_ITEM_REC *item) +static void command_me(const char *data, SILC_SERVER_REC *server, + WI_ITEM_REC *item) { - SILC_CHANNEL_REC *chanrec; - char *tmpcmd = "ME", *tmp; - SilcUInt32 argc = 0; - unsigned char *message = NULL; - unsigned char **argv; - SilcUInt32 *argv_lens, *argv_types; - int i; - + char *tmpcmd; + CMD_SILC_SERVER(server); if (!IS_SILC_SERVER(server) || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED); - if (!IS_SILC_CHANNEL(item)) + if (!IS_SILC_CHANNEL(item) && !IS_SILC_QUERY(item)) cmd_return_error(CMDERR_NOT_JOINED); - /* Now parse all arguments */ - tmp = g_strconcat(tmpcmd, " ", data, NULL); - silc_parse_command_line(tmp, &argv, &argv_lens, - &argv_types, &argc, 3); - g_free(tmp); - - if (argc < 3) - cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); + if (IS_SILC_CHANNEL(item)) + tmpcmd = g_strdup_printf("-channel %s %s", item->visible_name, data); + else + tmpcmd = g_strdup_printf("%s %s", item->visible_name, data); - chanrec = silc_channel_find(server, argv[1]); - if (chanrec == NULL) - cmd_return_error(CMDERR_CHAN_NOT_FOUND); - - if (!silc_term_utf8()) { - int len = silc_utf8_encoded_len(argv[2], argv_lens[2], - SILC_STRING_LANGUAGE); - message = silc_calloc(len + 1, sizeof(*message)); - g_return_if_fail(message != NULL); - silc_utf8_encode(argv[2], argv_lens[2], SILC_STRING_LANGUAGE, - message, len); - } - - /* Send the action message */ - silc_client_send_channel_message(silc_client, server->conn, - chanrec->entry, NULL, - SILC_MESSAGE_FLAG_ACTION | - SILC_MESSAGE_FLAG_UTF8, - message ? message : argv[2], - message ? strlen(message) : argv_lens[2], - TRUE); - - printformat_module("fe-common/silc", server, chanrec->entry->channel_name, - MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_OWNACTION, - server->conn->local_entry->nickname, argv[2]); - - for (i = 0; i < argc; i++) - silc_free(argv[i]); - silc_free(argv_lens); - silc_free(argv_types); - silc_free(message); + command_action(tmpcmd, server, item); + g_free(tmpcmd); } /* NOTICE local command. */ @@ -296,104 +332,141 @@ static void command_action(const char *data, SILC_SERVER_REC *server, static void command_notice(const char *data, SILC_SERVER_REC *server, WI_ITEM_REC *item) { - SILC_CHANNEL_REC *chanrec; - char *tmpcmd = "ME", *tmp; - SilcUInt32 argc = 0; - unsigned char *message = NULL; - unsigned char **argv; - SilcUInt32 *argv_lens, *argv_types; - int i; - + GHashTable *optlist; + char *target, *msg; + char *message = NULL; + int target_type; + void *free_arg; + SilcBool sign; + CMD_SILC_SERVER(server); if (!IS_SILC_SERVER(server) || !server->connected) cmd_return_error(CMDERR_NOT_CONNECTED); - if (!IS_SILC_CHANNEL(item)) + if ((item != NULL) && (!IS_SILC_CHANNEL(item) && !IS_SILC_QUERY(item))) cmd_return_error(CMDERR_NOT_JOINED); /* Now parse all arguments */ - tmp = g_strconcat(tmpcmd, " ", data, NULL); - silc_parse_command_line(tmp, &argv, &argv_lens, - &argv_types, &argc, 2); - g_free(tmp); - - if (argc < 2) - cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS); + if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | + PARAM_FLAG_GETREST, + "notice", &optlist, &target, &msg)) + return; - chanrec = silc_channel_find(server, item->name); - if (chanrec == NULL) - cmd_return_error(CMDERR_CHAN_NOT_FOUND); + if (*target == '\0' || *msg == '\0') + cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS); + + if (strcmp(target, "*") == 0) { + /* send to active channel/query */ + if (item == NULL) + cmd_param_error(CMDERR_NOT_JOINED); + + target_type = IS_SILC_CHANNEL(item) ? + SEND_TARGET_CHANNEL : SEND_TARGET_NICK; + target = (char *)window_item_get_target(item); + } else if (g_hash_table_lookup(optlist, "channel") != NULL) + target_type = SEND_TARGET_CHANNEL; + else { + target_type = SEND_TARGET_NICK; + } if (!silc_term_utf8()) { - int len = silc_utf8_encoded_len(argv[1], argv_lens[1], - SILC_STRING_LANGUAGE); + int len = silc_utf8_encoded_len(msg, strlen(msg), + SILC_STRING_LOCALE); message = silc_calloc(len + 1, sizeof(*message)); g_return_if_fail(message != NULL); - silc_utf8_encode(argv[1], argv_lens[1], SILC_STRING_LANGUAGE, + silc_utf8_encode(msg, strlen(msg), SILC_STRING_LOCALE, message, len); } - /* Send the action message */ - silc_client_send_channel_message(silc_client, server->conn, - chanrec->entry, NULL, - SILC_MESSAGE_FLAG_NOTICE | - SILC_MESSAGE_FLAG_UTF8, - message ? message : argv[1], - message ? strlen(message) : argv_lens[1], - TRUE); - - printformat_module("fe-common/silc", server, chanrec->entry->channel_name, - MSGLEVEL_NOTICES, SILCTXT_CHANNEL_OWNNOTICE, - server->conn->local_entry->nickname, argv[1]); - - for (i = 0; i < argc; i++) - silc_free(argv[i]); - silc_free(argv_lens); - silc_free(argv_types); + if (target != NULL) { + if (target_type == SEND_TARGET_CHANNEL) { + sign = (g_hash_table_lookup(optlist, "sign") ? TRUE : + settings_get_bool("sign_channel_messages") ? TRUE : FALSE); + if (silc_send_channel(server, target, (message != NULL ? message : msg), + SILC_MESSAGE_FLAG_NOTICE | SILC_MESSAGE_FLAG_UTF8 | + (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) { + if (g_hash_table_lookup(optlist, "sign")) + signal_emit("message silc signed_own_notice", 3, server, msg, target); + else + signal_emit("message silc own_notice", 3, server, msg, target); + } + } else { + sign = (g_hash_table_lookup(optlist, "sign") ? TRUE : + settings_get_bool("sign_private_messages") ? TRUE : FALSE); + if (silc_send_msg(server, target, (message != NULL ? message : msg), + (message != NULL ? strlen(message) : strlen(msg)), + SILC_MESSAGE_FLAG_NOTICE | SILC_MESSAGE_FLAG_UTF8 | + (sign ? SILC_MESSAGE_FLAG_SIGNED : 0))) { + if (g_hash_table_lookup(optlist, "sign")) + signal_emit("message silc signed_own_private_notice", 3, + server, msg, target); + else + signal_emit("message silc own_private_notice", 3, + server, msg, target); + } + } + } + + cmd_params_free(free_arg); silc_free(message); } /* AWAY local command. Sends UMODE command that sets the SILC_UMODE_GONE flag. */ -static void command_away(const char *data, SILC_SERVER_REC *server, - WI_ITEM_REC *item) +bool silc_set_away(const char *reason, SILC_SERVER_REC *server) { bool set; - CMD_SILC_SERVER(server); - if (!IS_SILC_SERVER(server) || !server->connected) - cmd_return_error(CMDERR_NOT_CONNECTED); + return FALSE; - if (*data == '\0') { + if (*reason == '\0') { /* Remove any possible away message */ silc_client_set_away_message(silc_client, server->conn, NULL); set = FALSE; - printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, + printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, SILCTXT_UNSET_AWAY); } else { /* Set the away message */ - silc_client_set_away_message(silc_client, server->conn, (char *)data); + silc_client_set_away_message(silc_client, server->conn, (char *)reason); set = TRUE; - printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, - SILCTXT_SET_AWAY, data); + printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, + SILCTXT_SET_AWAY, reason); } server->usermode_away = set; + g_free_and_null(server->away_reason); if (set) - server->away_reason = g_strdup((char *)data); + server->away_reason = g_strdup((char *)reason); + signal_emit("away mode changed", 1, server); - silc_command_exec(server, "UMODE", set ? "+g" : "-g"); + return set; +} + +static void command_away(const char *data, SILC_SERVER_REC *server, + WI_ITEM_REC *item) +{ + CMD_SILC_SERVER(server); + + if (!IS_SILC_SERVER(server) || !server->connected) + cmd_return_error(CMDERR_NOT_CONNECTED); + + g_free_and_null(server->away_reason); + if ((data) && (*data != '\0')) + server->away_reason = g_strdup(data); + + silc_command_exec(server, "UMODE", + (server->away_reason != NULL) ? "+g" : "-g"); } typedef struct { - int type; /* 1 = msg, 2 = channel */ - bool responder; SILC_SERVER_REC *server; + int type; /* 1 = msg, 2 = channel */ + SilcBool responder; } *KeyInternal; /* Key agreement callback that is called after the key agreement protocol @@ -408,7 +481,7 @@ static void keyagr_completion(SilcClient client, SilcClientConnection conn, SilcClientEntry client_entry, SilcKeyAgreementStatus status, - SilcSKEKeyMaterial *key, + SilcSKEKeyMaterial key, void *context) { KeyInternal i = (KeyInternal)context; @@ -422,30 +495,31 @@ static void keyagr_completion(SilcClient client, /* Set the private key for this client */ silc_client_del_private_message_key(client, conn, client_entry); silc_client_add_private_message_key_ske(client, conn, client_entry, - NULL, key, i->responder); + NULL, NULL, key); printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, - SILCTXT_KEY_AGREEMENT_PRIVMSG, + SILCTXT_KEY_AGREEMENT_PRIVMSG, client_entry->nickname); silc_ske_free_key_material(key); } - + break; - + case SILC_KEY_AGREEMENT_ERROR: + case SILC_KEY_AGREEMENT_NO_MEMORY: printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_ERROR, client_entry->nickname); break; - + case SILC_KEY_AGREEMENT_FAILURE: printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_FAILURE, client_entry->nickname); break; - + case SILC_KEY_AGREEMENT_TIMEOUT: printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_TIMEOUT, client_entry->nickname); break; - + case SILC_KEY_AGREEMENT_ABORTED: printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_ABORTED, client_entry->nickname); @@ -456,15 +530,15 @@ static void keyagr_completion(SilcClient client, SILCTXT_KEY_AGREEMENT_ALREADY_STARTED, client_entry->nickname); break; - + case SILC_KEY_AGREEMENT_SELF_DENIED: printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_SELF_DENIED); break; - + default: break; - } + } if (i) silc_free(i); @@ -490,14 +564,14 @@ typedef struct { static void silc_client_command_key_get_clients(SilcClient client, SilcClientConnection conn, - SilcClientEntry *clients, - SilcUInt32 clients_count, + SilcStatus status, + SilcDList clients, void *context) { KeyGetClients internal = (KeyGetClients)context; if (!clients) { - printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", + printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", internal->nick); silc_free(internal->data); silc_free(internal->nick); @@ -517,8 +591,8 @@ static void command_key(const char *data, SILC_SERVER_REC *server, WI_ITEM_REC *item) { SilcClientConnection conn; - SilcClientEntry *entrys, client_entry = NULL; - SilcUInt32 entry_count; + SilcClientEntry client_entry = NULL; + SilcDList clients; SILC_CHANNEL_REC *chanrec = NULL; SilcChannelEntry channel_entry = NULL; char *nickname = NULL, *tmp; @@ -529,7 +603,11 @@ static void command_key(const char *data, SILC_SERVER_REC *server, unsigned char **argv; SilcUInt32 *argv_lens, *argv_types; char *bindhost = NULL; - + SilcChannelPrivateKey ch = NULL; + SilcDList ckeys; + SilcBool udp = FALSE; + int i; + CMD_SILC_SERVER(server); if (!server || !IS_SILC_SERVER(server) || !server->connected) @@ -559,27 +637,26 @@ static void command_key(const char *data, SILC_SERVER_REC *server, nickname = strdup("*"); } else { /* Parse the typed nickname. */ - if (!silc_parse_userfqdn(argv[2], &nickname, NULL)) { - printformat_module("fe-common/silc", server, NULL, - MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]); - return; - } - + silc_client_nickname_parse(silc_client, conn, argv[2], &nickname); + if (!nickname) + nickname = strdup(argv[2]); + /* Find client entry */ - entrys = silc_client_get_clients_local(silc_client, conn, nickname, - argv[2], &entry_count); - if (!entrys) { + clients = silc_client_get_clients_local(silc_client, conn, argv[2], + FALSE); + if (!clients) { KeyGetClients inter = silc_calloc(1, sizeof(*inter)); inter->server = server; inter->data = strdup(data); inter->nick = strdup(nickname); inter->item = item; - silc_client_get_clients(silc_client, conn, nickname, argv[2], + silc_client_get_clients(silc_client, conn, nickname, NULL, silc_client_command_key_get_clients, inter); goto out; } - client_entry = entrys[0]; - silc_free(entrys); + + client_entry = silc_dlist_get(clients); + silc_client_list_free(silc_client, conn, clients); } } @@ -588,20 +665,16 @@ static void command_key(const char *data, SILC_SERVER_REC *server, char *name; if (argv[2][0] == '*') { - if (!conn->current_channel) { - silc_free(nickname); + if (!conn->current_channel) cmd_return_error(CMDERR_NOT_JOINED); - } name = conn->current_channel->channel_name; } else { name = argv[2]; } chanrec = silc_channel_find(server, name); - if (chanrec == NULL) { - silc_free(nickname); + if (chanrec == NULL) cmd_return_error(CMDERR_CHAN_NOT_FOUND); - } channel_entry = chanrec->entry; } @@ -610,64 +683,48 @@ static void command_key(const char *data, SILC_SERVER_REC *server, command = 1; if (argc >= 5) { + char *cipher = NULL, *hmac = NULL; + + if (argc >= 6) + cipher = argv[5]; + if (argc >= 7) + hmac = argv[6]; + if (type == 1 && client_entry) { /* Set private message key */ - silc_client_del_private_message_key(silc_client, conn, client_entry); - - if (argc >= 6) - silc_client_add_private_message_key(silc_client, conn, client_entry, - argv[5], argv[4], - argv_lens[4], - (argv[4][0] == '*' ? - TRUE : FALSE), FALSE); - else - silc_client_add_private_message_key(silc_client, conn, client_entry, - NULL, argv[4], - argv_lens[4], - (argv[4][0] == '*' ? - TRUE : FALSE), FALSE); - - /* Send the key to the remote client so that it starts using it - too. */ - silc_client_send_private_message_key(silc_client, conn, - client_entry, TRUE); + silc_client_add_private_message_key(silc_client, conn, client_entry, + cipher, hmac, + argv[4], argv_lens[4]); } else if (type == 2) { /* Set private channel key */ - char *cipher = NULL, *hmac = NULL; - if (!(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) { printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, - SILCTXT_CH_PRIVATE_KEY_NOMODE, + SILCTXT_CH_PRIVATE_KEY_NOMODE, channel_entry->channel_name); goto out; } - if (argc >= 6) - cipher = argv[5]; - if (argc >= 7) - hmac = argv[6]; - - if (!silc_client_add_channel_private_key(silc_client, conn, + if (!silc_client_add_channel_private_key(silc_client, conn, channel_entry, NULL, cipher, hmac, argv[4], - argv_lens[4])) { + argv_lens[4], NULL)) { printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, - SILCTXT_CH_PRIVATE_KEY_ERROR, + SILCTXT_CH_PRIVATE_KEY_ERROR, channel_entry->channel_name); goto out; } printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, - SILCTXT_CH_PRIVATE_KEY_ADD, + SILCTXT_CH_PRIVATE_KEY_ADD, channel_entry->channel_name); } } goto out; } - + /* Unset command */ if (!strcasecmp(argv[3], "unset")) { command = 2; @@ -677,30 +734,33 @@ static void command_key(const char *data, SILC_SERVER_REC *server, silc_client_del_private_message_key(silc_client, conn, client_entry); } else if (type == 2) { /* Unset channel key(s) */ - SilcChannelPrivateKey *keys; - SilcUInt32 keys_count; int number; if (argc == 4) - silc_client_del_channel_private_keys(silc_client, conn, + silc_client_del_channel_private_keys(silc_client, conn, channel_entry); if (argc > 4) { number = atoi(argv[4]); - keys = silc_client_list_channel_private_keys(silc_client, conn, - channel_entry, - &keys_count); - if (!keys) + ckeys = silc_client_list_channel_private_keys(silc_client, conn, + channel_entry); + if (!ckeys) goto out; - if (!number || number > keys_count) { - silc_client_free_channel_private_keys(keys, keys_count); + silc_dlist_start(ckeys); + if (!number || number > silc_dlist_count(ckeys)) { + silc_dlist_uninit(ckeys); goto out; } + for (i = 0; i < number; i++) + ch = silc_dlist_get(ckeys); + if (!ch) + goto out; + silc_client_del_channel_private_key(silc_client, conn, channel_entry, - keys[number - 1]); - silc_client_free_channel_private_keys(keys, keys_count); + ch); + silc_dlist_uninit(ckeys); } goto out; @@ -717,7 +777,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server, int k, i, len; char buf[1024]; - keys = silc_client_list_private_message_keys(silc_client, conn, + keys = silc_client_list_private_message_keys(silc_client, conn, &keys_count); if (!keys) goto out; @@ -735,7 +795,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server, for (i = 0; i < 30 - len; i++) strcat(buf, " "); strcat(buf, " "); - + len = strlen(keys[k].cipher); strncat(buf, keys[k].cipher, len > 14 ? 14 : len); if (len < 14) @@ -766,7 +826,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server, for (i = 0; i < 30 - len; i++) strcat(buf, " "); strcat(buf, " "); - + len = strlen(keys[k].cipher); strncat(buf, keys[k].cipher, len > 14 ? 14 : len); if (len < 14) @@ -786,46 +846,45 @@ static void command_key(const char *data, SILC_SERVER_REC *server, silc_client_free_private_message_keys(keys, keys_count); } else if (type == 2) { - SilcChannelPrivateKey *keys; - SilcUInt32 keys_count; - int k, i, len; + int len; char buf[1024]; - keys = silc_client_list_channel_private_keys(silc_client, conn, - channel_entry, - &keys_count); + ckeys = silc_client_list_channel_private_keys(silc_client, conn, + channel_entry); printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, SILCTXT_CH_PRIVATE_KEY_LIST, channel_entry->channel_name); - if (!keys) + if (!ckeys) goto out; - - for (k = 0; k < keys_count; k++) { + + silc_dlist_start(ckeys); + while ((ch = silc_dlist_get(ckeys))) { memset(buf, 0, sizeof(buf)); strncat(buf, " ", 2); - len = strlen(keys[k]->cipher->cipher->name); - strncat(buf, keys[k]->cipher->cipher->name, len > 16 ? 16 : len); + len = strlen(silc_cipher_get_name(ch->send_key)); + strncat(buf, silc_cipher_get_name(ch->send_key), + len > 16 ? 16 : len); if (len < 16) for (i = 0; i < 16 - len; i++) strcat(buf, " "); strcat(buf, " "); - - len = strlen(silc_hmac_get_name(keys[k]->hmac)); - strncat(buf, silc_hmac_get_name(keys[k]->hmac), len > 16 ? 16 : len); + + len = strlen(silc_hmac_get_name(ch->hmac)); + strncat(buf, silc_hmac_get_name(ch->hmac), len > 16 ? 16 : len); if (len < 16) for (i = 0; i < 16 - len; i++) strcat(buf, " "); strcat(buf, " "); - + strcat(buf, ""); silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf); } - - silc_client_free_channel_private_keys(keys, keys_count); + + silc_dlist_uninit(ckeys); } goto out; @@ -837,25 +896,30 @@ static void command_key(const char *data, SILC_SERVER_REC *server, if (argc >= 5) hostname = argv[4]; - if (argc >= 6) - port = atoi(argv[5]); + if (argc >= 6) { + if (!strcasecmp(argv[5], "UDP")) + udp = TRUE; + else + port = atoi(argv[5]); + } + if (argc >= 7) + udp = TRUE; internal = silc_calloc(1, sizeof(*internal)); internal->type = type; internal->server = server; - + if (!hostname) { if (settings_get_bool("use_auto_addr")) { - hostname = (char *)settings_get_str("auto_public_ip"); - /* If the hostname isn't set, treat this case as if auto_public_ip + /* If the hostname isn't set, treat this case as if auto_public_ip wasn't set. */ if ((hostname) && (*hostname == '\0')) { hostname = NULL; } else { bindhost = (char *)settings_get_str("auto_bind_ip"); - + /* if the bind_ip isn't set, but the public_ip IS, then assume then public_ip is the same value as the bind_ip. */ if ((bindhost) && (*bindhost == '\0')) @@ -873,8 +937,14 @@ static void command_key(const char *data, SILC_SERVER_REC *server, if (argc >= 5) hostname = argv[4]; - if (argc >= 6) - port = atoi(argv[5]); + if (argc >= 6) { + if (!strcasecmp(argv[5], "UDP")) + udp = TRUE; + else + port = atoi(argv[5]); + } + if (argc >= 7) + udp = TRUE; internal = silc_calloc(1, sizeof(*internal)); internal->type = type; @@ -886,39 +956,41 @@ static void command_key(const char *data, SILC_SERVER_REC *server, command = 6; if (type == 2) { /* Unset channel key(s) */ - SilcChannelPrivateKey *keys; - SilcUInt32 keys_count; int number; - keys = silc_client_list_channel_private_keys(silc_client, conn, - channel_entry, - &keys_count); - if (!keys) + ckeys = silc_client_list_channel_private_keys(silc_client, conn, + channel_entry); + if (!ckeys) goto out; + silc_dlist_start(ckeys); if (argc == 4) { chanrec->cur_key++; - if (chanrec->cur_key >= keys_count) + if (chanrec->cur_key >= silc_dlist_count(ckeys)) chanrec->cur_key = 0; } if (argc > 4) { number = atoi(argv[4]); - if (!number || number > keys_count) + if (!number || number > silc_dlist_count(ckeys)) chanrec->cur_key = 0; else chanrec->cur_key = number - 1; } + for (i = 0; i < chanrec->cur_key; i++) + ch = silc_dlist_get(ckeys); + if (!ch) + goto out; + /* Set the current channel private key */ - silc_client_current_channel_private_key(silc_client, conn, - channel_entry, - keys[chanrec->cur_key]); + silc_client_current_channel_private_key(silc_client, conn, + channel_entry, ch); printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, - SILCTXT_CH_PRIVATE_KEY_CHANGE, chanrec->cur_key + 1, + SILCTXT_CH_PRIVATE_KEY_CHANGE, i + 1, channel_entry->channel_name); - silc_client_free_channel_private_keys(keys, keys_count); + silc_dlist_uninit(ckeys); goto out; } } @@ -931,13 +1003,22 @@ static void command_key(const char *data, SILC_SERVER_REC *server, } if (command == 4 && client_entry) { + SilcClientConnectionParams params; + printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT, argv[2]); internal->responder = TRUE; + + memset(¶ms, 0, sizeof(params)); + params.local_ip = hostname; + params.bind_ip = bindhost; + params.local_port = port; + params.udp = udp; + params.timeout_secs = settings_get_int("key_exchange_timeout_secs"); + silc_client_send_key_agreement( - silc_client, conn, client_entry, hostname, - bindhost, port, - settings_get_int("key_exchange_timeout_secs"), + silc_client, conn, client_entry, ¶ms, + irssi_pubkey, irssi_privkey, keyagr_completion, internal); if (!hostname) silc_free(internal); @@ -945,25 +1026,234 @@ static void command_key(const char *data, SILC_SERVER_REC *server, } if (command == 5 && client_entry && hostname) { + SilcClientConnectionParams params; + printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, SILCTXT_KEY_AGREEMENT_NEGOTIATE, argv[2]); internal->responder = FALSE; - silc_client_perform_key_agreement(silc_client, conn, client_entry, - hostname, port, keyagr_completion, + + memset(¶ms, 0, sizeof(params)); + if (udp) { + if (settings_get_bool("use_auto_addr")) { + params.local_ip = (char *)settings_get_str("auto_public_ip"); + if ((params.local_ip) && (*params.local_ip == '\0')) { + params.local_ip = silc_net_localip(); + } else { + params.bind_ip = (char *)settings_get_str("auto_bind_ip"); + if ((params.bind_ip) && (*params.bind_ip == '\0')) + params.bind_ip = NULL; + params.local_port = settings_get_int("auto_bind_port"); + } + } + if (!params.local_ip) + params.local_ip = silc_net_localip(); + } + params.udp = udp; + params.timeout_secs = settings_get_int("key_exchange_timeout_secs"); + + silc_client_perform_key_agreement(silc_client, conn, client_entry, ¶ms, + irssi_pubkey, irssi_privkey, + hostname, port, keyagr_completion, internal); goto out; } out: silc_free(nickname); + return; } -/* Lists locally saved client and server public keys. */ +void silc_list_key(const char *pub_filename, int verbose) +{ + SilcPublicKey public_key; + SilcPublicKeyIdentifier ident; + SilcSILCPublicKey silc_pubkey; + char *fingerprint, *babbleprint; + unsigned char *pk; + SilcUInt32 pk_len; + SilcUInt32 key_len = 0; + int is_server_key = (strstr(pub_filename, "serverkeys") != NULL); + + if (!silc_pkcs_load_public_key((char *)pub_filename, SILC_PKCS_ANY, + &public_key)) { + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_LOADPUB, + pub_filename); + return; + } + + /* Print only SILC public keys */ + if (silc_pkcs_get_type(public_key) != SILC_PKCS_SILC) { + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_LOADPUB, + pub_filename); + return; + } + + silc_pubkey = silc_pkcs_public_key_get_pkcs(SILC_PKCS_SILC, public_key); + ident = &silc_pubkey->identifier; + + pk = silc_pkcs_public_key_encode(NULL, public_key, &pk_len); + if (!pk) + return; + fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); + babbleprint = silc_hash_babbleprint(NULL, pk, pk_len); + key_len = silc_pkcs_public_key_get_len(public_key); + + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_FILE, + pub_filename); + + if (verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_ALG, + silc_pkcs_get_name(public_key)); + if (key_len && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_BITS, + (unsigned int)key_len); + if (ident->version && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_VER, + ident->version); + if (ident->realname && (!is_server_key || verbose)) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_RN, + ident->realname); + if (ident->username && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_UN, + ident->username); + if (ident->host && (is_server_key || verbose)) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_HN, + ident->host); + if (ident->email && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_EMAIL, + ident->email); + if (ident->org && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_ORG, + ident->org); + if (ident->country && verbose) + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_C, + ident->country); + + if (verbose) { + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_FINGER, + fingerprint); + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_PUB_BABL, + babbleprint); + } + + silc_free(fingerprint); + silc_free(babbleprint); + silc_free(pk); + silc_pkcs_public_key_free(public_key); +} + +void silc_list_keys_in_dir(const char *dirname, const char *where) +{ + DIR *dir; + struct dirent *entry; + + dir = opendir(dirname); + + if (dir == NULL) + cmd_return_error(CMDERR_ERRNO); + + printformat_module("fe-common/silc", NULL, NULL, + MSGLEVEL_CRAP, SILCTXT_LISTKEY_LIST, + where); + + rewinddir(dir); + + while ((entry = readdir(dir)) != NULL) { + /* try to open everything that isn't a directory */ + struct stat buf; + char filename[256]; + + snprintf(filename, sizeof(filename) - 1, "%s/%s", dirname, entry->d_name); + if (!stat(filename, &buf) && S_ISREG(buf.st_mode)) + silc_list_key(filename, FALSE); + } + + closedir(dir); +} + +void silc_list_file(const char *filename) +{ + + char path[256]; + struct stat buf; + + snprintf(path, sizeof(path) - 1, "%s", filename); + if (!stat(path, &buf) && S_ISREG(buf.st_mode)) + goto list_key; + + snprintf(path, sizeof(path) - 1, "%s/%s", get_irssi_dir(), filename); + if (!stat(path, &buf) && S_ISREG(buf.st_mode)) + goto list_key; + + snprintf(path,sizeof(path) - 1, "%s/clientkeys/%s", get_irssi_dir(), + filename); + if (!stat(path, &buf) && S_ISREG(buf.st_mode)) + goto list_key; + + snprintf(path,sizeof(path) - 1, "%s/serverkeys/%s", get_irssi_dir(), + filename); + if (!stat(path, &buf) && S_ISREG(buf.st_mode)) + goto list_key; + + return; + +list_key: + + silc_list_key(path, TRUE); +} +/* Lists locally saved client and server public keys. */ static void command_listkeys(const char *data, SILC_SERVER_REC *server, WI_ITEM_REC *item) { + GHashTable *optlist; + char *filename; + void *free_arg; + char dirname[256]; + + if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS | + PARAM_FLAG_GETREST, "listkeys", &optlist, + &filename)) + return; + + if (*filename != '\0') { + + silc_list_file(filename); + + } else { + int clients, servers; + + clients = (g_hash_table_lookup(optlist, "clients") != NULL); + servers = (g_hash_table_lookup(optlist, "servers") != NULL); + if (!(clients || servers)) + clients = servers = 1; + + if (servers) { + snprintf(dirname, sizeof(dirname) - 1, "%s/serverkeys", get_irssi_dir()); + silc_list_keys_in_dir(dirname, "server"); + } + + if (clients) { + snprintf(dirname, sizeof(dirname) - 1, "%s/clientkeys", get_irssi_dir()); + silc_list_keys_in_dir(dirname, "client"); + } + } + cmd_params_free(free_arg); } void silc_channels_init(void) @@ -971,6 +1261,7 @@ void silc_channels_init(void) signal_add("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed); signal_add("server connected", (SIGNAL_FUNC) sig_connected); signal_add("server quit", (SIGNAL_FUNC) sig_server_quit); + signal_add("mime", (SIGNAL_FUNC) sig_mime); command_bind_silc("part", MODULE_NAME, (SIGNAL_FUNC) command_part); command_bind_silc("me", MODULE_NAME, (SIGNAL_FUNC) command_me); @@ -978,7 +1269,11 @@ void silc_channels_init(void) command_bind_silc("notice", MODULE_NAME, (SIGNAL_FUNC) command_notice); command_bind_silc("away", MODULE_NAME, (SIGNAL_FUNC) command_away); command_bind_silc("key", MODULE_NAME, (SIGNAL_FUNC) command_key); - command_bind_silc("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys); + command_bind("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys); + + command_set_options("listkeys", "clients servers"); + command_set_options("action", "sign channel"); + command_set_options("notice", "sign channel"); silc_nicklist_init(); } @@ -988,6 +1283,7 @@ void silc_channels_deinit(void) signal_remove("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed); signal_remove("server connected", (SIGNAL_FUNC) sig_connected); signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit); + signal_remove("mime", (SIGNAL_FUNC) sig_mime); command_unbind("part", (SIGNAL_FUNC) command_part); command_unbind("me", (SIGNAL_FUNC) command_me);