Fixed resuming to work even when there are no joined channels
[silc.git] / lib / silcclient / client_register.c
index 88726f5ea2f6961a4cb03ad63b065a2e089db9e4..a3cafe478b9697cf9bb6f153b4a5cf7e924af764 100644 (file)
@@ -34,20 +34,6 @@ typedef struct {
 
 /************************ Static utility functions **************************/
 
-/* Command callback.  Nothing interesting to do here. */
-
-static SilcBool
-silc_client_register_command_called(SilcClient client,
-                                   SilcClientConnection conn,
-                                   SilcCommand command,
-                                   SilcStatus status,
-                                   SilcStatus error,
-                                   void *context,
-                                   va_list ap)
-{
-  return FALSE;
-}
-
 /* Continues resuming after resolving.  Continue after last reply. */
 
 static SilcBool
@@ -92,6 +78,7 @@ SILC_FSM_STATE(silc_client_new_id)
   SilcClientConnection conn = fsm_context;
   SilcClient client = conn->client;
   SilcPacket packet = state_context;
+  char *nick;
   SilcID id;
 
   if (conn->local_id)
@@ -106,9 +93,15 @@ SILC_FSM_STATE(silc_client_new_id)
   SILC_LOG_DEBUG(("New ID %s", silc_id_render(&id.u.client_id,
                                              SILC_ID_CLIENT)));
 
+  /* From SILC protocol version 1.3, nickname is in NEW_CLIENT packet */
+  if (conn->internal->remote_version >= 13)
+    nick = (conn->internal->params.nickname ?
+           conn->internal->params.nickname : client->username);
+  else
+    nick = client->username;
+
   /* Create local client entry */
-  conn->local_entry = silc_client_add_client(client, conn,
-                                            client->username,
+  conn->local_entry = silc_client_add_client(client, conn, nick,
                                             client->username,
                                             client->realname,
                                             &id.u.client_id, 0);
@@ -225,7 +218,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
   /* Issue IDENTIFY command for itself to get resolved hostname
      correctly from server. */
   silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
-                          silc_client_register_command_called, NULL,
+                          silc_client_command_called_dummy, NULL,
                           1, 5, silc_buffer_data(conn->internal->local_idp),
                           silc_buffer_len(conn->internal->local_idp));
 
@@ -239,7 +232,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
   /* Issue INFO command to fetch the real server name and server
      information and other stuff. */
   silc_client_command_send(client, conn, SILC_COMMAND_INFO,
-                          silc_client_register_command_called, NULL,
+                          silc_client_command_called_dummy, NULL,
                           1, 2, silc_buffer_data(conn->internal->remote_idp),
                           silc_buffer_len(conn->internal->remote_idp));
 
@@ -250,6 +243,8 @@ SILC_FSM_STATE(silc_client_st_register_complete)
   conn->internal->registering = FALSE;
   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
                                silc_client_connect_timeout, conn);
+  silc_async_free(conn->internal->cop);
+  conn->internal->cop = NULL;
 
   return SILC_FSM_FINISH;
 }
@@ -394,7 +389,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
   /* Send UMODE command to get our own user mode in the network */
   SILC_LOG_DEBUG(("Resolving user mode"));
   silc_client_command_send(client, conn, SILC_COMMAND_UMODE,
-                          silc_client_register_command_called, NULL,
+                          silc_client_command_called_dummy, NULL,
                           1, 1, silc_buffer_data(conn->internal->local_idp),
                           silc_buffer_len(conn->internal->local_idp));
 
@@ -430,6 +425,8 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
     channel = silc_client_get_channel_by_id(client, conn, &channel_id);
     if (!channel)
       silc_client_add_channel(client, conn, name, 0, &channel_id);
+    else
+      silc_client_unref_channel(client, conn, channel);
 
     res_argv = silc_realloc(res_argv, sizeof(*res_argv) * (res_argc + 1));
     res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
@@ -539,14 +536,14 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
   /* Issue IDENTIFY command for itself to get resolved hostname
      correctly from server. */
   silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
-                          silc_client_register_command_called, NULL,
+                          silc_client_command_called_dummy, NULL,
                           1, 5, silc_buffer_data(conn->internal->local_idp),
                           silc_buffer_len(conn->internal->local_idp));
 
   /* Issue INFO command to fetch the real server name and server
      information and other stuff. */
   silc_client_command_send(client, conn, SILC_COMMAND_INFO,
-                          silc_client_register_command_called, NULL,
+                          silc_client_command_called_dummy, NULL,
                           1, 2, silc_buffer_data(conn->internal->remote_idp),
                           silc_buffer_len(conn->internal->remote_idp));
 
@@ -566,25 +563,26 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
                                      &conn->local_entry->id);
 
   /* Call JOIN command replies for all joined channel */
-  silc_idcache_get_all(conn->internal->channel_cache, &channels);
-  silc_list_start(channels);
-  while ((entry = silc_list_get(channels))) {
-    SilcHashTableList htl;
-    const char *cipher, *hmac;
-
-    channel = entry->context;
-    cipher = (channel->internal.send_key ?
-             silc_cipher_get_name(channel->internal.send_key) : NULL);
-    hmac = (channel->internal.hmac ?
-           silc_hmac_get_name(channel->internal.hmac) : NULL);
-    silc_hash_table_list(channel->user_list, &htl);
-    silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
-                                       channel->channel_name, channel,
-                                       channel->mode, &htl, channel->topic,
-                                       cipher, hmac, channel->founder_key,
-                                       channel->channel_pubkeys,
-                                       channel->user_limit);
-    silc_hash_table_list_reset(&htl);
+  if (silc_idcache_get_all(conn->internal->channel_cache, &channels)) {
+    silc_list_start(channels);
+    while ((entry = silc_list_get(channels))) {
+      SilcHashTableList htl;
+      const char *cipher, *hmac;
+
+      channel = entry->context;
+      cipher = (channel->internal.send_key ?
+               silc_cipher_get_name(channel->internal.send_key) : NULL);
+      hmac = (channel->internal.hmac ?
+             silc_hmac_get_name(channel->internal.hmac) : NULL);
+      silc_hash_table_list(channel->user_list, &htl);
+      silc_client_resume_command_callback(client, conn, SILC_COMMAND_JOIN,
+                                         channel->channel_name, channel,
+                                         channel->mode, &htl, channel->topic,
+                                         cipher, hmac, channel->founder_key,
+                                         channel->channel_pubkeys,
+                                         channel->user_limit);
+      silc_hash_table_list_reset(&htl);
+    }
   }
 
   conn->internal->registering = FALSE;
@@ -592,6 +590,8 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
                                silc_client_connect_timeout, conn);
   silc_free(resume->nickname);
   silc_free(resume);
+  silc_async_free(conn->internal->cop);
+  conn->internal->cop = NULL;
 
   return SILC_FSM_FINISH;
 }