Send channel entry in SIGNOFF notify to application.
[silc.git] / lib / silcclient / client_entry.c
index ae7cba717d102c8d597731245240ffab4de00585..c59a48fd3e4992008c26ed4a53cea0323688f2b5 100644 (file)
@@ -62,31 +62,47 @@ SilcClientEntry silc_client_get_client_by_id(SilcClient client,
 
 /* Finds clients by nickname from local cache. */
 
-SilcDList silc_client_get_clients_local(SilcClient client,
-                                       SilcClientConnection conn,
-                                       const char *nickname,
-                                       const char *format)
+SilcDList silc_client_get_clients_local_ext(SilcClient client,
+                                           SilcClientConnection conn,
+                                           const char *nickname,
+                                           SilcBool get_all,
+                                           SilcBool get_valid)
 {
   SilcIDCacheEntry id_cache;
   SilcList list;
   SilcDList clients;
   SilcClientEntry entry;
-  char *nicknamec;
+  char *nicknamec, *parsed = NULL, *format = NULL;
 
   if (!client || !conn || !nickname)
     return NULL;
 
-  SILC_LOG_DEBUG(("Find clients by nickname %s", nickname));
+  /* Parse nickname in case it is formatted */
+  if (!silc_client_nickname_parse(client, conn, (char *)nickname, &parsed))
+    return NULL;
+
+  if (!get_all && parsed)
+    format = (char *)nickname;
+  if (!parsed) {
+    parsed = silc_memdup(nickname, strlen(nickname));
+    if (!parsed)
+      return NULL;
+  }
+
+  SILC_LOG_DEBUG(("Find clients by nickname %s", parsed));
 
   /* Normalize nickname for search */
-  nicknamec = silc_identifier_check(nickname, strlen(nickname),
+  nicknamec = silc_identifier_check(parsed, strlen(parsed),
                                    SILC_STRING_UTF8, 128, NULL);
-  if (!nicknamec)
+  if (!nicknamec) {
+    silc_free(parsed);
     return NULL;
+  }
 
   clients = silc_dlist_init();
   if (!clients) {
     silc_free(nicknamec);
+    silc_free(parsed);
     return NULL;
   }
 
@@ -98,37 +114,66 @@ SilcDList silc_client_get_clients_local(SilcClient client,
                                 &list)) {
     silc_mutex_unlock(conn->internal->lock);
     silc_free(nicknamec);
+    silc_free(parsed);
     silc_dlist_uninit(clients);
     return NULL;
   }
+  silc_list_start(list);
 
-  if (!format) {
+  if (!format && get_all) {
     /* Take all without any further checking */
-    silc_list_start(list);
     while ((id_cache = silc_list_get(list))) {
-      silc_client_ref_client(client, conn, id_cache->context);
-      silc_dlist_add(clients, id_cache->context);
+      entry = id_cache->context;
+      if (!get_valid || entry->internal.valid) {
+       silc_client_ref_client(client, conn, id_cache->context);
+       silc_dlist_add(clients, id_cache->context);
+      }
     }
   } else {
     /* Check multiple cache entries for exact match */
-    silc_list_start(list);
     while ((id_cache = silc_list_get(list))) {
       entry = id_cache->context;
-      if (silc_utf8_strcasecmp(entry->nickname, format)) {
+      if (silc_utf8_strcasecmp(entry->nickname,
+                              format ? format : parsed) &&
+         (!get_valid || entry->internal.valid)) {
        silc_client_ref_client(client, conn, entry);
        silc_dlist_add(clients, entry);
+
+       /* If format is NULL, we find one exact match with the base
+          nickname (parsed). */
+       if (!format)
+         break;
       }
     }
   }
 
   silc_mutex_unlock(conn->internal->lock);
 
-  silc_dlist_start(clients);
-
   silc_free(nicknamec);
+  silc_free(parsed);
+
+  if (!silc_dlist_count(clients)) {
+    silc_dlist_uninit(clients);
+    return NULL;
+  }
+
+  SILC_LOG_DEBUG(("Found %d clients", silc_dlist_count(clients)));
+
+  silc_dlist_start(clients);
   return clients;
 }
 
+/* Finds clients by nickname from local cache. */
+
+SilcDList silc_client_get_clients_local(SilcClient client,
+                                       SilcClientConnection conn,
+                                       const char *nickname,
+                                       SilcBool return_all)
+{
+  return silc_client_get_clients_local_ext(client, conn, nickname, return_all,
+                                          TRUE);
+}
+
 /********************** Client Resolving from Server ************************/
 
 /* Resolving context */
@@ -212,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)));
@@ -275,22 +323,39 @@ static SilcUInt16 silc_client_get_clients_i(SilcClient client,
                                            void *context)
 {
   SilcClientGetClientInternal i;
-  char userhost[768 + 1];
+  char nick[128 + 1], serv[256 + 1], userhost[768 + 1], *parsed = NULL;
   int len;
 
   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),
+                         serv, sizeof(serv) == 2))
+    server = (const char *)serv;
+  nickname = (const char *)nick;
+
+  /* Parse nickname in case it is formatted */
+  if (silc_client_nickname_parse(client, conn, (char *)nickname, &parsed))
+    nickname = (const char *)parsed;
 
   i = silc_calloc(1, sizeof(*i));
-  if (!i)
+  if (!i) {
+    silc_free(parsed);
     return 0;
+  }
   i->clients = silc_dlist_init();
   if (!i->clients) {
+    silc_free(parsed);
     silc_free(i);
     return 0;
   }
@@ -306,6 +371,7 @@ static SilcUInt16 silc_client_get_clients_i(SilcClient client,
   } else if (nickname) {
     silc_strncat(userhost, sizeof(userhost) - 1, nickname, strlen(nickname));
   }
+  silc_free(parsed);
 
   /* Send the command */
   if (command == SILC_COMMAND_IDENTIFY)
@@ -1033,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;
@@ -1047,36 +1113,75 @@ SilcClientEntry silc_client_nickname_format(SilcClient client,
   /* Get all clients with same nickname. Do not perform the formatting
      if there aren't any clients with same nickname unless the application
      is forcing us to do so. */
-  clients = silc_client_get_clients_local(client, conn,
-                                         client_entry->nickname, NULL);
+  clients = silc_client_get_clients_local_ext(client, conn,
+                                             client_entry->nickname,
+                                             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;
@@ -1114,22 +1219,6 @@ SilcClientEntry silc_client_nickname_format(SilcClient client,
       memcpy(&newnick[off], client_entry->hostname, len);
       off += len;
       break;
-    case 's':
-      /* Stripped server name */
-      if (!client_entry->server)
-       break;
-      len = strcspn(client_entry->server, ".");
-      memcpy(&newnick[off], client_entry->server, len);
-      off += len;
-      break;
-    case 'S':
-      /* Full server name */
-      if (!client_entry->server)
-       break;
-      len = strlen(client_entry->server);
-      memcpy(&newnick[off], client_entry->server, len);
-      off += len;
-      break;
     case 'a':
       /* Ascending number */
       {
@@ -1185,8 +1274,10 @@ SilcBool silc_client_nickname_parse(SilcClient client,
   SilcBool n = FALSE;
   int len;
 
-  if (!client->internal->params->nickname_format[0])
+  if (!client->internal->params->nickname_format[0]) {
+    *ret_nick = NULL;
     return TRUE;
+  }
 
   if (!nickname || !nickname[0])
     return FALSE;
@@ -1205,8 +1296,6 @@ SilcBool silc_client_nickname_parse(SilcClient client,
 
     case 'h':
     case 'H':
-    case 's':
-    case 'S':
     case 'a':
       break;
 
@@ -1219,7 +1308,7 @@ SilcBool silc_client_nickname_parse(SilcClient client,
       break;
     }
 
-     cp++;
+    cp++;
   }
   if (!n)
     return FALSE;