If channel user list could not be resolved or was not even
[crypto.git] / lib / silcclient / command_reply.c
1 /*
2
3   command_reply.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "silcclient.h"
23 #include "client_internal.h"
24
25 /************************** Types and definitions ***************************/
26
27 /* Calls error command reply callback back to command sender. */
28 #define ERROR_CALLBACK(err)                                     \
29 do {                                                            \
30   void *arg1 = NULL, *arg2 = NULL;                              \
31   if (cmd->status != SILC_STATUS_OK)                            \
32     silc_status_get_args(cmd->status, args, &arg1, &arg2);      \
33   else                                                          \
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);                \
38 } while(0)
39
40 /* Check for error */
41 #define CHECK_STATUS(msg)                                               \
42   SILC_LOG_DEBUG(("%s", silc_get_command_name(cmd->cmd)));              \
43   if (cmd->error != SILC_STATUS_OK) {                                   \
44     if (cmd->verbose)                                                   \
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;                                           \
51   }
52
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;                                   \
60   }
61
62 #define SAY cmd->conn->client->internal->ops->say
63
64 /************************ Static utility functions **************************/
65
66 /* Delivers the command reply back to application */
67
68 static inline void
69 silc_client_command_callback(SilcClientCommandContext cmd, ...)
70 {
71   SilcClientCommandReplyCallback cb;
72   SilcList list;
73   va_list ap, cp;
74
75   va_start(ap, cmd);
76
77   /* Default reply callback */
78   if (cmd->called) {
79     silc_va_copy(cp, ap);
80     cmd->conn->client->internal->ops->command_reply(
81                        cmd->conn->client, cmd->conn, cmd->cmd, cmd->status,
82                        cmd->error, cp);
83     va_end(cp);
84   }
85
86   /* Reply callback */
87   list = cmd->reply_callbacks;
88   silc_list_start(list);
89   while ((cb = silc_list_get(list)))
90     if (!cb->do_not_call) {
91       silc_va_copy(cp, ap);
92       cb->do_not_call = !cb->reply(cmd->conn->client, cmd->conn, cmd->cmd,
93                                    cmd->status, cmd->error, cb->context, cp);
94       va_end(cp);
95     }
96
97   va_end(ap);
98 }
99
100 /* Handles common error status types. */
101
102 static void silc_client_command_process_error(SilcClientCommandContext cmd,
103                                               SilcCommandPayload payload,
104                                               SilcStatus error)
105 {
106   SilcClient client = cmd->conn->client;
107   SilcClientConnection conn = cmd->conn;
108   SilcArgumentPayload args = silc_command_get_args(payload);
109   SilcID id;
110
111   if (cmd->error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
112     SilcClientEntry client_entry;
113
114     /* Remove unknown client entry from cache */
115     if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
116       return;
117
118     client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
119     if (client_entry) {
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);
123     }
124     return;
125   }
126
127   if (cmd->error == SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID) {
128     SilcChannelEntry channel;
129
130     /* Remove unknown channel entry from cache */
131     if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
132       return;
133
134     channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
135     if (channel) {
136       silc_client_empty_channel(client, conn, channel);
137       silc_client_del_channel(client, conn, channel);
138       silc_client_unref_channel(client, conn, channel);
139     }
140     return;
141   }
142
143   if (cmd->error == SILC_STATUS_ERR_NO_SUCH_SERVER_ID) {
144     SilcServerEntry server_entry;
145
146     /* Remove unknown server entry from cache */
147     if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL))
148       return;
149
150     server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
151     if (server_entry) {
152       silc_client_del_server(client, conn, server_entry);
153       silc_client_unref_server(client, conn, server_entry);
154     }
155     return;
156   }
157 }
158
159 /***************************** Command Reply ********************************/
160
161 /* Process received command reply packet */
162
163 SILC_FSM_STATE(silc_client_command_reply)
164 {
165   SilcClientConnection conn = fsm_context;
166   SilcPacket packet = state_context;
167   SilcClientCommandContext cmd;
168   SilcCommandPayload payload;
169   SilcCommand command;
170   SilcUInt16 cmd_ident;
171
172   /* Get command reply payload from packet */
173   payload = silc_command_payload_parse(silc_buffer_datalen(&packet->buffer));
174   silc_packet_free(packet);
175   if (!payload) {
176     SILC_LOG_DEBUG(("Bad command reply packet"));
177     return SILC_FSM_FINISH;
178   }
179
180   cmd_ident = silc_command_get_ident(payload);
181   command = silc_command_get(payload);
182
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);
190       break;
191     }
192   }
193   silc_mutex_unlock(conn->internal->lock);
194
195   if (!cmd) {
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;
200   }
201
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);
209
210   return SILC_FSM_FINISH;
211 }
212
213 /* Wait here for command reply to arrive from remote host */
214
215 SILC_FSM_STATE(silc_client_command_reply_wait)
216 {
217   SilcClientCommandContext cmd = fsm_context;
218
219   SILC_LOG_DEBUG(("Wait for command reply"));
220
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;
226 }
227
228 /* Timeout occurred while waiting command reply */
229
230 SILC_FSM_STATE(silc_client_command_reply_timeout)
231 {
232   SilcClientCommandContext cmd = fsm_context;
233   SilcClientConnection conn = cmd->conn;
234   SilcArgumentPayload args = NULL;
235
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);
239     if (!cmd->called)
240       ERROR_CALLBACK(SILC_STATUS_ERR_TIMEDOUT);
241     return SILC_FSM_FINISH;
242   }
243
244   SILC_LOG_DEBUG(("Command %s timeout", silc_get_command_name(cmd->cmd)));
245
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;
250 }
251
252 /* Process received command reply payload */
253
254 SILC_FSM_STATE(silc_client_command_reply_process)
255 {
256   SilcClientCommandContext cmd = fsm_context;
257   SilcCommandPayload payload = state_context;
258
259   silc_command_get_status(payload, &cmd->status, &cmd->error);
260
261   switch (cmd->cmd) {
262   case SILC_COMMAND_WHOIS:
263     /** WHOIS */
264     silc_fsm_next(fsm, silc_client_command_reply_whois);
265     break;
266   case SILC_COMMAND_WHOWAS:
267     /** WHOWAS */
268     silc_fsm_next(fsm, silc_client_command_reply_whowas);
269     break;
270   case SILC_COMMAND_IDENTIFY:
271     /** IDENTIFY */
272     silc_fsm_next(fsm, silc_client_command_reply_identify);
273     break;
274   case SILC_COMMAND_NICK:
275     /** NICK */
276     silc_fsm_next(fsm, silc_client_command_reply_nick);
277     break;
278   case SILC_COMMAND_LIST:
279     /** LIST */
280     silc_fsm_next(fsm, silc_client_command_reply_list);
281     break;
282   case SILC_COMMAND_TOPIC:
283     /** TOPIC */
284     silc_fsm_next(fsm, silc_client_command_reply_topic);
285     break;
286   case SILC_COMMAND_INVITE:
287     /** INVITE */
288     silc_fsm_next(fsm, silc_client_command_reply_invite);
289     break;
290   case SILC_COMMAND_QUIT:
291     /** QUIT */
292     silc_fsm_next(fsm, silc_client_command_reply_quit);
293     break;
294   case SILC_COMMAND_KILL:
295     /** KILL */
296     silc_fsm_next(fsm, silc_client_command_reply_kill);
297     break;
298   case SILC_COMMAND_INFO:
299     /** INFO */
300     silc_fsm_next(fsm, silc_client_command_reply_info);
301     break;
302   case SILC_COMMAND_STATS:
303     /** STATS */
304     silc_fsm_next(fsm, silc_client_command_reply_stats);
305     break;
306   case SILC_COMMAND_PING:
307     /** PING */
308     silc_fsm_next(fsm, silc_client_command_reply_ping);
309     break;
310   case SILC_COMMAND_OPER:
311     /** OPER */
312     silc_fsm_next(fsm, silc_client_command_reply_oper);
313     break;
314   case SILC_COMMAND_JOIN:
315     /** JOIN */
316     silc_fsm_next(fsm, silc_client_command_reply_join);
317     break;
318   case SILC_COMMAND_MOTD:
319     /** MOTD */
320     silc_fsm_next(fsm, silc_client_command_reply_motd);
321     break;
322   case SILC_COMMAND_UMODE:
323     /** UMODE */
324     silc_fsm_next(fsm, silc_client_command_reply_umode);
325     break;
326   case SILC_COMMAND_CMODE:
327     /** CMODE */
328     silc_fsm_next(fsm, silc_client_command_reply_cmode);
329     break;
330   case SILC_COMMAND_CUMODE:
331     /** CUMODE */
332     silc_fsm_next(fsm, silc_client_command_reply_cumode);
333     break;
334   case SILC_COMMAND_KICK:
335     /** KICK */
336     silc_fsm_next(fsm, silc_client_command_reply_kick);
337     break;
338   case SILC_COMMAND_BAN:
339     /** BAN */
340     silc_fsm_next(fsm, silc_client_command_reply_ban);
341     break;
342   case SILC_COMMAND_DETACH:
343     /** DETACH */
344     silc_fsm_next(fsm, silc_client_command_reply_detach);
345     break;
346   case SILC_COMMAND_WATCH:
347     /** WATCH */
348     silc_fsm_next(fsm, silc_client_command_reply_watch);
349     break;
350   case SILC_COMMAND_SILCOPER:
351     /** SILCOPER */
352     silc_fsm_next(fsm, silc_client_command_reply_silcoper);
353     break;
354   case SILC_COMMAND_LEAVE:
355     /** LEAVE */
356     silc_fsm_next(fsm, silc_client_command_reply_leave);
357     break;
358   case SILC_COMMAND_USERS:
359     /** USERS */
360     silc_fsm_next(fsm, silc_client_command_reply_users);
361     break;
362   case SILC_COMMAND_GETKEY:
363     /** GETKEY */
364     silc_fsm_next(fsm, silc_client_command_reply_getkey);
365     break;
366   case SILC_COMMAND_SERVICE:
367     /** SERVICE */
368     silc_fsm_next(fsm, silc_client_command_reply_service);
369     break;
370   default:
371     return SILC_FSM_FINISH;
372   }
373
374   return SILC_FSM_CONTINUE;
375 }
376
377 /* Completes command reply processing */
378
379 SILC_FSM_STATE(silc_client_command_reply_processed)
380 {
381   SilcClientCommandContext cmd = fsm_context;
382   SilcClientConnection conn = cmd->conn;
383   SilcCommandPayload payload = state_context;
384
385   silc_command_payload_free(payload);
386
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;
390
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);
396
397   /** Wait more command payloads */
398   silc_fsm_next(fsm, silc_client_command_reply_wait);
399   return SILC_FSM_CONTINUE;
400 }
401
402 /******************************** WHOIS *************************************/
403
404 /* Received reply for WHOIS command. */
405
406 SILC_FSM_STATE(silc_client_command_reply_whois)
407 {
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;
418   SilcID id;
419   char *nickname = NULL, *username = NULL, *realname = NULL;
420   unsigned char *fingerprint, *tmp;
421
422   CHECK_STATUS("WHOIS: ");
423   CHECK_ARGS(5, 11);
424
425   /* Get Client ID */
426   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
427     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
428     goto out;
429   }
430
431   /* Get names */
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);
437     goto out;
438   }
439
440   /* Get joined channel list */
441   memset(&channels, 0, sizeof(channels));
442   tmp = silc_argument_get_arg_type(args, 6, &len);
443   if (tmp) {
444     has_channels = TRUE;
445     silc_buffer_set(&channels, tmp, len);
446
447     /* Get channel user mode list */
448     tmp = silc_argument_get_arg_type(args, 10, &len);
449     if (!tmp) {
450       ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
451       goto out;
452     }
453     silc_buffer_set(&ch_user_modes, tmp, len);
454   }
455
456   /* Get user mode */
457   tmp = silc_argument_get_arg_type(args, 7, &len);
458   if (tmp)
459     SILC_GET32_MSB(mode, tmp);
460
461   /* Get idle time */
462   tmp = silc_argument_get_arg_type(args, 8, &len);
463   if (tmp)
464     SILC_GET32_MSB(idle, tmp);
465
466   /* Get fingerprint */
467   fingerprint = silc_argument_get_arg_type(args, 9, &fingerprint_len);
468
469   /* Check if we have this client cached already. */
470   client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
471   if (!client_entry) {
472     SILC_LOG_DEBUG(("Adding new client entry (WHOIS)"));
473     client_entry =
474       silc_client_add_client(client, conn, nickname, username, realname,
475                              &id.u.client_id, mode);
476     if (!client_entry) {
477       ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
478       goto out;
479     }
480     silc_client_ref_client(client, conn, client_entry);
481   } else {
482     silc_client_update_client(client, conn, client_entry,
483                               nickname, username, realname, mode);
484   }
485
486   silc_rwlock_wrlock(client_entry->internal.lock);
487
488   if (fingerprint && fingerprint_len == sizeof(client_entry->fingerprint))
489     memcpy(client_entry->fingerprint, fingerprint, fingerprint_len);
490
491   /* Get user attributes */
492   tmp = silc_argument_get_arg_type(args, 11, &len);
493   if (tmp) {
494     if (client_entry->attrs)
495       silc_attribute_payload_list_free(client_entry->attrs);
496     client_entry->attrs = silc_attribute_payload_parse(tmp, len);
497   }
498
499   silc_rwlock_unlock(client_entry->internal.lock);
500
501   /* Parse channel and channel user mode list */
502   if (has_channels) {
503     channel_list = silc_channel_payload_parse_list(silc_buffer_data(&channels),
504                                                    silc_buffer_len(&channels));
505     if (channel_list)
506       silc_get_mode_list(&ch_user_modes, silc_dlist_count(channel_list),
507                          &umodes);
508   }
509
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);
514
515   silc_client_unref_client(client, conn, client_entry);
516   if (has_channels) {
517     silc_channel_payload_list_free(channel_list);
518     silc_free(umodes);
519   }
520
521  out:
522   silc_fsm_next(fsm, silc_client_command_reply_processed);
523   return SILC_FSM_CONTINUE;
524 }
525
526 /******************************** WHOWAS ************************************/
527
528 /* Received reply for WHOWAS command. */
529
530 SILC_FSM_STATE(silc_client_command_reply_whowas)
531 {
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;
538   SilcID id;
539   char *nickname, *username;
540   char *realname = NULL;
541
542   CHECK_STATUS("WHOWAS: ");
543   CHECK_ARGS(4, 5);
544
545   /* Get Client ID */
546   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
547     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
548     goto out;
549   }
550
551   /* Get the client entry */
552   client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
553
554   /* Get names */
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);
560     goto out;
561   }
562
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,
566                                realname);
567
568  out:
569   silc_client_unref_client(client, conn, client_entry);
570   silc_fsm_next(fsm, silc_client_command_reply_processed);
571   return SILC_FSM_CONTINUE;
572 }
573
574 /******************************** IDENTIFY **********************************/
575
576 /* Received reply for IDENTIFY command. */
577
578 SILC_FSM_STATE(silc_client_command_reply_identify)
579 {
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;
588   SilcUInt32 len;
589   SilcID id;
590   char *name = NULL, *info = NULL;
591
592   CHECK_STATUS("IDENTIFY: ");
593   CHECK_ARGS(2, 4);
594
595   /* Get the ID */
596   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
597     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
598     goto out;
599   }
600
601   /* Get names */
602   name = silc_argument_get_arg_type(args, 3, &len);
603   info = silc_argument_get_arg_type(args, 4, &len);
604
605   switch (id.type) {
606   case SILC_ID_CLIENT:
607     SILC_LOG_DEBUG(("Received client information"));
608
609     /* Check if we have this client cached already. */
610     client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
611     if (!client_entry) {
612       SILC_LOG_DEBUG(("Adding new client entry (IDENTIFY)"));
613       client_entry =
614         silc_client_add_client(client, conn, name, info, NULL,
615                                &id.u.client_id, 0);
616       if (!client_entry) {
617         ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
618         goto out;
619       }
620       silc_client_ref_client(client, conn, client_entry);
621     } else {
622       silc_client_update_client(client, conn, client_entry,
623                                 name, info, NULL, 0);
624     }
625
626     /* Notify application */
627     silc_client_command_callback(cmd, client_entry, name, info);
628     silc_client_unref_client(client, conn, client_entry);
629     break;
630
631   case SILC_ID_SERVER:
632     SILC_LOG_DEBUG(("Received server information"));
633
634     /* Check if we have this server cached already. */
635     server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
636     if (!server_entry) {
637       SILC_LOG_DEBUG(("Adding new server entry (IDENTIFY)"));
638       server_entry = silc_client_add_server(client, conn, name, info,
639                                             &id.u.server_id);
640       if (!server_entry) {
641         ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
642         goto out;
643       }
644       silc_client_ref_server(client, conn, server_entry);
645     } else {
646       silc_client_update_server(client, conn, server_entry, name, info);
647     }
648     server_entry->internal.resolve_cmd_ident = 0;
649
650     /* Notify application */
651     silc_client_command_callback(cmd, server_entry, name, info);
652     silc_client_unref_server(client, conn, server_entry);
653     break;
654
655   case SILC_ID_CHANNEL:
656     SILC_LOG_DEBUG(("Received channel information"));
657
658     /* Check if we have this channel cached already. */
659     channel_entry = silc_client_get_channel_by_id(client, conn,
660                                                   &id.u.channel_id);
661     if (!channel_entry) {
662       SILC_LOG_DEBUG(("Adding new channel entry (IDENTIFY)"));
663
664       if (!name) {
665         ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
666         goto out;
667       }
668
669       /* Add new channel entry */
670       channel_entry = silc_client_add_channel(client, conn, name, 0,
671                                               &id.u.channel_id);
672       if (!channel_entry) {
673         ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
674         goto out;
675       }
676       silc_client_ref_channel(client, conn, channel_entry);
677     }
678
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);
683     break;
684   }
685
686  out:
687   silc_fsm_next(fsm, silc_client_command_reply_processed);
688   return SILC_FSM_CONTINUE;
689 }
690
691 /********************************** NICK ************************************/
692
693 /* Received reply for command NICK. */
694
695 SILC_FSM_STATE(silc_client_command_reply_nick)
696 {
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;
705   SilcID id;
706
707   /* Sanity checks */
708   CHECK_STATUS("Cannot set nickname: ");
709   CHECK_ARGS(2, 3);
710
711   /* Take received Client ID */
712   idp = silc_argument_get_arg_type(args, 2, &idp_len);
713   if (!idp) {
714     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
715     goto out;
716   }
717   if (!silc_id_payload_parse_id(idp, idp_len, &id)) {
718     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
719     goto out;
720   }
721
722   /* Take the new nickname */
723   nick = silc_argument_get_arg_type(args, 3, &len);
724   if (!nick) {
725     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
726     goto out;
727   }
728
729   silc_rwlock_wrlock(conn->local_entry->internal.lock);
730
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);
737     goto out;
738   }
739
740   silc_rwlock_unlock(conn->local_entry->internal.lock);
741
742   /* Notify application */
743   silc_client_command_callback(cmd, conn->local_entry,
744                                conn->local_entry->nickname, &old_client_id);
745
746  out:
747   silc_fsm_next(fsm, silc_client_command_reply_processed);
748   return SILC_FSM_CONTINUE;
749 }
750
751 /********************************** LIST ************************************/
752
753 /* Received reply to the LIST command. */
754
755 SILC_FSM_STATE(silc_client_command_reply_list)
756 {
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;
765   SilcID id;
766
767   /* Sanity checks */
768   CHECK_STATUS("Cannot list channels: ");
769
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;
775   }
776
777   CHECK_ARGS(3, 5);
778
779   name = silc_argument_get_arg_type(args, 3, NULL);
780   if (!name) {
781     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
782     goto out;
783   }
784
785   topic = silc_argument_get_arg_type(args, 4, NULL);
786   tmp = silc_argument_get_arg_type(args, 5, NULL);
787   if (tmp)
788     SILC_GET32_MSB(usercount, tmp);
789
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,
792                                                 &id.u.channel_id);
793   if (!channel_entry) {
794     /* Add new channel entry */
795     channel_entry = silc_client_add_channel(client, conn, name, 0,
796                                             &id.u.channel_id);
797     if (!channel_entry) {
798       ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
799       goto out;
800     }
801     silc_client_ref_channel(client, conn, channel_entry);
802   }
803
804   /* Notify application */
805   silc_client_command_callback(cmd, channel_entry, channel_entry->channel_name,
806                                topic, usercount);
807
808  out:
809   silc_client_unref_channel(client, conn, channel_entry);
810   silc_fsm_next(fsm, silc_client_command_reply_processed);
811   return SILC_FSM_CONTINUE;
812 }
813
814 /********************************* TOPIC ************************************/
815
816 /* Received reply to topic command. */
817
818 SILC_FSM_STATE(silc_client_command_reply_topic)
819 {
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;
826   char *topic;
827   SilcUInt32 len;
828   SilcID id;
829
830   /* Sanity checks */
831   CHECK_STATUS("Cannot set topic: ");
832   CHECK_ARGS(2, 3);
833
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);
837     goto out;
838   }
839
840   /* Get the channel entry */
841   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
842   if (!channel) {
843     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
844     goto out;
845   }
846
847   silc_rwlock_wrlock(channel->internal.lock);
848
849   /* Take topic */
850   topic = silc_argument_get_arg_type(args, 3, &len);
851   if (topic) {
852     silc_free(channel->topic);
853     channel->topic = silc_memdup(topic, len);
854   }
855
856   silc_rwlock_unlock(channel->internal.lock);
857
858   /* Notify application */
859   silc_client_command_callback(cmd, channel, channel->topic);
860
861  out:
862   silc_client_unref_channel(client, conn, channel);
863   silc_fsm_next(fsm, silc_client_command_reply_processed);
864   return SILC_FSM_CONTINUE;
865 }
866
867 /********************************* INVITE ***********************************/
868
869 /* Received reply to invite command. */
870
871 SILC_FSM_STATE(silc_client_command_reply_invite)
872 {
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;
879   unsigned char *tmp;
880   SilcUInt32 len;
881   SilcArgumentPayload invite_args = NULL;
882   SilcID id;
883
884   /* Sanity checks */
885   CHECK_STATUS("Cannot invite: ");
886   CHECK_ARGS(2, 3);
887
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);
891     goto out;
892   }
893
894   /* Get the channel entry */
895   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
896   if (!channel) {
897     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
898     goto out;
899   }
900
901   /* Get the invite list */
902   tmp = silc_argument_get_arg_type(args, 3, &len);
903   if (tmp)
904     invite_args = silc_argument_list_parse(tmp, len);
905
906   /* Notify application */
907   silc_client_command_callback(cmd, channel, invite_args);
908
909   if (invite_args)
910     silc_argument_payload_free(invite_args);
911
912  out:
913   silc_client_unref_channel(client, conn, channel);
914   silc_fsm_next(fsm, silc_client_command_reply_processed);
915   return SILC_FSM_CONTINUE;
916 }
917
918 /********************************** KILL ************************************/
919
920 /* Received reply to the KILL command. */
921
922 SILC_FSM_STATE(silc_client_command_reply_kill)
923 {
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;
930   SilcID id;
931
932   /* Sanity checks */
933   CHECK_STATUS("Cannot kill: ");
934   CHECK_ARGS(2, 2);
935
936   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
937     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
938     goto out;
939   }
940
941   /* Get the client entry, if exists */
942   client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
943
944   /* Notify application */
945   silc_client_command_callback(cmd, client_entry);
946
947   /* Remove the client */
948   if (client_entry) {
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);
952   }
953
954  out:
955   silc_fsm_next(fsm, silc_client_command_reply_processed);
956   return SILC_FSM_CONTINUE;
957 }
958
959 /********************************** INFO ************************************/
960
961 /* Received reply to INFO command. We receive the server ID and some
962    information about the server user requested. */
963
964 SILC_FSM_STATE(silc_client_command_reply_info)
965 {
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;
973   SilcID id;
974
975   /* Sanity checks */
976   CHECK_STATUS("Cannot get info: ");
977   CHECK_ARGS(4, 4);
978
979   /* Get server ID */
980   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
981     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
982     goto out;
983   }
984
985   /* Get server name */
986   server_name = silc_argument_get_arg_type(args, 3, NULL);
987   if (!server_name) {
988     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
989     goto out;
990   }
991
992   /* Get server info */
993   server_info = silc_argument_get_arg_type(args, 4, NULL);
994   if (!server_info) {
995     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
996     goto out;
997   }
998
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);
1001   if (!server) {
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);
1005     if (!server)
1006       goto out;
1007     silc_client_ref_server(client, conn, server);
1008   }
1009
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);
1014
1015  out:
1016   silc_fsm_next(fsm, silc_client_command_reply_processed);
1017   return SILC_FSM_CONTINUE;
1018 }
1019
1020 /********************************** STATS ***********************************/
1021
1022 /* Received reply to STATS command. */
1023
1024 SILC_FSM_STATE(silc_client_command_reply_stats)
1025 {
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;
1032   SilcBufferStruct b;
1033   SilcID id;
1034
1035   /* Sanity checks */
1036   CHECK_STATUS("Cannot get stats: ");
1037   CHECK_ARGS(2, 3);
1038
1039   /* Get server ID */
1040   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1041     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1042     goto out;
1043   }
1044
1045   /* Get statistics structure */
1046   memset(&stats, 0, sizeof(stats));
1047   buf = silc_argument_get_arg_type(args, 3, &buf_len);
1048   if (buf) {
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),
1066                          SILC_STR_END);
1067   }
1068
1069   /* Notify application */
1070   silc_client_command_callback(cmd, &stats);
1071
1072  out:
1073   silc_fsm_next(fsm, silc_client_command_reply_processed);
1074   return SILC_FSM_CONTINUE;
1075 }
1076
1077 /********************************** PING ************************************/
1078
1079 /* Received reply to PING command. */
1080
1081 SILC_FSM_STATE(silc_client_command_reply_ping)
1082 {
1083   SilcClientCommandContext cmd = fsm_context;
1084   SilcClientConnection conn = cmd->conn;
1085   SilcClient client = conn->client;
1086   SilcInt64 diff;
1087
1088   diff = silc_time() - SILC_PTR_TO_64(cmd->context);
1089   if (cmd->verbose)
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");
1093
1094   /* Notify application */
1095   silc_client_command_callback(cmd);
1096
1097   silc_fsm_next(fsm, silc_client_command_reply_processed);
1098   return SILC_FSM_CONTINUE;
1099 }
1100
1101 /********************************** JOIN ************************************/
1102
1103 /* Continue JOIN command reply processing after resolving unknown users */
1104
1105 static void
1106 silc_client_command_reply_join_resolved(SilcClient client,
1107                                         SilcClientConnection conn,
1108                                         SilcStatus status,
1109                                         SilcDList clients,
1110                                         void *context)
1111 {
1112   SilcClientCommandContext cmd = context;
1113   SilcChannelEntry channel = cmd->context;
1114   SilcCommandPayload payload = silc_fsm_get_state_context(&cmd->thread);
1115   SilcArgumentPayload args = silc_command_get_args(payload);
1116   SilcUInt32 list_count;
1117   unsigned char *tmp;
1118   char msg[512];
1119
1120   if (!clients) {
1121     silc_snprintf(msg, sizeof(msg), "Error resolving channel %s user list",
1122                   channel->channel_name);
1123     SAY(client, conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR, msg);
1124   } else {
1125     tmp = silc_argument_get_arg_type(args, 12, NULL);
1126     if (tmp) {
1127       SILC_GET32_MSB(list_count, tmp);
1128       if (list_count - silc_dlist_count(clients) > 5) {
1129         silc_snprintf(msg, sizeof(msg),
1130                       "Channel %s user list was not fully resolved. "
1131                       "The channel may not be fully synced.",
1132                       channel->channel_name);
1133         SAY(client, conn, SILC_CLIENT_MESSAGE_WARNING, msg);
1134       }
1135     }
1136   }
1137
1138   channel->internal.resolve_cmd_ident = 0;
1139   silc_client_unref_channel(client, conn, channel);
1140
1141   SILC_FSM_CALL_CONTINUE(&cmd->thread);
1142 }
1143
1144
1145 /* Received reply for JOIN command. */
1146
1147 SILC_FSM_STATE(silc_client_command_reply_join)
1148 {
1149   SilcClientCommandContext cmd = fsm_context;
1150   SilcClientConnection conn = cmd->conn;
1151   SilcClient client = conn->client;
1152   SilcCommandPayload payload = state_context;
1153   SilcArgumentPayload args = silc_command_get_args(payload);
1154   SilcChannelEntry channel;
1155   SilcUInt32 mode = 0, len, list_count;
1156   char *topic, *tmp, *channel_name = NULL, *hmac;
1157   const char *cipher;
1158   SilcBufferStruct client_id_list, client_mode_list, keyp;
1159   SilcHashTableList htl;
1160   SilcID id;
1161   int i;
1162
1163   /* Sanity checks */
1164   CHECK_STATUS("Cannot join channel: ");
1165   CHECK_ARGS(9, 17);
1166
1167   /* Get channel name */
1168   channel_name = silc_argument_get_arg_type(args, 2, NULL);
1169   if (!channel_name) {
1170     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1171     goto out;
1172   }
1173
1174   /* Get Channel ID */
1175   if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1176     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1177     goto out;
1178   }
1179
1180   /* Check whether we have this channel entry already. */
1181   channel = silc_client_get_channel(client, conn, channel_name);
1182   if (channel) {
1183     if (!SILC_ID_CHANNEL_COMPARE(&channel->id, &id.u.channel_id))
1184       silc_client_replace_channel_id(client, conn, channel, &id.u.channel_id);
1185   } else {
1186     /* Create new channel entry */
1187     channel = silc_client_add_channel(client, conn, channel_name,
1188                                       mode, &id.u.channel_id);
1189     if (!channel) {
1190       ERROR_CALLBACK(SILC_STATUS_ERR_BAD_CHANNEL);
1191       goto out;
1192     }
1193     silc_client_ref_channel(client, conn, channel);
1194   }
1195
1196   /* Get the list count */
1197   tmp = silc_argument_get_arg_type(args, 12, &len);
1198   if (!tmp || len != 4) {
1199     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1200     goto out;
1201   }
1202   SILC_GET32_MSB(list_count, tmp);
1203
1204   /* Get Client ID list */
1205   tmp = silc_argument_get_arg_type(args, 13, &len);
1206   if (!tmp) {
1207     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1208     goto out;
1209   }
1210   silc_buffer_set(&client_id_list, tmp, len);
1211
1212   /* Resolve users we do not know about */
1213   if (!cmd->resolved) {
1214     cmd->resolved = TRUE;
1215     cmd->context = channel;
1216     SILC_FSM_CALL(channel->internal.resolve_cmd_ident =
1217                   silc_client_get_clients_by_list(
1218                           client, conn, list_count, &client_id_list,
1219                           silc_client_command_reply_join_resolved, cmd));
1220     /* NOT REACHED */
1221   }
1222
1223   /* Get client mode list */
1224   tmp = silc_argument_get_arg_type(args, 14, &len);
1225   if (!tmp) {
1226     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1227     goto out;
1228   }
1229   silc_buffer_set(&client_mode_list, tmp, len);
1230
1231   silc_rwlock_wrlock(channel->internal.lock);
1232
1233   /* Add clients we received in the reply to the channel */
1234   for (i = 0; i < list_count; i++) {
1235     SilcUInt16 idp_len;
1236     SilcID id;
1237     SilcClientEntry client_entry;
1238
1239     /* Client ID */
1240     SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1241     idp_len += 4;
1242     if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1243       continue;
1244
1245     /* Mode */
1246     SILC_GET32_MSB(mode, client_mode_list.data);
1247
1248     /* Get client entry */
1249     client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1250     if (client_entry && client_entry->internal.valid) {
1251       /* Join client to the channel */
1252       silc_rwlock_wrlock(client_entry->internal.lock);
1253       silc_client_add_to_channel(client, conn, channel, client_entry, mode);
1254       silc_rwlock_unlock(client_entry->internal.lock);
1255     }
1256     silc_client_unref_client(client, conn, client_entry);
1257
1258     if (!silc_buffer_pull(&client_id_list, idp_len)) {
1259       silc_rwlock_unlock(channel->internal.lock);
1260       goto out;
1261     }
1262     if (!silc_buffer_pull(&client_mode_list, 4)) {
1263       silc_rwlock_unlock(channel->internal.lock);
1264       goto out;
1265     }
1266   }
1267
1268   /* Get hmac */
1269   hmac = silc_argument_get_arg_type(args, 11, NULL);
1270   if (hmac) {
1271     if (!silc_hmac_alloc(hmac, NULL, &channel->internal.hmac)) {
1272       if (cmd->verbose)
1273         SAY(client, conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR,
1274             "Cannot join channel: Unsupported HMAC `%s'", hmac);
1275       ERROR_CALLBACK(SILC_STATUS_ERR_UNKNOWN_ALGORITHM);
1276       silc_rwlock_unlock(channel->internal.lock);
1277       goto out;
1278     }
1279   }
1280
1281   /* Get channel mode */
1282   tmp = silc_argument_get_arg_type(args, 5, &len);
1283   if (tmp && len == 4)
1284     SILC_GET32_MSB(mode, tmp);
1285   channel->mode = mode;
1286
1287   /* Get channel key and save it */
1288   tmp = silc_argument_get_arg_type(args, 7, &len);
1289   if (tmp) {
1290     silc_buffer_set(&keyp, tmp, len);
1291     silc_client_save_channel_key(client, conn, &keyp, channel);
1292   }
1293
1294   /* Get topic */
1295   topic = silc_argument_get_arg_type(args, 10, NULL);
1296   if (topic) {
1297     silc_free(channel->topic);
1298     channel->topic = silc_memdup(topic, strlen(topic));
1299   }
1300
1301   /* Get founder key */
1302   tmp = silc_argument_get_arg_type(args, 15, &len);
1303   if (tmp) {
1304     if (channel->founder_key)
1305       silc_pkcs_public_key_free(channel->founder_key);
1306     channel->founder_key = NULL;
1307     silc_public_key_payload_decode(tmp, len, &channel->founder_key);
1308   }
1309
1310   /* Get user limit */
1311   tmp = silc_argument_get_arg_type(args, 17, &len);
1312   if (tmp && len == 4)
1313     SILC_GET32_MSB(channel->user_limit, tmp);
1314   if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1315     channel->user_limit = 0;
1316
1317   /* Get channel public key list */
1318   tmp = silc_argument_get_arg_type(args, 16, &len);
1319   if (tmp)
1320     silc_client_channel_save_public_keys(channel, tmp, len, FALSE);
1321
1322   /* Set current channel */
1323   conn->current_channel = channel;
1324
1325   silc_rwlock_unlock(channel->internal.lock);
1326
1327   cipher = (channel->internal.send_key ?
1328             silc_cipher_get_name(channel->internal.send_key) : NULL);
1329   silc_hash_table_list(channel->user_list, &htl);
1330
1331   /* Notify application */
1332   silc_client_command_callback(cmd, channel->channel_name, channel, mode, &htl,
1333                                topic, cipher, hmac, channel->founder_key,
1334                                channel->channel_pubkeys, channel->user_limit);
1335
1336   silc_hash_table_list_reset(&htl);
1337   silc_client_unref_channel(client, conn, channel);
1338
1339  out:
1340   silc_fsm_next(fsm, silc_client_command_reply_processed);
1341   return SILC_FSM_CONTINUE;
1342 }
1343
1344 /********************************** MOTD ************************************/
1345
1346 /* Received reply for MOTD command */
1347
1348 SILC_FSM_STATE(silc_client_command_reply_motd)
1349 {
1350   SilcClientCommandContext cmd = fsm_context;
1351   SilcClientConnection conn = cmd->conn;
1352   SilcClient client = conn->client;
1353   SilcCommandPayload payload = state_context;
1354   SilcArgumentPayload args = silc_command_get_args(payload);
1355   SilcUInt32 i;
1356   char *motd = NULL, *cp, line[256];
1357
1358   /* Sanity checks */
1359   CHECK_STATUS("Cannot get motd: ");
1360   CHECK_ARGS(2, 3);
1361
1362   if (silc_argument_get_arg_num(args) == 3) {
1363     motd = silc_argument_get_arg_type(args, 3, NULL);
1364     if (!motd) {
1365       ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1366       goto out;
1367     }
1368
1369     i = 0;
1370     cp = motd;
1371     while(cp[i] != 0) {
1372       if (cp[i++] == '\n') {
1373         memset(line, 0, sizeof(line));
1374         silc_strncat(line, sizeof(line), cp, i - 1);
1375         cp += i;
1376
1377         if (i == 2)
1378           line[0] = ' ';
1379
1380         if (cmd->verbose)
1381           SAY(client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", line);
1382
1383         if (!strlen(cp))
1384           break;
1385         i = 0;
1386       }
1387     }
1388   }
1389
1390   /* Notify application */
1391   silc_client_command_callback(cmd, motd);
1392
1393  out:
1394   silc_fsm_next(fsm, silc_client_command_reply_processed);
1395   return SILC_FSM_CONTINUE;
1396 }
1397
1398 /********************************** UMODE ***********************************/
1399
1400 /* Received reply to the UMODE command. Save the current user mode */
1401
1402 SILC_FSM_STATE(silc_client_command_reply_umode)
1403 {
1404   SilcClientCommandContext cmd = fsm_context;
1405   SilcClientConnection conn = cmd->conn;
1406   SilcCommandPayload payload = state_context;
1407   SilcArgumentPayload args = silc_command_get_args(payload);
1408   unsigned char *tmp;
1409   SilcUInt32 mode, len;
1410
1411   /* Sanity checks */
1412   CHECK_STATUS("Cannot change mode: ");
1413   CHECK_ARGS(2, 2);
1414
1415   tmp = silc_argument_get_arg_type(args, 2, &len);
1416   if (!tmp || len != 4) {
1417     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1418     goto out;
1419   }
1420
1421   SILC_GET32_MSB(mode, tmp);
1422   silc_rwlock_wrlock(conn->local_entry->internal.lock);
1423   conn->local_entry->mode = mode;
1424   silc_rwlock_unlock(conn->local_entry->internal.lock);
1425
1426   /* Notify application */
1427   silc_client_command_callback(cmd, mode);
1428
1429  out:
1430   silc_fsm_next(fsm, silc_client_command_reply_processed);
1431   return SILC_FSM_CONTINUE;
1432 }
1433
1434 /********************************** CMODE ***********************************/
1435
1436 /* Received reply for CMODE command. */
1437
1438 SILC_FSM_STATE(silc_client_command_reply_cmode)
1439 {
1440   SilcClientCommandContext cmd = fsm_context;
1441   SilcClientConnection conn = cmd->conn;
1442   SilcClient client = conn->client;
1443   SilcCommandPayload payload = state_context;
1444   SilcArgumentPayload args = silc_command_get_args(payload);
1445   unsigned char *tmp;
1446   SilcUInt32 mode;
1447   SilcChannelEntry channel = NULL;
1448   SilcUInt32 len;
1449   SilcPublicKey public_key = NULL;
1450   SilcID id;
1451
1452   /* Sanity checks */
1453   CHECK_STATUS("Cannot change mode: ");
1454   CHECK_ARGS(3, 6);
1455
1456   /* Take Channel ID */
1457   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1458     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1459     goto out;
1460   }
1461
1462   /* Get the channel entry */
1463   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1464   if (!channel) {
1465     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1466     goto out;
1467   }
1468
1469   /* Get founder public key */
1470   tmp = silc_argument_get_arg_type(args, 4, &len);
1471   if (tmp)
1472     silc_public_key_payload_decode(tmp, len, &public_key);
1473
1474   /* Get channel mode */
1475   tmp = silc_argument_get_arg_type(args, 3, &len);
1476   if (!tmp || len != 4) {
1477     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1478     goto out;
1479   }
1480   SILC_GET32_MSB(mode, tmp);
1481
1482   silc_rwlock_wrlock(channel->internal.lock);
1483
1484   /* Get user limit */
1485   tmp = silc_argument_get_arg_type(args, 6, &len);
1486   if (tmp && len == 4)
1487     SILC_GET32_MSB(channel->user_limit, tmp);
1488   if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
1489     channel->user_limit = 0;
1490
1491   /* Get channel public key(s) */
1492   tmp = silc_argument_get_arg_type(args, 5, &len);
1493   if (tmp)
1494     silc_client_channel_save_public_keys(channel, tmp, len, FALSE);
1495   else if (channel->mode & SILC_CHANNEL_MODE_CHANNEL_AUTH)
1496     silc_client_channel_save_public_keys(channel, NULL, 0, TRUE);
1497
1498   /* Save the mode */
1499   channel->mode = mode;
1500
1501   silc_rwlock_unlock(channel->internal.lock);
1502
1503   /* Notify application */
1504   silc_client_command_callback(cmd, channel, mode, public_key,
1505                                channel->channel_pubkeys, channel->user_limit);
1506
1507   silc_rwlock_wrlock(channel->internal.lock);
1508
1509   /* If founder key changed, update it */
1510   if (public_key &&
1511       (!channel->founder_key ||
1512        !silc_pkcs_public_key_compare(public_key, channel->founder_key))) {
1513     if (channel->founder_key)
1514       silc_pkcs_public_key_free(channel->founder_key);
1515     channel->founder_key = public_key;
1516     public_key = NULL;
1517   }
1518
1519   silc_rwlock_unlock(channel->internal.lock);
1520
1521  out:
1522   silc_client_unref_channel(client, conn, channel);
1523   if (public_key)
1524     silc_pkcs_public_key_free(public_key);
1525   silc_fsm_next(fsm, silc_client_command_reply_processed);
1526   return SILC_FSM_CONTINUE;
1527 }
1528
1529 /********************************** CUMODE **********************************/
1530
1531 /* Received reply for CUMODE command */
1532
1533 SILC_FSM_STATE(silc_client_command_reply_cumode)
1534 {
1535   SilcClientCommandContext cmd = fsm_context;
1536   SilcClientConnection conn = cmd->conn;
1537   SilcClient client = conn->client;
1538   SilcCommandPayload payload = state_context;
1539   SilcArgumentPayload args = silc_command_get_args(payload);
1540   SilcClientEntry client_entry;
1541   SilcChannelEntry channel = NULL;
1542   SilcChannelUser chu;
1543   unsigned char *modev;
1544   SilcUInt32 len, mode;
1545   SilcID id;
1546
1547   /* Sanity checks */
1548   CHECK_STATUS("Cannot change mode: ");
1549   CHECK_ARGS(4, 4);
1550
1551   /* Get channel mode */
1552   modev = silc_argument_get_arg_type(args, 2, &len);
1553   if (!modev || len != 4) {
1554     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1555     goto out;
1556   }
1557   SILC_GET32_MSB(mode, modev);
1558
1559   /* Take Channel ID */
1560   if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1561     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1562     goto out;
1563   }
1564
1565   /* Get the channel entry */
1566   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1567   if (!channel) {
1568     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1569     goto out;
1570   }
1571
1572   /* Get Client ID */
1573   if (!silc_argument_get_decoded(args, 4, SILC_ARGUMENT_ID, &id, NULL)) {
1574     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1575     goto out;
1576   }
1577
1578   /* Get client entry */
1579   client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1580   if (!client_entry) {
1581     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1582     goto out;
1583   }
1584
1585   /* Save the mode */
1586   silc_rwlock_wrlock(channel->internal.lock);
1587   chu = silc_client_on_channel(channel, client_entry);
1588   if (chu)
1589     chu->mode = mode;
1590   silc_rwlock_unlock(channel->internal.lock);
1591
1592   /* Notify application */
1593   silc_client_command_callback(cmd, mode, channel, client_entry);
1594
1595   silc_client_unref_client(client, conn, client_entry);
1596
1597  out:
1598   silc_client_unref_channel(client, conn, channel);
1599   silc_fsm_next(fsm, silc_client_command_reply_processed);
1600   return SILC_FSM_CONTINUE;
1601 }
1602
1603 /********************************** KICK ************************************/
1604
1605 SILC_FSM_STATE(silc_client_command_reply_kick)
1606 {
1607   SilcClientCommandContext cmd = fsm_context;
1608   SilcClientConnection conn = cmd->conn;
1609   SilcClient client = conn->client;
1610   SilcCommandPayload payload = state_context;
1611   SilcArgumentPayload args = silc_command_get_args(payload);
1612   SilcClientEntry client_entry;
1613   SilcChannelEntry channel = NULL;
1614   SilcID id;
1615
1616   /* Sanity checks */
1617   CHECK_STATUS("Cannot kick: ");
1618   CHECK_ARGS(3, 3);
1619
1620   /* Take Channel ID */
1621   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1622     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1623     goto out;
1624   }
1625
1626   /* Get the channel entry */
1627   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1628   if (!channel) {
1629     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1630     goto out;
1631   }
1632
1633   /* Get Client ID */
1634   if (!silc_argument_get_decoded(args, 3, SILC_ARGUMENT_ID, &id, NULL)) {
1635     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1636     goto out;
1637   }
1638
1639   /* Get client entry */
1640   client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1641   if (!client_entry) {
1642     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1643     goto out;
1644   }
1645
1646   /* Notify application */
1647   silc_client_command_callback(cmd, channel, client_entry);
1648
1649   silc_client_unref_client(client, conn, client_entry);
1650
1651  out:
1652   silc_client_unref_channel(client, conn, channel);
1653   silc_fsm_next(fsm, silc_client_command_reply_processed);
1654   return SILC_FSM_CONTINUE;
1655 }
1656
1657 /******************************** SILCOPER **********************************/
1658
1659 SILC_FSM_STATE(silc_client_command_reply_silcoper)
1660 {
1661   SilcClientCommandContext cmd = fsm_context;
1662   SilcCommandPayload payload = state_context;
1663   SilcArgumentPayload args = silc_command_get_args(payload);
1664
1665   /* Sanity checks */
1666   CHECK_STATUS("Cannot change mode: ");
1667   CHECK_ARGS(1, 1);
1668
1669   /* Set user mode */
1670   cmd->conn->local_entry->mode |= SILC_UMODE_ROUTER_OPERATOR;
1671
1672   /* Notify application */
1673   silc_client_command_callback(cmd);
1674
1675   silc_fsm_next(fsm, silc_client_command_reply_processed);
1676   return SILC_FSM_CONTINUE;
1677 }
1678
1679 /********************************** OPER ************************************/
1680
1681 SILC_FSM_STATE(silc_client_command_reply_oper)
1682 {
1683   SilcClientCommandContext cmd = fsm_context;
1684   SilcCommandPayload payload = state_context;
1685   SilcArgumentPayload args = silc_command_get_args(payload);
1686
1687   /* Sanity checks */
1688   CHECK_STATUS("Cannot change mode: ");
1689   CHECK_ARGS(1, 1);
1690
1691   /* Set user mode */
1692   cmd->conn->local_entry->mode |= SILC_UMODE_SERVER_OPERATOR;
1693
1694   /* Notify application */
1695   silc_client_command_callback(cmd);
1696
1697   silc_fsm_next(fsm, silc_client_command_reply_processed);
1698   return SILC_FSM_CONTINUE;
1699 }
1700
1701 /********************************* DETACH ***********************************/
1702
1703 SILC_FSM_STATE(silc_client_command_reply_detach)
1704 {
1705   SilcClientCommandContext cmd = fsm_context;
1706   SilcClientConnection conn = cmd->conn;
1707   SilcClient client = conn->client;
1708   SilcCommandPayload payload = state_context;
1709   SilcArgumentPayload args = silc_command_get_args(payload);
1710   SilcBuffer detach;
1711
1712   /* Sanity checks */
1713   CHECK_STATUS("Cannot detach: ");
1714   CHECK_ARGS(1, 1);
1715
1716   /* Get detachment data */
1717   detach = silc_client_get_detach_data(client, conn);
1718   if (!detach) {
1719     ERROR_CALLBACK(SILC_STATUS_ERR_RESOURCE_LIMIT);
1720     goto out;
1721   }
1722
1723   /* Notify application */
1724   silc_client_command_callback(cmd, detach);
1725   silc_buffer_free(detach);
1726
1727  out:
1728   silc_fsm_next(fsm, silc_client_command_reply_processed);
1729   return SILC_FSM_CONTINUE;
1730 }
1731
1732 /********************************** WATCH ***********************************/
1733
1734 SILC_FSM_STATE(silc_client_command_reply_watch)
1735 {
1736   SilcClientCommandContext cmd = fsm_context;
1737   SilcCommandPayload payload = state_context;
1738   SilcArgumentPayload args = silc_command_get_args(payload);
1739
1740   /* Sanity checks */
1741   CHECK_STATUS("Cannot set watch: ");
1742   CHECK_ARGS(1, 1);
1743
1744   /* Notify application */
1745   silc_client_command_callback(cmd);
1746
1747   silc_fsm_next(fsm, silc_client_command_reply_processed);
1748   return SILC_FSM_CONTINUE;
1749 }
1750
1751 /*********************************** BAN ************************************/
1752
1753 SILC_FSM_STATE(silc_client_command_reply_ban)
1754 {
1755   SilcClientCommandContext cmd = fsm_context;
1756   SilcClientConnection conn = cmd->conn;
1757   SilcClient client = conn->client;
1758   SilcCommandPayload payload = state_context;
1759   SilcArgumentPayload args = silc_command_get_args(payload);
1760   SilcChannelEntry channel = NULL;
1761   unsigned char *tmp;
1762   SilcUInt32 len;
1763   SilcArgumentPayload invite_args = NULL;
1764   SilcID id;
1765
1766   /* Sanity checks */
1767   CHECK_STATUS("Cannot set ban: ");
1768   CHECK_ARGS(2, 3);
1769
1770   /* Take Channel ID */
1771   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1772     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1773     goto out;
1774   }
1775
1776   /* Get the channel entry */
1777   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1778   if (!channel) {
1779     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1780     goto out;
1781   }
1782
1783   /* Get the invite list */
1784   tmp = silc_argument_get_arg_type(args, 3, &len);
1785   if (tmp)
1786     invite_args = silc_argument_list_parse(tmp, len);
1787
1788   /* Notify application */
1789   silc_client_command_callback(cmd, channel, invite_args);
1790
1791   if (invite_args)
1792     silc_argument_payload_free(invite_args);
1793
1794  out:
1795   silc_client_unref_channel(client, conn, channel);
1796   silc_fsm_next(fsm, silc_client_command_reply_processed);
1797   return SILC_FSM_CONTINUE;
1798 }
1799
1800 /********************************** LEAVE ***********************************/
1801
1802 /* Reply to LEAVE command. */
1803
1804 SILC_FSM_STATE(silc_client_command_reply_leave)
1805 {
1806   SilcClientCommandContext cmd = fsm_context;
1807   SilcClientConnection conn = cmd->conn;
1808   SilcClient client = conn->client;
1809   SilcCommandPayload payload = state_context;
1810   SilcArgumentPayload args = silc_command_get_args(payload);
1811   SilcChannelEntry channel;
1812   SilcID id;
1813
1814   /* Sanity checks */
1815   CHECK_STATUS("Cannot set leave: ");
1816   CHECK_ARGS(2, 2);
1817
1818   /* Get Channel ID */
1819   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1820     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1821     goto out;
1822   }
1823
1824   /* Get the channel entry */
1825   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1826   if (!channel) {
1827     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1828     goto out;
1829   }
1830
1831   /* Remove us from this channel. */
1832   silc_client_remove_from_channel(client, conn, channel, conn->local_entry);
1833
1834   /* Notify application */
1835   silc_client_command_callback(cmd, channel);
1836
1837   /* Now delete the channel. */
1838   silc_client_empty_channel(client, conn, channel);
1839   silc_client_del_channel(client, conn, channel);
1840
1841  out:
1842   silc_fsm_next(fsm, silc_client_command_reply_processed);
1843   return SILC_FSM_CONTINUE;
1844 }
1845
1846 /********************************* USERS ************************************/
1847
1848 /* Continue USERS command reply processing after resolving unknown users */
1849
1850 static void
1851 silc_client_command_reply_users_resolved(SilcClient client,
1852                                          SilcClientConnection conn,
1853                                          SilcStatus status,
1854                                          SilcDList clients,
1855                                          void *context)
1856 {
1857   SilcClientCommandContext cmd = context;
1858   SILC_FSM_CALL_CONTINUE(&cmd->thread);
1859 }
1860
1861
1862 /* Continue USERS command after resolving unknown channel */
1863
1864 static void
1865 silc_client_command_reply_users_continue(SilcClient client,
1866                                          SilcClientConnection conn,
1867                                          SilcStatus status,
1868                                          SilcDList channels,
1869                                          void *context)
1870 {
1871   SilcClientCommandContext cmd = context;
1872
1873   if (!channels) {
1874     SilcCommandPayload payload = silc_fsm_get_state_context(&cmd->thread);
1875     SilcArgumentPayload args = silc_command_get_args(payload);
1876
1877     cmd->status = SILC_STATUS_ERR_NO_SUCH_CHANNEL_ID;
1878     ERROR_CALLBACK(cmd->status);
1879     silc_fsm_next(&cmd->thread, silc_client_command_reply_processed);
1880   }
1881
1882   SILC_FSM_CALL_CONTINUE(&cmd->thread);
1883 }
1884
1885 /* Reply to USERS command. Received list of client ID's and theirs modes
1886    on the channel we requested. */
1887
1888 SILC_FSM_STATE(silc_client_command_reply_users)
1889 {
1890   SilcClientCommandContext cmd = fsm_context;
1891   SilcClientConnection conn = cmd->conn;
1892   SilcClient client = conn->client;
1893   SilcCommandPayload payload = state_context;
1894   SilcArgumentPayload args = silc_command_get_args(payload);
1895   unsigned char *tmp;
1896   SilcUInt32 tmp_len, list_count, mode;
1897   SilcUInt16 idp_len;
1898   SilcHashTableList htl;
1899   SilcBufferStruct client_id_list, client_mode_list;
1900   SilcChannelEntry channel = NULL;
1901   SilcClientEntry client_entry;
1902   SilcID id;
1903   int i;
1904
1905   /* Sanity checks */
1906   CHECK_STATUS("Cannot get users: ");
1907   CHECK_ARGS(5, 5);
1908
1909   /* Get channel ID */
1910   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
1911     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1912     goto out;
1913   }
1914
1915   /* Get channel entry */
1916   channel = silc_client_get_channel_by_id(client, conn, &id.u.channel_id);
1917   if (!channel) {
1918     /* Resolve the channel from server */
1919     SILC_FSM_CALL(silc_client_get_channel_by_id_resolve(
1920                         client, conn, &id.u.channel_id,
1921                         silc_client_command_reply_users_continue, cmd));
1922     /* NOT REACHED */
1923   }
1924
1925   /* Get the list count */
1926   tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1927   if (!tmp || tmp_len != 4) {
1928     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1929     goto out;
1930   }
1931   SILC_GET32_MSB(list_count, tmp);
1932
1933   /* Get Client ID list */
1934   tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1935   if (!tmp) {
1936     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1937     goto out;
1938   }
1939   silc_buffer_set(&client_id_list, tmp, tmp_len);
1940
1941   /* Resolve users we do not know about */
1942   if (!cmd->resolved) {
1943     cmd->resolved = TRUE;
1944     silc_client_unref_channel(client, conn, channel);
1945     SILC_FSM_CALL(silc_client_get_clients_by_list(
1946                           client, conn, list_count, &client_id_list,
1947                           silc_client_command_reply_users_resolved, cmd));
1948     /* NOT REACHED */
1949   }
1950
1951   /* Get client mode list */
1952   tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
1953   if (!tmp) {
1954     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
1955     goto out;
1956   }
1957   silc_buffer_set(&client_mode_list, tmp, tmp_len);
1958
1959   SILC_LOG_DEBUG(("channel %s, %d users", channel->channel_name, list_count));
1960
1961   silc_rwlock_wrlock(channel->internal.lock);
1962
1963   /* Cache the received Client ID's and modes. */
1964   for (i = 0; i < list_count; i++) {
1965     SILC_GET16_MSB(idp_len, client_id_list.data + 2);
1966     idp_len += 4;
1967     if (!silc_id_payload_parse_id(client_id_list.data, idp_len, &id))
1968       goto out;
1969
1970     /* Mode */
1971     SILC_GET32_MSB(mode, client_mode_list.data);
1972
1973     /* Save the client on this channel.  Unknown clients are ignored as they
1974        clearly do not exist since the resolving didn't find them. */
1975     client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
1976     if (client_entry && client_entry->internal.valid) {
1977       silc_rwlock_wrlock(client_entry->internal.lock);
1978       silc_client_add_to_channel(client, conn, channel, client_entry, mode);
1979       silc_rwlock_unlock(client_entry->internal.lock);
1980     }
1981     silc_client_unref_client(client, conn, client_entry);
1982
1983     if (!silc_buffer_pull(&client_id_list, idp_len)) {
1984       silc_rwlock_unlock(channel->internal.lock);
1985       goto out;
1986     }
1987     if (!silc_buffer_pull(&client_mode_list, 4)) {
1988       silc_rwlock_unlock(channel->internal.lock);
1989       goto out;
1990     }
1991   }
1992
1993   silc_rwlock_unlock(channel->internal.lock);
1994
1995   /* Notify application */
1996   silc_hash_table_list(channel->user_list, &htl);
1997   silc_client_command_callback(cmd, channel, &htl);
1998   silc_hash_table_list_reset(&htl);
1999
2000  out:
2001   silc_client_unref_channel(client, conn, channel);
2002   silc_fsm_next(fsm, silc_client_command_reply_processed);
2003   return SILC_FSM_CONTINUE;
2004 }
2005
2006 /********************************** GETKEY **********************************/
2007
2008 /* Received command reply to GETKEY command. WE've received the remote
2009    client's public key. */
2010
2011 SILC_FSM_STATE(silc_client_command_reply_getkey)
2012 {
2013   SilcClientCommandContext cmd = fsm_context;
2014   SilcClientConnection conn = cmd->conn;
2015   SilcClient client = conn->client;
2016   SilcCommandPayload payload = state_context;
2017   SilcArgumentPayload args = silc_command_get_args(payload);
2018   SilcClientEntry client_entry;
2019   SilcServerEntry server_entry;
2020   unsigned char *tmp;
2021   SilcUInt32 len;
2022   SilcPublicKey public_key;
2023   SilcID id;
2024
2025   /* Sanity checks */
2026   CHECK_STATUS("Cannot get key: ");
2027   CHECK_ARGS(2, 3);
2028
2029   /* Get the ID */
2030   if (!silc_argument_get_decoded(args, 2, SILC_ARGUMENT_ID, &id, NULL)) {
2031     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2032     goto out;
2033   }
2034
2035   /* Get the public key */
2036   tmp = silc_argument_get_arg_type(args, 3, &len);
2037   if (!tmp) {
2038     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2039     goto out;
2040   }
2041   if (!silc_public_key_payload_decode(tmp, len, &public_key)) {
2042     SAY(client, conn, SILC_CLIENT_MESSAGE_COMMAND_ERROR,
2043         "Cannot decode public key: malformed/unsupported public key");
2044     ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2045     goto out;
2046   }
2047
2048   if (id.type == SILC_ID_CLIENT) {
2049     /* Received client's public key */
2050     client_entry = silc_client_get_client_by_id(client, conn, &id.u.client_id);
2051     if (!client_entry) {
2052       ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2053       goto out;
2054     }
2055
2056     silc_rwlock_wrlock(client_entry->internal.lock);
2057
2058     /* Save fingerprint */
2059     if (!client_entry->fingerprint)
2060       silc_hash_make(conn->internal->sha1hash, tmp + 4, len - 4,
2061                      client_entry->fingerprint);
2062     if (!client_entry->public_key) {
2063       client_entry->public_key = public_key;
2064       public_key = NULL;
2065     }
2066
2067     silc_rwlock_unlock(client_entry->internal.lock);
2068
2069     /* Notify application */
2070     silc_client_command_callback(cmd, SILC_ID_CLIENT, client_entry,
2071                                  client_entry->public_key);
2072     silc_client_unref_client(client, conn, client_entry);
2073   } else if (id.type == SILC_ID_SERVER) {
2074     /* Received server's public key */
2075     server_entry = silc_client_get_server_by_id(client, conn, &id.u.server_id);
2076     if (!server_entry) {
2077       ERROR_CALLBACK(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
2078       goto out;
2079     }
2080
2081     silc_rwlock_wrlock(server_entry->internal.lock);
2082
2083     if (!server_entry->public_key) {
2084       server_entry->public_key = public_key;
2085       public_key = NULL;
2086     }
2087
2088     silc_rwlock_unlock(server_entry->internal.lock);
2089
2090     /* Notify application */
2091     silc_client_command_callback(cmd, SILC_ID_SERVER, server_entry,
2092                                  server_entry->public_key);
2093     silc_client_unref_server(client, conn, server_entry);
2094   }
2095
2096  out:
2097   if (public_key)
2098     silc_pkcs_public_key_free(public_key);
2099   silc_fsm_next(fsm, silc_client_command_reply_processed);
2100   return SILC_FSM_CONTINUE;
2101 }
2102
2103 /********************************** SERVICE *********************************/
2104
2105 /* Reply to SERVICE command. */
2106 /* XXX incomplete */
2107
2108 SILC_FSM_STATE(silc_client_command_reply_service)
2109 {
2110   SilcClientCommandContext cmd = fsm_context;
2111   SilcCommandPayload payload = state_context;
2112   SilcArgumentPayload args = silc_command_get_args(payload);
2113   SilcUInt32 tmp_len;
2114   unsigned char *service_list, *name;
2115
2116   /* Sanity checks */
2117   CHECK_STATUS("Cannot get service: ");
2118
2119   /* Get service list */
2120   service_list = silc_argument_get_arg_type(args, 2, &tmp_len);
2121
2122   /* Get requested service name */
2123   name = silc_argument_get_arg_type(args, 3, &tmp_len);
2124
2125   /* Notify application */
2126   silc_client_command_callback(cmd, service_list, name);
2127
2128   silc_fsm_next(fsm, silc_client_command_reply_processed);
2129   return SILC_FSM_CONTINUE;
2130 }
2131
2132 /*********************************** QUIT ***********************************/
2133
2134 /* QUIT command reply stub */
2135
2136 SILC_FSM_STATE(silc_client_command_reply_quit)
2137 {
2138   silc_fsm_next(fsm, silc_client_command_reply_processed);
2139   return SILC_FSM_CONTINUE;
2140 }