X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilcclient%2Fcommand.c;h=2a388e96459215d6f69dc6448a17747e27ff65ca;hp=b23149360466262c0d358472f4e2ea6c4aad8b95;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hpb=8d1a8bbf644f67f86e7e1b95cf51d6d49406251f diff --git a/lib/silcclient/command.c b/lib/silcclient/command.c index b2314936..2a388e96 100644 --- a/lib/silcclient/command.c +++ b/lib/silcclient/command.c @@ -1,16 +1,15 @@ /* - command.c + command.c - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1997 - 2001 Pekka Riikonen + Copyright (C) 1997 - 2002 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. - + the Free Software Foundation; version 2 of the License. + 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 @@ -19,68 +18,31 @@ */ /* $Id$ */ -#include "clientlibincludes.h" +#include "silcincludes.h" +#include "silcclient.h" #include "client_internal.h" -/* Client command list. */ -SilcClientCommand silc_command_list[] = -{ - SILC_CLIENT_CMD(whois, WHOIS, "WHOIS", SILC_CF_LAG | SILC_CF_REG, 3), - SILC_CLIENT_CMD(whowas, WHOWAS, "WHOWAS", SILC_CF_LAG | SILC_CF_REG, 3), - SILC_CLIENT_CMD(identify, IDENTIFY, "IDENTIFY", - 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, 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, 2), - SILC_CLIENT_CMD(kill, KILL, "KILL", - 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, 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, 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, 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, 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(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), SILC_CLIENT_MESSAGE_ERROR, \ + x->internal->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. Usage: COMMAND; */ -#define COMMAND cmd->client->ops->command(cmd->client, cmd->conn, \ +#define COMMAND cmd->client->internal->ops->command(cmd->client, cmd->conn, \ cmd, TRUE, cmd->command->cmd) /* Error to application. Usage: COMMAND_ERROR; */ -#define COMMAND_ERROR cmd->client->ops->command(cmd->client, cmd->conn, \ - cmd, FALSE, cmd->command->cmd) +#define COMMAND_ERROR cmd->client->internal->ops->command(cmd->client, \ + cmd->conn, cmd, FALSE, cmd->command->cmd) + +#define SAY cmd->client->internal->ops->say /* 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, ...) +void silc_client_command_send(SilcClient client, SilcClientConnection conn, + SilcCommand command, SilcUInt16 ident, + SilcUInt32 argc, ...) { SilcBuffer packet; va_list ap; @@ -97,18 +59,32 @@ void silc_client_send_command(SilcClient client, SilcClientConnection conn, /* Finds and returns a pointer to the command list. Return NULL if the command is not found. */ -SilcClientCommand *silc_client_command_find(const char *name) +SilcClientCommand silc_client_command_find(SilcClient client, + const char *name) { - SilcClientCommand *cmd; + SilcClientCommand cmd; - for (cmd = silc_command_list; cmd->name; cmd++) { - if (!strcmp(cmd->name, name)) + silc_list_start(client->internal->commands); + while ((cmd = silc_list_get(client->internal->commands)) != SILC_LIST_END) { + if (cmd->name && !strcmp(cmd->name, name)) return cmd; } return NULL; } +/* Calls the command (executes it). Application can call this after + it has allocated the SilcClientCommandContext with the function + silc_client_command_alloc and found the command from the client + library by calling silc_client_command_find. This will execute + the command. */ + +void silc_client_command_call(SilcClientCommand command, + SilcClientCommandContext cmd) +{ + (*command->command)((void *)cmd, NULL); +} + /* 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 @@ -117,8 +93,7 @@ SilcClientCommand *silc_client_command_find(const char *name) void silc_client_command_pending(SilcClientConnection conn, SilcCommand reply_cmd, - uint16 ident, - SilcClientPendingDestructor destructor, + SilcUInt16 ident, SilcCommandCb callback, void *context) { @@ -129,7 +104,6 @@ void silc_client_command_pending(SilcClientConnection conn, reply->ident = ident; reply->context = context; reply->callback = callback; - reply->destructor = destructor; silc_dlist_add(conn->pending_commands, reply); } @@ -137,7 +111,7 @@ void silc_client_command_pending(SilcClientConnection conn, void silc_client_command_pending_del(SilcClientConnection conn, SilcCommand reply_cmd, - uint16 ident) + SilcUInt16 ident) { SilcClientCommandPending *r; @@ -156,7 +130,7 @@ void silc_client_command_pending_del(SilcClientConnection conn, int silc_client_command_pending_check(SilcClientConnection conn, SilcClientCommandReplyContext ctx, SilcCommand command, - uint16 ident) + SilcUInt16 ident) { SilcClientCommandPending *r; @@ -165,7 +139,6 @@ int silc_client_command_pending_check(SilcClientConnection conn, 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; } @@ -176,7 +149,7 @@ int silc_client_command_pending_check(SilcClientConnection conn, /* Allocate Command Context */ -SilcClientCommandContext silc_client_command_alloc() +SilcClientCommandContext silc_client_command_alloc(void) { SilcClientCommandContext ctx = silc_calloc(1, sizeof(*ctx)); ctx->users++; @@ -212,13 +185,6 @@ SilcClientCommandContext silc_client_command_dup(SilcClientCommandContext ctx) 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. */ @@ -237,7 +203,7 @@ SILC_CLIENT_CMD_FUNC(whois) /* 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, + silc_client_command_send(cmd->client, cmd->conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident, 1, 3, buffer->data, buffer->len); silc_buffer_free(buffer); @@ -279,8 +245,8 @@ SILC_CLIENT_CMD_FUNC(whowas) } if (cmd->argc < 2 || cmd->argc > 3) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /WHOWAS [@] []"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /WHOWAS [@] []"); COMMAND_ERROR; goto out; } @@ -305,7 +271,10 @@ SILC_CLIENT_CMD_FUNC(whowas) } /* Command IDENTIFY. This command is used to query information about - specific user, especially ID's. */ + specific user, especially ID's. + + NOTE: This command is used only internally by the client library + and application MUST NOT call this command directly. */ SILC_CLIENT_CMD_FUNC(identify) { @@ -315,14 +284,11 @@ SILC_CLIENT_CMD_FUNC(identify) if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); - COMMAND_ERROR; goto out; } - if (cmd->argc < 2 || cmd->argc > 3) { - COMMAND_ERROR; + if (cmd->argc < 2 || cmd->argc > 3) goto out; - } if (cmd->argc == 2) buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY, @@ -342,13 +308,41 @@ SILC_CLIENT_CMD_FUNC(identify) buffer->data, buffer->len, TRUE); silc_buffer_free(buffer); - /* Notify application */ - COMMAND; - out: silc_client_command_free(cmd); } +/* Pending callbcak that will be called after the NICK command was + replied by the server. This sets the nickname if there were no + errors. */ + +SILC_CLIENT_CMD_FUNC(nick_change) +{ + SilcClientCommandContext cmd = (SilcClientCommandContext)context; + SilcClientConnection conn = cmd->conn; + SilcClientCommandReplyContext reply = + (SilcClientCommandReplyContext)context2; + SilcCommandStatus status; + + SILC_GET16_MSB(status, silc_argument_get_arg_type(reply->args, 1, NULL)); + if (status == SILC_STATUS_OK) { + /* Set the nickname */ + silc_idcache_del_by_context(conn->client_cache, conn->local_entry); + if (conn->nickname) + silc_free(conn->nickname); + conn->nickname = strdup(cmd->argv[1]); + conn->local_entry->nickname = conn->nickname; + silc_client_nickname_format(cmd->client, conn, conn->local_entry); + silc_idcache_add(conn->client_cache, strdup(cmd->argv[1]), + conn->local_entry->id, conn->local_entry, 0, NULL); + COMMAND; + } else { + COMMAND_ERROR; + } + + silc_client_command_free(cmd); +} + /* Command NICK. Shows current nickname/sets new nickname on current window. */ @@ -365,8 +359,8 @@ SILC_CLIENT_CMD_FUNC(nick) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /NICK "); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /NICK "); COMMAND_ERROR; goto out; } @@ -377,36 +371,39 @@ SILC_CLIENT_CMD_FUNC(nick) /* Show current nickname */ if (cmd->argc < 2) { if (cmd->conn) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Your nickname is %s on server %s", - conn->nickname, conn->remote_host); + 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, SILC_CLIENT_MESSAGE_INFO, - "Your nickname is %s", conn->nickname); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Your nickname is %s", conn->nickname); } COMMAND; goto out; } - /* Set new nickname */ - buffer = silc_command_payload_encode(SILC_COMMAND_NICK, - cmd->argc - 1, ++cmd->argv, - ++cmd->argv_lens, ++cmd->argv_types, + if (cmd->argv_lens[1] > 128) + cmd->argv_lens[1] = 128; + + /* Send the NICK command */ + buffer = silc_command_payload_encode(SILC_COMMAND_NICK, 1, + &cmd->argv[1], + &cmd->argv_lens[1], + &cmd->argv_types[1], ++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); silc_buffer_free(buffer); - cmd->argv--; - cmd->argv_lens--; - cmd->argv_types--; - if (conn->nickname) - silc_free(conn->nickname); - conn->nickname = strdup(cmd->argv[1]); - /* Notify application */ - COMMAND; + /* Register pending callback that will actually set the new nickname + if there were no errors returned by the server. */ + silc_client_command_pending(conn, SILC_COMMAND_NICK, + cmd->conn->cmd_ident, + silc_client_command_nick_change, + silc_client_command_dup(cmd)); + cmd->pending = TRUE; out: silc_client_command_free(cmd); @@ -479,16 +476,16 @@ SILC_CLIENT_CMD_FUNC(topic) } if (cmd->argc < 2 || cmd->argc > 3) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /TOPIC []"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -498,16 +495,16 @@ SILC_CLIENT_CMD_FUNC(topic) } if (!conn->current_channel) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "You are not on that channel"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } @@ -549,7 +546,7 @@ SILC_CLIENT_CMD_FUNC(invite) SilcClientEntry client_entry = NULL; SilcChannelEntry channel; SilcBuffer buffer, clidp, chidp; - uint32 type = 0; + SilcUInt32 type = 0; char *nickname = NULL, *name; char *invite = NULL; @@ -560,17 +557,17 @@ SILC_CLIENT_CMD_FUNC(invite) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /INVITE [[@server>]" - "[+|-[[@[![@hostname>]]]]]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /INVITE [[@server>]" + "[+|-[[@[![@hostname>]]]]]"); 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -581,8 +578,8 @@ SILC_CLIENT_CMD_FUNC(invite) 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); COMMAND_ERROR; goto out; } @@ -591,8 +588,8 @@ SILC_CLIENT_CMD_FUNC(invite) /* 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); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[2], &nickname); else nickname = strdup(cmd->argv[2]); @@ -604,17 +601,15 @@ SILC_CLIENT_CMD_FUNC(invite) COMMAND_ERROR; goto out; } - silc_free(nickname); /* 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; + goto out; } } else { invite = cmd->argv[2]; @@ -668,7 +663,7 @@ SILC_TASK_CALLBACK(silc_client_command_quit_cb) QuitInternal q = (QuitInternal)context; /* Close connection */ - q->client->ops->disconnect(q->client, q->conn); + q->client->internal->ops->disconnect(q->client, q->conn); silc_client_close_connection(q->client, NULL, q->conn->sock->user_data); silc_free(q); @@ -704,6 +699,9 @@ SILC_CLIENT_CMD_FUNC(quit) q->client = cmd->client; q->conn = cmd->conn; + /* Sleep for a while */ + sleep(2); + /* 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, @@ -727,18 +725,17 @@ SILC_TASK_CALLBACK(silc_client_command_kill_remove_later) char *nickname = NULL; /* Parse the typed nickname. */ - if (client->params->nickname_parse) - client->params->nickname_parse(cmd->argv[1], &nickname); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[1], &nickname); else nickname = strdup(cmd->argv[1]); /* Get the target client */ target = silc_idlist_get_client(cmd->client, conn, nickname, cmd->argv[1], FALSE); - if (target) { - silc_client_remove_from_channels(client, conn, target); + if (target) + /* Remove the client from all channels and free it */ silc_client_del_client(client, conn, target); - } silc_free(nickname); silc_client_command_free(cmd); @@ -785,15 +782,15 @@ SILC_CLIENT_CMD_FUNC(kill) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /KILL []"); + 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); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[1], &nickname); else nickname = strdup(cmd->argv[1]); @@ -806,17 +803,14 @@ SILC_CLIENT_CMD_FUNC(kill) goto out; } - silc_free(nickname); - /* 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; + goto out; } /* Send the KILL command to the server */ @@ -842,7 +836,7 @@ SILC_CLIENT_CMD_FUNC(kill) /* Register a pending callback that will actually remove the killed client from our cache. */ silc_client_command_pending(conn, SILC_COMMAND_KILL, conn->cmd_ident, - NULL, silc_client_command_kill_remove, + silc_client_command_kill_remove, silc_client_command_dup(cmd)); out: @@ -929,7 +923,6 @@ SILC_CLIENT_CMD_FUNC(ping) 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; } } @@ -955,8 +948,10 @@ SILC_CLIENT_CMD_FUNC(join) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; - SilcIDCacheEntry id_cache = NULL; - SilcBuffer buffer, idp; + SilcChannelEntry channel; + SilcBuffer buffer, idp, auth = NULL; + char *name, *passphrase = NULL, *cipher = NULL, *hmac = NULL; + int i; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -964,38 +959,65 @@ SILC_CLIENT_CMD_FUNC(join) goto out; } + if (cmd->argc < 2) { + 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)) + channel = silc_client_get_channel(cmd->client, conn, cmd->argv[1]); + if (channel && silc_client_on_channel(channel, conn->local_entry)) 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]); + if (cmd->argv_lens[1] > 256) + cmd->argv_lens[1] = 256; + + name = cmd->argv[1]; + + for (i = 2; i < cmd->argc; i++) { + if (!strcasecmp(cmd->argv[i], "-cipher") && cmd->argc > i + 1) { + cipher = cmd->argv[i + 1]; + i++; + } else if (!strcasecmp(cmd->argv[i], "-hmac") && cmd->argc > i + 1) { + hmac = cmd->argv[i + 1]; + i++; + } else if (!strcasecmp(cmd->argv[i], "-founder") && cmd->argc > i + 1) { + if (!strcasecmp(cmd->argv[i + 1], "-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[i + 1], + cmd->argv_lens[i + 1]); + } + i++; + } else { + passphrase = cmd->argv[i]; + } + } + /* Send JOIN command to the server */ + buffer = + silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 6, + 1, name, strlen(name), + 2, idp->data, idp->len, + 3, passphrase, + passphrase ? strlen(passphrase) : 0, + 4, cipher, cipher ? strlen(cipher) : 0, + 5, hmac, hmac ? strlen(hmac) : 0, + 6, 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(idp); + if (auth) + silc_buffer_free(auth); /* Notify application */ COMMAND; @@ -1019,8 +1041,8 @@ SILC_CLIENT_CMD_FUNC(motd) } if (cmd->argc < 1 || cmd->argc > 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /MOTD []"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /MOTD []"); COMMAND_ERROR; goto out; } @@ -1054,7 +1076,7 @@ SILC_CLIENT_CMD_FUNC(umode) SilcClientConnection conn = cmd->conn; SilcBuffer buffer, idp; unsigned char *cp, modebuf[4]; - uint32 mode, add, len; + SilcUInt32 mode, add, len; int i; if (!cmd->conn) { @@ -1064,8 +1086,8 @@ SILC_CLIENT_CMD_FUNC(umode) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /UMODE +|-"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /UMODE +|-"); COMMAND_ERROR; goto out; } @@ -1149,7 +1171,7 @@ SILC_CLIENT_CMD_FUNC(cmode) 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; + SilcUInt32 mode, add, type, len, arg_len = 0; int i; if (!cmd->conn) { @@ -1159,16 +1181,16 @@ SILC_CLIENT_CMD_FUNC(cmode) } if (cmd->argc < 3) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -1179,8 +1201,8 @@ SILC_CLIENT_CMD_FUNC(cmode) 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); COMMAND_ERROR; goto out; } @@ -1238,8 +1260,8 @@ SILC_CLIENT_CMD_FUNC(cmode) mode |= SILC_CHANNEL_MODE_ULIMIT; type = 3; if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); COMMAND_ERROR; goto out; } @@ -1256,8 +1278,8 @@ SILC_CLIENT_CMD_FUNC(cmode) mode |= SILC_CHANNEL_MODE_PASSPHRASE; type = 4; if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); COMMAND_ERROR; goto out; } @@ -1272,8 +1294,8 @@ SILC_CLIENT_CMD_FUNC(cmode) mode |= SILC_CHANNEL_MODE_CIPHER; type = 5; if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); COMMAND_ERROR; goto out; } @@ -1288,8 +1310,8 @@ SILC_CLIENT_CMD_FUNC(cmode) mode |= SILC_CHANNEL_MODE_HMAC; type = 6; if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); COMMAND_ERROR; goto out; } @@ -1305,8 +1327,8 @@ SILC_CLIENT_CMD_FUNC(cmode) type = 7; if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CMODE +|- [{ }]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CMODE +|- [{ }]"); COMMAND_ERROR; goto out; } @@ -1379,7 +1401,7 @@ SILC_CLIENT_CMD_FUNC(cumode) SilcClientEntry client_entry; SilcBuffer buffer, clidp, chidp, auth = NULL; unsigned char *name, *cp, modebuf[4]; - uint32 mode = 0, add, len; + SilcUInt32 mode = 0, add, len; char *nickname = NULL; int i; @@ -1390,16 +1412,16 @@ SILC_CLIENT_CMD_FUNC(cumode) } if (cmd->argc < 4) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CUMODE +|- [@]"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -1410,16 +1432,16 @@ SILC_CLIENT_CMD_FUNC(cumode) 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"); + 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); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[3], &nickname); else nickname = strdup(cmd->argv[3]); @@ -1432,26 +1454,20 @@ SILC_CLIENT_CMD_FUNC(cumode) goto out; } - silc_free(nickname); - /* 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; + goto out; } /* Get the current mode */ - while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) { - if (chu->client == client_entry) { - mode = chu->mode; - break; - } - } + chu = silc_client_on_channel(channel, client_entry); + if (chu) + mode = chu->mode; /* Are we adding or removing mode */ if (cmd->argv[2][0] == '-') @@ -1476,11 +1492,11 @@ SILC_CLIENT_CMD_FUNC(cumode) 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); + 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]); @@ -1510,7 +1526,8 @@ SILC_CLIENT_CMD_FUNC(cumode) /* 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, + buffer = silc_command_payload_encode_va(SILC_COMMAND_CUMODE, 0, + auth ? 4 : 3, 1, chidp->data, chidp->len, 2, modebuf, 4, 3, clidp->data, clidp->len, @@ -1554,16 +1571,16 @@ SILC_CLIENT_CMD_FUNC(kick) } if (cmd->argc < 3) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /KICK []"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -1573,16 +1590,16 @@ SILC_CLIENT_CMD_FUNC(kick) } if (!conn->current_channel) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "You are not on that channel"); + 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } @@ -1590,8 +1607,8 @@ SILC_CLIENT_CMD_FUNC(kick) channel = (SilcChannelEntry)id_cache->context; /* Parse the typed nickname. */ - if (client->params->nickname_parse) - client->params->nickname_parse(cmd->argv[2], &nickname); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[2], &nickname); else nickname = strdup(cmd->argv[2]); @@ -1599,9 +1616,8 @@ SILC_CLIENT_CMD_FUNC(kick) 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]); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "No such client: %s", cmd->argv[2]); COMMAND_ERROR; goto out; } @@ -1634,7 +1650,7 @@ SILC_CLIENT_CMD_FUNC(kick) } static void silc_client_command_oper_send(unsigned char *data, - uint32 data_len, void *context) + SilcUInt32 data_len, void *context) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; @@ -1681,15 +1697,15 @@ SILC_CLIENT_CMD_FUNC(oper) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /OPER [-pubkey]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /OPER [-pubkey]"); COMMAND_ERROR; goto out; } if (cmd->argc < 3) { /* Get passphrase */ - cmd->client->ops->ask_passphrase(cmd->client, conn, + cmd->client->internal->ops->ask_passphrase(cmd->client, conn, silc_client_command_oper_send, context); return; @@ -1702,7 +1718,7 @@ SILC_CLIENT_CMD_FUNC(oper) } static void silc_client_command_silcoper_send(unsigned char *data, - uint32 data_len, void *context) + SilcUInt32 data_len, void *context) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; @@ -1749,15 +1765,15 @@ SILC_CLIENT_CMD_FUNC(silcoper) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /SILCOPER [-pubkey]"); + 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, + cmd->client->internal->ops->ask_passphrase(cmd->client, conn, silc_client_command_silcoper_send, context); return; @@ -1777,7 +1793,7 @@ SILC_CLIENT_CMD_FUNC(connect) SilcClientConnection conn = cmd->conn; SilcBuffer buffer; unsigned char port[4]; - uint32 tmp; + SilcUInt32 tmp; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -1786,8 +1802,8 @@ SILC_CLIENT_CMD_FUNC(connect) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CONNECT []"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CONNECT []"); COMMAND_ERROR; goto out; } @@ -1835,17 +1851,17 @@ SILC_CLIENT_CMD_FUNC(ban) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /BAN " - "[+|-[[@[![@hostname>]]]]]"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /BAN " + "[+|-[[@[![@hostname>]]]]]"); 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -1856,8 +1872,8 @@ SILC_CLIENT_CMD_FUNC(ban) 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are on that channel"); COMMAND_ERROR; goto out; } @@ -1904,7 +1920,7 @@ SILC_CLIENT_CMD_FUNC(close) SilcClientConnection conn = cmd->conn; SilcBuffer buffer; unsigned char port[4]; - uint32 tmp; + SilcUInt32 tmp; if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); @@ -1913,8 +1929,8 @@ SILC_CLIENT_CMD_FUNC(close) } if (cmd->argc < 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /CLOSE []"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /CLOSE []"); COMMAND_ERROR; goto out; } @@ -1957,7 +1973,7 @@ SILC_CLIENT_CMD_FUNC(shutdown) } /* Send the command */ - silc_client_send_command(cmd->client, cmd->conn, + silc_client_command_send(cmd->client, cmd->conn, SILC_COMMAND_SHUTDOWN, 0, 0); /* Notify application */ @@ -1973,8 +1989,8 @@ SILC_CLIENT_CMD_FUNC(leave) { SilcClientCommandContext cmd = (SilcClientCommandContext)context; SilcClientConnection conn = cmd->conn; - SilcIDCacheEntry id_cache = NULL; SilcChannelEntry channel; + SilcChannelUser chu; SilcBuffer buffer, idp; char *name; @@ -1985,16 +2001,16 @@ SILC_CLIENT_CMD_FUNC(leave) } if (cmd->argc != 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /LEAVE "); + 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, SILC_CLIENT_MESSAGE_INFO, - "You are not on any channel"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -2003,18 +2019,25 @@ SILC_CLIENT_CMD_FUNC(leave) name = cmd->argv[1]; } - /* 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"); + /* Get the channel entry */ + channel = silc_client_get_channel(cmd->client, conn, name); + if (!channel) { + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on that channel"); COMMAND_ERROR; goto out; } - channel = (SilcChannelEntry)id_cache->context; + /* Remove us from channel */ + chu = silc_client_on_channel(channel, conn->local_entry); + if (chu) { + silc_hash_table_del(chu->client->channels, chu->channel); + silc_hash_table_del(chu->channel->user_list, chu->client); + silc_free(chu); + } /* Send LEAVE command to the server */ - idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL); + idp = silc_id_payload_encode(channel->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, @@ -2051,16 +2074,16 @@ SILC_CLIENT_CMD_FUNC(users) } if (cmd->argc != 2) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, - "Usage: /USERS "); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "Usage: /USERS "); 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"); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, + "You are not on any channel"); COMMAND_ERROR; goto out; } @@ -2097,6 +2120,8 @@ SILC_CLIENT_CMD_FUNC(getkey) char *nickname = NULL; SilcBuffer idp, buffer; + SILC_LOG_DEBUG(("Start")); + if (!cmd->conn) { SILC_NOT_CONNECTED(cmd->client, cmd->conn); COMMAND_ERROR; @@ -2104,32 +2129,15 @@ SILC_CLIENT_CMD_FUNC(getkey) } if (cmd->argc < 2) { - client->ops->say(client, conn, SILC_CLIENT_MESSAGE_INFO, + client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_INFO, "Usage: /GETKEY "); COMMAND_ERROR; goto out; } - if (cmd->pending) { - SilcClientCommandReplyContext reply = - (SilcClientCommandReplyContext)context2; - SilcCommandStatus status; - unsigned char *tmp = silc_argument_get_arg_type(reply->args, 1, NULL); - SILC_GET16_MSB(status, tmp); - - if (status == SILC_STATUS_ERR_NO_SUCH_NICK || - status == SILC_STATUS_ERR_NO_SUCH_SERVER) { - cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, - "%s", - silc_client_command_status_message(status)); - COMMAND_ERROR; - goto out; - } - } - /* Parse the typed nickname. */ - if (client->params->nickname_parse) - client->params->nickname_parse(cmd->argv[1], &nickname); + if (client->internal->params->nickname_parse) + client->internal->params->nickname_parse(cmd->argv[1], &nickname); else nickname = strdup(cmd->argv[1]); @@ -2140,31 +2148,58 @@ SILC_CLIENT_CMD_FUNC(getkey) /* Check whether user requested server actually */ server_entry = silc_client_get_server(client, conn, cmd->argv[1]); - if (!server_entry && !cmd->pending) { + 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 IDENTIFY command to resolve the server. */ - silc_client_send_command(client, conn, SILC_COMMAND_IDENTIFY, - ++conn->cmd_ident, 1, - 2, cmd->argv[1], cmd->argv_lens[1]); - silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, - conn->cmd_ident, NULL, - silc_client_command_getkey, - silc_client_command_dup(cmd)); - - cmd->pending = 1; - silc_free(nickname); - return; + will first try to resolve the client, and if that fails then + we'll try to resolve the server. */ + + if (!cmd->pending) { + /* This will send the IDENTIFY command for nickname */ + 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_getkey, + silc_client_command_dup(cmd)); + cmd->pending = 1; + goto out; + } else { + SilcClientCommandReplyContext reply = + (SilcClientCommandReplyContext)context2; + SilcCommandStatus status; + unsigned char *tmp = silc_argument_get_arg_type(reply->args, 1, NULL); + SILC_GET16_MSB(status, tmp); + + /* If nickname was not found, then resolve the server. */ + if (status == SILC_STATUS_ERR_NO_SUCH_NICK) { + /* This sends the IDENTIFY command to resolve the server. */ + silc_client_command_register(client, SILC_COMMAND_IDENTIFY, + NULL, NULL, + silc_client_command_reply_identify_i, 0, + ++conn->cmd_ident); + silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY, + conn->cmd_ident, 1, + 2, cmd->argv[1], cmd->argv_lens[1]); + silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, + conn->cmd_ident, + silc_client_command_getkey, + silc_client_command_dup(cmd)); + goto out; + } + + /* If server was not found, then we've resolved both nickname and + server and did not find anybody. */ + if (status == SILC_STATUS_ERR_NO_SUCH_SERVER) { + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, "%s", + silc_client_command_status_message(SILC_STATUS_ERR_NO_SUCH_NICK)); + SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, "%s", + silc_client_command_status_message(status)); + COMMAND_ERROR; + goto out; + } + + COMMAND_ERROR; + goto out; + } } idp = silc_id_payload_encode(server_entry->server_id, SILC_ID_SERVER); @@ -2186,3 +2221,133 @@ SILC_CLIENT_CMD_FUNC(getkey) silc_free(nickname); silc_client_command_free(cmd); } + +/* Register a new command indicated by the `command' to the SILC client. + The `name' is optional command name. If provided the command may be + searched using the silc_client_command_find by that name. The + `command_function' is the function to be called when the command is + executed, and the `command_reply_function' is the function to be + called after the server has sent reply back to the command. + + The `ident' is optional identifier for the command. If non-zero + the `command_reply_function' for the command type `command' will be + called only if the command reply sent by server includes the + command identifier `ident'. Application usually does not need it + and set it to zero value. */ + +bool silc_client_command_register(SilcClient client, + SilcCommand command, + const char *name, + SilcCommandCb command_function, + SilcCommandCb command_reply_function, + SilcUInt8 max_args, + SilcUInt16 ident) +{ + SilcClientCommand cmd; + + cmd = silc_calloc(1, sizeof(*cmd)); + cmd->cmd = command; + cmd->command = command_function; + cmd->reply = command_reply_function; + cmd->name = name ? strdup(name) : NULL; + cmd->max_args = max_args; + cmd->ident = ident; + + silc_list_add(client->internal->commands, cmd); + + return TRUE; +} + +/* Unregister a command indicated by the `command' with command function + `command_function' and command reply function `command_reply_function'. + Returns TRUE if the command was found and unregistered. */ + +bool silc_client_command_unregister(SilcClient client, + SilcCommand command, + SilcCommandCb command_function, + SilcCommandCb command_reply_function, + SilcUInt16 ident) +{ + SilcClientCommand cmd; + + silc_list_start(client->internal->commands); + while ((cmd = silc_list_get(client->internal->commands)) != SILC_LIST_END) { + if (cmd->cmd == command && cmd->command == command_function && + cmd->reply == command_reply_function && cmd->ident == ident) { + silc_list_del(client->internal->commands, cmd); + silc_free(cmd->name); + silc_free(cmd); + return TRUE; + } + } + + return FALSE; +} + +/* Register all default commands provided by the client library for the + application. */ + +void silc_client_commands_register(SilcClient client) +{ + silc_list_init(client->internal->commands, struct SilcClientCommandStruct, + next); + + SILC_CLIENT_CMD(whois, WHOIS, "WHOIS", 3); + SILC_CLIENT_CMD(whowas, WHOWAS, "WHOWAS", 3); + SILC_CLIENT_CMD(identify, IDENTIFY, "IDENTIFY", 3); + SILC_CLIENT_CMD(nick, NICK, "NICK", 2); + SILC_CLIENT_CMD(list, LIST, "LIST", 2); + SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", 3); + SILC_CLIENT_CMD(invite, INVITE, "INVITE", 3); + SILC_CLIENT_CMD(quit, QUIT, "QUIT", 2); + SILC_CLIENT_CMD(kill, KILL, "KILL", 3); + SILC_CLIENT_CMD(info, INFO, "INFO", 2); + SILC_CLIENT_CMD(connect, CONNECT, "CONNECT", 3); + SILC_CLIENT_CMD(ping, PING, "PING", 2); + SILC_CLIENT_CMD(oper, OPER, "OPER", 3); + SILC_CLIENT_CMD(join, JOIN, "JOIN", 9); + SILC_CLIENT_CMD(motd, MOTD, "MOTD", 2); + SILC_CLIENT_CMD(umode, UMODE, "UMODE", 2); + SILC_CLIENT_CMD(cmode, CMODE, "CMODE", 4); + SILC_CLIENT_CMD(cumode, CUMODE, "CUMODE", 5); + SILC_CLIENT_CMD(kick, KICK, "KICK", 4); + SILC_CLIENT_CMD(ban, BAN, "BAN", 3); + SILC_CLIENT_CMD(close, CLOSE, "CLOSE", 3); + SILC_CLIENT_CMD(shutdown, SHUTDOWN, "SHUTDOWN", 1); + SILC_CLIENT_CMD(silcoper, SILCOPER, "SILCOPER", 3); + SILC_CLIENT_CMD(leave, LEAVE, "LEAVE", 2); + SILC_CLIENT_CMD(users, USERS, "USERS", 2); + SILC_CLIENT_CMD(getkey, GETKEY, "GETKEY", 2); +} + +/* Unregister all commands. */ + +void silc_client_commands_unregister(SilcClient client) +{ + SILC_CLIENT_CMDU(whois, WHOIS, "WHOIS"); + SILC_CLIENT_CMDU(whowas, WHOWAS, "WHOWAS"); + SILC_CLIENT_CMDU(identify, IDENTIFY, "IDENTIFY"); + SILC_CLIENT_CMDU(nick, NICK, "NICK"); + SILC_CLIENT_CMDU(list, LIST, "LIST"); + SILC_CLIENT_CMDU(topic, TOPIC, "TOPIC"); + SILC_CLIENT_CMDU(invite, INVITE, "INVITE"); + SILC_CLIENT_CMDU(quit, QUIT, "QUIT"); + SILC_CLIENT_CMDU(kill, KILL, "KILL"); + SILC_CLIENT_CMDU(info, INFO, "INFO"); + SILC_CLIENT_CMDU(connect, CONNECT, "CONNECT"); + SILC_CLIENT_CMDU(ping, PING, "PING"); + SILC_CLIENT_CMDU(oper, OPER, "OPER"); + SILC_CLIENT_CMDU(join, JOIN, "JOIN"); + SILC_CLIENT_CMDU(motd, MOTD, "MOTD"); + SILC_CLIENT_CMDU(umode, UMODE, "UMODE"); + SILC_CLIENT_CMDU(cmode, CMODE, "CMODE"); + SILC_CLIENT_CMDU(cumode, CUMODE, "CUMODE"); + SILC_CLIENT_CMDU(kick, KICK, "KICK"); + SILC_CLIENT_CMDU(ban, BAN, "BAN"); + SILC_CLIENT_CMDU(close, CLOSE, "CLOSE"); + SILC_CLIENT_CMDU(shutdown, SHUTDOWN, "SHUTDOWN"); + SILC_CLIENT_CMDU(silcoper, SILCOPER, "SILCOPER"); + SILC_CLIENT_CMDU(leave, LEAVE, "LEAVE"); + SILC_CLIENT_CMDU(users, USERS, "USERS"); + SILC_CLIENT_CMDU(getkey, GETKEY, "GETKEY"); +}