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 /** Bad reply payload */
139 SILC_LOG_DEBUG(("Bad command reply packet"));
140 silc_fsm_next(fsm, silc_client_connection_st_packet);
141 return SILC_FSM_CONTINUE;
144 cmd_ident = silc_command_get_ident(payload);
145 command = silc_command_get(payload);
147 /* Find the command pending reply */
148 silc_mutex_lock(conn->internal->lock);
149 silc_list_start(conn->internal->pending_commands);
150 while ((cmd = silc_list_get(conn->internal->pending_commands)))
151 if (cmd->cmd == command && cmd->cmd_ident == cmd_ident)
153 silc_mutex_unlock(conn->internal->lock);
156 /** Unknown command reply */
157 SILC_LOG_DEBUG(("Unknown command reply"));
158 silc_command_payload_free(payload);
159 silc_fsm_next(fsm, silc_client_connection_st_packet);
160 return SILC_FSM_CONTINUE;
163 /* Signal command thread that command reply has arrived */
164 silc_fsm_set_state_context(&cmd->thread, payload);
165 silc_fsm_next(&cmd->thread, silc_client_command_reply_process);
166 silc_fsm_continue_sync(&cmd->thread);
168 /** Packet processed */
169 silc_fsm_next(fsm, silc_client_connection_st_packet);
170 return SILC_FSM_CONTINUE;
173 /* Wait here for command reply to arrive from remote host */
175 SILC_FSM_STATE(silc_client_command_reply_wait)
177 SilcClientCommandContext cmd = fsm_context;
179 SILC_LOG_DEBUG(("Wait for command reply"));
181 /** Wait for command reply */
182 cmd->processed = FALSE;
183 silc_fsm_set_state_context(fsm, NULL);
184 silc_fsm_next_later(fsm, silc_client_command_reply_process, 20, 0);
185 return SILC_FSM_WAIT;
188 /* Process received command reply payload */
190 SILC_FSM_STATE(silc_client_command_reply_process)
192 SilcClientCommandContext cmd = fsm_context;
193 SilcCommandPayload payload = state_context;
196 /* Timeout, reply not received in timely fashion */
197 SilcArgumentPayload args = NULL;
198 ERROR_CALLBACK(SILC_STATUS_ERR_TIMEDOUT);
199 return SILC_FSM_FINISH;
202 if (cmd->processed) {
203 /* Command reply processed */
204 silc_command_payload_free(payload);
206 if (cmd->status == SILC_STATUS_OK || cmd->status == SILC_STATUS_LIST_END ||
207 SILC_STATUS_IS_ERROR(cmd->status))
208 return SILC_FSM_FINISH;
210 /** Wait more command payloads */
211 silc_fsm_next(fsm, silc_client_command_reply_wait);
212 return SILC_FSM_CONTINUE;
215 silc_command_get_status(payload, &cmd->status, &cmd->error);
216 silc_fsm_set_state_context(fsm, payload);
217 cmd->processed = TRUE;
220 case SILC_COMMAND_WHOIS:
222 silc_fsm_next(fsm, silc_client_command_reply_whois);
224 case SILC_COMMAND_WHOWAS:
226 silc_fsm_next(fsm, silc_client_command_reply_whowas);
228 case SILC_COMMAND_IDENTIFY:
230 silc_fsm_next(fsm, silc_client_command_reply_identify);
232 case SILC_COMMAND_NICK:
234 silc_fsm_next(fsm, silc_client_command_reply_nick);
236 case SILC_COMMAND_LIST:
238 silc_fsm_next(fsm, silc_client_command_reply_list);
240 case SILC_COMMAND_TOPIC:
242 silc_fsm_next(fsm, silc_client_command_reply_topic);
244 case SILC_COMMAND_INVITE:
246 silc_fsm_next(fsm, silc_client_command_reply_invite);
248 case SILC_COMMAND_QUIT:
250 silc_fsm_next(fsm, silc_client_command_reply_quit);
252 case SILC_COMMAND_KILL:
254 silc_fsm_next(fsm, silc_client_command_reply_kill);
256 case SILC_COMMAND_INFO:
258 silc_fsm_next(fsm, silc_client_command_reply_info);
260 case SILC_COMMAND_STATS:
262 silc_fsm_next(fsm, silc_client_command_reply_stats);
264 case SILC_COMMAND_PING:
266 silc_fsm_next(fsm, silc_client_command_reply_ping);
268 case SILC_COMMAND_OPER:
270 silc_fsm_next(fsm, silc_client_command_reply_oper);
272 case SILC_COMMAND_JOIN:
274 silc_fsm_next(fsm, silc_client_command_reply_join);
276 case SILC_COMMAND_MOTD:
278 silc_fsm_next(fsm, silc_client_command_reply_motd);
280 case SILC_COMMAND_UMODE:
282 silc_fsm_next(fsm, silc_client_command_reply_umode);
284 case SILC_COMMAND_CMODE:
286 silc_fsm_next(fsm, silc_client_command_reply_cmode);
288 case SILC_COMMAND_CUMODE:
290 silc_fsm_next(fsm, silc_client_command_reply_cumode);
292 case SILC_COMMAND_KICK:
294 silc_fsm_next(fsm, silc_client_command_reply_kick);
296 case SILC_COMMAND_BAN:
298 silc_fsm_next(fsm, silc_client_command_reply_ban);
300 case SILC_COMMAND_DETACH:
302 silc_fsm_next(fsm, silc_client_command_reply_detach);
304 case SILC_COMMAND_WATCH:
306 silc_fsm_next(fsm, silc_client_command_reply_watch);
308 case SILC_COMMAND_SILCOPER:
310 silc_fsm_next(fsm, silc_client_command_reply_silcoper);
312 case SILC_COMMAND_LEAVE:
314 silc_fsm_next(fsm, silc_client_command_reply_leave);
316 case SILC_COMMAND_USERS:
318 silc_fsm_next(fsm, silc_client_command_reply_users);
320 case SILC_COMMAND_GETKEY:
322 silc_fsm_next(fsm, silc_client_command_reply_getkey);
324 case SILC_COMMAND_SERVICE:
326 silc_fsm_next(fsm, silc_client_command_reply_service);
329 return SILC_FSM_FINISH;
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_process);
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_process);
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->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_process);
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_process);
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_process);
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_process);
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_process);
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_process);
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_process);
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_process);
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_process);
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_process);
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 SilcChannelUser chu;
1033 SilcUInt32 mode = 0, len, list_count;
1034 char *topic, *tmp, *channel_name = NULL, *hmac;
1035 SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
1036 SilcBufferStruct chpklist;
1041 CHECK_STATUS("Cannot join channel: ");
1044 /* Get channel name */
1045 channel_name = silc_argument_get_arg_type(args, 2, NULL);
1046 if (!channel_name) {
1047 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1051 /* Get Channel ID */
1052 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1053 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1057 /* Get channel mode */
1058 tmp = silc_argument_get_arg_type(args, 5, NULL);
1060 SILC_GET32_MSB(mode, tmp);
1062 /* Get channel key */
1063 tmp = silc_argument_get_arg_type(args, 7, &len);
1065 keyp = silc_buffer_alloc_size(len);
1067 silc_buffer_put(keyp, tmp, len);
1071 topic = silc_argument_get_arg_type(args, 10, NULL);
1073 /* Check whether we have this channel entry already. */
1074 channel = silc_client_get_channel(client, conn, channel_name);
1076 if (!SILC_ID_CHANNEL_COMPARE(channel->id, &id.u.channel_id))
1077 silc_client_replace_channel_id(client, conn, channel, &id.u.channel_id);
1079 /* Create new channel entry */
1080 channel = silc_client_add_channel(client, conn, channel_name,
1081 mode, &id.u.channel_id);
1083 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_CHANNEL);
1088 conn->current_channel = channel;
1089 channel->mode = mode;
1092 hmac = silc_argument_get_arg_type(args, 11, NULL);
1094 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
1096 SAY(client, conn, SILC_CLIENT_MESSAGE_ERROR,
1097 "Cannot join channel: Unsupported HMAC `%s'", hmac);
1098 ERROR_CALLBACK(SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
1103 /* Get the list count */
1104 tmp = silc_argument_get_arg_type(args, 12, &len);
1106 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1109 SILC_GET32_MSB(list_count, tmp);
1111 /* Get Client ID list */
1112 tmp = silc_argument_get_arg_type(args, 13, &len);
1114 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1118 client_id_list = silc_buffer_alloc_size(len);
1120 silc_buffer_put(client_id_list, tmp, len);
1122 /* Get client mode list */
1123 tmp = silc_argument_get_arg_type(args, 14, &len);
1125 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1129 client_mode_list = silc_buffer_alloc_size(len);
1130 if (client_mode_list)
1131 silc_buffer_put(client_mode_list, tmp, len);
1133 /* Add clients we received in the reply to the channel */
1134 for (i = 0; i < list_count; i++) {
1138 SilcClientEntry client_entry;
1141 SILC_GET16_MSB(idp_len, client_id_list->data + 2);
1143 if (!silc_id_payload_parse_id(client_id_list->data, idp_len, &id))
1147 SILC_GET32_MSB(mode, client_mode_list->data);
1149 /* Check if we have this client cached already. */
1150 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1151 if (!client_entry) {
1152 /* No, we don't have it, add entry for it. */
1154 silc_client_add_client(client, conn, NULL, NULL, NULL,
1155 &id.u.client_id, 0);
1160 /* Join client to the channel */
1161 if (!silc_client_on_channel(channel, client_entry)) {
1162 chu = silc_calloc(1, sizeof(*chu));
1164 silc_client_unref_client(client, conn, client_entry);
1167 chu->client = client_entry;
1168 chu->channel = channel;
1170 silc_hash_table_add(channel->user_list, client_entry, chu);
1171 silc_hash_table_add(client_entry->channels, channel, chu);
1173 silc_client_unref_client(client, conn, client_entry);
1175 if (!silc_buffer_pull(client_id_list, idp_len))
1177 if (!silc_buffer_pull(client_mode_list, 4))
1180 silc_buffer_start(client_id_list);
1181 silc_buffer_start(client_mode_list);
1183 /* Save channel key */
1186 silc_client_save_channel_key(client, conn, keyp, channel);
1189 /* Get founder key */
1190 tmp = silc_argument_get_arg_type(args, 15, &len);
1192 if (channel->founder_key)
1193 silc_pkcs_public_key_free(channel->founder_key);
1194 channel->founder_key = NULL;
1195 silc_public_key_payload_decode(tmp, len, &channel->founder_key);
1198 /* Get user limit */
1199 tmp = silc_argument_get_arg_type(args, 17, &len);
1200 if (tmp && len == 4)
1201 SILC_GET32_MSB(channel->user_limit, tmp);
1202 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1203 channel->user_limit = 0;
1205 /* Get channel public key list */
1206 tmp = silc_argument_get_arg_type(args, 16, &len);
1208 silc_buffer_set(&chpklist, tmp, len);
1211 silc_free(channel->topic);
1212 channel->topic = silc_memdup(topic, strlen(topic));
1215 /* Notify application */
1216 silc_client_command_callback(cmd, channel_name, channel, mode, 0,
1217 keyp ? keyp->head : NULL, NULL,
1218 NULL, topic, hmac, list_count, client_id_list,
1219 client_mode_list, channel->founder_key,
1220 tmp ? &chpklist : NULL, channel->user_limit);
1223 silc_buffer_free(keyp);
1224 silc_buffer_free(client_id_list);
1225 silc_buffer_free(client_mode_list);
1226 silc_fsm_next(fsm, silc_client_command_reply_process);
1227 return SILC_FSM_CONTINUE;
1230 /********************************** MOTD ************************************/
1232 /* Received reply for MOTD command */
1234 SILC_FSM_STATE(silc_client_command_reply_motd)
1236 SilcClientCommandContext cmd = fsm_context;
1237 SilcClientConnection conn = cmd->conn;
1238 SilcClient client = conn->client;
1239 SilcCommandPayload payload = state_context;
1240 SilcArgumentPayload args = silc_command_get_args(payload);
1242 char *motd = NULL, *cp, line[256];
1245 CHECK_STATUS("Cannot get motd: ");
1248 if (silc_argument_get_arg_num(args) == 3) {
1249 motd = silc_argument_get_arg_type(args, 3, NULL);
1251 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1258 if (cp[i++] == '\n') {
1259 memset(line, 0, sizeof(line));
1260 silc_strncat(line, sizeof(line), cp, i - 1);
1267 SAY(client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", line);
1276 /* Notify application */
1277 silc_client_command_callback(cmd, motd);
1280 silc_fsm_next(fsm, silc_client_command_reply_process);
1281 return SILC_FSM_CONTINUE;
1284 /********************************** UMODE ***********************************/
1286 /* Received reply tot he UMODE command. Save the current user mode */
1288 SILC_FSM_STATE(silc_client_command_reply_umode)
1290 SilcClientCommandContext cmd = fsm_context;
1291 SilcClientConnection conn = cmd->conn;
1292 SilcCommandPayload payload = state_context;
1293 SilcArgumentPayload args = silc_command_get_args(payload);
1295 SilcUInt32 mode, len;
1298 CHECK_STATUS("Cannot change mode: ");
1301 tmp = silc_argument_get_arg_type(args, 2, &len);
1302 if (!tmp || len != 4) {
1303 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1307 SILC_GET32_MSB(mode, tmp);
1308 conn->local_entry->mode = mode;
1310 /* Notify application */
1311 silc_client_command_callback(cmd, mode);
1314 silc_fsm_next(fsm, silc_client_command_reply_process);
1315 return SILC_FSM_CONTINUE;
1318 /********************************** CMODE ***********************************/
1320 /* Received reply for CMODE command. */
1322 SILC_FSM_STATE(silc_client_command_reply_cmode)
1324 SilcClientCommandContext cmd = fsm_context;
1325 SilcClientConnection conn = cmd->conn;
1326 SilcClient client = conn->client;
1327 SilcCommandPayload payload = state_context;
1328 SilcArgumentPayload args = silc_command_get_args(payload);
1331 SilcChannelEntry channel;
1333 SilcPublicKey public_key = NULL;
1334 SilcBufferStruct channel_pubkeys;
1338 CHECK_STATUS("Cannot change mode: ");
1341 /* Take Channel ID */
1342 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1343 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1347 /* Get the channel entry */
1348 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1350 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1354 /* Get channel mode */
1355 tmp = silc_argument_get_arg_type(args, 3, &len);
1356 if (!tmp || len != 4) {
1357 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1362 SILC_GET32_MSB(mode, tmp);
1363 channel->mode = mode;
1365 /* Get founder public key */
1366 tmp = silc_argument_get_arg_type(args, 4, &len);
1368 silc_public_key_payload_decode(tmp, len, &public_key);
1370 /* Get user limit */
1371 tmp = silc_argument_get_arg_type(args, 6, &len);
1372 if (tmp && len == 4)
1373 SILC_GET32_MSB(channel->user_limit, tmp);
1374 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1375 channel->user_limit = 0;
1377 /* Get channel public key(s) */
1378 tmp = silc_argument_get_arg_type(args, 5, &len);
1380 silc_buffer_set(&channel_pubkeys, tmp, len);
1382 /* Notify application */
1383 silc_client_command_callback(cmd, channel, mode, public_key,
1384 tmp ? &channel_pubkeys : NULL,
1385 channel->user_limit);
1389 silc_pkcs_public_key_free(public_key);
1390 silc_fsm_next(fsm, silc_client_command_reply_process);
1391 return SILC_FSM_CONTINUE;
1394 /********************************** CUMODE **********************************/
1396 /* Received reply for CUMODE command */
1398 SILC_FSM_STATE(silc_client_command_reply_cumode)
1400 SilcClientCommandContext cmd = fsm_context;
1401 SilcClientConnection conn = cmd->conn;
1402 SilcClient client = conn->client;
1403 SilcCommandPayload payload = state_context;
1404 SilcArgumentPayload args = silc_command_get_args(payload);
1405 SilcClientEntry client_entry;
1406 SilcChannelEntry channel;
1407 SilcChannelUser chu;
1408 unsigned char *modev;
1409 SilcUInt32 len, mode;
1413 CHECK_STATUS("Cannot change mode: ");
1416 /* Get channel mode */
1417 modev = silc_argument_get_arg_type(args, 2, &len);
1418 if (!modev || len != 4) {
1419 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1422 SILC_GET32_MSB(mode, modev);
1424 /* Take Channel ID */
1425 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1426 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1430 /* Get the channel entry */
1431 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1433 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1438 if (!silc_argument_get_decoded(args, 4, SILC_ARGUMENT_ID, &id, NULL)) {
1439 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1443 /* Get client entry */
1444 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1445 if (!client_entry) {
1446 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1451 chu = silc_client_on_channel(channel, client_entry);
1455 /* Notify application */
1456 silc_client_command_callback(cmd, mode, channel, client_entry);
1458 silc_client_unref_client(client, conn, client_entry);
1461 silc_fsm_next(fsm, silc_client_command_reply_process);
1462 return SILC_FSM_CONTINUE;
1465 /********************************** KICK ************************************/
1467 SILC_FSM_STATE(silc_client_command_reply_kick)
1469 SilcClientCommandContext cmd = fsm_context;
1470 SilcClientConnection conn = cmd->conn;
1471 SilcClient client = conn->client;
1472 SilcCommandPayload payload = state_context;
1473 SilcArgumentPayload args = silc_command_get_args(payload);
1474 SilcClientEntry client_entry;
1475 SilcChannelEntry channel;
1479 CHECK_STATUS("Cannot kick: ");
1482 /* Take Channel ID */
1483 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1484 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1488 /* Get the channel entry */
1489 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1491 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1496 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1497 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1501 /* Get client entry */
1502 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1503 if (!client_entry) {
1504 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1508 /* Notify application */
1509 silc_client_command_callback(cmd, channel, client_entry);
1511 silc_client_unref_client(client, conn, client_entry);
1514 silc_fsm_next(fsm, silc_client_command_reply_process);
1515 return SILC_FSM_CONTINUE;
1518 /******************************** SILCOPER **********************************/
1520 SILC_FSM_STATE(silc_client_command_reply_silcoper)
1522 SilcClientCommandContext cmd = fsm_context;
1523 SilcCommandPayload payload = state_context;
1524 SilcArgumentPayload args = silc_command_get_args(payload);
1527 CHECK_STATUS("Cannot change mode: ");
1530 /* Notify application */
1531 silc_client_command_callback(cmd);
1533 silc_fsm_next(fsm, silc_client_command_reply_process);
1534 return SILC_FSM_CONTINUE;
1537 /********************************** OPER ************************************/
1539 SILC_FSM_STATE(silc_client_command_reply_oper)
1541 SilcClientCommandContext cmd = fsm_context;
1542 SilcCommandPayload payload = state_context;
1543 SilcArgumentPayload args = silc_command_get_args(payload);
1546 CHECK_STATUS("Cannot change mode: ");
1549 /* Notify application */
1550 silc_client_command_callback(cmd);
1552 silc_fsm_next(fsm, silc_client_command_reply_process);
1553 return SILC_FSM_CONTINUE;
1556 /********************************* DETACH ***********************************/
1558 SILC_FSM_STATE(silc_client_command_reply_detach)
1560 SilcClientCommandContext cmd = fsm_context;
1561 SilcClientConnection conn = cmd->conn;
1562 SilcClient client = conn->client;
1563 SilcCommandPayload payload = state_context;
1564 SilcArgumentPayload args = silc_command_get_args(payload);
1568 CHECK_STATUS("Cannot detach: ");
1571 /* Notify application */
1572 silc_client_command_callback(cmd);
1575 /* Generate the detachment data and deliver it to the client in the
1576 detach client operation */
1577 detach = silc_client_get_detach_data(client, conn);
1579 client->internal->ops->detach(client, conn, silc_buffer_data(detach),
1580 silc_buffer_len(detach));
1581 silc_buffer_free(detach);
1585 silc_fsm_next(fsm, silc_client_command_reply_process);
1586 return SILC_FSM_CONTINUE;
1589 /********************************** WATCH ***********************************/
1591 SILC_FSM_STATE(silc_client_command_reply_watch)
1593 SilcClientCommandContext cmd = fsm_context;
1594 SilcCommandPayload payload = state_context;
1595 SilcArgumentPayload args = silc_command_get_args(payload);
1598 CHECK_STATUS("Cannot set watch: ");
1601 /* Notify application */
1602 silc_client_command_callback(cmd);
1604 silc_fsm_next(fsm, silc_client_command_reply_process);
1605 return SILC_FSM_CONTINUE;
1608 /*********************************** BAN ************************************/
1610 SILC_FSM_STATE(silc_client_command_reply_ban)
1612 SilcClientCommandContext cmd = fsm_context;
1613 SilcClientConnection conn = cmd->conn;
1614 SilcClient client = conn->client;
1615 SilcCommandPayload payload = state_context;
1616 SilcArgumentPayload args = silc_command_get_args(payload);
1617 SilcChannelEntry channel;
1620 SilcBufferStruct buf;
1624 CHECK_STATUS("Cannot set ban: ");
1627 /* Take Channel ID */
1628 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1629 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1633 /* Get the channel entry */
1634 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1636 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1640 /* Get the ban list */
1641 tmp = silc_argument_get_arg_type(args, 3, &len);
1643 silc_buffer_set(&buf, tmp, len);
1645 /* Notify application */
1646 silc_client_command_callback(cmd, channel, tmp ? &buf : NULL);
1649 silc_fsm_next(fsm, silc_client_command_reply_process);
1650 return SILC_FSM_CONTINUE;
1653 /********************************** LEAVE ***********************************/
1655 /* Reply to LEAVE command. */
1657 SILC_FSM_STATE(silc_client_command_reply_leave)
1659 SilcClientCommandContext cmd = fsm_context;
1660 SilcClientConnection conn = cmd->conn;
1661 SilcClient client = conn->client;
1662 SilcCommandPayload payload = state_context;
1663 SilcArgumentPayload args = silc_command_get_args(payload);
1664 SilcChannelEntry channel;
1665 SilcChannelUser chu;
1669 CHECK_STATUS("Cannot set leave: ");
1672 /* Get Channel ID */
1673 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1674 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1678 /* Get the channel entry */
1679 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1681 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1685 /* Remove us from this channel. */
1686 chu = silc_client_on_channel(channel, conn->local_entry);
1688 silc_hash_table_del(chu->client->channels, chu->channel);
1689 silc_hash_table_del(chu->channel->user_list, chu->client);
1693 /* Notify application */
1694 silc_client_command_callback(cmd, channel);
1696 /* Now delete the channel. */
1697 silc_client_del_channel(client, conn, channel);
1700 silc_fsm_next(fsm, silc_client_command_reply_process);
1701 return SILC_FSM_CONTINUE;
1704 /********************************* USERS ************************************/
1707 silc_client_command_reply_users_continue(SilcClient client,
1708 SilcClientConnection conn,
1709 SilcCommand command,
1715 SilcClientCommandContext cmd = context;
1720 /* Continue USERS command after resolving unknown users */
1723 silc_client_command_reply_users_resolved(SilcClient client,
1724 SilcClientConnection conn,
1729 SilcClientCommandContext cmd = context;
1730 SILC_FSM_CALL_CONTINUE_SYNC(&cmd->thread);
1733 /* Reply to USERS command. Received list of client ID's and theirs modes
1734 on the channel we requested. */
1736 SILC_FSM_STATE(silc_client_command_reply_users)
1738 SilcClientCommandContext cmd = fsm_context;
1739 SilcClientConnection conn = cmd->conn;
1740 SilcClient client = conn->client;
1741 SilcCommandPayload payload = state_context;
1742 SilcArgumentPayload args = silc_command_get_args(payload);
1744 SilcUInt32 tmp_len, list_count;
1745 SilcUInt16 idp_len, mode;
1746 SilcHashTableList htl;
1747 SilcBufferStruct client_id_list, client_mode_list;
1748 SilcChannelEntry channel;
1749 SilcClientEntry client_entry;
1750 SilcChannelUser chu;
1755 CHECK_STATUS("Cannot get users: ");
1758 /* Get channel ID */
1759 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1760 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1764 /* Get channel entry */
1765 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1767 /* Resolve the channel from server */
1769 SILC_FSM_CALL(silc_client_get_channel_by_id_resolve(
1770 client, conn, &id.u.channel_id,
1771 silc_client_command_reply_users_continue, cmd));
1776 /* Get the list count */
1777 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1778 if (!tmp || tmp_len != 4) {
1779 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1782 SILC_GET32_MSB(list_count, tmp);
1784 /* Get Client ID list */
1785 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1787 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1790 silc_buffer_set(&client_id_list, tmp, tmp_len);
1792 /* Resolve users we do not know about */
1793 if (!cmd->resolved) {
1794 cmd->resolved = TRUE;
1795 SILC_FSM_CALL(silc_client_get_clients_by_list(
1796 client, conn, list_count, &client_id_list,
1797 silc_client_command_reply_users_resolved, cmd));
1801 /* Get client mode list */
1802 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1804 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1807 silc_buffer_set(&client_mode_list, tmp, tmp_len);
1809 SILC_LOG_DEBUG(("channel %s, %d users", channel->channel_name, list_count));
1811 /* Cache the received Client ID's and modes. */
1812 for (i = 0; i < list_count; i++) {
1813 SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1815 if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1819 SILC_GET32_MSB(mode, client_mode_list.data);
1821 /* Save the client on this channel. Unknown clients are ignored as they
1822 clearly do not exist since the resolving didn't find them. */
1823 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1824 if (client_entry && !silc_client_on_channel(channel, client_entry)) {
1825 chu = silc_calloc(1, sizeof(*chu));
1827 silc_client_unref_client(client, conn, client_entry);
1830 chu->client = client_entry;
1832 chu->channel = channel;
1833 silc_hash_table_add(channel->user_list, client_entry, chu);
1834 silc_hash_table_add(client_entry->channels, channel, chu);
1836 silc_client_unref_client(client, conn, client_entry);
1838 if (!silc_buffer_pull(&client_id_list, idp_len))
1840 if (!silc_buffer_pull(&client_mode_list, 4))
1844 /* Notify application */
1845 silc_hash_table_list(channel->user_list, &htl);
1846 silc_client_command_callback(cmd, channel, &htl);
1847 silc_hash_table_list_reset(&htl);
1850 silc_fsm_next(fsm, silc_client_command_reply_process);
1851 return SILC_FSM_CONTINUE;
1854 /********************************** GETKEY **********************************/
1856 /* Received command reply to GETKEY command. WE've received the remote
1857 client's public key. */
1859 SILC_FSM_STATE(silc_client_command_reply_getkey)
1861 SilcClientCommandContext cmd = fsm_context;
1862 SilcClientConnection conn = cmd->conn;
1863 SilcClient client = conn->client;
1864 SilcCommandPayload payload = state_context;
1865 SilcArgumentPayload args = silc_command_get_args(payload);
1866 SilcClientEntry client_entry;
1867 SilcServerEntry server_entry;
1870 SilcPublicKey public_key;
1874 CHECK_STATUS("Cannot get key: ");
1878 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1879 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1883 /* Get the public key */
1884 tmp = silc_argument_get_arg_type(args, 3, &len);
1886 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1889 if (!silc_public_key_payload_decode(tmp, len, &public_key)) {
1890 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1894 if (id.type == SILC_ID_CLIENT) {
1895 /* Received client's public key */
1896 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1897 if (!client_entry) {
1898 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1902 /* Save fingerprint */
1903 if (!client_entry->fingerprint)
1904 silc_hash_make(client->sha1hash, tmp + 4, len - 4,
1905 client_entry->fingerprint);
1906 if (!client_entry->public_key) {
1907 client_entry->public_key = public_key;
1911 /* Notify application */
1912 silc_client_command_callback(cmd, SILC_ID_CLIENT, client_entry,
1913 client_entry->public_key);
1914 silc_client_unref_client(client, conn, client_entry);
1915 } else if (id.type == SILC_ID_SERVER) {
1916 /* Received server's public key */
1917 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
1918 if (!server_entry) {
1919 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1923 /* Notify application */
1924 silc_client_command_callback(cmd, SILC_ID_SERVER, server_entry,
1930 silc_pkcs_public_key_free(public_key);
1931 silc_fsm_next(fsm, silc_client_command_reply_process);
1932 return SILC_FSM_CONTINUE;
1935 /********************************** SERVICE *********************************/
1937 /* Reply to SERVICE command. */
1938 /* XXX incomplete */
1940 SILC_FSM_STATE(silc_client_command_reply_service)
1942 SilcClientCommandContext cmd = fsm_context;
1943 SilcCommandPayload payload = state_context;
1944 SilcArgumentPayload args = silc_command_get_args(payload);
1946 unsigned char *service_list, *name;
1949 CHECK_STATUS("Cannot get service: ");
1951 /* Get service list */
1952 service_list = silc_argument_get_arg_type(args, 2, &tmp_len);
1954 /* Get requested service name */
1955 name = silc_argument_get_arg_type(args, 3, &tmp_len);
1957 /* Notify application */
1958 silc_client_command_callback(cmd, service_list, name);
1960 silc_fsm_next(fsm, silc_client_command_reply_process);
1961 return SILC_FSM_CONTINUE;
1964 /*********************************** QUIT ***********************************/
1966 /* QUIT command reply stub */
1968 SILC_FSM_STATE(silc_client_command_reply_quit)
1970 silc_fsm_next(fsm, silc_client_command_reply_process);
1971 return SILC_FSM_CONTINUE;