Fixed WATCH notify handling.
[silc.git] / lib / silcclient / client_register.c
1 /*
2
3   client_register.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2006 - 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
20 #include "silc.h"
21 #include "silcclient.h"
22 #include "client_internal.h"
23
24 /************************** Types and definitions ***************************/
25
26 /* Resume session context */
27 typedef struct {
28   SilcClient client;
29   SilcClientConnection conn;
30   SilcBufferStruct detach;
31   char *nickname;
32   SilcUInt32 channel_count;
33 } *SilcClientResumeSession;
34
35 /************************ Static utility functions **************************/
36
37 /* Continues resuming after resolving.  Continue after last reply. */
38
39 static SilcBool
40 silc_client_resume_continue(SilcClient client,
41                             SilcClientConnection conn,
42                             SilcCommand command,
43                             SilcStatus status,
44                             SilcStatus error,
45                             void *context,
46                             va_list ap)
47 {
48   if (status == SILC_STATUS_OK || status == SILC_STATUS_LIST_END ||
49       SILC_STATUS_IS_ERROR(status)) {
50     silc_fsm_continue(&conn->internal->event_thread);
51     return FALSE;
52   }
53
54   return TRUE;
55 }
56
57 /* Function used to call command replies back to application in resuming. */
58
59 static void
60 silc_client_resume_command_callback(SilcClient client,
61                                     SilcClientConnection conn,
62                                     SilcCommand command, ...)
63 {
64   va_list ap;
65   va_start(ap, command);
66   client->internal->ops->command_reply(client, conn, command,
67                                        SILC_STATUS_OK, SILC_STATUS_OK, ap);
68   va_end(ap);
69 }
70
71
72 /****************************** NEW_ID packet *******************************/
73
74 /* Received new ID packet from server during registering to SILC network */
75
76 SILC_FSM_STATE(silc_client_new_id)
77 {
78   SilcClientConnection conn = fsm_context;
79   SilcClient client = conn->client;
80   SilcPacket packet = state_context;
81   SilcID id;
82
83   if (conn->local_id)
84     goto out;
85
86   SILC_LOG_DEBUG(("New ID received from server"));
87
88   if (!silc_id_payload_parse_id(silc_buffer_data(&packet->buffer),
89                                 silc_buffer_len(&packet->buffer), &id))
90     goto out;
91
92   SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
93                                               SILC_ID_CLIENT)));
94
95   /* Create local client entry */
96   conn->local_entry = silc_client_add_client(client, conn,
97                                              client->username,
98                                              client->username,
99                                              client->realname,
100                                              &id.u.client_id, 0);
101   if (!conn->local_entry)
102     goto out;
103
104   /* Save the ID.  Take reference to conn->local_id. */
105   conn->local_id = &conn->local_entry->id;
106   conn->internal->local_idp = silc_buffer_copy(&packet->buffer);
107
108   /* Save remote ID */
109   if (packet->src_id_len) {
110     conn->internal->remote_idp =
111       silc_id_payload_encode_data(packet->src_id,
112                                   packet->src_id_len,
113                                   packet->src_id_type);
114     if (!conn->internal->remote_idp)
115       goto out;
116     silc_id_payload_parse_id(silc_buffer_data(conn->internal->remote_idp),
117                              silc_buffer_len(conn->internal->remote_idp),
118                              &conn->remote_id);
119   }
120
121   /* Set IDs to the packet stream */
122   silc_packet_set_ids(conn->stream, SILC_ID_CLIENT, conn->local_id,
123                       conn->remote_id.type, SILC_ID_GET_ID(conn->remote_id));
124
125   /* Signal connection that new ID was received so it can continue
126      with the registering. */
127   if (conn->internal->registering)
128     silc_fsm_continue_sync(&conn->internal->event_thread);
129
130  out:
131   /** Packet processed */
132   silc_packet_free(packet);
133   return SILC_FSM_FINISH;
134 }
135
136
137 /************************ Register to SILC network **************************/
138
139 /* Register to network */
140
141 SILC_FSM_STATE(silc_client_st_register)
142 {
143   SilcClientConnection conn = fsm_context;
144   SilcClient client = conn->client;
145   char *nick = NULL;
146
147   SILC_LOG_DEBUG(("Register to network"));
148
149   /* From SILC protocol version 1.3, nickname is in NEW_CLIENT packet */
150   if (conn->internal->remote_version >= 13)
151     nick = (conn->internal->params.nickname ?
152             conn->internal->params.nickname : client->username);
153
154   /* Send NEW_CLIENT packet to register to network */
155   if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
156                            SILC_STR_UI_SHORT(strlen(client->username)),
157                            SILC_STR_DATA(client->username,
158                                          strlen(client->username)),
159                            SILC_STR_UI_SHORT(strlen(client->realname)),
160                            SILC_STR_DATA(client->realname,
161                                          strlen(client->realname)),
162                            SILC_STR_UI_SHORT(nick ? strlen(nick) : 0),
163                            SILC_STR_DATA(nick, nick ? strlen(nick) : 0),
164                            SILC_STR_END)) {
165     /** Error sending packet */
166     silc_fsm_next(fsm, silc_client_st_register_error);
167     return SILC_FSM_CONTINUE;
168   }
169
170   /** Wait for new ID */
171   conn->internal->registering = TRUE;
172   silc_fsm_next_later(fsm, silc_client_st_register_complete,
173                       conn->internal->retry_timer, 0);
174   return SILC_FSM_WAIT;
175 }
176
177 /* Wait for NEW_ID packet to arrive */
178
179 SILC_FSM_STATE(silc_client_st_register_complete)
180 {
181   SilcClientConnection conn = fsm_context;
182   SilcClient client = conn->client;
183
184   if (conn->internal->disconnected) {
185     /** Disconnected */
186     silc_fsm_next(fsm, silc_client_st_register_error);
187     return SILC_FSM_CONTINUE;
188   }
189
190   if (!conn->local_id) {
191     if (conn->internal->retry_count++ >= SILC_CLIENT_RETRY_COUNT) {
192       /** Timeout, ID not received */
193       conn->internal->registering = FALSE;
194       conn->internal->retry_count = 0;
195       conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
196       silc_fsm_next(fsm, silc_client_st_register_error);
197       return SILC_FSM_CONTINUE;
198     }
199
200     /** Resend registering packet */
201     silc_fsm_next(fsm, silc_client_st_register);
202     conn->internal->retry_timer = ((conn->internal->retry_timer *
203                                     SILC_CLIENT_RETRY_MUL) +
204                                    (silc_rng_get_rn16(client->rng) %
205                                     SILC_CLIENT_RETRY_RAND));
206     return SILC_FSM_CONTINUE;
207   }
208
209   SILC_LOG_DEBUG(("Registered to network"));
210
211   /* Issue IDENTIFY command for itself to get resolved hostname
212      correctly from server. */
213   silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
214                            silc_client_command_called_dummy, NULL,
215                            1, 5, silc_buffer_data(conn->internal->local_idp),
216                            silc_buffer_len(conn->internal->local_idp));
217
218   /* With SILC protocol version 1.2 call NICK command if the nickname was
219      set by the application. */
220   if (conn->internal->params.nickname && conn->internal->remote_version < 13 &&
221       !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
222     silc_client_command_call(client, conn, NULL,
223                              "NICK", conn->internal->params.nickname, NULL);
224
225   /* Issue INFO command to fetch the real server name and server
226      information and other stuff. */
227   silc_client_command_send(client, conn, SILC_COMMAND_INFO,
228                            silc_client_command_called_dummy, NULL,
229                            1, 2, silc_buffer_data(conn->internal->remote_idp),
230                            silc_buffer_len(conn->internal->remote_idp));
231
232   /* Call connection callback.  We are now inside SILC network. */
233   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS, 0, NULL,
234                  conn->callback_context);
235
236   conn->internal->registering = FALSE;
237   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
238                                 silc_client_connect_timeout, conn);
239
240   return SILC_FSM_FINISH;
241 }
242
243 /* Error registering to network */
244
245 SILC_FSM_STATE(silc_client_st_register_error)
246 {
247   SilcClientConnection conn = fsm_context;
248
249   SILC_LOG_DEBUG(("Error registering to network"));
250
251   /* Signal to close connection */
252   conn->internal->status = SILC_CLIENT_CONN_ERROR;
253   if (!conn->internal->disconnected) {
254     conn->internal->disconnected = TRUE;
255     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
256   }
257
258   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
259                                 silc_client_connect_timeout, conn);
260
261   return SILC_FSM_FINISH;
262 }
263
264 /************************* Resume detached session **************************/
265
266 /* Resume detached session */
267
268 SILC_FSM_STATE(silc_client_st_resume)
269 {
270   SilcClientConnection conn = fsm_context;
271   SilcClient client = conn->client;
272   SilcClientResumeSession resume;
273   SilcBuffer auth;
274   unsigned char *id;
275   SilcUInt16 id_len;
276   SilcClientID client_id;
277   int ret;
278
279   SILC_LOG_DEBUG(("Resuming detached session"));
280
281   resume = silc_calloc(1, sizeof(*resume));
282   if (!resume) {
283     /** Out of memory */
284     silc_fsm_next(fsm, silc_client_st_resume_error);
285     return SILC_FSM_CONTINUE;
286   }
287   silc_fsm_set_state_context(fsm, resume);
288
289   silc_buffer_set(&resume->detach, conn->internal->params.detach_data,
290                   conn->internal->params.detach_data_len);
291   SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(&resume->detach),
292                    silc_buffer_len(&resume->detach));
293
294   /* Take the old client ID from the detachment data */
295   ret = silc_buffer_unformat(&resume->detach,
296                              SILC_STR_ADVANCE,
297                              SILC_STR_UI16_NSTRING_ALLOC(&resume->nickname,
298                                                          NULL),
299                              SILC_STR_UI16_NSTRING(&id, &id_len),
300                              SILC_STR_UI_INT(NULL),
301                              SILC_STR_UI_INT(&resume->channel_count),
302                              SILC_STR_END);
303   if (ret < 0) {
304     /** Malformed detach data */
305     SILC_LOG_DEBUG(("Malformed detachment data"));
306     silc_fsm_next(fsm, silc_client_st_resume_error);
307     return SILC_FSM_CONTINUE;
308   }
309
310   if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
311                       sizeof(client_id))) {
312     /** Malformed ID */
313     SILC_LOG_DEBUG(("Malformed ID"));
314     silc_fsm_next(fsm, silc_client_st_resume_error);
315     return SILC_FSM_CONTINUE;
316   }
317
318   /* Generate authentication data that server will verify */
319   auth = silc_auth_public_key_auth_generate(conn->public_key,
320                                             conn->private_key,
321                                             client->rng,
322                                             conn->internal->hash,
323                                             &client_id, SILC_ID_CLIENT);
324   if (!auth) {
325     /** Out of memory */
326     silc_fsm_next(fsm, silc_client_st_resume_error);
327     return SILC_FSM_CONTINUE;
328   }
329
330   /* Send RESUME_CLIENT packet to resume to network */
331   if (!silc_packet_send_va(conn->stream, SILC_PACKET_RESUME_CLIENT, 0,
332                            SILC_STR_UI_SHORT(id_len),
333                            SILC_STR_DATA(id, id_len),
334                            SILC_STR_DATA(silc_buffer_data(auth),
335                                          silc_buffer_len(auth)),
336                            SILC_STR_END)) {
337     /** Error sending packet */
338     SILC_LOG_DEBUG(("Error sending packet"));
339     silc_fsm_next(fsm, silc_client_st_resume_error);
340     return SILC_FSM_CONTINUE;
341   }
342
343   /** Wait for new ID */
344   conn->internal->registering = TRUE;
345   silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
346   return SILC_FSM_WAIT;
347 }
348
349 /* Resolve the old session information, user mode and joined channels. */
350
351 SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
352 {
353   SilcClientConnection conn = fsm_context;
354   SilcClient client = conn->client;
355   SilcClientResumeSession resume = state_context;
356   SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
357   unsigned char **res_argv = NULL;
358   int i;
359
360   if (conn->internal->disconnected) {
361     /** Disconnected */
362     silc_fsm_next(fsm, silc_client_st_resume_error);
363     return SILC_FSM_CONTINUE;
364   }
365
366   if (!conn->local_id) {
367     /** Timeout, ID not received */
368     conn->internal->registering = FALSE;
369     silc_fsm_next(fsm, silc_client_st_resume_error);
370     return SILC_FSM_CONTINUE;
371   }
372
373   /** Wait for channels */
374   silc_fsm_next(fsm, silc_client_st_resume_resolve_cmodes);
375
376   /* Change our nickname */
377   silc_client_change_nickname(client, conn, conn->local_entry,
378                               resume->nickname, NULL, NULL, 0);
379
380   /* Send UMODE command to get our own user mode in the network */
381   SILC_LOG_DEBUG(("Resolving user mode"));
382   silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
383                            silc_client_command_called_dummy, NULL,
384                            1, 1, silc_buffer_data(conn->internal->local_idp),
385                            silc_buffer_len(conn->internal->local_idp));
386
387   if (!resume->channel_count)
388     return SILC_FSM_YIELD;
389
390   /* Send IDENTIFY command for all channels we know about.  These are the
391      channels we've joined to according our detachment data. */
392   for (i = 0; i < resume->channel_count; i++) {
393     SilcChannelEntry channel;
394     unsigned char *chid;
395     SilcUInt16 chid_len;
396     SilcBuffer idp;
397     SilcChannelID channel_id;
398     char *name;
399
400     if (silc_buffer_unformat(&resume->detach,
401                              SILC_STR_ADVANCE,
402                              SILC_STR_UI16_NSTRING(&name, NULL),
403                              SILC_STR_UI16_NSTRING(&chid, &chid_len),
404                              SILC_STR_UI_INT(NULL),
405                              SILC_STR_END) < 0)
406       continue;
407
408     if (!silc_id_str2id(chid, chid_len, SILC_ID_CHANNEL, &channel_id,
409                         sizeof(channel_id)))
410       continue;
411     idp = silc_id_payload_encode_data(chid, chid_len, SILC_ID_CHANNEL);
412     if (!idp)
413       continue;
414
415     /* Add the channel to cache */
416     channel = silc_client_get_channel_by_id(client, conn, &channel_id);
417     if (!channel)
418       silc_client_add_channel(client, conn, name, 0, &channel_id);
419
420     res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
421     res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
422                                  (res_argc + 1));
423     res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
424                                   (res_argc + 1));
425     res_argv[res_argc] = silc_buffer_steal(idp, &res_argv_lens[res_argc]);
426     res_argv_types[res_argc] = res_argc + 5;
427     res_argc++;
428     silc_buffer_free(idp);
429   }
430
431   /* Send IDENTIFY command */
432   SILC_LOG_DEBUG(("Resolving joined channels"));
433   silc_client_command_send_argv(client, conn, SILC_COMMAND_IDENTIFY,
434                                 silc_client_resume_continue, conn,
435                                 res_argc, res_argv, res_argv_lens,
436                                 res_argv_types);
437
438   for (i = 0; i < resume->channel_count; i++)
439     silc_free(res_argv[i]);
440   silc_free(res_argv);
441   silc_free(res_argv_lens);
442   silc_free(res_argv_types);
443
444   return SILC_FSM_WAIT;
445 }
446
447 /* Resolve joined channel modes, users and topics. */
448
449 SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
450 {
451   SilcClientConnection conn = fsm_context;
452   SilcClient client = conn->client;
453   SilcClientResumeSession resume = state_context;
454   SilcIDCacheEntry entry;
455   SilcChannelEntry channel;
456   SilcList channels;
457   SilcBuffer idp;
458
459   if (conn->internal->disconnected) {
460     /** Disconnected */
461     silc_fsm_next(fsm, silc_client_st_resume_error);
462     return SILC_FSM_CONTINUE;
463   }
464
465   SILC_LOG_DEBUG(("Resolving channel details"));
466
467   /** Wait for channel modes */
468   silc_fsm_next(fsm, silc_client_st_resume_completed);
469
470   if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
471     return SILC_FSM_YIELD;
472
473   /* Resolve channels' mode, users and topic */
474   resume->channel_count = silc_list_count(channels) * 3;
475   silc_list_start(channels);
476   while ((entry = silc_list_get(channels))) {
477     channel = entry->context;
478     idp = silc_id_payload_encode(&channel->id, SILC_ID_CHANNEL);
479     if (!idp)
480       continue;
481
482     silc_client_command_send(client, conn, SILC_COMMAND_CMODE,
483                              silc_client_resume_continue, conn, 1,
484                              1, silc_buffer_data(idp),
485                              silc_buffer_len(idp));
486     silc_client_command_send(client, conn, SILC_COMMAND_USERS,
487                              silc_client_resume_continue, conn, 1,
488                              1, silc_buffer_data(idp),
489                              silc_buffer_len(idp));
490     silc_client_command_send(client, conn, SILC_COMMAND_TOPIC,
491                              silc_client_resume_continue, conn, 1,
492                              1, silc_buffer_data(idp),
493                              silc_buffer_len(idp));
494     silc_buffer_free(idp);
495   }
496
497   return SILC_FSM_WAIT;
498 }
499
500 /* Resuming completed */
501
502 SILC_FSM_STATE(silc_client_st_resume_completed)
503 {
504   SilcClientConnection conn = fsm_context;
505   SilcClient client = conn->client;
506   SilcClientResumeSession resume = state_context;
507   SilcIDCacheEntry entry;
508   SilcChannelEntry channel;
509   SilcList channels;
510
511   if (conn->internal->disconnected) {
512     /** Disconnected */
513     silc_fsm_next(fsm, silc_client_st_resume_error);
514     return SILC_FSM_CONTINUE;
515   }
516
517   if (resume->channel_count > 0) {
518     resume->channel_count--;
519     if (resume->channel_count)
520       return SILC_FSM_WAIT;
521   }
522
523   SILC_LOG_DEBUG(("Resuming completed"));
524
525   /* Issue IDENTIFY command for itself to get resolved hostname
526      correctly from server. */
527   silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
528                            silc_client_command_called_dummy, NULL,
529                            1, 5, silc_buffer_data(conn->internal->local_idp),
530                            silc_buffer_len(conn->internal->local_idp));
531
532   /* Issue INFO command to fetch the real server name and server
533      information and other stuff. */
534   silc_client_command_send(client, conn, SILC_COMMAND_INFO,
535                            silc_client_command_called_dummy, NULL,
536                            1, 2, silc_buffer_data(conn->internal->remote_idp),
537                            silc_buffer_len(conn->internal->remote_idp));
538
539   /* Call connection callback.  We have now resumed to SILC network. */
540   conn->callback(client, conn, SILC_CLIENT_CONN_SUCCESS_RESUME, 0, NULL,
541                  conn->callback_context);
542
543   /* Call UMODE command reply. */
544   if (conn->local_entry->mode)
545     silc_client_resume_command_callback(client, conn, SILC_COMMAND_UMODE,
546                                         conn->local_entry->mode);
547
548   /* Call NICK command reply. */
549   silc_client_resume_command_callback(client, conn, SILC_COMMAND_NICK,
550                                       conn->local_entry,
551                                       conn->local_entry->nickname,
552                                       &conn->local_entry->id);
553
554   /* Call JOIN command replies for all joined channel */
555   silc_idcache_get_all(conn->internal->channel_cache, &channels);
556   silc_list_start(channels);
557   while ((entry = silc_list_get(channels))) {
558     SilcHashTableList htl;
559     const char *cipher, *hmac;
560
561     channel = entry->context;
562     cipher = (channel->internal.send_key ?
563               silc_cipher_get_name(channel->internal.send_key) : NULL);
564     hmac = (channel->internal.hmac ?
565             silc_hmac_get_name(channel->internal.hmac) : NULL);
566     silc_hash_table_list(channel->user_list, &htl);
567     silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
568                                         channel->channel_name, channel,
569                                         channel->mode, &htl, channel->topic,
570                                         cipher, hmac, channel->founder_key,
571                                         channel->channel_pubkeys,
572                                         channel->user_limit);
573     silc_hash_table_list_reset(&htl);
574   }
575
576   conn->internal->registering = FALSE;
577   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
578                                 silc_client_connect_timeout, conn);
579   silc_free(resume->nickname);
580   silc_free(resume);
581
582   return SILC_FSM_FINISH;
583 }
584
585 /* Error resuming to network */
586
587 SILC_FSM_STATE(silc_client_st_resume_error)
588 {
589   SilcClientConnection conn = fsm_context;
590   SilcClientResumeSession resume = state_context;
591
592   if (conn->internal->disconnected) {
593     if (resume) {
594       silc_free(resume->nickname);
595       silc_free(resume);
596     }
597     return SILC_FSM_FINISH;
598   }
599
600   SILC_LOG_DEBUG(("Error resuming to network"));
601
602   /* Signal to close connection */
603   conn->internal->status = SILC_CLIENT_CONN_ERROR;
604   if (!conn->internal->disconnected) {
605     conn->internal->disconnected = TRUE;
606     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
607   }
608
609   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
610                                 silc_client_connect_timeout, conn);
611
612   if (resume) {
613     silc_free(resume->nickname);
614     silc_free(resume);
615   }
616
617   return SILC_FSM_FINISH;
618 }
619
620 /* Generates the session detachment data. This data can be used later
621    to resume back to the server. */
622
623 SilcBuffer silc_client_get_detach_data(SilcClient client,
624                                        SilcClientConnection conn)
625 {
626   SilcBuffer detach;
627   SilcHashTableList htl;
628   SilcChannelUser chu;
629   unsigned char id[64];
630   SilcUInt32 id_len;
631   int ret, ch_count;
632
633   SILC_LOG_DEBUG(("Creating detachment data"));
634
635   ch_count = silc_hash_table_count(conn->local_entry->channels);
636   silc_id_id2str(conn->local_id, SILC_ID_CLIENT, id, sizeof(id), &id_len);
637
638   /* Save the nickname, Client ID and user mode in SILC network */
639   detach = silc_buffer_alloc(0);
640   if (!detach)
641     return NULL;
642   ret =
643     silc_buffer_format(detach,
644                        SILC_STR_ADVANCE,
645                        SILC_STR_UI_SHORT(strlen(conn->local_entry->nickname)),
646                        SILC_STR_DATA(conn->local_entry->nickname,
647                                      strlen(conn->local_entry->nickname)),
648                        SILC_STR_UI_SHORT(id_len),
649                        SILC_STR_DATA(id, id_len),
650                        SILC_STR_UI_INT(conn->local_entry->mode),
651                        SILC_STR_UI_INT(ch_count),
652                        SILC_STR_END);
653   if (ret < 0) {
654     silc_buffer_free(detach);
655     return NULL;
656   }
657
658   /* Save all joined channels */
659   silc_hash_table_list(conn->local_entry->channels, &htl);
660   while (silc_hash_table_get(&htl, NULL, (void *)&chu)) {
661     unsigned char chid[32];
662     SilcUInt32 chid_len;
663
664     silc_id_id2str(&chu->channel->id, SILC_ID_CHANNEL, chid, sizeof(chid),
665                    &chid_len);
666     silc_buffer_format(detach,
667                        SILC_STR_ADVANCE,
668                        SILC_STR_UI_SHORT(strlen(chu->channel->channel_name)),
669                        SILC_STR_DATA(chu->channel->channel_name,
670                                      strlen(chu->channel->channel_name)),
671                        SILC_STR_UI_SHORT(chid_len),
672                        SILC_STR_DATA(chid, chid_len),
673                        SILC_STR_UI_INT(chu->channel->mode),
674                        SILC_STR_END);
675   }
676   silc_hash_table_list_reset(&htl);
677
678   silc_buffer_start(detach);
679   SILC_LOG_HEXDUMP(("Detach data"), silc_buffer_data(detach),
680                    silc_buffer_len(detach));
681
682   return detach;
683 }