Added support for SILC protocol 1.3 NEW_CLIENT packet.
[silc.git] / lib / silcclient / client_register.c
index 2901210288d95dfa50e8f8c41706131b17e3f998..88726f5ea2f6961a4cb03ad63b065a2e089db9e4 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2006 Pekka Riikonen
+  Copyright (C) 2006 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -144,7 +144,7 @@ SILC_FSM_STATE(silc_client_new_id)
  out:
   /** Packet processed */
   silc_packet_free(packet);
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 
@@ -156,9 +156,15 @@ SILC_FSM_STATE(silc_client_st_register)
 {
   SilcClientConnection conn = fsm_context;
   SilcClient client = conn->client;
+  char *nick = NULL;
 
   SILC_LOG_DEBUG(("Register to network"));
 
+  /* 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);
+
   /* Send NEW_CLIENT packet to register to network */
   if (!silc_packet_send_va(conn->stream, SILC_PACKET_NEW_CLIENT, 0,
                           SILC_STR_UI_SHORT(strlen(client->username)),
@@ -167,17 +173,19 @@ SILC_FSM_STATE(silc_client_st_register)
                           SILC_STR_UI_SHORT(strlen(client->realname)),
                           SILC_STR_DATA(client->realname,
                                         strlen(client->realname)),
+                          SILC_STR_UI_SHORT(nick ? strlen(nick) : 0),
+                          SILC_STR_DATA(nick, nick ? strlen(nick) : 0),
                           SILC_STR_END)) {
     /** Error sending packet */
     silc_fsm_next(fsm, silc_client_st_register_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /** Wait for new ID */
   conn->internal->registering = TRUE;
   silc_fsm_next_later(fsm, silc_client_st_register_complete,
                      conn->internal->retry_timer, 0);
-  SILC_FSM_WAIT;
+  return SILC_FSM_WAIT;
 }
 
 /* Wait for NEW_ID packet to arrive */
@@ -190,7 +198,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
   if (conn->internal->disconnected) {
     /** Disconnected */
     silc_fsm_next(fsm, silc_client_st_register_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   if (!conn->local_id) {
@@ -200,7 +208,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
       conn->internal->retry_count = 0;
       conn->internal->retry_timer = SILC_CLIENT_RETRY_MIN;
       silc_fsm_next(fsm, silc_client_st_register_error);
-      SILC_FSM_CONTINUE;
+      return SILC_FSM_CONTINUE;
     }
 
     /** Resend registering packet */
@@ -209,7 +217,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
                                    SILC_CLIENT_RETRY_MUL) +
                                   (silc_rng_get_rn16(client->rng) %
                                    SILC_CLIENT_RETRY_RAND));
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   SILC_LOG_DEBUG(("Registered to network"));
@@ -221,9 +229,9 @@ SILC_FSM_STATE(silc_client_st_register_complete)
                           1, 5, silc_buffer_data(conn->internal->local_idp),
                           silc_buffer_len(conn->internal->local_idp));
 
-  /* Call NICK command if the nickname was set by the application (and is
-     not same as the username). */
-  if (conn->internal->params.nickname &&
+  /* With SILC protocol version 1.2 call NICK command if the nickname was
+     set by the application. */
+  if (conn->internal->params.nickname && conn->internal->remote_version < 13 &&
       !silc_utf8_strcasecmp(conn->internal->params.nickname, client->username))
     silc_client_command_call(client, conn, NULL,
                             "NICK", conn->internal->params.nickname, NULL);
@@ -243,7 +251,7 @@ SILC_FSM_STATE(silc_client_st_register_complete)
   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
                                silc_client_connect_timeout, conn);
 
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 /* Error registering to network */
@@ -251,26 +259,20 @@ SILC_FSM_STATE(silc_client_st_register_complete)
 SILC_FSM_STATE(silc_client_st_register_error)
 {
   SilcClientConnection conn = fsm_context;
-  SilcClient client = conn->client;
 
   SILC_LOG_DEBUG(("Error registering to network"));
 
   /* Signal to close connection */
+  conn->internal->status = SILC_CLIENT_CONN_ERROR;
   if (!conn->internal->disconnected) {
     conn->internal->disconnected = TRUE;
     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
   }
 
-  /* Call connect callback */
-  if (conn->internal->callback_called)
-    conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
-                  conn->callback_context);
-  conn->internal->callback_called = TRUE;
-
   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
                                silc_client_connect_timeout, conn);
 
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 /************************* Resume detached session **************************/
@@ -294,7 +296,7 @@ SILC_FSM_STATE(silc_client_st_resume)
   if (!resume) {
     /** Out of memory */
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
   silc_fsm_set_state_context(fsm, resume);
 
@@ -316,7 +318,7 @@ SILC_FSM_STATE(silc_client_st_resume)
     /** Malformed detach data */
     SILC_LOG_DEBUG(("Malformed detachment data"));
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   if (!silc_id_str2id(id, id_len, SILC_ID_CLIENT, &client_id,
@@ -324,7 +326,7 @@ SILC_FSM_STATE(silc_client_st_resume)
     /** Malformed ID */
     SILC_LOG_DEBUG(("Malformed ID"));
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /* Generate authentication data that server will verify */
@@ -336,7 +338,7 @@ SILC_FSM_STATE(silc_client_st_resume)
   if (!auth) {
     /** Out of memory */
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /* Send RESUME_CLIENT packet to resume to network */
@@ -349,13 +351,13 @@ SILC_FSM_STATE(silc_client_st_resume)
     /** Error sending packet */
     SILC_LOG_DEBUG(("Error sending packet"));
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /** Wait for new ID */
   conn->internal->registering = TRUE;
   silc_fsm_next_later(fsm, silc_client_st_resume_resolve_channels, 15, 0);
-  SILC_FSM_WAIT;
+  return SILC_FSM_WAIT;
 }
 
 /* Resolve the old session information, user mode and joined channels. */
@@ -372,14 +374,14 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
   if (conn->internal->disconnected) {
     /** Disconnected */
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   if (!conn->local_id) {
     /** Timeout, ID not received */
     conn->internal->registering = FALSE;
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /** Wait for channels */
@@ -397,7 +399,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
                           silc_buffer_len(conn->internal->local_idp));
 
   if (!resume->channel_count)
-    SILC_FSM_YIELD;
+    return SILC_FSM_YIELD;
 
   /* Send IDENTIFY command for all channels we know about.  These are the
      channels we've joined to according our detachment data. */
@@ -453,7 +455,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_channels)
   silc_free(res_argv_lens);
   silc_free(res_argv_types);
 
-  SILC_FSM_WAIT;
+  return SILC_FSM_WAIT;
 }
 
 /* Resolve joined channel modes, users and topics. */
@@ -471,7 +473,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
   if (conn->internal->disconnected) {
     /** Disconnected */
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   SILC_LOG_DEBUG(("Resolving channel details"));
@@ -480,7 +482,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
   silc_fsm_next(fsm, silc_client_st_resume_completed);
 
   if (!silc_idcache_get_all(conn->internal->channel_cache, &channels))
-    SILC_FSM_YIELD;
+    return SILC_FSM_YIELD;
 
   /* Resolve channels' mode, users and topic */
   resume->channel_count = silc_list_count(channels) * 3;
@@ -506,7 +508,7 @@ SILC_FSM_STATE(silc_client_st_resume_resolve_cmodes)
     silc_buffer_free(idp);
   }
 
-  SILC_FSM_WAIT;
+  return SILC_FSM_WAIT;
 }
 
 /* Resuming completed */
@@ -523,13 +525,13 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
   if (conn->internal->disconnected) {
     /** Disconnected */
     silc_fsm_next(fsm, silc_client_st_resume_error);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   if (resume->channel_count > 0) {
     resume->channel_count--;
     if (resume->channel_count)
-      SILC_FSM_WAIT;
+      return SILC_FSM_WAIT;
   }
 
   SILC_LOG_DEBUG(("Resuming completed"));
@@ -591,7 +593,7 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
   silc_free(resume->nickname);
   silc_free(resume);
 
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 /* Error resuming to network */
@@ -599,7 +601,6 @@ SILC_FSM_STATE(silc_client_st_resume_completed)
 SILC_FSM_STATE(silc_client_st_resume_error)
 {
   SilcClientConnection conn = fsm_context;
-  SilcClient client = conn->client;
   SilcClientResumeSession resume = state_context;
 
   if (conn->internal->disconnected) {
@@ -607,23 +608,18 @@ SILC_FSM_STATE(silc_client_st_resume_error)
       silc_free(resume->nickname);
       silc_free(resume);
     }
-    SILC_FSM_FINISH;
+    return SILC_FSM_FINISH;
   }
 
   SILC_LOG_DEBUG(("Error resuming to network"));
 
   /* Signal to close connection */
+  conn->internal->status = SILC_CLIENT_CONN_ERROR;
   if (!conn->internal->disconnected) {
     conn->internal->disconnected = TRUE;
     SILC_FSM_EVENT_SIGNAL(&conn->internal->wait_event);
   }
 
-  /* Call connect callback */
-  if (conn->internal->callback_called)
-    conn->callback(client, conn, SILC_CLIENT_CONN_ERROR, 0, NULL,
-                  conn->callback_context);
-  conn->internal->callback_called = TRUE;
-
   silc_schedule_task_del_by_all(conn->internal->schedule, 0,
                                silc_client_connect_timeout, conn);
 
@@ -632,7 +628,7 @@ SILC_FSM_STATE(silc_client_st_resume_error)
     silc_free(resume);
   }
 
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 /* Generates the session detachment data. This data can be used later