- out:
- 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;
-}
-
-/* Private range commands, specific to this implementation (and compatible
- with SILC Server). */
-
-/* CONNECT command. Connects the server to another server. */
-
-SILC_CLIENT_CMD_FUNC(connect)
-{
- SilcClientCommandContext cmd = (SilcClientCommandContext)context;
- SilcClientConnection conn = cmd->conn;
- SilcBuffer buffer;
- unsigned char port[4];
- SilcUInt32 tmp;
-
- if (!cmd->conn) {
- SILC_NOT_CONNECTED(cmd->client, cmd->conn);
- COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
- goto out;
- }
-
- if (cmd->argc < 2) {
- SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO,
- "Usage: /CONNECT <server> [<port>]");
- COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
- 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_PRIV_CONNECT,
- ++conn->cmd_ident, 2,
- 1, cmd->argv[1],
- strlen(cmd->argv[1]),
- 2, port, 4);
- else
- buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CONNECT,
- ++conn->cmd_ident, 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(SILC_STATUS_OK);
-
- 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];
- SilcUInt32 tmp;
-
- if (!cmd->conn) {
- SILC_NOT_CONNECTED(cmd->client, cmd->conn);
- COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
- goto out;
- }
-
- if (cmd->argc < 2) {
- SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO,
- "Usage: /CLOSE <server> [<port>]");
- COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
- 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_PRIV_CLOSE,
- ++conn->cmd_ident, 2,
- 1, cmd->argv[1],
- strlen(cmd->argv[1]),
- 2, port, 4);
- else
- buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CLOSE,
- ++conn->cmd_ident, 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(SILC_STATUS_OK);
-
- out:
- silc_client_command_free(cmd);
-}
-
-/* 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(SILC_STATUS_ERR_NOT_REGISTERED);
- goto out;
- }
-
- /* Send the command */
- silc_client_command_send(cmd->client, cmd->conn,
- SILC_COMMAND_PRIV_SHUTDOWN, 0, 0);
-
- /* Notify application */
- COMMAND(SILC_STATUS_OK);
-
- out:
- silc_client_command_free(cmd);