+ if (type == 1) {
+ if (argv[2][0] == '*') {
+ nickname = "*";
+ } 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;
+ }
+
+ /* Find client entry */
+ client_entry = silc_idlist_get_client(silc_client, conn, nickname,
+ argv[2], TRUE);
+ if (!client_entry) {
+ KeyGetClients inter = silc_calloc(1, sizeof(*inter));
+ inter->server = server;
+ inter->data = strdup(data);
+ inter->item = item;
+
+ /* Client entry not found, it was requested thus mark this to be
+ pending command. */
+ silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY,
+ conn->cmd_ident,
+ NULL, silc_client_command_key_get_clients,
+ inter);
+ goto out;
+ }
+ }
+ }
+
+ if (type == 2) {
+ /* Get channel entry */
+ char *name;
+
+ if (argv[2][0] == '*') {
+ if (!conn->current_channel) {
+ silc_free(nickname);
+ cmd_return_error(CMDERR_NOT_JOINED);
+ }
+ name = conn->current_channel->channel_name;
+ } else {
+ name = argv[2];
+ }
+
+ channel_entry = silc_client_get_channel(silc_client, conn, name);
+ if (!channel_entry) {
+ silc_free(nickname);
+ cmd_return_error(CMDERR_NOT_JOINED);
+ }
+ }
+
+ /* Set command */
+ if (!strcasecmp(argv[3], "set")) {
+ command = 1;
+
+ if (argc >= 5) {
+ 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);
+ } 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,
+ 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,
+ channel_entry,
+ cipher, hmac,
+ argv[4],
+ argv_lens[4])) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ 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,
+ channel_entry->channel_name);
+ }
+ }
+
+ goto out;
+ }
+
+ /* Unset command */
+ if (!strcasecmp(argv[3], "unset")) {
+ command = 2;
+
+ if (type == 1 && client_entry) {
+ /* Unset private message key */
+ silc_client_del_private_message_key(silc_client, conn, client_entry);
+ } else if (type == 2) {
+ /* Unset channel key(s) */
+ SilcChannelPrivateKey *keys;
+ uint32 keys_count;
+ int number;
+
+ if (argc == 4)
+ 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)
+ goto out;
+
+ if (!number || number > keys_count) {
+ silc_client_free_channel_private_keys(keys, keys_count);
+ 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);
+ }
+
+ goto out;
+ }
+ }
+
+ /* List command */
+ if (!strcasecmp(argv[3], "list")) {
+ command = 3;
+
+ if (type == 1) {
+ SilcPrivateMessageKeys keys;
+ uint32 keys_count;
+ int k, i, len;
+ char buf[1024];
+
+ keys = silc_client_list_private_message_keys(silc_client, conn,
+ &keys_count);
+ if (!keys)
+ goto out;
+
+ /* list the private message key(s) */
+ if (nickname[0] == '*') {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_PRIVATE_KEY_LIST);
+ for (k = 0; k < keys_count; k++) {
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+ len = strlen(keys[k].client_entry->nickname);
+ strncat(buf, keys[k].client_entry->nickname, len > 30 ? 30 : len);
+ if (len < 30)
+ 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)
+ for (i = 0; i < 14 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ if (keys[k].key)
+ strcat(buf, "<hidden>");
+ else
+ strcat(buf, "*generated*");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+ } else {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_PRIVATE_KEY_LIST_NICK,
+ client_entry->nickname);
+ for (k = 0; k < keys_count; k++) {
+ if (keys[k].client_entry != client_entry)
+ continue;
+
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+ len = strlen(keys[k].client_entry->nickname);
+ strncat(buf, keys[k].client_entry->nickname, len > 30 ? 30 : len);
+ if (len < 30)
+ 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)
+ for (i = 0; i < 14 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ if (keys[k].key)
+ strcat(buf, "<hidden>");
+ else
+ strcat(buf, "*generated*");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+ }
+
+ silc_client_free_private_message_keys(keys, keys_count);
+ } else if (type == 2) {
+ SilcChannelPrivateKey *keys;
+ uint32 keys_count;
+ int k, i, len;
+ char buf[1024];
+
+ keys = silc_client_list_channel_private_keys(silc_client, conn,
+ channel_entry,
+ &keys_count);
+ if (!keys)
+ goto out;
+
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_LIST,
+ channel_entry->channel_name);
+ for (k = 0; k < keys_count; k++) {
+ 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);
+ if (len < 16)
+ for (i = 0; i < 16 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ len = strlen(keys[k]->hmac->hmac->name);
+ strncat(buf, keys[k]->hmac->hmac->name, len > 16 ? 16 : len);
+ if (len < 16)
+ for (i = 0; i < 16 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ strcat(buf, "<hidden>");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+
+ silc_client_free_channel_private_keys(keys, keys_count);
+ }
+
+ goto out;
+ }
+
+ /* Send command is used to send key agreement */
+ if (!strcasecmp(argv[3], "agreement")) {
+ command = 4;
+
+ if (argc >= 5)
+ hostname = argv[4];
+ if (argc >= 6)
+ port = atoi(argv[5]);
+
+ internal = silc_calloc(1, sizeof(*internal));
+ internal->type = type;
+ internal->server = server;
+ }
+
+ /* Start command is used to start key agreement (after receiving the
+ key_agreement client operation). */
+ if (!strcasecmp(argv[3], "negotiate")) {
+ command = 5;
+
+ if (argc >= 5)
+ hostname = argv[4];
+ if (argc >= 6)
+ port = atoi(argv[5]);
+
+ internal = silc_calloc(1, sizeof(*internal));
+ internal->type = type;
+ internal->server = server;
+ }
+
+ if (command == 0) {
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO,
+ "Usage: /KEY msg|channel <nickname|channel> "
+ "set|unset|agreement|negotiate [<arguments>]");
+ goto out;
+ }
+
+ if (command == 4 && client_entry) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_NOTICES,
+ SILCTXT_KEY_AGREEMENT, argv[2]);
+ internal->responder = TRUE;
+ silc_client_send_key_agreement(silc_client, conn, client_entry, hostname,
+ port, 120, keyagr_completion, internal);
+ if (!hostname)
+ silc_free(internal);
+ goto out;
+ }
+
+ if (command == 5 && client_entry && hostname) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_NOTICES,
+ SILCTXT_KEY_AGREEMENT_NEGOTIATE, argv[2]);
+ internal->responder = FALSE;
+ silc_client_perform_key_agreement(silc_client, conn, client_entry,
+ hostname, port, keyagr_completion,
+ internal);
+ goto out;
+ }
+
+ out:
+ silc_free(nickname);
+}
+
+/* Lists locally saved client and server public keys. */
+
+static void command_listkeys(const char *data, SILC_SERVER_REC *server,
+ WI_ITEM_REC *item)
+{
+
+}
+
+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("silc event join", (SIGNAL_FUNC) event_join);
+ signal_add("silc event leave", (SIGNAL_FUNC) event_leave);
+ signal_add("silc event signoff", (SIGNAL_FUNC) event_signoff);
+ signal_add("silc event topic", (SIGNAL_FUNC) event_topic);
+ signal_add("silc event invite", (SIGNAL_FUNC) event_invite);
+ signal_add("silc event nick", (SIGNAL_FUNC) event_nick);
+ signal_add("silc event cmode", (SIGNAL_FUNC) event_cmode);
+ signal_add("silc event cumode", (SIGNAL_FUNC) event_cumode);
+ signal_add("silc event motd", (SIGNAL_FUNC) event_motd);
+ signal_add("silc event channel_change", (SIGNAL_FUNC) event_channel_change);
+ signal_add("silc event server_signoff", (SIGNAL_FUNC) event_server_signoff);
+ signal_add("silc event kick", (SIGNAL_FUNC) event_kick);
+ signal_add("silc event kill", (SIGNAL_FUNC) event_kill);
+
+ command_bind("part", MODULE_NAME, (SIGNAL_FUNC) command_part);
+ command_bind("me", MODULE_NAME, (SIGNAL_FUNC) command_me);
+ command_bind("action", MODULE_NAME, (SIGNAL_FUNC) command_action);
+ command_bind("notice", MODULE_NAME, (SIGNAL_FUNC) command_notice);
+ command_bind("away", MODULE_NAME, (SIGNAL_FUNC) command_away);
+ command_bind("key", MODULE_NAME, (SIGNAL_FUNC) command_key);
+ command_bind("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys);
+
+ silc_nicklist_init();
+}
+
+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("silc event join", (SIGNAL_FUNC) event_join);
+ signal_remove("silc event leave", (SIGNAL_FUNC) event_leave);
+ signal_remove("silc event signoff", (SIGNAL_FUNC) event_signoff);
+ signal_remove("silc event topic", (SIGNAL_FUNC) event_topic);
+ signal_remove("silc event invite", (SIGNAL_FUNC) event_invite);
+ signal_remove("silc event nick", (SIGNAL_FUNC) event_nick);
+ signal_remove("silc event cmode", (SIGNAL_FUNC) event_cmode);
+ signal_remove("silc event cumode", (SIGNAL_FUNC) event_cumode);
+ signal_remove("silc event motd", (SIGNAL_FUNC) event_motd);
+ signal_remove("silc event channel_change",
+ (SIGNAL_FUNC) event_channel_change);
+ signal_remove("silc event server_signoff",
+ (SIGNAL_FUNC) event_server_signoff);
+ signal_remove("silc event kick", (SIGNAL_FUNC) event_kick);
+ signal_remove("silc event kill", (SIGNAL_FUNC) event_kill);
+
+ command_unbind("part", (SIGNAL_FUNC) command_part);
+ command_unbind("me", (SIGNAL_FUNC) command_me);
+ command_unbind("action", (SIGNAL_FUNC) command_action);
+ command_unbind("notice", (SIGNAL_FUNC) command_notice);
+ command_unbind("away", (SIGNAL_FUNC) command_away);
+ command_unbind("key", (SIGNAL_FUNC) command_key);
+ command_unbind("listkeys", (SIGNAL_FUNC) command_listkeys);
+
+ silc_nicklist_deinit();