5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 #include "silcclient.h"
23 #include "client_internal.h"
25 /************************** Types and definitions ***************************/
27 /* Calls error command reply callback back to command sender. */
28 #define ERROR_CALLBACK(error) \
30 void *arg1 = NULL, *arg2 = NULL; \
31 if (cmd->status != SILC_STATUS_OK) \
32 silc_status_get_args(cmd->status, args, &arg1, &arg2); \
34 cmd->status = error; \
35 silc_client_command_callback(cmd, arg1, arg2); \
39 #define CHECK_STATUS(msg) \
40 SILC_LOG_DEBUG(("Start")); \
41 if (cmd->error != SILC_STATUS_OK) { \
43 SAY(cmd->conn->client, cmd->conn, SILC_CLIENT_MESSAGE_ERROR, \
44 msg "%s", silc_get_status_message(cmd->error)); \
45 ERROR_CALLBACK(cmd->error); \
46 silc_client_command_process_error(cmd, state_context, cmd->error); \
47 silc_fsm_next(fsm, silc_client_command_reply_process); \
48 return SILC_FSM_YIELD; \
51 /* Check for correct arguments */
52 #define CHECK_ARGS(min, max) \
53 if (silc_argument_get_arg_num(args) < min || \
54 silc_argument_get_arg_num(args) > max) { \
55 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
56 silc_fsm_next(fsm, silc_client_command_reply_process); \
57 return SILC_FSM_YIELD; \
60 #define SAY cmd->conn->client->internal->ops->say
62 /************************ Static utility functions **************************/
64 /* Delivers the command reply back to application */
67 silc_client_command_callback(SilcClientCommandContext cmd, ...)
69 SilcClientCommandReplyCallback cb;
74 /* Default reply callback */
77 cmd->conn->client->internal->ops->command_reply(
78 cmd->conn->client, cmd->conn, cmd->cmd, cmd->status,
84 silc_list_start(cmd->reply_callbacks);
85 while ((cb = silc_list_get(cmd->reply_callbacks)))
86 if (!cb->do_not_call) {
88 cb->do_not_call = cb->reply(cmd->conn->client, cmd->conn, cmd->cmd,
89 cmd->status, cmd->error, cb->context, cp);
96 /* Handles common error status types. */
98 static void silc_client_command_process_error(SilcClientCommandContext cmd,
99 SilcCommandPayload payload,
102 SilcClient client = cmd->conn->client;
103 SilcClientConnection conn = cmd->conn;
104 SilcArgumentPayload args = silc_command_get_args(payload);
105 SilcClientEntry client_entry;
108 if (cmd->error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
109 /* Remove unknown client entry from cache */
110 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
113 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
115 silc_client_unref_client(client, conn, client_entry);
116 silc_client_del_client(client, conn, client_entry);
121 /***************************** Command Reply ********************************/
123 /* Process received command reply packet */
125 SILC_FSM_STATE(silc_client_command_reply)
127 SilcClientConnection conn = fsm_context;
128 SilcPacket packet = state_context;
129 SilcClientCommandContext cmd;
130 SilcCommandPayload payload;
132 SilcUInt16 cmd_ident;
134 /* Get command reply payload from packet */
135 payload = silc_command_payload_parse(silc_buffer_datalen(&packet->buffer));
136 silc_packet_free(packet);
138 SILC_LOG_DEBUG(("Bad command reply packet"));
139 return SILC_FSM_FINISH;
142 cmd_ident = silc_command_get_ident(payload);
143 command = silc_command_get(payload);
145 /* Find the command pending reply */
146 silc_mutex_lock(conn->internal->lock);
147 silc_list_start(conn->internal->pending_commands);
148 while ((cmd = silc_list_get(conn->internal->pending_commands)))
149 if ((cmd->cmd == command || cmd->cmd == SILC_COMMAND_NONE)
150 && cmd->cmd_ident == cmd_ident)
152 silc_mutex_unlock(conn->internal->lock);
155 SILC_LOG_DEBUG(("Unknown command reply"));
156 silc_command_payload_free(payload);
157 return SILC_FSM_FINISH;
160 /* Signal command thread that command reply has arrived */
161 silc_fsm_set_state_context(&cmd->thread, payload);
162 silc_fsm_next(&cmd->thread, silc_client_command_reply_process);
163 silc_fsm_continue_sync(&cmd->thread);
165 return SILC_FSM_FINISH;
168 /* Wait here for command reply to arrive from remote host */
170 SILC_FSM_STATE(silc_client_command_reply_wait)
172 SILC_LOG_DEBUG(("Wait for command reply"));
174 /** Wait for command reply */
175 silc_fsm_set_state_context(fsm, NULL);
176 silc_fsm_next_later(fsm, silc_client_command_reply_timeout, 20, 0);
177 return SILC_FSM_WAIT;
180 /* Timeout occurred while waiting command reply */
182 SILC_FSM_STATE(silc_client_command_reply_timeout)
184 SilcClientCommandContext cmd = fsm_context;
185 SilcArgumentPayload args = NULL;
187 /* Timeout, reply not received in timely fashion */
188 ERROR_CALLBACK(SILC_STATUS_ERR_TIMEDOUT);
189 return SILC_FSM_FINISH;
192 /* Process received command reply payload */
194 SILC_FSM_STATE(silc_client_command_reply_process)
196 SilcClientCommandContext cmd = fsm_context;
197 SilcCommandPayload payload = state_context;
199 silc_command_get_status(payload, &cmd->status, &cmd->error);
202 case SILC_COMMAND_WHOIS:
204 silc_fsm_next(fsm, silc_client_command_reply_whois);
206 case SILC_COMMAND_WHOWAS:
208 silc_fsm_next(fsm, silc_client_command_reply_whowas);
210 case SILC_COMMAND_IDENTIFY:
212 silc_fsm_next(fsm, silc_client_command_reply_identify);
214 case SILC_COMMAND_NICK:
216 silc_fsm_next(fsm, silc_client_command_reply_nick);
218 case SILC_COMMAND_LIST:
220 silc_fsm_next(fsm, silc_client_command_reply_list);
222 case SILC_COMMAND_TOPIC:
224 silc_fsm_next(fsm, silc_client_command_reply_topic);
226 case SILC_COMMAND_INVITE:
228 silc_fsm_next(fsm, silc_client_command_reply_invite);
230 case SILC_COMMAND_QUIT:
232 silc_fsm_next(fsm, silc_client_command_reply_quit);
234 case SILC_COMMAND_KILL:
236 silc_fsm_next(fsm, silc_client_command_reply_kill);
238 case SILC_COMMAND_INFO:
240 silc_fsm_next(fsm, silc_client_command_reply_info);
242 case SILC_COMMAND_STATS:
244 silc_fsm_next(fsm, silc_client_command_reply_stats);
246 case SILC_COMMAND_PING:
248 silc_fsm_next(fsm, silc_client_command_reply_ping);
250 case SILC_COMMAND_OPER:
252 silc_fsm_next(fsm, silc_client_command_reply_oper);
254 case SILC_COMMAND_JOIN:
256 silc_fsm_next(fsm, silc_client_command_reply_join);
258 case SILC_COMMAND_MOTD:
260 silc_fsm_next(fsm, silc_client_command_reply_motd);
262 case SILC_COMMAND_UMODE:
264 silc_fsm_next(fsm, silc_client_command_reply_umode);
266 case SILC_COMMAND_CMODE:
268 silc_fsm_next(fsm, silc_client_command_reply_cmode);
270 case SILC_COMMAND_CUMODE:
272 silc_fsm_next(fsm, silc_client_command_reply_cumode);
274 case SILC_COMMAND_KICK:
276 silc_fsm_next(fsm, silc_client_command_reply_kick);
278 case SILC_COMMAND_BAN:
280 silc_fsm_next(fsm, silc_client_command_reply_ban);
282 case SILC_COMMAND_DETACH:
284 silc_fsm_next(fsm, silc_client_command_reply_detach);
286 case SILC_COMMAND_WATCH:
288 silc_fsm_next(fsm, silc_client_command_reply_watch);
290 case SILC_COMMAND_SILCOPER:
292 silc_fsm_next(fsm, silc_client_command_reply_silcoper);
294 case SILC_COMMAND_LEAVE:
296 silc_fsm_next(fsm, silc_client_command_reply_leave);
298 case SILC_COMMAND_USERS:
300 silc_fsm_next(fsm, silc_client_command_reply_users);
302 case SILC_COMMAND_GETKEY:
304 silc_fsm_next(fsm, silc_client_command_reply_getkey);
306 case SILC_COMMAND_SERVICE:
308 silc_fsm_next(fsm, silc_client_command_reply_service);
311 return SILC_FSM_FINISH;
314 return SILC_FSM_CONTINUE;
317 /* Completes command reply processing */
319 SILC_FSM_STATE(silc_client_command_reply_processed)
321 SilcClientCommandContext cmd = fsm_context;
322 SilcCommandPayload payload = state_context;
324 silc_command_payload_free(payload);
326 if (cmd->status == SILC_STATUS_OK || cmd->status == SILC_STATUS_LIST_END ||
327 SILC_STATUS_IS_ERROR(cmd->status))
328 return SILC_FSM_FINISH;
330 /** Wait more command payloads */
331 silc_fsm_next(fsm, silc_client_command_reply_wait);
332 return SILC_FSM_CONTINUE;
335 /******************************** WHOIS *************************************/
337 /* Received reply for WHOIS command. */
339 SILC_FSM_STATE(silc_client_command_reply_whois)
341 SilcClientCommandContext cmd = fsm_context;
342 SilcClientConnection conn = cmd->conn;
343 SilcClient client = conn->client;
344 SilcCommandPayload payload = state_context;
345 SilcArgumentPayload args = silc_command_get_args(payload);
346 SilcClientEntry client_entry = NULL;
347 SilcUInt32 idle = 0, mode = 0, fingerprint_len, len, *umodes = NULL;
348 SilcBufferStruct channels, ch_user_modes;
349 SilcBool has_channels = FALSE;
350 SilcDList channel_list = NULL;
352 char *nickname = NULL, *username = NULL, *realname = NULL;
353 unsigned char *fingerprint, *tmp;
355 CHECK_STATUS("WHOIS: ");
359 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
360 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
365 nickname = silc_argument_get_arg_type(args, 3, NULL);
366 username = silc_argument_get_arg_type(args, 4, NULL);
367 realname = silc_argument_get_arg_type(args, 5, NULL);
368 if (!nickname || !username || !realname) {
369 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
373 /* Get joined channel list */
374 memset(&channels, 0, sizeof(channels));
375 tmp = silc_argument_get_arg_type(args, 6, &len);
378 silc_buffer_set(&channels, tmp, len);
380 /* Get channel user mode list */
381 tmp = silc_argument_get_arg_type(args, 10, &len);
383 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
386 silc_buffer_set(&ch_user_modes, tmp, len);
390 tmp = silc_argument_get_arg_type(args, 7, &len);
392 SILC_GET32_MSB(mode, tmp);
395 tmp = silc_argument_get_arg_type(args, 8, &len);
397 SILC_GET32_MSB(idle, tmp);
399 /* Get fingerprint */
400 fingerprint = silc_argument_get_arg_type(args, 9, &fingerprint_len);
402 /* Check if we have this client cached already. */
403 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
405 SILC_LOG_DEBUG(("Adding new client entry (WHOIS)"));
407 silc_client_add_client(client, conn, nickname, username, realname,
408 &id.u.client_id, mode);
410 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
414 silc_client_update_client(client, conn, client_entry,
415 nickname, username, realname, mode);
418 if (fingerprint && fingerprint_len == sizeof(client_entry->fingerprint))
419 memcpy(client_entry->fingerprint, fingerprint, fingerprint_len);
421 /* Get user attributes */
422 tmp = silc_argument_get_arg_type(args, 11, &len);
424 if (client_entry->attrs)
425 silc_attribute_payload_list_free(client_entry->attrs);
426 client_entry->attrs = silc_attribute_payload_parse(tmp, len);
429 /* Parse channel and channel user mode list */
431 channel_list = silc_channel_payload_parse_list(silc_buffer_data(&channels),
432 silc_buffer_len(&channels));
434 silc_get_mode_list(&ch_user_modes, silc_dlist_count(channel_list),
438 /* Notify application */
439 silc_client_command_callback(cmd, client_entry, nickname, username,
440 realname, channel_list, mode, idle, fingerprint,
441 umodes, client_entry->attrs);
443 silc_client_unref_client(client, conn, client_entry);
445 silc_dlist_uninit(channel_list);
450 silc_fsm_next(fsm, silc_client_command_reply_processed);
451 return SILC_FSM_CONTINUE;
454 /******************************** WHOWAS ************************************/
456 /* Received reply for WHOWAS command. */
458 SILC_FSM_STATE(silc_client_command_reply_whowas)
460 SilcClientCommandContext cmd = fsm_context;
461 SilcClientConnection conn = cmd->conn;
462 SilcClient client = conn->client;
463 SilcCommandPayload payload = state_context;
464 SilcArgumentPayload args = silc_command_get_args(payload);
465 SilcClientEntry client_entry = NULL;
467 char *nickname, *username;
468 char *realname = NULL;
470 CHECK_STATUS("WHOWAS: ");
474 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
475 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
479 /* Get the client entry */
480 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
483 nickname = silc_argument_get_arg_type(args, 3, NULL);
484 username = silc_argument_get_arg_type(args, 4, NULL);
485 realname = silc_argument_get_arg_type(args, 5, NULL);
486 if (!nickname || !username) {
487 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
491 /* Notify application. We don't save any history information to any
492 cache. Just pass the data to the application. */
493 silc_client_command_callback(cmd, client_entry, nickname, username,
497 silc_client_unref_client(client, conn, client_entry);
498 silc_fsm_next(fsm, silc_client_command_reply_processed);
499 return SILC_FSM_CONTINUE;
502 /******************************** IDENTIFY **********************************/
504 /* Received reply for IDENTIFY command. */
506 SILC_FSM_STATE(silc_client_command_reply_identify)
508 SilcClientCommandContext cmd = fsm_context;
509 SilcClientConnection conn = cmd->conn;
510 SilcClient client = conn->client;
511 SilcCommandPayload payload = state_context;
512 SilcArgumentPayload args = silc_command_get_args(payload);
513 SilcClientEntry client_entry;
514 SilcServerEntry server_entry;
515 SilcChannelEntry channel_entry;
518 char *name = NULL, *info = NULL;
520 CHECK_STATUS("IDENTIFY: ");
524 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
525 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
530 name = silc_argument_get_arg_type(args, 3, &len);
531 info = silc_argument_get_arg_type(args, 4, &len);
535 SILC_LOG_DEBUG(("Received client information"));
537 /* Check if we have this client cached already. */
538 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
540 SILC_LOG_DEBUG(("Adding new client entry (IDENTIFY)"));
542 silc_client_add_client(client, conn, name, info, NULL,
545 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
549 silc_client_update_client(client, conn, client_entry,
550 name, info, NULL, 0);
553 /* Notify application */
554 silc_client_command_callback(cmd, client_entry, name, info);
555 silc_client_unref_client(client, conn, client_entry);
559 SILC_LOG_DEBUG(("Received server information"));
561 /* Check if we have this server cached already. */
562 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
564 SILC_LOG_DEBUG(("Adding new server entry (IDENTIFY)"));
565 server_entry = silc_client_add_server(client, conn, name, info,
568 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
572 silc_client_update_server(client, conn, server_entry, name, info);
574 server_entry->internal.resolve_cmd_ident = 0;
576 /* Notify application */
577 silc_client_command_callback(cmd, server_entry, name, info);
580 case SILC_ID_CHANNEL:
581 SILC_LOG_DEBUG(("Received channel information"));
583 /* Check if we have this channel cached already. */
584 channel_entry = silc_client_get_channel_by_id(client, conn,
586 if (!channel_entry) {
587 SILC_LOG_DEBUG(("Adding new channel entry (IDENTIFY"));
590 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
594 /* Add new channel entry */
595 channel_entry = silc_client_add_channel(client, conn, name, 0,
597 if (!channel_entry) {
598 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
603 /* Notify application */
604 silc_client_command_callback(cmd, channel_entry, name, info);
609 silc_fsm_next(fsm, silc_client_command_reply_processed);
610 return SILC_FSM_CONTINUE;
613 /********************************** NICK ************************************/
615 /* Received reply for command NICK. */
617 SILC_FSM_STATE(silc_client_command_reply_nick)
619 SilcClientCommandContext cmd = fsm_context;
620 SilcClientConnection conn = cmd->conn;
621 SilcClient client = conn->client;
622 SilcCommandPayload payload = state_context;
623 SilcArgumentPayload args = silc_command_get_args(payload);
624 unsigned char *tmp, *nick, *idp;
625 SilcUInt32 len, idp_len;
626 SilcClientID old_client_id;
630 CHECK_STATUS("Cannot set nickname: ");
633 old_client_id = *conn->local_id;
635 /* Take received Client ID */
636 idp = silc_argument_get_arg_type(args, 2, &idp_len);
638 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
641 if (!silc_id_payload_parse_id(idp, idp_len, &id)) {
642 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
646 /* Take the new nickname */
647 nick = silc_argument_get_arg_type(args, 3, &len);
649 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
653 /* Normalize nickname */
654 tmp = silc_identifier_check(nick, len, SILC_STRING_UTF8, 128, NULL);
656 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_NICKNAME);
660 /* Update the client entry */
661 if (!silc_idcache_update(conn->internal->client_cache,
662 conn->internal->local_entry,
663 &conn->local_entry->id,
665 conn->local_entry->nickname_normalized,
668 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_NICKNAME);
671 memcpy(conn->local_entry->nickname, nick, strlen(nick));
672 conn->local_entry->nickname_normalized = tmp;
673 silc_buffer_enlarge(conn->local_idp, idp_len);
674 silc_buffer_put(conn->local_idp, idp, idp_len);
675 silc_client_nickname_format(client, conn, conn->local_entry);
677 /* Notify application */
678 silc_client_command_callback(cmd, conn->local_entry,
679 conn->local_entry->nickname, &old_client_id);
682 silc_fsm_next(fsm, silc_client_command_reply_processed);
683 return SILC_FSM_CONTINUE;
686 /********************************** LIST ************************************/
688 /* Received reply to the LIST command. */
690 SILC_FSM_STATE(silc_client_command_reply_list)
692 SilcClientCommandContext cmd = fsm_context;
693 SilcClientConnection conn = cmd->conn;
694 SilcClient client = conn->client;
695 SilcCommandPayload payload = state_context;
696 SilcArgumentPayload args = silc_command_get_args(payload);
697 unsigned char *tmp, *name, *topic;
698 SilcUInt32 usercount = 0;
699 SilcChannelEntry channel_entry;
703 CHECK_STATUS("Cannot list channels: ");
705 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
706 /* There were no channels in the network. */
707 silc_client_command_callback(cmd, NULL, NULL, NULL, 0);
708 silc_fsm_next(fsm, silc_client_command_reply_processed);
709 return SILC_FSM_CONTINUE;
714 name = silc_argument_get_arg_type(args, 3, NULL);
716 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
720 topic = silc_argument_get_arg_type(args, 4, NULL);
721 tmp = silc_argument_get_arg_type(args, 5, NULL);
723 SILC_GET32_MSB(usercount, tmp);
725 /* Check whether the channel exists, and add it to cache if it doesn't. */
726 channel_entry = silc_client_get_channel_by_id(client, conn,
728 if (!channel_entry) {
729 /* Add new channel entry */
730 channel_entry = silc_client_add_channel(client, conn, name, 0,
732 if (!channel_entry) {
733 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
738 /* Notify application */
739 silc_client_command_callback(cmd, channel_entry, name, topic, usercount);
742 silc_fsm_next(fsm, silc_client_command_reply_processed);
743 return SILC_FSM_CONTINUE;
746 /********************************* TOPIC ************************************/
748 /* Received reply to topic command. */
750 SILC_FSM_STATE(silc_client_command_reply_topic)
752 SilcClientCommandContext cmd = fsm_context;
753 SilcClientConnection conn = cmd->conn;
754 SilcClient client = conn->client;
755 SilcCommandPayload payload = state_context;
756 SilcArgumentPayload args = silc_command_get_args(payload);
757 SilcChannelEntry channel;
763 CHECK_STATUS("Cannot set topic: ");
766 /* Take Channel ID */
767 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
768 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
772 /* Get the channel entry */
773 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
775 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
780 topic = silc_argument_get_arg_type(args, 3, &len);
782 silc_free(channel->topic);
783 channel->topic = silc_memdup(topic, len);
786 /* Notify application */
787 silc_client_command_callback(cmd, channel, channel->topic);
790 silc_fsm_next(fsm, silc_client_command_reply_processed);
791 return SILC_FSM_CONTINUE;
794 /********************************* INVITE ***********************************/
796 /* Received reply to invite command. */
798 SILC_FSM_STATE(silc_client_command_reply_invite)
800 SilcClientCommandContext cmd = fsm_context;
801 SilcClientConnection conn = cmd->conn;
802 SilcClient client = conn->client;
803 SilcCommandPayload payload = state_context;
804 SilcArgumentPayload args = silc_command_get_args(payload);
805 SilcChannelEntry channel;
808 SilcBufferStruct buf;
812 CHECK_STATUS("Cannot invite: ");
815 /* Take Channel ID */
816 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
817 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
821 /* Get the channel entry */
822 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
824 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
828 /* Get the invite list */
829 tmp = silc_argument_get_arg_type(args, 3, &len);
831 silc_buffer_set(&buf, tmp, len);
833 /* Notify application */
834 silc_client_command_callback(cmd, channel, tmp ? &buf : NULL);
837 silc_fsm_next(fsm, silc_client_command_reply_processed);
838 return SILC_FSM_CONTINUE;
841 /********************************** KILL ************************************/
843 /* Received reply to the KILL command. */
845 SILC_FSM_STATE(silc_client_command_reply_kill)
847 SilcClientCommandContext cmd = fsm_context;
848 SilcClientConnection conn = cmd->conn;
849 SilcClient client = conn->client;
850 SilcCommandPayload payload = state_context;
851 SilcArgumentPayload args = silc_command_get_args(payload);
852 SilcClientEntry client_entry;
856 CHECK_STATUS("Cannot kill: ");
859 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
860 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
864 /* Get the client entry, if exists */
865 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
867 /* Notify application */
868 silc_client_command_callback(cmd, client_entry);
870 /* Remove the client from all channels and free it */
872 silc_client_del_client(client, conn, client_entry);
873 silc_client_unref_client(client, conn, client_entry);
877 silc_fsm_next(fsm, silc_client_command_reply_processed);
878 return SILC_FSM_CONTINUE;
881 /********************************** INFO ************************************/
883 /* Received reply to INFO command. We receive the server ID and some
884 information about the server user requested. */
886 SILC_FSM_STATE(silc_client_command_reply_info)
888 SilcClientCommandContext cmd = fsm_context;
889 SilcClientConnection conn = cmd->conn;
890 SilcClient client = conn->client;
891 SilcCommandPayload payload = state_context;
892 SilcArgumentPayload args = silc_command_get_args(payload);
893 SilcServerEntry server;
894 char *server_name, *server_info;
898 CHECK_STATUS("Cannot get info: ");
902 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
903 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
907 /* Get server name */
908 server_name = silc_argument_get_arg_type(args, 3, NULL);
910 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
914 /* Get server info */
915 server_info = silc_argument_get_arg_type(args, 4, NULL);
917 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
921 /* See whether we have this server cached. If not create it. */
922 server = silc_client_get_server_by_id(client, conn, &id.u.server_id);
924 SILC_LOG_DEBUG(("New server entry"));
925 server = silc_client_add_server(client, conn, server_name,
926 server_info, &id.u.server_id);
931 /* Notify application */
932 silc_client_command_callback(cmd, server, server->server_name,
933 server->server_info);
936 silc_fsm_next(fsm, silc_client_command_reply_processed);
937 return SILC_FSM_CONTINUE;
940 /********************************** STATS ***********************************/
942 /* Received reply to STATS command. */
944 SILC_FSM_STATE(silc_client_command_reply_stats)
946 SilcClientCommandContext cmd = fsm_context;
947 SilcCommandPayload payload = state_context;
948 SilcArgumentPayload args = silc_command_get_args(payload);
949 SilcClientStats stats;
950 unsigned char *buf = NULL;
951 SilcUInt32 buf_len = 0;
956 CHECK_STATUS("Cannot get stats: ");
960 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
961 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
965 /* Get statistics structure */
966 memset(&stats, 0, sizeof(stats));
967 buf = silc_argument_get_arg_type(args, 3, &buf_len);
969 silc_buffer_set(&b, buf, buf_len);
970 silc_buffer_unformat(&b,
971 SILC_STR_UI_INT(&stats.starttime),
972 SILC_STR_UI_INT(&stats.uptime),
973 SILC_STR_UI_INT(&stats.my_clients),
974 SILC_STR_UI_INT(&stats.my_channels),
975 SILC_STR_UI_INT(&stats.my_server_ops),
976 SILC_STR_UI_INT(&stats.my_router_ops),
977 SILC_STR_UI_INT(&stats.cell_clients),
978 SILC_STR_UI_INT(&stats.cell_channels),
979 SILC_STR_UI_INT(&stats.cell_servers),
980 SILC_STR_UI_INT(&stats.clients),
981 SILC_STR_UI_INT(&stats.channels),
982 SILC_STR_UI_INT(&stats.servers),
983 SILC_STR_UI_INT(&stats.routers),
984 SILC_STR_UI_INT(&stats.server_ops),
985 SILC_STR_UI_INT(&stats.router_ops),
989 /* Notify application */
990 silc_client_command_callback(cmd, &stats);
993 silc_fsm_next(fsm, silc_client_command_reply_processed);
994 return SILC_FSM_CONTINUE;
997 /********************************** PING ************************************/
999 /* Received reply to PING command. */
1001 SILC_FSM_STATE(silc_client_command_reply_ping)
1003 SilcClientCommandContext cmd = fsm_context;
1004 SilcClientConnection conn = cmd->conn;
1005 SilcClient client = conn->client;
1008 diff = silc_time() - SILC_PTR_TO_64(cmd->context);
1009 SAY(client, conn, SILC_CLIENT_MESSAGE_INFO,
1010 "Ping reply from %s: %d second%s", conn->remote_host,
1011 (int)diff, diff == 1 ? "" : "s");
1013 /* Notify application */
1014 silc_client_command_callback(cmd);
1016 silc_fsm_next(fsm, silc_client_command_reply_processed);
1017 return SILC_FSM_CONTINUE;
1020 /********************************** JOIN ************************************/
1022 /* Received reply for JOIN command. */
1024 SILC_FSM_STATE(silc_client_command_reply_join)
1026 SilcClientCommandContext cmd = fsm_context;
1027 SilcClientConnection conn = cmd->conn;
1028 SilcClient client = conn->client;
1029 SilcCommandPayload payload = state_context;
1030 SilcArgumentPayload args = silc_command_get_args(payload);
1031 SilcChannelEntry channel;
1032 SilcUInt32 mode = 0, len, list_count;
1033 char *topic, *tmp, *channel_name = NULL, *hmac;
1034 SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
1035 SilcBufferStruct chpklist;
1040 CHECK_STATUS("Cannot join channel: ");
1043 /* Get channel name */
1044 channel_name = silc_argument_get_arg_type(args, 2, NULL);
1045 if (!channel_name) {
1046 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1050 /* Get Channel ID */
1051 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1052 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1056 /* Get channel mode */
1057 tmp = silc_argument_get_arg_type(args, 5, NULL);
1059 SILC_GET32_MSB(mode, tmp);
1061 /* Get channel key */
1062 tmp = silc_argument_get_arg_type(args, 7, &len);
1064 keyp = silc_buffer_alloc_size(len);
1066 silc_buffer_put(keyp, tmp, len);
1070 topic = silc_argument_get_arg_type(args, 10, NULL);
1072 /* Check whether we have this channel entry already. */
1073 channel = silc_client_get_channel(client, conn, channel_name);
1075 if (!SILC_ID_CHANNEL_COMPARE(&channel->id, &id.u.channel_id))
1076 silc_client_replace_channel_id(client, conn, channel, &id.u.channel_id);
1078 /* Create new channel entry */
1079 channel = silc_client_add_channel(client, conn, channel_name,
1080 mode, &id.u.channel_id);
1082 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_CHANNEL);
1087 conn->current_channel = channel;
1088 channel->mode = mode;
1091 hmac = silc_argument_get_arg_type(args, 11, NULL);
1093 if (!silc_hmac_alloc(hmac, NULL, &channel->internal.hmac)) {
1095 SAY(client, conn, SILC_CLIENT_MESSAGE_ERROR,
1096 "Cannot join channel: Unsupported HMAC `%s'", hmac);
1097 ERROR_CALLBACK(SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
1102 /* Get the list count */
1103 tmp = silc_argument_get_arg_type(args, 12, &len);
1105 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1108 SILC_GET32_MSB(list_count, tmp);
1110 /* Get Client ID list */
1111 tmp = silc_argument_get_arg_type(args, 13, &len);
1113 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1117 client_id_list = silc_buffer_alloc_size(len);
1119 silc_buffer_put(client_id_list, tmp, len);
1121 /* Get client mode list */
1122 tmp = silc_argument_get_arg_type(args, 14, &len);
1124 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1128 client_mode_list = silc_buffer_alloc_size(len);
1129 if (client_mode_list)
1130 silc_buffer_put(client_mode_list, tmp, len);
1132 /* Add clients we received in the reply to the channel */
1133 for (i = 0; i < list_count; i++) {
1137 SilcClientEntry client_entry;
1140 SILC_GET16_MSB(idp_len, client_id_list->data + 2);
1142 if (!silc_id_payload_parse_id(client_id_list->data, idp_len, &id))
1146 SILC_GET32_MSB(mode, client_mode_list->data);
1148 /* Check if we have this client cached already. */
1149 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1150 if (!client_entry) {
1151 /* No, we don't have it, add entry for it. */
1153 silc_client_add_client(client, conn, NULL, NULL, NULL,
1154 &id.u.client_id, 0);
1159 /* Join client to the channel */
1160 if (!silc_client_add_to_channel(channel, client_entry, mode)) {
1161 silc_client_unref_client(client, conn, client_entry);
1164 silc_client_unref_client(client, conn, client_entry);
1166 if (!silc_buffer_pull(client_id_list, idp_len))
1168 if (!silc_buffer_pull(client_mode_list, 4))
1171 silc_buffer_start(client_id_list);
1172 silc_buffer_start(client_mode_list);
1174 /* Save channel key */
1177 silc_client_save_channel_key(client, conn, keyp, channel);
1180 /* Get founder key */
1181 tmp = silc_argument_get_arg_type(args, 15, &len);
1183 if (channel->founder_key)
1184 silc_pkcs_public_key_free(channel->founder_key);
1185 channel->founder_key = NULL;
1186 silc_public_key_payload_decode(tmp, len, &channel->founder_key);
1189 /* Get user limit */
1190 tmp = silc_argument_get_arg_type(args, 17, &len);
1191 if (tmp && len == 4)
1192 SILC_GET32_MSB(channel->user_limit, tmp);
1193 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1194 channel->user_limit = 0;
1196 /* Get channel public key list */
1197 tmp = silc_argument_get_arg_type(args, 16, &len);
1199 silc_buffer_set(&chpklist, tmp, len);
1202 silc_free(channel->topic);
1203 channel->topic = silc_memdup(topic, strlen(topic));
1206 /* Notify application */
1207 silc_client_command_callback(cmd, channel_name, channel, mode, 0,
1208 keyp ? keyp->head : NULL, NULL,
1209 NULL, topic, hmac, list_count, client_id_list,
1210 client_mode_list, channel->founder_key,
1211 tmp ? &chpklist : NULL, channel->user_limit);
1214 silc_buffer_free(keyp);
1215 silc_buffer_free(client_id_list);
1216 silc_buffer_free(client_mode_list);
1217 silc_fsm_next(fsm, silc_client_command_reply_processed);
1218 return SILC_FSM_CONTINUE;
1221 /********************************** MOTD ************************************/
1223 /* Received reply for MOTD command */
1225 SILC_FSM_STATE(silc_client_command_reply_motd)
1227 SilcClientCommandContext cmd = fsm_context;
1228 SilcClientConnection conn = cmd->conn;
1229 SilcClient client = conn->client;
1230 SilcCommandPayload payload = state_context;
1231 SilcArgumentPayload args = silc_command_get_args(payload);
1233 char *motd = NULL, *cp, line[256];
1236 CHECK_STATUS("Cannot get motd: ");
1239 if (silc_argument_get_arg_num(args) == 3) {
1240 motd = silc_argument_get_arg_type(args, 3, NULL);
1242 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1249 if (cp[i++] == '\n') {
1250 memset(line, 0, sizeof(line));
1251 silc_strncat(line, sizeof(line), cp, i - 1);
1258 SAY(client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", line);
1267 /* Notify application */
1268 silc_client_command_callback(cmd, motd);
1271 silc_fsm_next(fsm, silc_client_command_reply_processed);
1272 return SILC_FSM_CONTINUE;
1275 /********************************** UMODE ***********************************/
1277 /* Received reply to the UMODE command. Save the current user mode */
1279 SILC_FSM_STATE(silc_client_command_reply_umode)
1281 SilcClientCommandContext cmd = fsm_context;
1282 SilcClientConnection conn = cmd->conn;
1283 SilcCommandPayload payload = state_context;
1284 SilcArgumentPayload args = silc_command_get_args(payload);
1286 SilcUInt32 mode, len;
1289 CHECK_STATUS("Cannot change mode: ");
1292 tmp = silc_argument_get_arg_type(args, 2, &len);
1293 if (!tmp || len != 4) {
1294 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1298 SILC_GET32_MSB(mode, tmp);
1299 conn->local_entry->mode = mode;
1301 /* Notify application */
1302 silc_client_command_callback(cmd, mode);
1305 silc_fsm_next(fsm, silc_client_command_reply_processed);
1306 return SILC_FSM_CONTINUE;
1309 /********************************** CMODE ***********************************/
1311 /* Received reply for CMODE command. */
1313 SILC_FSM_STATE(silc_client_command_reply_cmode)
1315 SilcClientCommandContext cmd = fsm_context;
1316 SilcClientConnection conn = cmd->conn;
1317 SilcClient client = conn->client;
1318 SilcCommandPayload payload = state_context;
1319 SilcArgumentPayload args = silc_command_get_args(payload);
1322 SilcChannelEntry channel;
1324 SilcPublicKey public_key = NULL;
1325 SilcDList channel_pubkeys = NULL;
1329 CHECK_STATUS("Cannot change mode: ");
1332 /* Take Channel ID */
1333 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1334 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1338 /* Get the channel entry */
1339 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1341 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1345 /* Get channel mode */
1346 tmp = silc_argument_get_arg_type(args, 3, &len);
1347 if (!tmp || len != 4) {
1348 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1353 SILC_GET32_MSB(mode, tmp);
1354 channel->mode = mode;
1356 /* Get founder public key */
1357 tmp = silc_argument_get_arg_type(args, 4, &len);
1359 silc_public_key_payload_decode(tmp, len, &public_key);
1361 /* Get user limit */
1362 tmp = silc_argument_get_arg_type(args, 6, &len);
1363 if (tmp && len == 4)
1364 SILC_GET32_MSB(channel->user_limit, tmp);
1365 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1366 channel->user_limit = 0;
1368 /* Get channel public key(s) */
1369 tmp = silc_argument_get_arg_type(args, 5, &len);
1372 silc_argument_list_parse_decoded(tmp, len, SILC_ARGUMENT_PUBLIC_KEY);
1374 /* Notify application */
1375 silc_client_command_callback(cmd, channel, mode, public_key,
1376 channel_pubkeys, channel->user_limit);
1378 silc_argument_list_free(channel_pubkeys, SILC_ARGUMENT_PUBLIC_KEY);
1382 silc_pkcs_public_key_free(public_key);
1383 silc_fsm_next(fsm, silc_client_command_reply_processed);
1384 return SILC_FSM_CONTINUE;
1387 /********************************** CUMODE **********************************/
1389 /* Received reply for CUMODE command */
1391 SILC_FSM_STATE(silc_client_command_reply_cumode)
1393 SilcClientCommandContext cmd = fsm_context;
1394 SilcClientConnection conn = cmd->conn;
1395 SilcClient client = conn->client;
1396 SilcCommandPayload payload = state_context;
1397 SilcArgumentPayload args = silc_command_get_args(payload);
1398 SilcClientEntry client_entry;
1399 SilcChannelEntry channel;
1400 SilcChannelUser chu;
1401 unsigned char *modev;
1402 SilcUInt32 len, mode;
1406 CHECK_STATUS("Cannot change mode: ");
1409 /* Get channel mode */
1410 modev = silc_argument_get_arg_type(args, 2, &len);
1411 if (!modev || len != 4) {
1412 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1415 SILC_GET32_MSB(mode, modev);
1417 /* Take Channel ID */
1418 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1419 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1423 /* Get the channel entry */
1424 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1426 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1431 if (!silc_argument_get_decoded(args, 4, SILC_ARGUMENT_ID, &id, NULL)) {
1432 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1436 /* Get client entry */
1437 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1438 if (!client_entry) {
1439 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1444 chu = silc_client_on_channel(channel, client_entry);
1448 /* Notify application */
1449 silc_client_command_callback(cmd, mode, channel, client_entry);
1451 silc_client_unref_client(client, conn, client_entry);
1454 silc_fsm_next(fsm, silc_client_command_reply_processed);
1455 return SILC_FSM_CONTINUE;
1458 /********************************** KICK ************************************/
1460 SILC_FSM_STATE(silc_client_command_reply_kick)
1462 SilcClientCommandContext cmd = fsm_context;
1463 SilcClientConnection conn = cmd->conn;
1464 SilcClient client = conn->client;
1465 SilcCommandPayload payload = state_context;
1466 SilcArgumentPayload args = silc_command_get_args(payload);
1467 SilcClientEntry client_entry;
1468 SilcChannelEntry channel;
1472 CHECK_STATUS("Cannot kick: ");
1475 /* Take Channel ID */
1476 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1477 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1481 /* Get the channel entry */
1482 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1484 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1489 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1490 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1494 /* Get client entry */
1495 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1496 if (!client_entry) {
1497 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1501 /* Notify application */
1502 silc_client_command_callback(cmd, channel, client_entry);
1504 silc_client_unref_client(client, conn, client_entry);
1507 silc_fsm_next(fsm, silc_client_command_reply_processed);
1508 return SILC_FSM_CONTINUE;
1511 /******************************** SILCOPER **********************************/
1513 SILC_FSM_STATE(silc_client_command_reply_silcoper)
1515 SilcClientCommandContext cmd = fsm_context;
1516 SilcCommandPayload payload = state_context;
1517 SilcArgumentPayload args = silc_command_get_args(payload);
1520 CHECK_STATUS("Cannot change mode: ");
1523 /* Notify application */
1524 silc_client_command_callback(cmd);
1526 silc_fsm_next(fsm, silc_client_command_reply_processed);
1527 return SILC_FSM_CONTINUE;
1530 /********************************** OPER ************************************/
1532 SILC_FSM_STATE(silc_client_command_reply_oper)
1534 SilcClientCommandContext cmd = fsm_context;
1535 SilcCommandPayload payload = state_context;
1536 SilcArgumentPayload args = silc_command_get_args(payload);
1539 CHECK_STATUS("Cannot change mode: ");
1542 /* Notify application */
1543 silc_client_command_callback(cmd);
1545 silc_fsm_next(fsm, silc_client_command_reply_processed);
1546 return SILC_FSM_CONTINUE;
1549 /********************************* DETACH ***********************************/
1551 SILC_FSM_STATE(silc_client_command_reply_detach)
1553 SilcClientCommandContext cmd = fsm_context;
1554 SilcClientConnection conn = cmd->conn;
1555 SilcClient client = conn->client;
1556 SilcCommandPayload payload = state_context;
1557 SilcArgumentPayload args = silc_command_get_args(payload);
1561 CHECK_STATUS("Cannot detach: ");
1564 /* Notify application */
1565 silc_client_command_callback(cmd);
1568 /* Generate the detachment data and deliver it to the client in the
1569 detach client operation */
1570 detach = silc_client_get_detach_data(client, conn);
1572 client->internal->ops->detach(client, conn, silc_buffer_data(detach),
1573 silc_buffer_len(detach));
1574 silc_buffer_free(detach);
1578 silc_fsm_next(fsm, silc_client_command_reply_processed);
1579 return SILC_FSM_CONTINUE;
1582 /********************************** WATCH ***********************************/
1584 SILC_FSM_STATE(silc_client_command_reply_watch)
1586 SilcClientCommandContext cmd = fsm_context;
1587 SilcCommandPayload payload = state_context;
1588 SilcArgumentPayload args = silc_command_get_args(payload);
1591 CHECK_STATUS("Cannot set watch: ");
1594 /* Notify application */
1595 silc_client_command_callback(cmd);
1597 silc_fsm_next(fsm, silc_client_command_reply_processed);
1598 return SILC_FSM_CONTINUE;
1601 /*********************************** BAN ************************************/
1603 SILC_FSM_STATE(silc_client_command_reply_ban)
1605 SilcClientCommandContext cmd = fsm_context;
1606 SilcClientConnection conn = cmd->conn;
1607 SilcClient client = conn->client;
1608 SilcCommandPayload payload = state_context;
1609 SilcArgumentPayload args = silc_command_get_args(payload);
1610 SilcChannelEntry channel;
1613 SilcBufferStruct buf;
1617 CHECK_STATUS("Cannot set ban: ");
1620 /* Take Channel ID */
1621 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1622 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1626 /* Get the channel entry */
1627 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1629 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1633 /* Get the ban list */
1634 tmp = silc_argument_get_arg_type(args, 3, &len);
1636 silc_buffer_set(&buf, tmp, len);
1638 /* Notify application */
1639 silc_client_command_callback(cmd, channel, tmp ? &buf : NULL);
1642 silc_fsm_next(fsm, silc_client_command_reply_processed);
1643 return SILC_FSM_CONTINUE;
1646 /********************************** LEAVE ***********************************/
1648 /* Reply to LEAVE command. */
1650 SILC_FSM_STATE(silc_client_command_reply_leave)
1652 SilcClientCommandContext cmd = fsm_context;
1653 SilcClientConnection conn = cmd->conn;
1654 SilcClient client = conn->client;
1655 SilcCommandPayload payload = state_context;
1656 SilcArgumentPayload args = silc_command_get_args(payload);
1657 SilcChannelEntry channel;
1661 CHECK_STATUS("Cannot set leave: ");
1664 /* Get Channel ID */
1665 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1666 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1670 /* Get the channel entry */
1671 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1673 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1677 /* Remove us from this channel. */
1678 silc_client_remove_from_channel(channel, conn->local_entry);
1680 /* Notify application */
1681 silc_client_command_callback(cmd, channel);
1683 /* Now delete the channel. */
1684 silc_client_del_channel(client, conn, channel);
1687 silc_fsm_next(fsm, silc_client_command_reply_processed);
1688 return SILC_FSM_CONTINUE;
1691 /********************************* USERS ************************************/
1693 /* Continue USERS command after resolving unknown users */
1696 silc_client_command_reply_users_resolved(SilcClient client,
1697 SilcClientConnection conn,
1702 SilcClientCommandContext cmd = context;
1703 SILC_FSM_CALL_CONTINUE(&cmd->thread);
1706 /* Continue USERS command after resolving unknown channel */
1709 silc_client_command_reply_users_continue(SilcClient client,
1710 SilcClientConnection conn,
1715 SilcClientCommandContext cmd = context;
1718 SilcCommandPayload payload = silc_fsm_get_state_context(&cmd->thread);
1719 SilcArgumentPayload args = silc_command_get_args(payload);
1721 cmd->status = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1722 ERROR_CALLBACK(cmd->status);
1723 silc_fsm_next(&cmd->thread, silc_client_command_reply_processed);
1726 SILC_FSM_CALL_CONTINUE(&cmd->thread);
1729 /* Reply to USERS command. Received list of client ID's and theirs modes
1730 on the channel we requested. */
1732 SILC_FSM_STATE(silc_client_command_reply_users)
1734 SilcClientCommandContext cmd = fsm_context;
1735 SilcClientConnection conn = cmd->conn;
1736 SilcClient client = conn->client;
1737 SilcCommandPayload payload = state_context;
1738 SilcArgumentPayload args = silc_command_get_args(payload);
1740 SilcUInt32 tmp_len, list_count;
1741 SilcUInt16 idp_len, mode;
1742 SilcHashTableList htl;
1743 SilcBufferStruct client_id_list, client_mode_list;
1744 SilcChannelEntry channel;
1745 SilcClientEntry client_entry;
1750 CHECK_STATUS("Cannot get users: ");
1753 /* Get channel ID */
1754 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1755 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1759 /* Get channel entry */
1760 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1762 /* Resolve the channel from server */
1763 SILC_FSM_CALL(silc_client_get_channel_by_id_resolve(
1764 client, conn, &id.u.channel_id,
1765 silc_client_command_reply_users_continue, cmd));
1769 /* Get the list count */
1770 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1771 if (!tmp || tmp_len != 4) {
1772 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1775 SILC_GET32_MSB(list_count, tmp);
1777 /* Get Client ID list */
1778 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1780 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1783 silc_buffer_set(&client_id_list, tmp, tmp_len);
1785 /* Resolve users we do not know about */
1786 if (!cmd->resolved) {
1787 cmd->resolved = TRUE;
1788 SILC_FSM_CALL(silc_client_get_clients_by_list(
1789 client, conn, list_count, &client_id_list,
1790 silc_client_command_reply_users_resolved, cmd));
1794 /* Get client mode list */
1795 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1797 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1800 silc_buffer_set(&client_mode_list, tmp, tmp_len);
1802 SILC_LOG_DEBUG(("channel %s, %d users", channel->channel_name, list_count));
1804 /* Cache the received Client ID's and modes. */
1805 for (i = 0; i < list_count; i++) {
1806 SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1808 if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1812 SILC_GET32_MSB(mode, client_mode_list.data);
1814 /* Save the client on this channel. Unknown clients are ignored as they
1815 clearly do not exist since the resolving didn't find them. */
1816 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1818 silc_client_add_to_channel(channel, client_entry, mode);
1819 silc_client_unref_client(client, conn, client_entry);
1821 if (!silc_buffer_pull(&client_id_list, idp_len))
1823 if (!silc_buffer_pull(&client_mode_list, 4))
1827 /* Notify application */
1828 silc_hash_table_list(channel->user_list, &htl);
1829 silc_client_command_callback(cmd, channel, &htl);
1830 silc_hash_table_list_reset(&htl);
1833 silc_fsm_next(fsm, silc_client_command_reply_processed);
1834 return SILC_FSM_CONTINUE;
1837 /********************************** GETKEY **********************************/
1839 /* Received command reply to GETKEY command. WE've received the remote
1840 client's public key. */
1842 SILC_FSM_STATE(silc_client_command_reply_getkey)
1844 SilcClientCommandContext cmd = fsm_context;
1845 SilcClientConnection conn = cmd->conn;
1846 SilcClient client = conn->client;
1847 SilcCommandPayload payload = state_context;
1848 SilcArgumentPayload args = silc_command_get_args(payload);
1849 SilcClientEntry client_entry;
1850 SilcServerEntry server_entry;
1853 SilcPublicKey public_key;
1857 CHECK_STATUS("Cannot get key: ");
1861 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1862 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1866 /* Get the public key */
1867 tmp = silc_argument_get_arg_type(args, 3, &len);
1869 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1872 if (!silc_public_key_payload_decode(tmp, len, &public_key)) {
1873 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1877 if (id.type == SILC_ID_CLIENT) {
1878 /* Received client's public key */
1879 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1880 if (!client_entry) {
1881 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1885 /* Save fingerprint */
1886 if (!client_entry->fingerprint)
1887 silc_hash_make(client->sha1hash, tmp + 4, len - 4,
1888 client_entry->fingerprint);
1889 if (!client_entry->public_key) {
1890 client_entry->public_key = public_key;
1894 /* Notify application */
1895 silc_client_command_callback(cmd, SILC_ID_CLIENT, client_entry,
1896 client_entry->public_key);
1897 silc_client_unref_client(client, conn, client_entry);
1898 } else if (id.type == SILC_ID_SERVER) {
1899 /* Received server's public key */
1900 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
1901 if (!server_entry) {
1902 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1906 if (!server_entry->public_key) {
1907 server_entry->public_key = public_key;
1911 /* Notify application */
1912 silc_client_command_callback(cmd, SILC_ID_SERVER, server_entry,
1913 server_entry->public_key);
1918 silc_pkcs_public_key_free(public_key);
1919 silc_fsm_next(fsm, silc_client_command_reply_processed);
1920 return SILC_FSM_CONTINUE;
1923 /********************************** SERVICE *********************************/
1925 /* Reply to SERVICE command. */
1926 /* XXX incomplete */
1928 SILC_FSM_STATE(silc_client_command_reply_service)
1930 SilcClientCommandContext cmd = fsm_context;
1931 SilcCommandPayload payload = state_context;
1932 SilcArgumentPayload args = silc_command_get_args(payload);
1934 unsigned char *service_list, *name;
1937 CHECK_STATUS("Cannot get service: ");
1939 /* Get service list */
1940 service_list = silc_argument_get_arg_type(args, 2, &tmp_len);
1942 /* Get requested service name */
1943 name = silc_argument_get_arg_type(args, 3, &tmp_len);
1945 /* Notify application */
1946 silc_client_command_callback(cmd, service_list, name);
1948 silc_fsm_next(fsm, silc_client_command_reply_processed);
1949 return SILC_FSM_CONTINUE;
1952 /*********************************** QUIT ***********************************/
1954 /* QUIT command reply stub */
1956 SILC_FSM_STATE(silc_client_command_reply_quit)
1958 silc_fsm_next(fsm, silc_client_command_reply_processed);
1959 return SILC_FSM_CONTINUE;