5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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(err) \
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 = cmd->error = err; \
35 SILC_LOG_DEBUG(("Error in command reply: %s", \
36 silc_get_status_message(cmd->status))); \
37 silc_client_command_callback(cmd, arg1, arg2); \
41 #define CHECK_STATUS(msg) \
42 SILC_LOG_DEBUG(("%s", silc_get_command_name(cmd->cmd))); \
43 if (cmd->error != SILC_STATUS_OK) { \
45 SAY(cmd->conn->client, cmd->conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR, \
46 msg "%s", silc_get_status_message(cmd->error)); \
47 ERROR_CALLBACK(cmd->error); \
48 silc_client_command_process_error(cmd, state_context, cmd->error); \
49 silc_fsm_next(fsm, silc_client_command_reply_processed); \
50 return SILC_FSM_CONTINUE; \
53 /* Check for correct arguments */
54 #define CHECK_ARGS(min, max) \
55 if (silc_argument_get_arg_num(args) < min || \
56 silc_argument_get_arg_num(args) > max) { \
57 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS); \
58 silc_fsm_next(fsm, silc_client_command_reply_processed); \
59 return SILC_FSM_CONTINUE; \
62 #define SAY cmd->conn->client->internal->ops->say
64 /************************ Static utility functions **************************/
66 /* Delivers the command reply back to application */
69 silc_client_command_callback(SilcClientCommandContext cmd, ...)
71 SilcClientCommandReplyCallback cb;
77 /* Default reply callback */
80 cmd->conn->client->internal->ops->command_reply(
81 cmd->conn->client, cmd->conn, cmd->cmd, cmd->status,
87 list = cmd->reply_callbacks;
88 silc_list_start(list);
89 while ((cb = silc_list_get(list)))
90 if (!cb->do_not_call) {
92 cb->do_not_call = !cb->reply(cmd->conn->client, cmd->conn, cmd->cmd,
93 cmd->status, cmd->error, cb->context, cp);
100 /* Handles common error status types. */
102 static void silc_client_command_process_error(SilcClientCommandContext cmd,
103 SilcCommandPayload payload,
106 SilcClient client = cmd->conn->client;
107 SilcClientConnection conn = cmd->conn;
108 SilcArgumentPayload args = silc_command_get_args(payload);
111 if (cmd->error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
112 SilcClientEntry client_entry;
114 /* Remove unknown client entry from cache */
115 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
118 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
120 silc_client_remove_from_channels(client, conn, client_entry);
121 silc_client_del_client(client, conn, client_entry);
122 silc_client_unref_client(client, conn, client_entry);
127 if (cmd->error == SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID) {
128 SilcChannelEntry channel;
130 /* Remove unknown channel entry from cache */
131 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
134 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
136 silc_client_empty_channel(client, conn, channel);
137 silc_client_del_channel(client, conn, channel);
138 silc_client_unref_channel(client, conn, channel);
143 if (cmd->error == SILC_STATUS_ERR_NO_SUCH_SERVER_ID) {
144 SilcServerEntry server_entry;
146 /* Remove unknown server entry from cache */
147 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
150 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
152 silc_client_del_server(client, conn, server_entry);
153 silc_client_unref_server(client, conn, server_entry);
159 /***************************** Command Reply ********************************/
161 /* Process received command reply packet */
163 SILC_FSM_STATE(silc_client_command_reply)
165 SilcClientConnection conn = fsm_context;
166 SilcPacket packet = state_context;
167 SilcClientCommandContext cmd;
168 SilcCommandPayload payload;
170 SilcUInt16 cmd_ident;
172 /* Get command reply payload from packet */
173 payload = silc_command_payload_parse(silc_buffer_datalen(&packet->buffer));
174 silc_packet_free(packet);
176 SILC_LOG_DEBUG(("Bad command reply packet"));
177 return SILC_FSM_FINISH;
180 cmd_ident = silc_command_get_ident(payload);
181 command = silc_command_get(payload);
183 /* Find the command pending reply */
184 silc_mutex_lock(conn->internal->lock);
185 silc_list_start(conn->internal->pending_commands);
186 while ((cmd = silc_list_get(conn->internal->pending_commands))) {
187 if ((cmd->cmd == command || cmd->cmd == SILC_COMMAND_NONE)
188 && cmd->cmd_ident == cmd_ident) {
189 silc_list_del(conn->internal->pending_commands, cmd);
193 silc_mutex_unlock(conn->internal->lock);
196 SILC_LOG_DEBUG(("Unknown command reply %s, ident %d",
197 silc_get_command_name(command), cmd_ident));
198 silc_command_payload_free(payload);
199 return SILC_FSM_FINISH;
202 /* Signal command thread that command reply has arrived. We continue
203 command reply processing synchronously because we save the command
204 payload into state context. No other reply may arrive to this command
205 while we're processing this reply. */
206 silc_fsm_set_state_context(&cmd->thread, payload);
207 silc_fsm_next(&cmd->thread, silc_client_command_reply_process);
208 silc_fsm_continue_sync(&cmd->thread);
210 return SILC_FSM_FINISH;
213 /* Wait here for command reply to arrive from remote host */
215 SILC_FSM_STATE(silc_client_command_reply_wait)
217 SilcClientCommandContext cmd = fsm_context;
219 SILC_LOG_DEBUG(("Wait for command reply"));
221 /** Wait for command reply */
222 silc_fsm_set_state_context(fsm, NULL);
223 silc_fsm_next_later(fsm, silc_client_command_reply_timeout,
224 cmd->cmd != SILC_COMMAND_PING ? 40 : 60, 0);
225 return SILC_FSM_WAIT;
228 /* Timeout occurred while waiting command reply */
230 SILC_FSM_STATE(silc_client_command_reply_timeout)
232 SilcClientCommandContext cmd = fsm_context;
233 SilcClientConnection conn = cmd->conn;
234 SilcArgumentPayload args = NULL;
236 if (conn->internal->disconnected) {
237 SILC_LOG_DEBUG(("Command %s canceled", silc_get_command_name(cmd->cmd)));
238 silc_list_del(conn->internal->pending_commands, cmd);
240 ERROR_CALLBACK(SILC_STATUS_ERR_TIMEDOUT);
241 return SILC_FSM_FINISH;
244 SILC_LOG_DEBUG(("Command %s timeout", silc_get_command_name(cmd->cmd)));
246 /* Timeout, reply not received in timely fashion */
247 silc_list_del(conn->internal->pending_commands, cmd);
248 ERROR_CALLBACK(SILC_STATUS_ERR_TIMEDOUT);
249 return SILC_FSM_FINISH;
252 /* Process received command reply payload */
254 SILC_FSM_STATE(silc_client_command_reply_process)
256 SilcClientCommandContext cmd = fsm_context;
257 SilcCommandPayload payload = state_context;
259 silc_command_get_status(payload, &cmd->status, &cmd->error);
262 case SILC_COMMAND_WHOIS:
264 silc_fsm_next(fsm, silc_client_command_reply_whois);
266 case SILC_COMMAND_WHOWAS:
268 silc_fsm_next(fsm, silc_client_command_reply_whowas);
270 case SILC_COMMAND_IDENTIFY:
272 silc_fsm_next(fsm, silc_client_command_reply_identify);
274 case SILC_COMMAND_NICK:
276 silc_fsm_next(fsm, silc_client_command_reply_nick);
278 case SILC_COMMAND_LIST:
280 silc_fsm_next(fsm, silc_client_command_reply_list);
282 case SILC_COMMAND_TOPIC:
284 silc_fsm_next(fsm, silc_client_command_reply_topic);
286 case SILC_COMMAND_INVITE:
288 silc_fsm_next(fsm, silc_client_command_reply_invite);
290 case SILC_COMMAND_QUIT:
292 silc_fsm_next(fsm, silc_client_command_reply_quit);
294 case SILC_COMMAND_KILL:
296 silc_fsm_next(fsm, silc_client_command_reply_kill);
298 case SILC_COMMAND_INFO:
300 silc_fsm_next(fsm, silc_client_command_reply_info);
302 case SILC_COMMAND_STATS:
304 silc_fsm_next(fsm, silc_client_command_reply_stats);
306 case SILC_COMMAND_PING:
308 silc_fsm_next(fsm, silc_client_command_reply_ping);
310 case SILC_COMMAND_OPER:
312 silc_fsm_next(fsm, silc_client_command_reply_oper);
314 case SILC_COMMAND_JOIN:
316 silc_fsm_next(fsm, silc_client_command_reply_join);
318 case SILC_COMMAND_MOTD:
320 silc_fsm_next(fsm, silc_client_command_reply_motd);
322 case SILC_COMMAND_UMODE:
324 silc_fsm_next(fsm, silc_client_command_reply_umode);
326 case SILC_COMMAND_CMODE:
328 silc_fsm_next(fsm, silc_client_command_reply_cmode);
330 case SILC_COMMAND_CUMODE:
332 silc_fsm_next(fsm, silc_client_command_reply_cumode);
334 case SILC_COMMAND_KICK:
336 silc_fsm_next(fsm, silc_client_command_reply_kick);
338 case SILC_COMMAND_BAN:
340 silc_fsm_next(fsm, silc_client_command_reply_ban);
342 case SILC_COMMAND_DETACH:
344 silc_fsm_next(fsm, silc_client_command_reply_detach);
346 case SILC_COMMAND_WATCH:
348 silc_fsm_next(fsm, silc_client_command_reply_watch);
350 case SILC_COMMAND_SILCOPER:
352 silc_fsm_next(fsm, silc_client_command_reply_silcoper);
354 case SILC_COMMAND_LEAVE:
356 silc_fsm_next(fsm, silc_client_command_reply_leave);
358 case SILC_COMMAND_USERS:
360 silc_fsm_next(fsm, silc_client_command_reply_users);
362 case SILC_COMMAND_GETKEY:
364 silc_fsm_next(fsm, silc_client_command_reply_getkey);
366 case SILC_COMMAND_SERVICE:
368 silc_fsm_next(fsm, silc_client_command_reply_service);
371 return SILC_FSM_FINISH;
374 return SILC_FSM_CONTINUE;
377 /* Completes command reply processing */
379 SILC_FSM_STATE(silc_client_command_reply_processed)
381 SilcClientCommandContext cmd = fsm_context;
382 SilcClientConnection conn = cmd->conn;
383 SilcCommandPayload payload = state_context;
385 silc_command_payload_free(payload);
387 if (cmd->status == SILC_STATUS_OK || cmd->status == SILC_STATUS_LIST_END ||
388 SILC_STATUS_IS_ERROR(cmd->status))
389 return SILC_FSM_FINISH;
391 /* Add back to pending command reply list */
392 silc_mutex_lock(conn->internal->lock);
393 cmd->resolved = FALSE;
394 silc_list_add(conn->internal->pending_commands, cmd);
395 silc_mutex_unlock(conn->internal->lock);
397 /** Wait more command payloads */
398 silc_fsm_next(fsm, silc_client_command_reply_wait);
399 return SILC_FSM_CONTINUE;
402 /******************************** WHOIS *************************************/
404 /* Received reply for WHOIS command. */
406 SILC_FSM_STATE(silc_client_command_reply_whois)
408 SilcClientCommandContext cmd = fsm_context;
409 SilcClientConnection conn = cmd->conn;
410 SilcClient client = conn->client;
411 SilcCommandPayload payload = state_context;
412 SilcArgumentPayload args = silc_command_get_args(payload);
413 SilcClientEntry client_entry = NULL;
414 SilcUInt32 idle = 0, mode = 0, fingerprint_len, len, *umodes = NULL;
415 SilcBufferStruct channels, ch_user_modes;
416 SilcBool has_channels = FALSE;
417 SilcDList channel_list = NULL;
419 char *nickname = NULL, *username = NULL, *realname = NULL;
420 unsigned char *fingerprint, *tmp;
422 CHECK_STATUS("WHOIS: ");
426 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
427 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
432 nickname = silc_argument_get_arg_type(args, 3, NULL);
433 username = silc_argument_get_arg_type(args, 4, NULL);
434 realname = silc_argument_get_arg_type(args, 5, NULL);
435 if (!nickname || !username || !realname) {
436 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
440 /* Get joined channel list */
441 memset(&channels, 0, sizeof(channels));
442 tmp = silc_argument_get_arg_type(args, 6, &len);
445 silc_buffer_set(&channels, tmp, len);
447 /* Get channel user mode list */
448 tmp = silc_argument_get_arg_type(args, 10, &len);
450 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
453 silc_buffer_set(&ch_user_modes, tmp, len);
457 tmp = silc_argument_get_arg_type(args, 7, &len);
459 SILC_GET32_MSB(mode, tmp);
462 tmp = silc_argument_get_arg_type(args, 8, &len);
464 SILC_GET32_MSB(idle, tmp);
466 /* Get fingerprint */
467 fingerprint = silc_argument_get_arg_type(args, 9, &fingerprint_len);
469 /* Check if we have this client cached already. */
470 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
472 SILC_LOG_DEBUG(("Adding new client entry (WHOIS)"));
474 silc_client_add_client(client, conn, nickname, username, realname,
475 &id.u.client_id, mode);
477 ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
480 silc_client_ref_client(client, conn, client_entry);
482 silc_client_update_client(client, conn, client_entry,
483 nickname, username, realname, mode);
486 silc_rwlock_wrlock(client_entry->internal.lock);
488 if (fingerprint && fingerprint_len == sizeof(client_entry->fingerprint))
489 memcpy(client_entry->fingerprint, fingerprint, fingerprint_len);
491 /* Get user attributes */
492 tmp = silc_argument_get_arg_type(args, 11, &len);
494 if (client_entry->attrs)
495 silc_attribute_payload_list_free(client_entry->attrs);
496 client_entry->attrs = silc_attribute_payload_parse(tmp, len);
499 silc_rwlock_unlock(client_entry->internal.lock);
501 /* Parse channel and channel user mode list */
503 channel_list = silc_channel_payload_parse_list(silc_buffer_data(&channels),
504 silc_buffer_len(&channels));
506 silc_get_mode_list(&ch_user_modes, silc_dlist_count(channel_list),
510 /* Notify application */
511 silc_client_command_callback(cmd, client_entry, nickname, username,
512 realname, channel_list, mode, idle, fingerprint,
513 umodes, client_entry->attrs);
515 silc_client_unref_client(client, conn, client_entry);
517 silc_channel_payload_list_free(channel_list);
522 silc_fsm_next(fsm, silc_client_command_reply_processed);
523 return SILC_FSM_CONTINUE;
526 /******************************** WHOWAS ************************************/
528 /* Received reply for WHOWAS command. */
530 SILC_FSM_STATE(silc_client_command_reply_whowas)
532 SilcClientCommandContext cmd = fsm_context;
533 SilcClientConnection conn = cmd->conn;
534 SilcClient client = conn->client;
535 SilcCommandPayload payload = state_context;
536 SilcArgumentPayload args = silc_command_get_args(payload);
537 SilcClientEntry client_entry = NULL;
539 char *nickname, *username;
540 char *realname = NULL;
542 CHECK_STATUS("WHOWAS: ");
546 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
547 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
551 /* Get the client entry */
552 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
555 nickname = silc_argument_get_arg_type(args, 3, NULL);
556 username = silc_argument_get_arg_type(args, 4, NULL);
557 realname = silc_argument_get_arg_type(args, 5, NULL);
558 if (!nickname || !username) {
559 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
563 /* Notify application. We don't save any history information to any
564 cache. Just pass the data to the application. */
565 silc_client_command_callback(cmd, client_entry, nickname, username,
569 silc_client_unref_client(client, conn, client_entry);
570 silc_fsm_next(fsm, silc_client_command_reply_processed);
571 return SILC_FSM_CONTINUE;
574 /******************************** IDENTIFY **********************************/
576 /* Received reply for IDENTIFY command. */
578 SILC_FSM_STATE(silc_client_command_reply_identify)
580 SilcClientCommandContext cmd = fsm_context;
581 SilcClientConnection conn = cmd->conn;
582 SilcClient client = conn->client;
583 SilcCommandPayload payload = state_context;
584 SilcArgumentPayload args = silc_command_get_args(payload);
585 SilcClientEntry client_entry;
586 SilcServerEntry server_entry;
587 SilcChannelEntry channel_entry;
590 char *name = NULL, *info = NULL;
592 CHECK_STATUS("IDENTIFY: ");
596 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
597 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
602 name = silc_argument_get_arg_type(args, 3, &len);
603 info = silc_argument_get_arg_type(args, 4, &len);
607 SILC_LOG_DEBUG(("Received client information"));
609 /* Check if we have this client cached already. */
610 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
612 SILC_LOG_DEBUG(("Adding new client entry (IDENTIFY)"));
614 silc_client_add_client(client, conn, name, info, NULL,
617 ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
620 silc_client_ref_client(client, conn, client_entry);
622 silc_client_update_client(client, conn, client_entry,
623 name, info, NULL, 0);
626 /* Notify application */
627 silc_client_command_callback(cmd, client_entry, name, info);
628 silc_client_unref_client(client, conn, client_entry);
632 SILC_LOG_DEBUG(("Received server information"));
634 /* Check if we have this server cached already. */
635 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
637 SILC_LOG_DEBUG(("Adding new server entry (IDENTIFY)"));
638 server_entry = silc_client_add_server(client, conn, name, info,
641 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
644 silc_client_ref_server(client, conn, server_entry);
646 silc_client_update_server(client, conn, server_entry, name, info);
648 server_entry->internal.resolve_cmd_ident = 0;
650 /* Notify application */
651 silc_client_command_callback(cmd, server_entry, name, info);
652 silc_client_unref_server(client, conn, server_entry);
655 case SILC_ID_CHANNEL:
656 SILC_LOG_DEBUG(("Received channel information"));
658 /* Check if we have this channel cached already. */
659 channel_entry = silc_client_get_channel_by_id(client, conn,
661 if (!channel_entry) {
662 SILC_LOG_DEBUG(("Adding new channel entry (IDENTIFY)"));
665 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
669 /* Add new channel entry */
670 channel_entry = silc_client_add_channel(client, conn, name, 0,
672 if (!channel_entry) {
673 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
676 silc_client_ref_channel(client, conn, channel_entry);
679 /* Notify application */
680 silc_client_command_callback(cmd, channel_entry,
681 channel_entry->channel_name, info);
682 silc_client_unref_channel(client, conn, channel_entry);
687 silc_fsm_next(fsm, silc_client_command_reply_processed);
688 return SILC_FSM_CONTINUE;
691 /********************************** NICK ************************************/
693 /* Received reply for command NICK. */
695 SILC_FSM_STATE(silc_client_command_reply_nick)
697 SilcClientCommandContext cmd = fsm_context;
698 SilcClientConnection conn = cmd->conn;
699 SilcClient client = conn->client;
700 SilcCommandPayload payload = state_context;
701 SilcArgumentPayload args = silc_command_get_args(payload);
702 unsigned char *nick, *idp;
703 SilcUInt32 len, idp_len;
704 SilcClientID old_client_id;
708 CHECK_STATUS("Cannot set nickname: ");
711 /* Take received Client ID */
712 idp = silc_argument_get_arg_type(args, 2, &idp_len);
714 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
717 if (!silc_id_payload_parse_id(idp, idp_len, &id)) {
718 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
722 /* Take the new nickname */
723 nick = silc_argument_get_arg_type(args, 3, &len);
725 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
729 silc_rwlock_wrlock(conn->local_entry->internal.lock);
731 /* Change the nickname */
732 old_client_id = *conn->local_id;
733 if (!silc_client_change_nickname(client, conn, conn->local_entry,
734 nick, &id.u.client_id, idp, idp_len)) {
735 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_NICKNAME);
736 silc_rwlock_unlock(conn->local_entry->internal.lock);
740 silc_rwlock_unlock(conn->local_entry->internal.lock);
742 /* Notify application */
743 silc_client_command_callback(cmd, conn->local_entry,
744 conn->local_entry->nickname, &old_client_id);
747 silc_fsm_next(fsm, silc_client_command_reply_processed);
748 return SILC_FSM_CONTINUE;
751 /********************************** LIST ************************************/
753 /* Received reply to the LIST command. */
755 SILC_FSM_STATE(silc_client_command_reply_list)
757 SilcClientCommandContext cmd = fsm_context;
758 SilcClientConnection conn = cmd->conn;
759 SilcClient client = conn->client;
760 SilcCommandPayload payload = state_context;
761 SilcArgumentPayload args = silc_command_get_args(payload);
762 unsigned char *tmp, *name, *topic;
763 SilcUInt32 usercount = 0;
764 SilcChannelEntry channel_entry = NULL;
768 CHECK_STATUS("Cannot list channels: ");
770 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
771 /* There were no channels in the network. */
772 silc_client_command_callback(cmd, NULL, NULL, NULL, 0);
773 silc_fsm_next(fsm, silc_client_command_reply_processed);
774 return SILC_FSM_CONTINUE;
779 name = silc_argument_get_arg_type(args, 3, NULL);
781 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
785 topic = silc_argument_get_arg_type(args, 4, NULL);
786 tmp = silc_argument_get_arg_type(args, 5, NULL);
788 SILC_GET32_MSB(usercount, tmp);
790 /* Check whether the channel exists, and add it to cache if it doesn't. */
791 channel_entry = silc_client_get_channel_by_id(client, conn,
793 if (!channel_entry) {
794 /* Add new channel entry */
795 channel_entry = silc_client_add_channel(client, conn, name, 0,
797 if (!channel_entry) {
798 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
801 silc_client_ref_channel(client, conn, channel_entry);
804 /* Notify application */
805 silc_client_command_callback(cmd, channel_entry, channel_entry->channel_name,
809 silc_client_unref_channel(client, conn, channel_entry);
810 silc_fsm_next(fsm, silc_client_command_reply_processed);
811 return SILC_FSM_CONTINUE;
814 /********************************* TOPIC ************************************/
816 /* Received reply to topic command. */
818 SILC_FSM_STATE(silc_client_command_reply_topic)
820 SilcClientCommandContext cmd = fsm_context;
821 SilcClientConnection conn = cmd->conn;
822 SilcClient client = conn->client;
823 SilcCommandPayload payload = state_context;
824 SilcArgumentPayload args = silc_command_get_args(payload);
825 SilcChannelEntry channel = NULL;
831 CHECK_STATUS("Cannot set topic: ");
834 /* Take Channel ID */
835 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
836 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
840 /* Get the channel entry */
841 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
843 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
847 silc_rwlock_wrlock(channel->internal.lock);
850 topic = silc_argument_get_arg_type(args, 3, &len);
852 silc_free(channel->topic);
853 channel->topic = silc_memdup(topic, len);
856 silc_rwlock_unlock(channel->internal.lock);
858 /* Notify application */
859 silc_client_command_callback(cmd, channel, channel->topic);
862 silc_client_unref_channel(client, conn, channel);
863 silc_fsm_next(fsm, silc_client_command_reply_processed);
864 return SILC_FSM_CONTINUE;
867 /********************************* INVITE ***********************************/
869 /* Received reply to invite command. */
871 SILC_FSM_STATE(silc_client_command_reply_invite)
873 SilcClientCommandContext cmd = fsm_context;
874 SilcClientConnection conn = cmd->conn;
875 SilcClient client = conn->client;
876 SilcCommandPayload payload = state_context;
877 SilcArgumentPayload args = silc_command_get_args(payload);
878 SilcChannelEntry channel = NULL;
881 SilcArgumentPayload invite_args = NULL;
885 CHECK_STATUS("Cannot invite: ");
888 /* Take Channel ID */
889 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
890 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
894 /* Get the channel entry */
895 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
897 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
901 /* Get the invite list */
902 tmp = silc_argument_get_arg_type(args, 3, &len);
904 invite_args = silc_argument_list_parse(tmp, len);
906 /* Notify application */
907 silc_client_command_callback(cmd, channel, invite_args);
910 silc_argument_payload_free(invite_args);
913 silc_client_unref_channel(client, conn, channel);
914 silc_fsm_next(fsm, silc_client_command_reply_processed);
915 return SILC_FSM_CONTINUE;
918 /********************************** KILL ************************************/
920 /* Received reply to the KILL command. */
922 SILC_FSM_STATE(silc_client_command_reply_kill)
924 SilcClientCommandContext cmd = fsm_context;
925 SilcClientConnection conn = cmd->conn;
926 SilcClient client = conn->client;
927 SilcCommandPayload payload = state_context;
928 SilcArgumentPayload args = silc_command_get_args(payload);
929 SilcClientEntry client_entry;
933 CHECK_STATUS("Cannot kill: ");
936 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
937 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
941 /* Get the client entry, if exists */
942 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
944 /* Notify application */
945 silc_client_command_callback(cmd, client_entry);
947 /* Remove the client */
949 silc_client_remove_from_channels(client, conn, client_entry);
950 silc_client_del_client(client, conn, client_entry);
951 silc_client_unref_client(client, conn, client_entry);
955 silc_fsm_next(fsm, silc_client_command_reply_processed);
956 return SILC_FSM_CONTINUE;
959 /********************************** INFO ************************************/
961 /* Received reply to INFO command. We receive the server ID and some
962 information about the server user requested. */
964 SILC_FSM_STATE(silc_client_command_reply_info)
966 SilcClientCommandContext cmd = fsm_context;
967 SilcClientConnection conn = cmd->conn;
968 SilcClient client = conn->client;
969 SilcCommandPayload payload = state_context;
970 SilcArgumentPayload args = silc_command_get_args(payload);
971 SilcServerEntry server;
972 char *server_name, *server_info;
976 CHECK_STATUS("Cannot get info: ");
980 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
981 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
985 /* Get server name */
986 server_name = silc_argument_get_arg_type(args, 3, NULL);
988 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
992 /* Get server info */
993 server_info = silc_argument_get_arg_type(args, 4, NULL);
995 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
999 /* See whether we have this server cached. If not create it. */
1000 server = silc_client_get_server_by_id(client, conn, &id.u.server_id);
1002 SILC_LOG_DEBUG(("Add new server entry (INFO)"));
1003 server = silc_client_add_server(client, conn, server_name,
1004 server_info, &id.u.server_id);
1007 silc_client_ref_server(client, conn, server);
1010 /* Notify application */
1011 silc_client_command_callback(cmd, server, server->server_name,
1012 server->server_info);
1013 silc_client_unref_server(client, conn, server);
1016 silc_fsm_next(fsm, silc_client_command_reply_processed);
1017 return SILC_FSM_CONTINUE;
1020 /********************************** STATS ***********************************/
1022 /* Received reply to STATS command. */
1024 SILC_FSM_STATE(silc_client_command_reply_stats)
1026 SilcClientCommandContext cmd = fsm_context;
1027 SilcCommandPayload payload = state_context;
1028 SilcArgumentPayload args = silc_command_get_args(payload);
1029 SilcClientStats stats;
1030 unsigned char *buf = NULL;
1031 SilcUInt32 buf_len = 0;
1036 CHECK_STATUS("Cannot get stats: ");
1040 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1041 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1045 /* Get statistics structure */
1046 memset(&stats, 0, sizeof(stats));
1047 buf = silc_argument_get_arg_type(args, 3, &buf_len);
1049 silc_buffer_set(&b, buf, buf_len);
1050 silc_buffer_unformat(&b,
1051 SILC_STR_UI_INT(&stats.starttime),
1052 SILC_STR_UI_INT(&stats.uptime),
1053 SILC_STR_UI_INT(&stats.my_clients),
1054 SILC_STR_UI_INT(&stats.my_channels),
1055 SILC_STR_UI_INT(&stats.my_server_ops),
1056 SILC_STR_UI_INT(&stats.my_router_ops),
1057 SILC_STR_UI_INT(&stats.cell_clients),
1058 SILC_STR_UI_INT(&stats.cell_channels),
1059 SILC_STR_UI_INT(&stats.cell_servers),
1060 SILC_STR_UI_INT(&stats.clients),
1061 SILC_STR_UI_INT(&stats.channels),
1062 SILC_STR_UI_INT(&stats.servers),
1063 SILC_STR_UI_INT(&stats.routers),
1064 SILC_STR_UI_INT(&stats.server_ops),
1065 SILC_STR_UI_INT(&stats.router_ops),
1069 /* Notify application */
1070 silc_client_command_callback(cmd, &stats);
1073 silc_fsm_next(fsm, silc_client_command_reply_processed);
1074 return SILC_FSM_CONTINUE;
1077 /********************************** PING ************************************/
1079 /* Received reply to PING command. */
1081 SILC_FSM_STATE(silc_client_command_reply_ping)
1083 SilcClientCommandContext cmd = fsm_context;
1084 SilcClientConnection conn = cmd->conn;
1085 SilcClient client = conn->client;
1088 diff = silc_time() - SILC_PTR_TO_64(cmd->context);
1090 SAY(client, conn, SILC_CLIENT_MESSAGE_INFO,
1091 "Ping reply from %s: %d second%s", conn->remote_host,
1092 (int)diff, diff == 1 ? "" : "s");
1094 /* Notify application */
1095 silc_client_command_callback(cmd);
1097 silc_fsm_next(fsm, silc_client_command_reply_processed);
1098 return SILC_FSM_CONTINUE;
1101 /********************************** JOIN ************************************/
1103 /* Continue JOIN command reply processing after resolving unknown users */
1106 silc_client_command_reply_join_resolved(SilcClient client,
1107 SilcClientConnection conn,
1112 SilcClientCommandContext cmd = context;
1113 SilcChannelEntry channel = cmd->context;
1115 channel->internal.resolve_cmd_ident = 0;
1116 silc_client_unref_channel(client, conn, channel);
1118 SILC_FSM_CALL_CONTINUE(&cmd->thread);
1122 /* Received reply for JOIN command. */
1124 SILC_FSM_STATE(silc_client_command_reply_join)
1126 SilcClientCommandContext cmd = fsm_context;
1127 SilcClientConnection conn = cmd->conn;
1128 SilcClient client = conn->client;
1129 SilcCommandPayload payload = state_context;
1130 SilcArgumentPayload args = silc_command_get_args(payload);
1131 SilcChannelEntry channel;
1132 SilcUInt32 mode = 0, len, list_count;
1133 char *topic, *tmp, *channel_name = NULL, *hmac;
1135 SilcBufferStruct client_id_list, client_mode_list, keyp;
1136 SilcHashTableList htl;
1141 CHECK_STATUS("Cannot join channel: ");
1144 /* Get channel name */
1145 channel_name = silc_argument_get_arg_type(args, 2, NULL);
1146 if (!channel_name) {
1147 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1151 /* Get Channel ID */
1152 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1153 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1157 /* Check whether we have this channel entry already. */
1158 channel = silc_client_get_channel(client, conn, channel_name);
1160 if (!SILC_ID_CHANNEL_COMPARE(&channel->id, &id.u.channel_id))
1161 silc_client_replace_channel_id(client, conn, channel, &id.u.channel_id);
1163 /* Create new channel entry */
1164 channel = silc_client_add_channel(client, conn, channel_name,
1165 mode, &id.u.channel_id);
1167 ERROR_CALLBACK(SILC_STATUS_ERR_BAD_CHANNEL);
1170 silc_client_ref_channel(client, conn, channel);
1173 /* Get the list count */
1174 tmp = silc_argument_get_arg_type(args, 12, &len);
1176 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1179 SILC_GET32_MSB(list_count, tmp);
1181 /* Get Client ID list */
1182 tmp = silc_argument_get_arg_type(args, 13, &len);
1184 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1187 silc_buffer_set(&client_id_list, tmp, len);
1189 /* Resolve users we do not know about */
1190 if (!cmd->resolved) {
1191 cmd->resolved = TRUE;
1192 cmd->context = channel;
1193 SILC_FSM_CALL(channel->internal.resolve_cmd_ident =
1194 silc_client_get_clients_by_list(
1195 client, conn, list_count, &client_id_list,
1196 silc_client_command_reply_join_resolved, cmd));
1200 /* Get client mode list */
1201 tmp = silc_argument_get_arg_type(args, 14, &len);
1203 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1206 silc_buffer_set(&client_mode_list, tmp, len);
1208 silc_rwlock_wrlock(channel->internal.lock);
1210 /* Add clients we received in the reply to the channel */
1211 for (i = 0; i < list_count; i++) {
1214 SilcClientEntry client_entry;
1217 SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1219 if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1223 SILC_GET32_MSB(mode, client_mode_list.data);
1225 /* Get client entry */
1226 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1227 if (client_entry && client_entry->internal.valid) {
1228 /* Join client to the channel */
1229 silc_rwlock_wrlock(client_entry->internal.lock);
1230 silc_client_add_to_channel(client, conn, channel, client_entry, mode);
1231 silc_rwlock_unlock(client_entry->internal.lock);
1233 silc_client_unref_client(client, conn, client_entry);
1235 if (!silc_buffer_pull(&client_id_list, idp_len)) {
1236 silc_rwlock_unlock(channel->internal.lock);
1239 if (!silc_buffer_pull(&client_mode_list, 4)) {
1240 silc_rwlock_unlock(channel->internal.lock);
1246 hmac = silc_argument_get_arg_type(args, 11, NULL);
1248 if (!silc_hmac_alloc(hmac, NULL, &channel->internal.hmac)) {
1250 SAY(client, conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR,
1251 "Cannot join channel: Unsupported HMAC `%s'", hmac);
1252 ERROR_CALLBACK(SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
1253 silc_rwlock_unlock(channel->internal.lock);
1258 /* Get channel mode */
1259 tmp = silc_argument_get_arg_type(args, 5, &len);
1260 if (tmp && len == 4)
1261 SILC_GET32_MSB(mode, tmp);
1262 channel->mode = mode;
1264 /* Get channel key and save it */
1265 tmp = silc_argument_get_arg_type(args, 7, &len);
1267 silc_buffer_set(&keyp, tmp, len);
1268 silc_client_save_channel_key(client, conn, &keyp, channel);
1272 topic = silc_argument_get_arg_type(args, 10, NULL);
1274 silc_free(channel->topic);
1275 channel->topic = silc_memdup(topic, strlen(topic));
1278 /* Get founder key */
1279 tmp = silc_argument_get_arg_type(args, 15, &len);
1281 if (channel->founder_key)
1282 silc_pkcs_public_key_free(channel->founder_key);
1283 channel->founder_key = NULL;
1284 silc_public_key_payload_decode(tmp, len, &channel->founder_key);
1287 /* Get user limit */
1288 tmp = silc_argument_get_arg_type(args, 17, &len);
1289 if (tmp && len == 4)
1290 SILC_GET32_MSB(channel->user_limit, tmp);
1291 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1292 channel->user_limit = 0;
1294 /* Get channel public key list */
1295 tmp = silc_argument_get_arg_type(args, 16, &len);
1297 silc_client_channel_save_public_keys(channel, tmp, len, FALSE);
1299 /* Set current channel */
1300 conn->current_channel = channel;
1302 silc_rwlock_unlock(channel->internal.lock);
1304 cipher = (channel->internal.send_key ?
1305 silc_cipher_get_name(channel->internal.send_key) : NULL);
1306 silc_hash_table_list(channel->user_list, &htl);
1308 /* Notify application */
1309 silc_client_command_callback(cmd, channel->channel_name, channel, mode, &htl,
1310 topic, cipher, hmac, channel->founder_key,
1311 channel->channel_pubkeys, channel->user_limit);
1313 silc_hash_table_list_reset(&htl);
1314 silc_client_unref_channel(client, conn, channel);
1317 silc_fsm_next(fsm, silc_client_command_reply_processed);
1318 return SILC_FSM_CONTINUE;
1321 /********************************** MOTD ************************************/
1323 /* Received reply for MOTD command */
1325 SILC_FSM_STATE(silc_client_command_reply_motd)
1327 SilcClientCommandContext cmd = fsm_context;
1328 SilcClientConnection conn = cmd->conn;
1329 SilcClient client = conn->client;
1330 SilcCommandPayload payload = state_context;
1331 SilcArgumentPayload args = silc_command_get_args(payload);
1333 char *motd = NULL, *cp, line[256];
1336 CHECK_STATUS("Cannot get motd: ");
1339 if (silc_argument_get_arg_num(args) == 3) {
1340 motd = silc_argument_get_arg_type(args, 3, NULL);
1342 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1349 if (cp[i++] == '\n') {
1350 memset(line, 0, sizeof(line));
1351 silc_strncat(line, sizeof(line), cp, i - 1);
1358 SAY(client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", line);
1367 /* Notify application */
1368 silc_client_command_callback(cmd, motd);
1371 silc_fsm_next(fsm, silc_client_command_reply_processed);
1372 return SILC_FSM_CONTINUE;
1375 /********************************** UMODE ***********************************/
1377 /* Received reply to the UMODE command. Save the current user mode */
1379 SILC_FSM_STATE(silc_client_command_reply_umode)
1381 SilcClientCommandContext cmd = fsm_context;
1382 SilcClientConnection conn = cmd->conn;
1383 SilcCommandPayload payload = state_context;
1384 SilcArgumentPayload args = silc_command_get_args(payload);
1386 SilcUInt32 mode, len;
1389 CHECK_STATUS("Cannot change mode: ");
1392 tmp = silc_argument_get_arg_type(args, 2, &len);
1393 if (!tmp || len != 4) {
1394 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1398 SILC_GET32_MSB(mode, tmp);
1399 silc_rwlock_wrlock(conn->local_entry->internal.lock);
1400 conn->local_entry->mode = mode;
1401 silc_rwlock_unlock(conn->local_entry->internal.lock);
1403 /* Notify application */
1404 silc_client_command_callback(cmd, mode);
1407 silc_fsm_next(fsm, silc_client_command_reply_processed);
1408 return SILC_FSM_CONTINUE;
1411 /********************************** CMODE ***********************************/
1413 /* Received reply for CMODE command. */
1415 SILC_FSM_STATE(silc_client_command_reply_cmode)
1417 SilcClientCommandContext cmd = fsm_context;
1418 SilcClientConnection conn = cmd->conn;
1419 SilcClient client = conn->client;
1420 SilcCommandPayload payload = state_context;
1421 SilcArgumentPayload args = silc_command_get_args(payload);
1424 SilcChannelEntry channel = NULL;
1426 SilcPublicKey public_key = NULL;
1430 CHECK_STATUS("Cannot change mode: ");
1433 /* Take Channel ID */
1434 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1435 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1439 /* Get the channel entry */
1440 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1442 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1446 /* Get founder public key */
1447 tmp = silc_argument_get_arg_type(args, 4, &len);
1449 silc_public_key_payload_decode(tmp, len, &public_key);
1451 /* Get channel mode */
1452 tmp = silc_argument_get_arg_type(args, 3, &len);
1453 if (!tmp || len != 4) {
1454 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1457 SILC_GET32_MSB(mode, tmp);
1459 silc_rwlock_wrlock(channel->internal.lock);
1461 /* Get user limit */
1462 tmp = silc_argument_get_arg_type(args, 6, &len);
1463 if (tmp && len == 4)
1464 SILC_GET32_MSB(channel->user_limit, tmp);
1465 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1466 channel->user_limit = 0;
1468 /* Get channel public key(s) */
1469 tmp = silc_argument_get_arg_type(args, 5, &len);
1471 silc_client_channel_save_public_keys(channel, tmp, len, FALSE);
1472 else if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH)
1473 silc_client_channel_save_public_keys(channel, NULL, 0, TRUE);
1476 channel->mode = mode;
1478 silc_rwlock_unlock(channel->internal.lock);
1480 /* Notify application */
1481 silc_client_command_callback(cmd, channel, mode, public_key,
1482 channel->channel_pubkeys, channel->user_limit);
1484 silc_rwlock_wrlock(channel->internal.lock);
1486 /* If founder key changed, update it */
1488 (!channel->founder_key ||
1489 !silc_pkcs_public_key_compare(public_key, channel->founder_key))) {
1490 if (channel->founder_key)
1491 silc_pkcs_public_key_free(channel->founder_key);
1492 channel->founder_key = public_key;
1496 silc_rwlock_unlock(channel->internal.lock);
1499 silc_client_unref_channel(client, conn, channel);
1501 silc_pkcs_public_key_free(public_key);
1502 silc_fsm_next(fsm, silc_client_command_reply_processed);
1503 return SILC_FSM_CONTINUE;
1506 /********************************** CUMODE **********************************/
1508 /* Received reply for CUMODE command */
1510 SILC_FSM_STATE(silc_client_command_reply_cumode)
1512 SilcClientCommandContext cmd = fsm_context;
1513 SilcClientConnection conn = cmd->conn;
1514 SilcClient client = conn->client;
1515 SilcCommandPayload payload = state_context;
1516 SilcArgumentPayload args = silc_command_get_args(payload);
1517 SilcClientEntry client_entry;
1518 SilcChannelEntry channel = NULL;
1519 SilcChannelUser chu;
1520 unsigned char *modev;
1521 SilcUInt32 len, mode;
1525 CHECK_STATUS("Cannot change mode: ");
1528 /* Get channel mode */
1529 modev = silc_argument_get_arg_type(args, 2, &len);
1530 if (!modev || len != 4) {
1531 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1534 SILC_GET32_MSB(mode, modev);
1536 /* Take Channel ID */
1537 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1538 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1542 /* Get the channel entry */
1543 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1545 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1550 if (!silc_argument_get_decoded(args, 4, SILC_ARGUMENT_ID, &id, NULL)) {
1551 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1555 /* Get client entry */
1556 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1557 if (!client_entry) {
1558 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1563 silc_rwlock_wrlock(channel->internal.lock);
1564 chu = silc_client_on_channel(channel, client_entry);
1567 silc_rwlock_unlock(channel->internal.lock);
1569 /* Notify application */
1570 silc_client_command_callback(cmd, mode, channel, client_entry);
1572 silc_client_unref_client(client, conn, client_entry);
1575 silc_client_unref_channel(client, conn, channel);
1576 silc_fsm_next(fsm, silc_client_command_reply_processed);
1577 return SILC_FSM_CONTINUE;
1580 /********************************** KICK ************************************/
1582 SILC_FSM_STATE(silc_client_command_reply_kick)
1584 SilcClientCommandContext cmd = fsm_context;
1585 SilcClientConnection conn = cmd->conn;
1586 SilcClient client = conn->client;
1587 SilcCommandPayload payload = state_context;
1588 SilcArgumentPayload args = silc_command_get_args(payload);
1589 SilcClientEntry client_entry;
1590 SilcChannelEntry channel = NULL;
1594 CHECK_STATUS("Cannot kick: ");
1597 /* Take Channel ID */
1598 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1599 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1603 /* Get the channel entry */
1604 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1606 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1611 if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1612 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1616 /* Get client entry */
1617 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1618 if (!client_entry) {
1619 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1623 /* Notify application */
1624 silc_client_command_callback(cmd, channel, client_entry);
1626 silc_client_unref_client(client, conn, client_entry);
1629 silc_client_unref_channel(client, conn, channel);
1630 silc_fsm_next(fsm, silc_client_command_reply_processed);
1631 return SILC_FSM_CONTINUE;
1634 /******************************** SILCOPER **********************************/
1636 SILC_FSM_STATE(silc_client_command_reply_silcoper)
1638 SilcClientCommandContext cmd = fsm_context;
1639 SilcCommandPayload payload = state_context;
1640 SilcArgumentPayload args = silc_command_get_args(payload);
1643 CHECK_STATUS("Cannot change mode: ");
1647 cmd->conn->local_entry->mode |= SILC_UMODE_ROUTER_OPERATOR;
1649 /* Notify application */
1650 silc_client_command_callback(cmd);
1652 silc_fsm_next(fsm, silc_client_command_reply_processed);
1653 return SILC_FSM_CONTINUE;
1656 /********************************** OPER ************************************/
1658 SILC_FSM_STATE(silc_client_command_reply_oper)
1660 SilcClientCommandContext cmd = fsm_context;
1661 SilcCommandPayload payload = state_context;
1662 SilcArgumentPayload args = silc_command_get_args(payload);
1665 CHECK_STATUS("Cannot change mode: ");
1669 cmd->conn->local_entry->mode |= SILC_UMODE_SERVER_OPERATOR;
1671 /* Notify application */
1672 silc_client_command_callback(cmd);
1674 silc_fsm_next(fsm, silc_client_command_reply_processed);
1675 return SILC_FSM_CONTINUE;
1678 /********************************* DETACH ***********************************/
1680 SILC_FSM_STATE(silc_client_command_reply_detach)
1682 SilcClientCommandContext cmd = fsm_context;
1683 SilcClientConnection conn = cmd->conn;
1684 SilcClient client = conn->client;
1685 SilcCommandPayload payload = state_context;
1686 SilcArgumentPayload args = silc_command_get_args(payload);
1690 CHECK_STATUS("Cannot detach: ");
1693 /* Get detachment data */
1694 detach = silc_client_get_detach_data(client, conn);
1696 ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
1700 /* Notify application */
1701 silc_client_command_callback(cmd, detach);
1702 silc_buffer_free(detach);
1705 silc_fsm_next(fsm, silc_client_command_reply_processed);
1706 return SILC_FSM_CONTINUE;
1709 /********************************** WATCH ***********************************/
1711 SILC_FSM_STATE(silc_client_command_reply_watch)
1713 SilcClientCommandContext cmd = fsm_context;
1714 SilcCommandPayload payload = state_context;
1715 SilcArgumentPayload args = silc_command_get_args(payload);
1718 CHECK_STATUS("Cannot set watch: ");
1721 /* Notify application */
1722 silc_client_command_callback(cmd);
1724 silc_fsm_next(fsm, silc_client_command_reply_processed);
1725 return SILC_FSM_CONTINUE;
1728 /*********************************** BAN ************************************/
1730 SILC_FSM_STATE(silc_client_command_reply_ban)
1732 SilcClientCommandContext cmd = fsm_context;
1733 SilcClientConnection conn = cmd->conn;
1734 SilcClient client = conn->client;
1735 SilcCommandPayload payload = state_context;
1736 SilcArgumentPayload args = silc_command_get_args(payload);
1737 SilcChannelEntry channel = NULL;
1740 SilcArgumentPayload invite_args = NULL;
1744 CHECK_STATUS("Cannot set ban: ");
1747 /* Take Channel ID */
1748 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1749 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1753 /* Get the channel entry */
1754 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1756 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1760 /* Get the invite list */
1761 tmp = silc_argument_get_arg_type(args, 3, &len);
1763 invite_args = silc_argument_list_parse(tmp, len);
1765 /* Notify application */
1766 silc_client_command_callback(cmd, channel, invite_args);
1769 silc_argument_payload_free(invite_args);
1772 silc_client_unref_channel(client, conn, channel);
1773 silc_fsm_next(fsm, silc_client_command_reply_processed);
1774 return SILC_FSM_CONTINUE;
1777 /********************************** LEAVE ***********************************/
1779 /* Reply to LEAVE command. */
1781 SILC_FSM_STATE(silc_client_command_reply_leave)
1783 SilcClientCommandContext cmd = fsm_context;
1784 SilcClientConnection conn = cmd->conn;
1785 SilcClient client = conn->client;
1786 SilcCommandPayload payload = state_context;
1787 SilcArgumentPayload args = silc_command_get_args(payload);
1788 SilcChannelEntry channel;
1792 CHECK_STATUS("Cannot set leave: ");
1795 /* Get Channel ID */
1796 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1797 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1801 /* Get the channel entry */
1802 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1804 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1808 /* Remove us from this channel. */
1809 silc_client_remove_from_channel(client, conn, channel, conn->local_entry);
1811 /* Notify application */
1812 silc_client_command_callback(cmd, channel);
1814 /* Now delete the channel. */
1815 silc_client_empty_channel(client, conn, channel);
1816 silc_client_del_channel(client, conn, channel);
1819 silc_fsm_next(fsm, silc_client_command_reply_processed);
1820 return SILC_FSM_CONTINUE;
1823 /********************************* USERS ************************************/
1825 /* Continue USERS command reply processing after resolving unknown users */
1828 silc_client_command_reply_users_resolved(SilcClient client,
1829 SilcClientConnection conn,
1834 SilcClientCommandContext cmd = context;
1835 SILC_FSM_CALL_CONTINUE(&cmd->thread);
1839 /* Continue USERS command after resolving unknown channel */
1842 silc_client_command_reply_users_continue(SilcClient client,
1843 SilcClientConnection conn,
1848 SilcClientCommandContext cmd = context;
1851 SilcCommandPayload payload = silc_fsm_get_state_context(&cmd->thread);
1852 SilcArgumentPayload args = silc_command_get_args(payload);
1854 cmd->status = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1855 ERROR_CALLBACK(cmd->status);
1856 silc_fsm_next(&cmd->thread, silc_client_command_reply_processed);
1859 SILC_FSM_CALL_CONTINUE(&cmd->thread);
1862 /* Reply to USERS command. Received list of client ID's and theirs modes
1863 on the channel we requested. */
1865 SILC_FSM_STATE(silc_client_command_reply_users)
1867 SilcClientCommandContext cmd = fsm_context;
1868 SilcClientConnection conn = cmd->conn;
1869 SilcClient client = conn->client;
1870 SilcCommandPayload payload = state_context;
1871 SilcArgumentPayload args = silc_command_get_args(payload);
1873 SilcUInt32 tmp_len, list_count, mode;
1875 SilcHashTableList htl;
1876 SilcBufferStruct client_id_list, client_mode_list;
1877 SilcChannelEntry channel = NULL;
1878 SilcClientEntry client_entry;
1883 CHECK_STATUS("Cannot get users: ");
1886 /* Get channel ID */
1887 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1888 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1892 /* Get channel entry */
1893 channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1895 /* Resolve the channel from server */
1896 SILC_FSM_CALL(silc_client_get_channel_by_id_resolve(
1897 client, conn, &id.u.channel_id,
1898 silc_client_command_reply_users_continue, cmd));
1902 /* Get the list count */
1903 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1904 if (!tmp || tmp_len != 4) {
1905 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1908 SILC_GET32_MSB(list_count, tmp);
1910 /* Get Client ID list */
1911 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1913 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1916 silc_buffer_set(&client_id_list, tmp, tmp_len);
1918 /* Resolve users we do not know about */
1919 if (!cmd->resolved) {
1920 cmd->resolved = TRUE;
1921 silc_client_unref_channel(client, conn, channel);
1922 SILC_FSM_CALL(silc_client_get_clients_by_list(
1923 client, conn, list_count, &client_id_list,
1924 silc_client_command_reply_users_resolved, cmd));
1928 /* Get client mode list */
1929 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1931 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1934 silc_buffer_set(&client_mode_list, tmp, tmp_len);
1936 SILC_LOG_DEBUG(("channel %s, %d users", channel->channel_name, list_count));
1938 silc_rwlock_wrlock(channel->internal.lock);
1940 /* Cache the received Client ID's and modes. */
1941 for (i = 0; i < list_count; i++) {
1942 SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1944 if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1948 SILC_GET32_MSB(mode, client_mode_list.data);
1950 /* Save the client on this channel. Unknown clients are ignored as they
1951 clearly do not exist since the resolving didn't find them. */
1952 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1953 if (client_entry && client_entry->internal.valid) {
1954 silc_rwlock_wrlock(client_entry->internal.lock);
1955 silc_client_add_to_channel(client, conn, channel, client_entry, mode);
1956 silc_rwlock_unlock(client_entry->internal.lock);
1958 silc_client_unref_client(client, conn, client_entry);
1960 if (!silc_buffer_pull(&client_id_list, idp_len)) {
1961 silc_rwlock_unlock(channel->internal.lock);
1964 if (!silc_buffer_pull(&client_mode_list, 4)) {
1965 silc_rwlock_unlock(channel->internal.lock);
1970 silc_rwlock_unlock(channel->internal.lock);
1972 /* Notify application */
1973 silc_hash_table_list(channel->user_list, &htl);
1974 silc_client_command_callback(cmd, channel, &htl);
1975 silc_hash_table_list_reset(&htl);
1978 silc_client_unref_channel(client, conn, channel);
1979 silc_fsm_next(fsm, silc_client_command_reply_processed);
1980 return SILC_FSM_CONTINUE;
1983 /********************************** GETKEY **********************************/
1985 /* Received command reply to GETKEY command. WE've received the remote
1986 client's public key. */
1988 SILC_FSM_STATE(silc_client_command_reply_getkey)
1990 SilcClientCommandContext cmd = fsm_context;
1991 SilcClientConnection conn = cmd->conn;
1992 SilcClient client = conn->client;
1993 SilcCommandPayload payload = state_context;
1994 SilcArgumentPayload args = silc_command_get_args(payload);
1995 SilcClientEntry client_entry;
1996 SilcServerEntry server_entry;
1999 SilcPublicKey public_key;
2003 CHECK_STATUS("Cannot get key: ");
2007 if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
2008 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2012 /* Get the public key */
2013 tmp = silc_argument_get_arg_type(args, 3, &len);
2015 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2018 if (!silc_public_key_payload_decode(tmp, len, &public_key)) {
2019 SAY(client, conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR,
2020 "Cannot decode public key: malformed/unsupported public key");
2021 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2025 if (id.type == SILC_ID_CLIENT) {
2026 /* Received client's public key */
2027 client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
2028 if (!client_entry) {
2029 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2033 silc_rwlock_wrlock(client_entry->internal.lock);
2035 /* Save fingerprint */
2036 if (!client_entry->fingerprint)
2037 silc_hash_make(conn->internal->sha1hash, tmp + 4, len - 4,
2038 client_entry->fingerprint);
2039 if (!client_entry->public_key) {
2040 client_entry->public_key = public_key;
2044 silc_rwlock_unlock(client_entry->internal.lock);
2046 /* Notify application */
2047 silc_client_command_callback(cmd, SILC_ID_CLIENT, client_entry,
2048 client_entry->public_key);
2049 silc_client_unref_client(client, conn, client_entry);
2050 } else if (id.type == SILC_ID_SERVER) {
2051 /* Received server's public key */
2052 server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
2053 if (!server_entry) {
2054 ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2058 silc_rwlock_wrlock(server_entry->internal.lock);
2060 if (!server_entry->public_key) {
2061 server_entry->public_key = public_key;
2065 silc_rwlock_unlock(server_entry->internal.lock);
2067 /* Notify application */
2068 silc_client_command_callback(cmd, SILC_ID_SERVER, server_entry,
2069 server_entry->public_key);
2070 silc_client_unref_server(client, conn, server_entry);
2075 silc_pkcs_public_key_free(public_key);
2076 silc_fsm_next(fsm, silc_client_command_reply_processed);
2077 return SILC_FSM_CONTINUE;
2080 /********************************** SERVICE *********************************/
2082 /* Reply to SERVICE command. */
2083 /* XXX incomplete */
2085 SILC_FSM_STATE(silc_client_command_reply_service)
2087 SilcClientCommandContext cmd = fsm_context;
2088 SilcCommandPayload payload = state_context;
2089 SilcArgumentPayload args = silc_command_get_args(payload);
2091 unsigned char *service_list, *name;
2094 CHECK_STATUS("Cannot get service: ");
2096 /* Get service list */
2097 service_list = silc_argument_get_arg_type(args, 2, &tmp_len);
2099 /* Get requested service name */
2100 name = silc_argument_get_arg_type(args, 3, &tmp_len);
2102 /* Notify application */
2103 silc_client_command_callback(cmd, service_list, name);
2105 silc_fsm_next(fsm, silc_client_command_reply_processed);
2106 return SILC_FSM_CONTINUE;
2109 /*********************************** QUIT ***********************************/
2111 /* QUIT command reply stub */
2113 SILC_FSM_STATE(silc_client_command_reply_quit)
2115 silc_fsm_next(fsm, silc_client_command_reply_processed);
2116 return SILC_FSM_CONTINUE;