Do not allow stopping client library before it has been started.
[silc.git] / lib / silcclient / client_entry.c
index f4354a8aba9371d0b17d93aab74bc27a20e0a874..a6dc88d578946a133a49320a97725dc94166ed9a 100644 (file)
@@ -257,8 +257,11 @@ silc_client_get_client_by_id_resolve(SilcClient client,
   SilcBuffer idp;
   SilcUInt16 cmd_ident;
 
-  if (!client || !conn | !client_id)
+  if (!client || !conn | !client_id) {
+    SILC_LOG_ERROR(("Missing arguments to "
+                   "silc_client_get_clients_by_id_resolve call"));
     return 0;
+  }
 
   SILC_LOG_DEBUG(("Resolve client by ID (%s)",
                  silc_id_render(client_id, SILC_ID_CLIENT)));
@@ -326,10 +329,14 @@ static SilcUInt16 silc_client_get_clients_i(SilcClient client,
   SILC_LOG_DEBUG(("Resolve client by %s command",
                  silc_get_command_name(command)));
 
-  if (!client || !conn)
+  if (!client || !conn) {
+    SILC_LOG_ERROR(("Missing arguments to silc_client_get_clients call"));
     return 0;
-  if (!nickname && !attributes)
+  }
+  if (!nickname && !attributes) {
+    SILC_LOG_ERROR(("Missing arguments to silc_client_get_clients call"));
     return 0;
+  }
 
   /* Parse server name from the nickname if set */
   if (silc_parse_userfqdn(nickname, nick, sizeof(nick),
@@ -1092,9 +1099,9 @@ SilcClientEntry silc_client_nickname_format(SilcClient client,
   char *cp;
   char newnick[128 + 1];
   int i, off = 0, len;
-  SilcBool freebase;
   SilcDList clients;
   SilcClientEntry entry, unformatted = NULL;
+  SilcBool formatted = FALSE;
 
   if (!client->internal->params->nickname_format[0])
     return client_entry;
@@ -1111,32 +1118,70 @@ SilcClientEntry silc_client_nickname_format(SilcClient client,
                                              TRUE, FALSE);
   if (!clients)
     return NULL;
-  if (silc_dlist_count(clients) == 1 &&
+  if (silc_dlist_count(clients) == 1 && !priority &&
       !client->internal->params->nickname_force_format) {
     silc_client_list_free(client, conn, clients);
     return client_entry;
   }
 
-  len = 0;
-  freebase = TRUE;
+  /* Is the requested client formatted already */
+  if (!silc_utf8_strcasecmp(client_entry->nickname, 
+                           client_entry->nickname_normalized))
+    formatted = TRUE;
+
+  if (client->internal->params->nickname_force_format)
+    formatted = FALSE;
+
+  /* Find unformatted client entry */
   while ((entry = silc_dlist_get(clients))) {
-    if (entry->internal.valid && entry != client_entry)
-      len++;
-    if (entry->internal.valid && entry != client_entry &&
-       silc_utf8_strcasecmp(entry->nickname, client_entry->nickname)) {
-      freebase = FALSE;
+    if (!entry->internal.valid)
+      continue;
+    if (entry == client_entry)
+      continue;
+    if (silc_utf8_strcasecmp(entry->nickname, entry->nickname_normalized)) {
       unformatted = entry;
       break;
     }
-  }
-  if (!len || freebase) {
+  }  
+
+  /* If there are no other unformatted clients and the requested client is
+     unformatted, just return it. */
+  if (!unformatted && !formatted) {
     silc_client_list_free(client, conn, clients);
     return client_entry;
   }
 
-  /* If priority formatting, this client always gets unformatted nickname. */
-  if (unformatted && priority)
+  /* If priority formatting then the requested client will get the 
+     unformatted nickname, and the unformatted client will get a new
+     formatted nickname. */
+  if (priority) {
+    if (formatted) {
+      /* Simply change the client's nickname to unformatted */
+      if (!silc_client_nickname_parse(client, conn, client_entry->nickname,
+                                     &cp))
+        return NULL;
+
+      silc_snprintf(client_entry->nickname, sizeof(client_entry->nickname), 
+                   cp);
+      silc_free(cp);
+    }
+
+    if (!unformatted) {
+      /* There was no other unformatted client */
+      silc_client_list_free(client, conn, clients);
+      return client_entry;
+    }
+
+    /* Now format the previously unformatted client */
     client_entry = unformatted;
+    formatted = FALSE;
+  }
+
+  /* If already formatted just return it */
+  if (formatted) {
+    silc_client_list_free(client, conn, clients);
+    return client_entry;
+  }
 
   memset(newnick, 0, sizeof(newnick));
   cp = client->internal->params->nickname_format;
@@ -1577,9 +1622,10 @@ SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
                                 SilcChannelEntry channel)
 {
   SilcIDCacheEntry id_cache;
-  SilcBool ret;
+  SilcBool ret = TRUE;
   SilcCipher key;
   SilcHmac hmac;
+  char *namec;
 
   if (!channel)
     return FALSE;
@@ -1591,10 +1637,12 @@ SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
 
   silc_mutex_lock(conn->internal->lock);
   if (silc_idcache_find_by_context(conn->internal->channel_cache, channel,
-                                  &id_cache))
-    silc_free(id_cache->name);
-  ret = silc_idcache_del_by_context(conn->internal->channel_cache,
-                                   channel, NULL);
+                                  &id_cache)) {
+    namec = id_cache->name;
+    ret = silc_idcache_del_by_context(conn->internal->channel_cache,
+                                     channel, NULL);
+    silc_free(namec);
+  }
   silc_mutex_unlock(conn->internal->lock);
 
   if (!ret)
@@ -1984,7 +2032,8 @@ SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
                                SilcServerEntry server)
 {
   SilcIDCacheEntry id_cache;
-  SilcBool ret;
+  SilcBool ret = TRUE;
+  char *namec;
 
   if (!server)
     return FALSE;
@@ -1996,10 +2045,12 @@ SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
 
   silc_mutex_lock(conn->internal->lock);
   if (silc_idcache_find_by_context(conn->internal->server_cache, server,
-                                  &id_cache))
-    silc_free(id_cache->name);
-  ret = silc_idcache_del_by_context(conn->internal->server_cache,
-                                   server, NULL);
+                                  &id_cache)) {
+    namec = id_cache->name;
+    ret = silc_idcache_del_by_context(conn->internal->server_cache,
+                                     server, NULL);
+    silc_free(namec);
+  }
   silc_mutex_unlock(conn->internal->lock);
 
   silc_free(server->server_name);