X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcclient%2Fcommand.c;h=b449b1150ed9da09d1d1eccd113d1f948bf63754;hb=017dec75a98209fbef49eb496c2269b0c49e736d;hp=1784d4e8779d376f311e57b50c5dd8bc94150e89;hpb=318d79b391bf6288e3e28c840217a7097f3d0392;p=silc.git diff --git a/lib/silcclient/command.c b/lib/silcclient/command.c index 1784d4e8..b449b115 100644 --- a/lib/silcclient/command.c +++ b/lib/silcclient/command.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2000 Pekka Riikonen + Copyright (C) 1997 - 2001 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 @@ -20,6 +20,7 @@ /* $Id$ */ #include "clientlibincludes.h" +#include "client_internal.h" /* Client command list. */ SilcClientCommand silc_command_list[] = @@ -30,38 +31,39 @@ SilcClientCommand silc_command_list[] = SILC_CF_LAG | SILC_CF_REG, 3), SILC_CLIENT_CMD(nick, NICK, "NICK", SILC_CF_LAG | SILC_CF_REG, 2), SILC_CLIENT_CMD(list, LIST, "LIST", SILC_CF_LAG | SILC_CF_REG, 2), - SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", SILC_CF_LAG | SILC_CF_REG, 2), + SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", SILC_CF_LAG | SILC_CF_REG, 3), SILC_CLIENT_CMD(invite, INVITE, "INVITE", SILC_CF_LAG | SILC_CF_REG, 3), - SILC_CLIENT_CMD(quit, QUIT, "QUIT", SILC_CF_LAG | SILC_CF_REG, 1), + SILC_CLIENT_CMD(quit, QUIT, "QUIT", SILC_CF_LAG | SILC_CF_REG, 2), SILC_CLIENT_CMD(kill, KILL, "KILL", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), + SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 3), SILC_CLIENT_CMD(info, INFO, "INFO", SILC_CF_LAG | SILC_CF_REG, 2), SILC_CLIENT_CMD(connect, CONNECT, "CONNECT", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), + SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 3), SILC_CLIENT_CMD(ping, PING, "PING", SILC_CF_LAG | SILC_CF_REG, 2), SILC_CLIENT_CMD(oper, OPER, "OPER", SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), - SILC_CLIENT_CMD(join, JOIN, "JOIN", SILC_CF_LAG | SILC_CF_REG, 2), + SILC_CLIENT_CMD(join, JOIN, "JOIN", SILC_CF_LAG | SILC_CF_REG, 5), SILC_CLIENT_CMD(motd, MOTD, "MOTD", SILC_CF_LAG | SILC_CF_REG, 2), SILC_CLIENT_CMD(umode, UMODE, "UMODE", SILC_CF_LAG | SILC_CF_REG, 2), - SILC_CLIENT_CMD(cmode, CMODE, "CMODE", SILC_CF_LAG | SILC_CF_REG, 2), - SILC_CLIENT_CMD(kick, KICK, "KICK", SILC_CF_LAG | SILC_CF_REG, 2), - SILC_CLIENT_CMD(restart, RESTART, "RESTART", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), + SILC_CLIENT_CMD(cmode, CMODE, "CMODE", SILC_CF_LAG | SILC_CF_REG, 4), + SILC_CLIENT_CMD(cumode, CUMODE, "CUMODE", SILC_CF_LAG | SILC_CF_REG, 5), + SILC_CLIENT_CMD(kick, KICK, "KICK", SILC_CF_LAG | SILC_CF_REG, 4), + SILC_CLIENT_CMD(ban, BAN, "BAN", SILC_CF_LAG | SILC_CF_REG, 3), SILC_CLIENT_CMD(close, CLOSE, "CLOSE", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), - SILC_CLIENT_CMD(die, DIE, "DIE", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2), - SILC_CLIENT_CMD(silcoper, SILCOPER, "SILOPER", - SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER, 2), + SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 3), + SILC_CLIENT_CMD(shutdown, SHUTDOWN, "SHUTDOWN", + SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 1), + SILC_CLIENT_CMD(silcoper, SILCOPER, "SILCOPER", + SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER, 3), SILC_CLIENT_CMD(leave, LEAVE, "LEAVE", SILC_CF_LAG | SILC_CF_REG, 2), - SILC_CLIENT_CMD(names, NAMES, "NAMES", SILC_CF_LAG | SILC_CF_REG, 2), + SILC_CLIENT_CMD(users, USERS, "USERS", SILC_CF_LAG | SILC_CF_REG, 2), + SILC_CLIENT_CMD(getkey, GETKEY, "GETKEY", SILC_CF_LAG | SILC_CF_REG, 2), { NULL, 0, NULL, 0, 0 }, }; #define SILC_NOT_CONNECTED(x, c) \ - x->ops->say((x), (c), \ + x->ops->say((x), (c), SILC_CLIENT_MESSAGE_ERROR, \ "You are not connected to a server, use /SERVER to connect"); /* Command operation that is called at the end of all commands. @@ -73,8 +75,24 @@ SilcClientCommand silc_command_list[] = #define COMMAND_ERROR cmd->client->ops->command(cmd->client, cmd->conn, \ cmd, FALSE, cmd->command->cmd) -/* List of pending commands. */ -SilcClientCommandPending *silc_command_pending = NULL; +/* Generic function to send any command. The arguments must be sent already + encoded into correct form and in correct order. */ + +void silc_client_send_command(SilcClient client, SilcClientConnection conn, + SilcCommand command, uint16 ident, + uint32 argc, ...) +{ + SilcBuffer packet; + va_list ap; + + va_start(ap, argc); + + packet = silc_command_payload_encode_vap(command, ident, argc, ap); + silc_client_packet_send(client, conn->sock, SILC_PACKET_COMMAND, + NULL, 0, NULL, NULL, packet->data, + packet->len, TRUE); + silc_buffer_free(packet); +} /* Finds and returns a pointer to the command list. Return NULL if the command is not found. */ @@ -91,78 +109,116 @@ SilcClientCommand *silc_client_command_find(const char *name) return NULL; } -/* Add new pending command to the list of pending commands. Currently - pending commands are executed from command replies, thus we can - execute any command after receiving some specific command reply. - - The argument `reply_cmd' is the command reply from where the callback - function is to be called, thus, it IS NOT the command to be executed. - - XXX: If needed in the future this support may be extended for - commands as well, when any command could be executed after executing - some specific command. */ - -void silc_client_command_pending(SilcCommand reply_cmd, - SilcClientCommandCallback callback, +/* Add new pending command to be executed when reply to a command has been + received. The `reply_cmd' is the command that will call the `callback' + with `context' when reply has been received. If `ident is non-zero + the `callback' will be executed when received reply with command + identifier `ident'. */ + +void silc_client_command_pending(SilcClientConnection conn, + SilcCommand reply_cmd, + uint16 ident, + SilcClientPendingDestructor destructor, + SilcCommandCb callback, void *context) { - SilcClientCommandPending *reply, *r; + SilcClientCommandPending *reply; reply = silc_calloc(1, sizeof(*reply)); reply->reply_cmd = reply_cmd; + reply->ident = ident; reply->context = context; reply->callback = callback; + reply->destructor = destructor; + silc_dlist_add(conn->pending_commands, reply); +} - if (silc_command_pending == NULL) { - silc_command_pending = reply; - return; - } +/* Deletes pending command by reply command type. */ + +void silc_client_command_pending_del(SilcClientConnection conn, + SilcCommand reply_cmd, + uint16 ident) +{ + SilcClientCommandPending *r; - for (r = silc_command_pending; r; r = r->next) { - if (r->next == NULL) { - r->next = reply; + silc_dlist_start(conn->pending_commands); + while ((r = silc_dlist_get(conn->pending_commands)) != SILC_LIST_END) { + if (r->reply_cmd == reply_cmd && r->ident == ident) { + silc_dlist_del(conn->pending_commands, r); break; } } } -/* Deletes pending command by reply command type. */ +/* Checks for pending commands and marks callbacks to be called from + the command reply function. Returns TRUE if there were pending command. */ -void silc_client_command_pending_del(SilcCommand reply_cmd) +int silc_client_command_pending_check(SilcClientConnection conn, + SilcClientCommandReplyContext ctx, + SilcCommand command, + uint16 ident) { - SilcClientCommandPending *r, *tmp; - - if (silc_command_pending) { - if (silc_command_pending->reply_cmd == reply_cmd) { - silc_free(silc_command_pending); - silc_command_pending = NULL; - return; - } - - for (r = silc_command_pending; r; r = r->next) { - if (r->next && r->next->reply_cmd == reply_cmd) { - tmp = r->next; - r->next = r->next->next; - silc_free(tmp); - break; - } + SilcClientCommandPending *r; + + silc_dlist_start(conn->pending_commands); + while ((r = silc_dlist_get(conn->pending_commands)) != SILC_LIST_END) { + if (r->reply_cmd == command && r->ident == ident) { + ctx->context = r->context; + ctx->callback = r->callback; + ctx->destructor = r->destructor; + ctx->ident = ident; + return TRUE; } } + + return FALSE; } -/* Free command context and its internals */ +/* Allocate Command Context */ -void silc_client_command_free(SilcClientCommandContext cmd) +SilcClientCommandContext silc_client_command_alloc() { - int i; + SilcClientCommandContext ctx = silc_calloc(1, sizeof(*ctx)); + ctx->users++; + return ctx; +} + +/* Free command context and its internals */ - if (cmd) { - for (i = 0; i < cmd->argc; i++) - silc_free(cmd->argv[i]); - silc_free(cmd); +void silc_client_command_free(SilcClientCommandContext ctx) +{ + ctx->users--; + SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1, + ctx->users)); + if (ctx->users < 1) { + int i; + + for (i = 0; i < ctx->argc; i++) + silc_free(ctx->argv[i]); + silc_free(ctx->argv_lens); + silc_free(ctx->argv_types); + silc_free(ctx); } } +/* Duplicate Command Context by adding reference counter. The context won't + be free'd untill it hits zero. */ + +SilcClientCommandContext silc_client_command_dup(SilcClientCommandContext ctx) +{ + ctx->users++; + SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1, + ctx->users)); + return ctx; +} + +/* Pending command destructor. */ + +static void silc_client_command_destructor(void *context) +{ + silc_client_command_free((SilcClientCommandContext)context); +} + /* Command WHOIS. This command is used to query information about specific user. */ @@ -178,16 +234,20 @@ SILC_CLIENT_CMD_FUNC(whois) goto out; } - if (cmd->argc < 2 || cmd->argc > 3) { - cmd->client->ops->say(cmd->client, conn, - "Usage: /WHOIS [@] []"); - COMMAND_ERROR; + /* Given without arguments fetches client's own information */ + if (cmd->argc < 2) { + buffer = silc_id_payload_encode(cmd->conn->local_id, SILC_ID_CLIENT); + silc_client_send_command(cmd->client, cmd->conn, SILC_COMMAND_WHOIS, + ++conn->cmd_ident, + 1, 3, buffer->data, buffer->len); + silc_buffer_free(buffer); goto out; } - buffer = silc_command_encode_payload(SILC_COMMAND_WHOIS, + buffer = silc_command_payload_encode(SILC_COMMAND_WHOIS, cmd->argc - 1, ++cmd->argv, - ++cmd->argv_lens, ++cmd->argv_types); + ++cmd->argv_lens, ++cmd->argv_types, + 0); silc_client_packet_send(cmd->client, cmd->conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); @@ -203,8 +263,45 @@ SILC_CLIENT_CMD_FUNC(whois) silc_client_command_free(cmd); } +/* Command WHOWAS. This command is used to query history information about + specific user that used to exist in the network. */ + SILC_CLIENT_CMD_FUNC(whowas) { + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 2 || cmd->argc > 3) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /WHOWAS [@] []"); + COMMAND_ERROR; + goto out; + } + + buffer = silc_command_payload_encode(SILC_COMMAND_WHOWAS, + cmd->argc - 1, ++cmd->argv, + ++cmd->argv_lens, ++cmd->argv_types, + 0); + silc_client_packet_send(cmd->client, cmd->conn->sock, + SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, + buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + cmd->argv--; + cmd->argv_lens--; + cmd->argv_types--; + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); } /* Command IDENTIFY. This command is used to query information about @@ -223,22 +320,27 @@ SILC_CLIENT_CMD_FUNC(identify) } if (cmd->argc < 2 || cmd->argc > 3) { - cmd->client->ops->say(cmd->client, conn, - "Usage: /IDENTIFY [@] []"); COMMAND_ERROR; goto out; } - buffer = silc_command_encode_payload(SILC_COMMAND_IDENTIFY, - cmd->argc - 1, ++cmd->argv, - ++cmd->argv_lens, ++cmd->argv_types); + if (cmd->argc == 2) + buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY, + ++conn->cmd_ident, 1, + 1, cmd->argv[1], + cmd->argv_lens[1]); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY, + ++conn->cmd_ident, 2, + 1, cmd->argv[1], + cmd->argv_lens[1], + 4, cmd->argv[2], + cmd->argv_lens[2]); + silc_client_packet_send(cmd->client, cmd->conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - cmd->argv--; - cmd->argv_lens--; - cmd->argv_types--; /* Notify application */ COMMAND; @@ -262,25 +364,36 @@ SILC_CLIENT_CMD_FUNC(nick) goto out; } + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /NICK "); + COMMAND_ERROR; + goto out; + } + + if (!strcmp(conn->nickname, cmd->argv[1])) + goto out; + /* Show current nickname */ if (cmd->argc < 2) { if (cmd->conn) { - cmd->client->ops->say(cmd->client, conn, + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, "Your nickname is %s on server %s", conn->nickname, conn->remote_host); } else { - cmd->client->ops->say(cmd->client, conn, + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, "Your nickname is %s", conn->nickname); } - /* XXX Notify application */ + COMMAND; goto out; } /* Set new nickname */ - buffer = silc_command_encode_payload(SILC_COMMAND_NICK, + buffer = silc_command_payload_encode(SILC_COMMAND_NICK, cmd->argc - 1, ++cmd->argv, - ++cmd->argv_lens, ++cmd->argv_types); + ++cmd->argv_lens, ++cmd->argv_types, + ++cmd->conn->cmd_ident); silc_client_packet_send(cmd->client, cmd->conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); @@ -299,27 +412,65 @@ SILC_CLIENT_CMD_FUNC(nick) silc_client_command_free(cmd); } +/* Command LIST. Lists channels on the current server. */ + SILC_CLIENT_CMD_FUNC(list) { -} + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcIDCacheEntry id_cache = NULL; + SilcChannelEntry channel; + SilcBuffer buffer, idp = NULL; + char *name; -SILC_CLIENT_CMD_FUNC(topic) -{ + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc == 2) { + name = cmd->argv[1]; + + /* Get the Channel ID of the channel */ + if (silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) { + channel = (SilcChannelEntry)id_cache->context; + idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL); + } + } + + if (!idp) + buffer = silc_command_payload_encode_va(SILC_COMMAND_LIST, + ++conn->cmd_ident, 0); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_LIST, + ++conn->cmd_ident, 1, + 1, idp->data, idp->len); + + silc_client_packet_send(cmd->client, cmd->conn->sock, + SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, + buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + if (idp) + silc_buffer_free(idp); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); } -/* Command INVITE. Invites specific client to join a channel. */ +/* Command TOPIC. Sets/shows topic on a channel. */ -SILC_CLIENT_CMD_FUNC(invite) +SILC_CLIENT_CMD_FUNC(topic) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; - SilcClient client = cmd->client; SilcClientConnection conn = cmd->conn; - SilcClientEntry client_entry; - SilcChannelEntry channel_entry; - SilcBuffer buffer; - unsigned int num = 0; - char *nickname = NULL, *server = NULL; - unsigned char *client_id, *channel_id; + SilcIDCacheEntry id_cache = NULL; + SilcChannelEntry channel; + SilcBuffer buffer, idp; + char *name; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -327,67 +478,210 @@ SILC_CLIENT_CMD_FUNC(invite) goto out; } - if (cmd->argc != 3) { - cmd->client->ops->say(cmd->client, conn, - "Usage: /INVITE [@] "); + if (cmd->argc < 2 || cmd->argc > 3) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /TOPIC []"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + name = conn->current_channel->channel_name; + } else { + name = cmd->argv[1]; + } + + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } - /* Parse the typed nickname. */ - if (!silc_parse_nickname(cmd->argv[1], &nickname, &server, &num)) { - cmd->client->ops->say(cmd->client, conn, "Bad nickname"); + /* Get the Channel ID of the channel */ + if (!silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } - /* Find client entry */ - client_entry = silc_idlist_get_client(client, conn, nickname, server, num); - if (!client_entry) { - /* Client entry not found, it was requested thus mark this to be - pending command. */ - silc_client_command_pending(SILC_COMMAND_IDENTIFY, - silc_client_command_invite, context); - return; + channel = (SilcChannelEntry)id_cache->context; + + /* Send TOPIC command to the server */ + idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL); + if (cmd->argc > 2) + buffer = silc_command_payload_encode_va(SILC_COMMAND_TOPIC, + ++conn->cmd_ident, 2, + 1, idp->data, idp->len, + 2, cmd->argv[2], + strlen(cmd->argv[2])); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_TOPIC, + ++conn->cmd_ident, 1, + 1, idp->data, idp->len); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(idp); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* Command INVITE. Invites specific client to join a channel. This is + also used to mange the invite list of the channel. */ + +SILC_CLIENT_CMD_FUNC(invite) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClient client = cmd->client; + SilcClientConnection conn = cmd->conn; + SilcClientEntry client_entry = NULL; + SilcChannelEntry channel; + SilcBuffer buffer, clidp, chidp; + uint32 type = 0; + char *nickname = NULL, *name; + char *invite = NULL; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; } - - client_id = silc_id_id2str(client_entry->id, SILC_ID_CLIENT); - /* Find channel entry */ - channel_entry = silc_idlist_get_channel(client, conn, cmd->argv[2]); - if (!channel_entry) { - cmd->client->ops->say(cmd->client, conn, "You are not on that channel"); - silc_free(client_id); + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /INVITE [[@server>]" + "[+|-[[@[![@hostname>]]]]]"); COMMAND_ERROR; goto out; } - channel_id = silc_id_id2str(channel_entry->id, SILC_ID_CHANNEL); + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + + channel = conn->current_channel; + } else { + name = cmd->argv[1]; + + channel = silc_client_get_channel(cmd->client, conn, name); + if (!channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); + COMMAND_ERROR; + goto out; + } + } + + /* Parse the typed nickname. */ + if (cmd->argc == 3) { + if (cmd->argv[2][0] != '+' && cmd->argv[2][0] != '-') { + if (client->params->nickname_parse) + client->params->nickname_parse(cmd->argv[2], &nickname); + else + nickname = strdup(cmd->argv[2]); + + /* Find client entry */ + client_entry = silc_idlist_get_client(client, conn, nickname, + cmd->argv[2], TRUE); + if (!client_entry) { + silc_free(nickname); + + if (cmd->pending) { + COMMAND_ERROR; + goto out; + } + + /* 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, + silc_client_command_destructor, + silc_client_command_invite, + silc_client_command_dup(cmd)); + cmd->pending = 1; + return; + } + } else { + invite = cmd->argv[2]; + invite++; + if (cmd->argv[2][0] == '+') + type = 3; + else + type = 4; + } + } + + /* Send the command */ + chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL); + if (client_entry) { + clidp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT); + buffer = silc_command_payload_encode_va(SILC_COMMAND_INVITE, + ++conn->cmd_ident, 3, + 1, chidp->data, chidp->len, + 2, clidp->data, clidp->len, + type, invite, invite ? + strlen(invite) : 0); + silc_buffer_free(clidp); + } else { + buffer = silc_command_payload_encode_va(SILC_COMMAND_INVITE, + ++conn->cmd_ident, 2, + 1, chidp->data, chidp->len, + type, invite, invite ? + strlen(invite) : 0); + } - buffer = silc_command_encode_payload_va(SILC_COMMAND_INVITE, 2, - 1, client_id, SILC_ID_CLIENT_LEN, - 2, channel_id, SILC_ID_CHANNEL_LEN); silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - - cmd->client->ops->say(cmd->client, conn, - "Inviting %s to channel %s", cmd->argv[1], - cmd->argv[2]); + silc_buffer_free(chidp); /* Notify application */ COMMAND; out: + silc_free(nickname); silc_client_command_free(cmd); } +typedef struct { + SilcClient client; + SilcClientConnection conn; +} *QuitInternal; + +SILC_TASK_CALLBACK(silc_client_command_quit_cb) +{ + QuitInternal q = (QuitInternal)context; + + /* Close connection */ + q->client->ops->disconnect(q->client, q->conn); + silc_client_close_connection(q->client, NULL, q->conn->sock->user_data); + + silc_free(q); +} + /* Command QUIT. Closes connection with current server. */ SILC_CLIENT_CMD_FUNC(quit) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcBuffer buffer; + QuitInternal q; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -395,20 +689,26 @@ SILC_CLIENT_CMD_FUNC(quit) goto out; } - buffer = silc_command_encode_payload(SILC_COMMAND_QUIT, cmd->argc - 1, - ++cmd->argv, ++cmd->argv_lens, - ++cmd->argv_types); + if (cmd->argc > 1) + buffer = silc_command_payload_encode(SILC_COMMAND_QUIT, cmd->argc - 1, + &cmd->argv[1], &cmd->argv_lens[1], + &cmd->argv_types[1], 0); + else + buffer = silc_command_payload_encode(SILC_COMMAND_QUIT, 0, + NULL, NULL, NULL, 0); silc_client_packet_send(cmd->client, cmd->conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - cmd->argv--; - cmd->argv_lens--; - cmd->argv_types--; - /* Close connection */ - silc_client_close_connection(cmd->client, cmd->conn->sock); - cmd->client->ops->disconnect(cmd->client, cmd->conn); + q = silc_calloc(1, sizeof(*q)); + q->client = cmd->client; + q->conn = cmd->conn; + + /* We quit the connection with little timeout */ + silc_schedule_task_add(cmd->client->schedule, cmd->conn->sock->sock, + silc_client_command_quit_cb, (void *)q, + 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); /* Notify application */ COMMAND; @@ -417,19 +717,17 @@ SILC_CLIENT_CMD_FUNC(quit) silc_client_command_free(cmd); } -SILC_CLIENT_CMD_FUNC(kill) -{ -} - -/* Command INFO. Request information about specific server. If specific - server is not provided the current server is used. */ +/* Command KILL. Router operator can use this command to remove an client + fromthe SILC Network. */ -SILC_CLIENT_CMD_FUNC(info) +SILC_CLIENT_CMD_FUNC(kill) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClient client = cmd->client; SilcClientConnection conn = cmd->conn; - SilcBuffer buffer; - char *name; + SilcBuffer buffer, idp; + SilcClientEntry target; + char *nickname = NULL; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -437,40 +735,113 @@ SILC_CLIENT_CMD_FUNC(info) goto out; } - if (cmd->argc < 2) - name = strdup(conn->remote_host); + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /KILL []"); + COMMAND_ERROR; + goto out; + } + + /* Parse the typed nickname. */ + if (client->params->nickname_parse) + client->params->nickname_parse(cmd->argv[1], &nickname); else - name = strdup(cmd->argv[1]); + nickname = strdup(cmd->argv[1]); - /* Send the command */ - buffer = silc_command_encode_payload_va(SILC_COMMAND_INFO, 1, - 1, name, strlen(name)); - silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + /* Get the target client */ + target = silc_idlist_get_client(cmd->client, conn, nickname, + cmd->argv[1], TRUE); + if (!target) { + silc_free(nickname); + + if (cmd->pending) { + COMMAND_ERROR; + goto out; + } + + /* 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, + silc_client_command_destructor, + silc_client_command_kill, + silc_client_command_dup(cmd)); + cmd->pending = 1; + return; + } + + /* Send the KILL command to the server */ + idp = silc_id_payload_encode(target->id, SILC_ID_CLIENT); + if (cmd->argc == 2) + buffer = silc_command_payload_encode_va(SILC_COMMAND_KILL, 0, 1, + 1, idp->data, idp->len); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_KILL, 0, 2, + 1, idp->data, idp->len, + 2, cmd->argv[2], + strlen(cmd->argv[2])); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); + silc_buffer_free(idp); /* Notify application */ COMMAND; out: + silc_free(nickname); silc_client_command_free(cmd); } -SILC_CLIENT_CMD_FUNC(connect) -{ -} - -/* Command PING. Sends ping to server. This is used to test the - communication channel. */ +/* Command INFO. Request information about specific server. If specific + server is not provided the current server is used. */ -SILC_CLIENT_CMD_FUNC(ping) +SILC_CLIENT_CMD_FUNC(info) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer; + char *name = NULL; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc == 2) + name = strdup(cmd->argv[1]); + + /* Send the command */ + if (name) + buffer = silc_command_payload_encode_va(SILC_COMMAND_INFO, 0, 1, + 1, name, strlen(name)); + else + buffer = silc_command_payload_encode(SILC_COMMAND_INFO, 0, + NULL, NULL, NULL, 0); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + if (name) + silc_free(name); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* Command PING. Sends ping to server. This is used to test the + communication channel. */ + +SILC_CLIENT_CMD_FUNC(ping) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; SilcBuffer buffer; void *id; int i; - char *name = NULL; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -478,65 +849,878 @@ SILC_CLIENT_CMD_FUNC(ping) goto out; } - if (cmd->argc == 1 || !strcmp(cmd->argv[1], conn->remote_host)) - name = strdup(conn->remote_host); - - id = silc_id_str2id(conn->remote_id_data, SILC_ID_SERVER); - - /* Send the command */ - buffer = silc_command_encode_payload_va(SILC_COMMAND_PING, 1, - 1, conn->remote_id_data, - SILC_ID_SERVER_LEN); - silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, - 0, NULL, NULL, buffer->data, buffer->len, TRUE); - silc_buffer_free(buffer); - - /* Start counting time */ - for (i = 0; i < conn->ping_count; i++) { - if (conn->ping[i].dest_id == NULL) { - conn->ping[i].start_time = time(NULL); - conn->ping[i].dest_id = id; - conn->ping[i].dest_name = name; - conn->ping_count++; - break; - } + /* Send the command */ + buffer = silc_command_payload_encode_va(SILC_COMMAND_PING, 0, 1, + 1, conn->remote_id_data, + silc_id_get_len(conn->remote_id, + SILC_ID_SERVER)); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + + id = silc_id_str2id(conn->remote_id_data, conn->remote_id_data_len, + SILC_ID_SERVER); + if (!id) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + /* Start counting time */ + for (i = 0; i < conn->ping_count; i++) { + if (conn->ping[i].dest_id == NULL) { + conn->ping[i].start_time = time(NULL); + conn->ping[i].dest_id = id; + conn->ping[i].dest_name = strdup(conn->remote_host); + conn->ping_count++; + break; + } + } + if (i >= conn->ping_count) { + i = conn->ping_count; + conn->ping = silc_realloc(conn->ping, sizeof(*conn->ping) * (i + 1)); + conn->ping[i].start_time = time(NULL); + conn->ping[i].dest_id = id; + conn->ping[i].dest_name = strdup(conn->remote_host); + conn->ping_count++; + } + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* Command JOIN. Joins to a channel. */ + +SILC_CLIENT_CMD_FUNC(join) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcIDCacheEntry id_cache = NULL; + SilcBuffer buffer, idp; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + /* See if we have joined to the requested channel already */ + if (silc_idcache_find_by_name_one(conn->channel_cache, cmd->argv[1], + &id_cache)) + goto out; + + idp = silc_id_payload_encode(conn->local_id, SILC_ID_CLIENT); + + /* Send JOIN command to the server */ + if (cmd->argc == 2) + buffer = + silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 2, + 1, cmd->argv[1], cmd->argv_lens[1], + 2, idp->data, idp->len); + else if (cmd->argc == 3) + /* XXX Buggy */ + buffer = + silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 3, + 1, cmd->argv[1], cmd->argv_lens[1], + 2, idp->data, idp->len, + 3, cmd->argv[2], cmd->argv_lens[2]); + else + buffer = + silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 4, + 1, cmd->argv[1], cmd->argv_lens[1], + 2, idp->data, idp->len, + 3, cmd->argv[2], cmd->argv_lens[2], + 4, cmd->argv[3], cmd->argv_lens[3]); + + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(idp); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* MOTD command. Requests motd from server. */ + +SILC_CLIENT_CMD_FUNC(motd) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 1 || cmd->argc > 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /MOTD []"); + COMMAND_ERROR; + goto out; + } + + /* Send TOPIC command to the server */ + if (cmd->argc == 1) + buffer = silc_command_payload_encode_va(SILC_COMMAND_MOTD, 0, 1, + 1, conn->remote_host, + strlen(conn->remote_host)); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_MOTD, 0, 1, + 1, cmd->argv[1], + cmd->argv_lens[1]); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* UMODE. Set/unset user mode in SILC. This is used mainly to unset the + modes as client cannot set itself server/router operator privileges. */ + +SILC_CLIENT_CMD_FUNC(umode) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer, idp; + unsigned char *cp, modebuf[4]; + uint32 mode, add, len; + int i; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /UMODE +|-"); + COMMAND_ERROR; + goto out; + } + + mode = conn->local_entry->mode; + + /* Are we adding or removing mode */ + if (cmd->argv[1][0] == '-') + add = FALSE; + else + add = TRUE; + + /* Parse mode */ + cp = cmd->argv[1] + 1; + len = strlen(cp); + for (i = 0; i < len; i++) { + switch(cp[i]) { + case 'a': + if (add) { + mode = 0; + mode |= SILC_UMODE_SERVER_OPERATOR; + mode |= SILC_UMODE_ROUTER_OPERATOR; + } else { + mode = SILC_UMODE_NONE; + } + break; + case 's': + if (add) + mode |= SILC_UMODE_SERVER_OPERATOR; + else + mode &= ~SILC_UMODE_SERVER_OPERATOR; + break; + case 'r': + if (add) + mode |= SILC_UMODE_ROUTER_OPERATOR; + else + mode &= ~SILC_UMODE_ROUTER_OPERATOR; + break; + case 'g': + if (add) + mode |= SILC_UMODE_GONE; + else + mode &= ~SILC_UMODE_GONE; + break; + default: + COMMAND_ERROR; + goto out; + break; + } + } + + idp = silc_id_payload_encode(conn->local_id, SILC_ID_CLIENT); + SILC_PUT32_MSB(mode, modebuf); + + /* Send the command packet. We support sending only one mode at once + that requires an argument. */ + buffer = + silc_command_payload_encode_va(SILC_COMMAND_UMODE, ++conn->cmd_ident, 2, + 1, idp->data, idp->len, + 2, modebuf, sizeof(modebuf)); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(idp); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* CMODE command. Sets channel mode. Modes that does not require any arguments + can be set several at once. Those modes that require argument must be set + separately (unless set with modes that does not require arguments). */ + +SILC_CLIENT_CMD_FUNC(cmode) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcChannelEntry channel; + SilcBuffer buffer, chidp, auth = NULL; + unsigned char *name, *cp, modebuf[4], tmp[4], *arg = NULL; + uint32 mode, add, type, len, arg_len = 0; + int i; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 3) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + + channel = conn->current_channel; + } else { + name = cmd->argv[1]; + + channel = silc_client_get_channel(cmd->client, conn, name); + if (!channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); + COMMAND_ERROR; + goto out; + } + } + + mode = channel->mode; + + /* Are we adding or removing mode */ + if (cmd->argv[2][0] == '-') + add = FALSE; + else + add = TRUE; + + /* Argument type to be sent to server */ + type = 0; + + /* Parse mode */ + cp = cmd->argv[2] + 1; + len = strlen(cp); + for (i = 0; i < len; i++) { + switch(cp[i]) { + case 'p': + if (add) + mode |= SILC_CHANNEL_MODE_PRIVATE; + else + mode &= ~SILC_CHANNEL_MODE_PRIVATE; + break; + case 's': + if (add) + mode |= SILC_CHANNEL_MODE_SECRET; + else + mode &= ~SILC_CHANNEL_MODE_SECRET; + break; + case 'k': + if (add) + mode |= SILC_CHANNEL_MODE_PRIVKEY; + else + mode &= ~SILC_CHANNEL_MODE_PRIVKEY; + break; + case 'i': + if (add) + mode |= SILC_CHANNEL_MODE_INVITE; + else + mode &= ~SILC_CHANNEL_MODE_INVITE; + break; + case 't': + if (add) + mode |= SILC_CHANNEL_MODE_TOPIC; + else + mode &= ~SILC_CHANNEL_MODE_TOPIC; + break; + case 'l': + if (add) { + int ll; + mode |= SILC_CHANNEL_MODE_ULIMIT; + type = 3; + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + ll = atoi(cmd->argv[3]); + SILC_PUT32_MSB(ll, tmp); + arg = tmp; + arg_len = 4; + } else { + mode &= ~SILC_CHANNEL_MODE_ULIMIT; + } + break; + case 'a': + if (add) { + mode |= SILC_CHANNEL_MODE_PASSPHRASE; + type = 4; + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + arg = cmd->argv[3]; + arg_len = cmd->argv_lens[3]; + } else { + mode &= ~SILC_CHANNEL_MODE_PASSPHRASE; + } + break; + case 'c': + if (add) { + mode |= SILC_CHANNEL_MODE_CIPHER; + type = 5; + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + arg = cmd->argv[3]; + arg_len = cmd->argv_lens[3]; + } else { + mode &= ~SILC_CHANNEL_MODE_CIPHER; + } + break; + case 'h': + if (add) { + mode |= SILC_CHANNEL_MODE_HMAC; + type = 6; + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + arg = cmd->argv[3]; + arg_len = cmd->argv_lens[3]; + } else { + mode &= ~SILC_CHANNEL_MODE_HMAC; + } + break; + case 'f': + if (add) { + mode |= SILC_CHANNEL_MODE_FOUNDER_AUTH; + type = 7; + + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); + COMMAND_ERROR; + goto out; + } + + if (!strcasecmp(cmd->argv[3], "-pubkey")) { + auth = silc_auth_public_key_auth_generate(cmd->client->public_key, + cmd->client->private_key, + conn->hash, + conn->local_id, + SILC_ID_CLIENT); + } else { + auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0, + cmd->argv[3], cmd->argv_lens[3]); + } + + arg = auth->data; + arg_len = auth->len; + } else { + mode &= ~SILC_CHANNEL_MODE_FOUNDER_AUTH; + } + break; + default: + COMMAND_ERROR; + goto out; + break; + } + } + + chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL); + SILC_PUT32_MSB(mode, modebuf); + + /* Send the command packet. We support sending only one mode at once + that requires an argument. */ + if (type && arg) { + buffer = + silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 3, + 1, chidp->data, chidp->len, + 2, modebuf, sizeof(modebuf), + type, arg, arg_len); + } else { + buffer = + silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 2, + 1, chidp->data, chidp->len, + 2, modebuf, sizeof(modebuf)); + } + + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(chidp); + if (auth) + silc_buffer_free(auth); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* CUMODE command. Changes client's mode on a channel. */ + +SILC_CLIENT_CMD_FUNC(cumode) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClient client = cmd->client; + SilcClientConnection conn = cmd->conn; + SilcChannelEntry channel; + SilcChannelUser chu; + SilcClientEntry client_entry; + SilcBuffer buffer, clidp, chidp, auth = NULL; + unsigned char *name, *cp, modebuf[4]; + uint32 mode = 0, add, len; + char *nickname = NULL; + int i; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 4) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CUMODE +|- [@]"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + + channel = conn->current_channel; + } else { + name = cmd->argv[1]; + + channel = silc_client_get_channel(cmd->client, conn, name); + if (!channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); + COMMAND_ERROR; + goto out; + } + } + + /* Parse the typed nickname. */ + if (client->params->nickname_parse) + client->params->nickname_parse(cmd->argv[3], &nickname); + else + nickname = strdup(cmd->argv[3]); + + /* Find client entry */ + client_entry = silc_idlist_get_client(cmd->client, conn, nickname, + cmd->argv[3], TRUE); + if (!client_entry) { + silc_free(nickname); + + if (cmd->pending) { + COMMAND_ERROR; + goto out; + } + + /* 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, + silc_client_command_destructor, + silc_client_command_cumode, + silc_client_command_dup(cmd)); + cmd->pending = 1; + return; + } + + /* Get the current mode */ + while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) { + if (chu->client == client_entry) { + mode = chu->mode; + break; + } + } + + /* Are we adding or removing mode */ + if (cmd->argv[2][0] == '-') + add = FALSE; + else + add = TRUE; + + /* Parse mode */ + cp = cmd->argv[2] + 1; + len = strlen(cp); + for (i = 0; i < len; i++) { + switch(cp[i]) { + case 'a': + if (add) { + mode |= SILC_CHANNEL_UMODE_CHANFO; + mode |= SILC_CHANNEL_UMODE_CHANOP; + } else { + mode = SILC_CHANNEL_UMODE_NONE; + } + break; + case 'f': + if (add) { + if (cmd->argc == 5) { + if (!strcasecmp(cmd->argv[4], "-pubkey")) { + auth = silc_auth_public_key_auth_generate(cmd->client->public_key, + cmd->client->private_key, + conn->hash, + conn->local_id, + SILC_ID_CLIENT); + } else { + auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0, + cmd->argv[4], cmd->argv_lens[4]); + } + } + mode |= SILC_CHANNEL_UMODE_CHANFO; + } else { + mode &= ~SILC_CHANNEL_UMODE_CHANFO; + } + break; + case 'o': + if (add) + mode |= SILC_CHANNEL_UMODE_CHANOP; + else + mode &= ~SILC_CHANNEL_UMODE_CHANOP; + break; + default: + COMMAND_ERROR; + goto out; + break; + } + } + + chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL); + SILC_PUT32_MSB(mode, modebuf); + clidp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT); + + /* Send the command packet. We support sending only one mode at once + that requires an argument. */ + buffer = silc_command_payload_encode_va(SILC_COMMAND_CUMODE, 0, 4, + 1, chidp->data, chidp->len, + 2, modebuf, 4, + 3, clidp->data, clidp->len, + 4, auth ? auth->data : NULL, + auth ? auth->len : 0); + + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(chidp); + silc_buffer_free(clidp); + if (auth) + silc_buffer_free(auth); + + /* Notify application */ + COMMAND; + + out: + silc_free(nickname); + silc_client_command_free(cmd); +} + +/* KICK command. Kicks a client out of channel. */ + +SILC_CLIENT_CMD_FUNC(kick) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClient client = cmd->client; + SilcClientConnection conn = cmd->conn; + SilcIDCacheEntry id_cache = NULL; + SilcChannelEntry channel; + SilcBuffer buffer, idp, idp2; + SilcClientEntry target; + char *name; + char *nickname = NULL; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 3) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /KICK []"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + name = conn->current_channel->channel_name; + } else { + name = cmd->argv[1]; + } + + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); + COMMAND_ERROR; + goto out; + } + + /* Get the Channel ID of the channel */ + if (!silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); + COMMAND_ERROR; + goto out; + } + + channel = (SilcChannelEntry)id_cache->context; + + /* Parse the typed nickname. */ + if (client->params->nickname_parse) + client->params->nickname_parse(cmd->argv[2], &nickname); + else + nickname = strdup(cmd->argv[2]); + + /* Get the target client */ + target = silc_idlist_get_client(cmd->client, conn, nickname, + cmd->argv[2], FALSE); + if (!target) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "No such client: %s", + cmd->argv[2]); + COMMAND_ERROR; + goto out; + } + + /* Send KICK command to the server */ + idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL); + idp2 = silc_id_payload_encode(target->id, SILC_ID_CLIENT); + if (cmd->argc == 3) + buffer = silc_command_payload_encode_va(SILC_COMMAND_KICK, 0, 2, + 1, idp->data, idp->len, + 2, idp2->data, idp2->len); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_KICK, 0, 3, + 1, idp->data, idp->len, + 2, idp2->data, idp2->len, + 3, cmd->argv[3], + strlen(cmd->argv[3])); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(idp); + silc_buffer_free(idp2); + + /* Notify application */ + COMMAND; + + out: + silc_free(nickname); + silc_client_command_free(cmd); +} + +static void silc_client_command_oper_send(unsigned char *data, + uint32 data_len, void *context) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer, auth; + + if (cmd->argc >= 3) { + /* Encode the public key authentication payload */ + auth = silc_auth_public_key_auth_generate(cmd->client->public_key, + cmd->client->private_key, + conn->hash, + conn->local_id, + SILC_ID_CLIENT); + } else { + /* Encode the password authentication payload */ + auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0, + data, data_len); + } + + buffer = silc_command_payload_encode_va(SILC_COMMAND_OPER, 0, 2, + 1, cmd->argv[1], + strlen(cmd->argv[1]), + 2, auth->data, auth->len); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + + silc_buffer_free(buffer); + silc_buffer_free(auth); + + /* Notify application */ + COMMAND; +} + +/* OPER command. Used to obtain server operator privileges. */ + +SILC_CLIENT_CMD_FUNC(oper) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /OPER [-pubkey]"); + COMMAND_ERROR; + goto out; } - if (i >= conn->ping_count) { - i = conn->ping_count; - conn->ping = silc_realloc(conn->ping, sizeof(*conn->ping) * (i + 1)); - conn->ping[i].start_time = time(NULL); - conn->ping[i].dest_id = id; - conn->ping[i].dest_name = name; - conn->ping_count++; + + if (cmd->argc < 3) { + /* Get passphrase */ + cmd->client->ops->ask_passphrase(cmd->client, conn, + silc_client_command_oper_send, + context); + return; } - - /* Notify application */ - COMMAND; + + silc_client_command_oper_send(NULL, 0, context); out: silc_client_command_free(cmd); } -SILC_CLIENT_CMD_FUNC(oper) +static void silc_client_command_silcoper_send(unsigned char *data, + uint32 data_len, void *context) { -} + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer, auth; + + if (cmd->argc >= 3) { + /* Encode the public key authentication payload */ + auth = silc_auth_public_key_auth_generate(cmd->client->public_key, + cmd->client->private_key, + conn->hash, + conn->local_id, + SILC_ID_CLIENT); + } else { + /* Encode the password authentication payload */ + auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0, + data, data_len); + } -SILC_CLIENT_CMD_FUNC(trace) -{ + buffer = silc_command_payload_encode_va(SILC_COMMAND_SILCOPER, 0, 2, + 1, cmd->argv[1], + strlen(cmd->argv[1]), + 2, auth->data, auth->len); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + + silc_buffer_free(buffer); + silc_buffer_free(auth); + + /* Notify application */ + COMMAND; } -SILC_CLIENT_CMD_FUNC(notice) +/* SILCOPER command. Used to obtain router operator privileges. */ + +SILC_CLIENT_CMD_FUNC(silcoper) { + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /SILCOPER [-pubkey]"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 3) { + /* Get passphrase */ + cmd->client->ops->ask_passphrase(cmd->client, conn, + silc_client_command_silcoper_send, + context); + return; + } + + silc_client_command_silcoper_send(NULL, 0, context); + + out: + silc_client_command_free(cmd); } -/* Command JOIN. Joins to a channel. */ +/* CONNECT command. Connects the server to another server. */ -SILC_CLIENT_CMD_FUNC(join) +SILC_CLIENT_CMD_FUNC(connect) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; - SilcIDCacheEntry id_cache = NULL; SilcBuffer buffer; + unsigned char port[4]; + uint32 tmp; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -545,34 +1729,29 @@ SILC_CLIENT_CMD_FUNC(join) } if (cmd->argc < 2) { - /* Show channels currently joined to */ - + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CONNECT []"); + COMMAND_ERROR; goto out; } - /* See if we have joined to the requested channel already */ - if (silc_idcache_find_by_data_one(conn->channel_cache, cmd->argv[1], - &id_cache)) { - cmd->client->ops->say(cmd->client, conn, - "You are talking to channel %s", cmd->argv[1]); - conn->current_channel = (SilcChannelEntry)id_cache->context; -#if 0 - cmd->client->screen->bottom_line->channel = cmd->argv[1]; - silc_screen_print_bottom_line(cmd->client->screen, 0); -#endif - goto out; + if (cmd->argc == 3) { + tmp = atoi(cmd->argv[2]); + SILC_PUT32_MSB(tmp, port); } - /* Send JOIN command to the server */ - buffer = silc_command_encode_payload(SILC_COMMAND_JOIN, - cmd->argc - 1, ++cmd->argv, - ++cmd->argv_lens, ++cmd->argv_types); - silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + if (cmd->argc == 3) + buffer = silc_command_payload_encode_va(SILC_COMMAND_CONNECT, 0, 2, + 1, cmd->argv[1], + strlen(cmd->argv[1]), + 2, port, 4); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_CONNECT, 0, 1, + 1, cmd->argv[1], + strlen(cmd->argv[1])); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - cmd->argv--; - cmd->argv_lens--; - cmd->argv_types--; /* Notify application */ COMMAND; @@ -581,36 +1760,154 @@ SILC_CLIENT_CMD_FUNC(join) silc_client_command_free(cmd); } -SILC_CLIENT_CMD_FUNC(motd) -{ -} +/* Command BAN. This is used to manage the ban list of the channel. */ -SILC_CLIENT_CMD_FUNC(umode) +SILC_CLIENT_CMD_FUNC(ban) { -} + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcChannelEntry channel; + SilcBuffer buffer, chidp; + int type = 0; + char *name, *ban = NULL; -SILC_CLIENT_CMD_FUNC(cmode) -{ -} + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } -SILC_CLIENT_CMD_FUNC(kick) -{ -} + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /BAN " + "[+|-[[@[![@hostname>]]]]]"); + COMMAND_ERROR; + goto out; + } -SILC_CLIENT_CMD_FUNC(restart) -{ + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } + + channel = conn->current_channel; + } else { + name = cmd->argv[1]; + + channel = silc_client_get_channel(cmd->client, conn, name); + if (!channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); + COMMAND_ERROR; + goto out; + } + } + + if (cmd->argc == 3) { + if (cmd->argv[2][0] == '+') + type = 2; + else + type = 3; + + ban = cmd->argv[2]; + ban++; + } + + chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL); + + /* Send the command */ + if (ban) + buffer = silc_command_payload_encode_va(SILC_COMMAND_BAN, 0, 2, + 1, chidp->data, chidp->len, + type, ban, strlen(ban)); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_BAN, 0, 1, + 1, chidp->data, chidp->len); + + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + silc_buffer_free(chidp); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); } + +/* CLOSE command. Close server connection to the remote server */ SILC_CLIENT_CMD_FUNC(close) { + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcBuffer buffer; + unsigned char port[4]; + uint32 tmp; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc < 2) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CLOSE []"); + COMMAND_ERROR; + goto out; + } + + if (cmd->argc == 3) { + tmp = atoi(cmd->argv[2]); + SILC_PUT32_MSB(tmp, port); + } + + if (cmd->argc == 3) + buffer = silc_command_payload_encode_va(SILC_COMMAND_CLOSE, 0, 2, + 1, cmd->argv[1], + strlen(cmd->argv[1]), + 2, port, 4); + else + buffer = silc_command_payload_encode_va(SILC_COMMAND_CLOSE, 0, 1, + 1, cmd->argv[1], + strlen(cmd->argv[1])); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, + 0, NULL, NULL, buffer->data, buffer->len, TRUE); + silc_buffer_free(buffer); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); } -SILC_CLIENT_CMD_FUNC(die) -{ -} - -SILC_CLIENT_CMD_FUNC(silcoper) +/* SHUTDOWN command. Shutdowns the server. */ + +SILC_CLIENT_CMD_FUNC(shutdown) { + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); + COMMAND_ERROR; + goto out; + } + + /* Send the command */ + silc_client_send_command(cmd->client, cmd->conn, + SILC_COMMAND_SHUTDOWN, 0, 0); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); } /* LEAVE command. Leaves a channel. Client removes itself from a channel. */ @@ -621,8 +1918,7 @@ SILC_CLIENT_CMD_FUNC(leave) SilcClientConnection conn = cmd->conn; SilcIDCacheEntry id_cache = NULL; SilcChannelEntry channel; - SilcBuffer buffer; - unsigned char *id_string; + SilcBuffer buffer, idp; char *name; if (!cmd->conn) { @@ -632,14 +1928,16 @@ SILC_CLIENT_CMD_FUNC(leave) } if (cmd->argc != 2) { - cmd->client->ops->say(cmd->client, conn, "Usage: /LEAVE "); + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /LEAVE "); COMMAND_ERROR; goto out; } if (cmd->argv[1][0] == '*') { if (!conn->current_channel) { - cmd->client->ops->say(cmd->client, conn, "You are not on any chanenl"); + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -648,15 +1946,10 @@ SILC_CLIENT_CMD_FUNC(leave) name = cmd->argv[1]; } - if (!conn->current_channel) { - cmd->client->ops->say(cmd->client, conn, "You are not on that channel"); - COMMAND_ERROR; - goto out; - } - /* Get the Channel ID of the channel */ - if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) { - cmd->client->ops->say(cmd->client, conn, "You are not on that channel"); + if (!silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } @@ -664,44 +1957,35 @@ SILC_CLIENT_CMD_FUNC(leave) channel = (SilcChannelEntry)id_cache->context; /* Send LEAVE command to the server */ - id_string = silc_id_id2str(id_cache->id, SILC_ID_CHANNEL); - buffer = silc_command_encode_payload_va(SILC_COMMAND_LEAVE, 1, - 1, id_string, SILC_ID_CHANNEL_LEN); + idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL); + buffer = silc_command_payload_encode_va(SILC_COMMAND_LEAVE, 0, 1, + 1, idp->data, idp->len); silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - - /* We won't talk anymore on this channel */ - cmd->client->ops->say(cmd->client, conn, "You have left channel %s", name); - - conn->current_channel = NULL; - - silc_idcache_del_by_id(conn->channel_cache, SILC_ID_CHANNEL, channel->id); - silc_free(channel->channel_name); - silc_free(channel->id); - silc_free(channel->key); - silc_cipher_free(channel->channel_key); - silc_free(channel); - silc_free(id_string); + silc_buffer_free(idp); /* Notify application */ COMMAND; + if (conn->current_channel == channel) + conn->current_channel = NULL; + + silc_client_del_channel(cmd->client, cmd->conn, channel); + out: silc_client_command_free(cmd); } -/* Command NAMES. Requests the names of the clients joined on requested +/* Command USERS. Requests the USERS of the clients joined on requested channel. */ -SILC_CLIENT_CMD_FUNC(names) +SILC_CLIENT_CMD_FUNC(users) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; - SilcIDCacheEntry id_cache = NULL; SilcBuffer buffer; char *name; - unsigned char *id_string; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -710,48 +1994,122 @@ SILC_CLIENT_CMD_FUNC(names) } if (cmd->argc != 2) { - cmd->client->ops->say(cmd->client, conn, "Usage: /NAMES "); + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /USERS "); COMMAND_ERROR; goto out; } - if (cmd->argv[1][0] == '*') + if (cmd->argv[1][0] == '*') { + if (!conn->current_channel) { + cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); + COMMAND_ERROR; + goto out; + } name = conn->current_channel->channel_name; - else + } else { name = cmd->argv[1]; + } - /* Get the Channel ID of the channel */ - if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) { - /* XXX should resolve the channel ID; LIST command */ - cmd->client->ops->say(cmd->client, conn, - "You are not on that channel", name); + /* Send USERS command to the server */ + buffer = silc_command_payload_encode_va(SILC_COMMAND_USERS, + ++conn->cmd_ident, 1, + 2, name, strlen(name)); + silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, + NULL, 0, NULL, NULL, buffer->data, + buffer->len, TRUE); + silc_buffer_free(buffer); + + /* Notify application */ + COMMAND; + + out: + silc_client_command_free(cmd); +} + +/* Command GETKEY. Used to fetch remote client's public key. */ + +SILC_CLIENT_CMD_FUNC(getkey) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcClient client = cmd->client; + SilcClientEntry client_entry = NULL; + SilcServerEntry server_entry = NULL; + char *nickname = NULL; + SilcBuffer idp, buffer; + + if (!cmd->conn) { + SILC_NOT_CONNECTED(cmd->client, cmd->conn); COMMAND_ERROR; goto out; } - /* Send NAMES command to the server */ - id_string = silc_id_id2str(id_cache->id, SILC_ID_CHANNEL); - buffer = silc_command_encode_payload_va(SILC_COMMAND_NAMES, 1, - 1, id_string, SILC_ID_CHANNEL_LEN); + if (cmd->argc < 2) { + client->ops->say(client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /GETKEY "); + COMMAND_ERROR; + goto out; + } + + /* Parse the typed nickname. */ + if (client->params->nickname_parse) + client->params->nickname_parse(cmd->argv[1], &nickname); + else + nickname = strdup(cmd->argv[1]); + + /* Find client entry */ + client_entry = silc_idlist_get_client(client, conn, nickname, cmd->argv[1], + FALSE); + if (!client_entry) { + /* Check whether user requested server actually */ + server_entry = silc_client_get_server(client, conn, cmd->argv[1]); + + if (!server_entry) { + /* No. what ever user wants we don't have it, so resolve it. We + will try to resolve both client and server, one of them is + bound to be wrong. */ + + /* This will send the IDENTIFY command */ + silc_idlist_get_client(client, conn, nickname, cmd->argv[1], TRUE); + silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, + conn->cmd_ident, + silc_client_command_destructor, + silc_client_command_getkey, + silc_client_command_dup(cmd)); + + /* This sends the INFO command to resolve the server. */ + silc_client_send_command(client, conn, SILC_COMMAND_INFO, + ++conn->cmd_ident, 1, + 1, cmd->argv[1], cmd->argv_lens[1]); + silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, + conn->cmd_ident, + silc_client_command_destructor, + silc_client_command_getkey, + silc_client_command_dup(cmd)); + + cmd->pending = 1; + silc_free(nickname); + return; + } + + idp = silc_id_payload_encode(server_entry->server_id, SILC_ID_SERVER); + } else { + idp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT); + } + + buffer = silc_command_payload_encode_va(SILC_COMMAND_GETKEY, 0, 1, + 1, idp->data, idp->len); silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 0, NULL, NULL, buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - silc_free(id_string); - - /* Register dummy pending command that will tell the reply command - that user called this command. Server may send reply to this command - even if user did not send this command thus we want to handle things - differently when user sent the command. This is dummy and won't be - execute. */ - /* XXX this is kludge and should be removed after pending command reply - support is added. Currently only commands may be pending not command - replies. */ - silc_client_command_pending(SILC_COMMAND_NAMES, - silc_client_command_names, NULL); + silc_buffer_free(idp); /* Notify application */ COMMAND; out: + silc_free(nickname); silc_client_command_free(cmd); }