updates.
[silc.git] / lib / silcclient / client_notify.c
index a6379da13e8cc8e35c135294dfa8194dc4ae0156..a07991736323e7930e1f33c0657609266cffc75c 100644 (file)
    is required before processing the notify message. This calls again the
    silc_client_notify_by_server and reprocesses the original notify packet. */
 
-static void silc_client_notify_by_server_pending(void *context)
+static void silc_client_notify_by_server_pending(void *context, void *context2)
 {
   SilcPacketContext *p = (SilcPacketContext *)context;
   silc_client_notify_by_server(p->context, p->sock, p);
+  silc_socket_free(p->sock);
 }
 
 /* Destructor for the pending command callback */
@@ -53,7 +54,7 @@ static void silc_client_notify_by_server_resolve(SilcClient client,
   SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
 
   p->context = (void *)client;
-  p->sock = conn->sock;
+  p->sock = silc_socket_dup(conn->sock);
 
   silc_client_send_command(client, conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident,
                           1, 3, idp->data, idp->len);
@@ -75,15 +76,18 @@ void silc_client_notify_by_server(SilcClient client,
   SilcNotifyType type;
   SilcArgumentPayload args;
 
+  SilcIDPayload idp;
   SilcClientID *client_id = NULL;
   SilcChannelID *channel_id = NULL;
+  SilcServerID *server_id = NULL;
   SilcClientEntry client_entry;
   SilcClientEntry client_entry2;
   SilcChannelEntry channel;
   SilcChannelUser chu;
+  SilcServerEntry server;
   SilcIDCacheEntry id_cache = NULL;
   unsigned char *tmp;
-  unsigned int tmp_len, mode;
+  uint32 tmp_len, mode;
 
   payload = silc_notify_payload_parse(buffer);
   if (!payload)
@@ -107,11 +111,26 @@ void silc_client_notify_by_server(SilcClient client,
      * for the application.
      */
     
-    /* Get Client ID */
+    /* Get Channel ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
 
+    channel_id = silc_id_payload_parse_id(tmp, tmp_len);
+    if (!channel_id)
+      goto out;
+
+    /* Get the channel entry */
+    channel = NULL;
+    if (silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
+                               &id_cache))
+      channel = (SilcChannelEntry)id_cache->context;
+
+    /* Get sender Client ID */
+    tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
+    if (!tmp)
+      goto out;
+
     client_id = silc_id_payload_parse_id(tmp, tmp_len);
     if (!client_id)
       goto out;
@@ -123,28 +142,13 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
     }
 
-    /* Get Channel ID */
+    /* Get the channel name */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
     if (!tmp)
       goto out;
 
-    channel_id = silc_id_payload_parse_id(tmp, tmp_len);
-    if (!channel_id)
-      goto out;
-
-    /* XXX Will ALWAYS fail because currently we don't have way to resolve
-       channel information for channel that we're not joined to. */
-    /* XXX ways to fix: use (extended) LIST command, or define the channel
-       name to the notfy type when name resolving is not mandatory. */
-    /* Find channel entry */
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
-      goto out;
-
-    channel = (SilcChannelEntry)id_cache->context;
-
     /* Notify application */
-    client->ops->notify(client, conn, type, client_entry, channel);
+    client->ops->notify(client, conn, type, channel, tmp, client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_JOIN:
@@ -186,7 +190,7 @@ void silc_client_notify_by_server(SilcClient client,
 
     /* Get channel entry */
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                    &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
@@ -234,7 +238,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!channel_id)
       goto out;
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
@@ -279,8 +283,7 @@ void silc_client_notify_by_server(SilcClient client,
     silc_client_remove_from_channels(client, conn, client_entry);
 
     /* Remove from cache */
-    silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, 
-                          client_entry->id);
+    silc_idcache_del_by_context(conn->client_cache, client_entry);
 
     /* Get signoff message */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
@@ -291,16 +294,7 @@ void silc_client_notify_by_server(SilcClient client,
     client->ops->notify(client, conn, type, client_entry, tmp);
 
     /* Free data */
-    if (client_entry->nickname)
-      silc_free(client_entry->nickname);
-    if (client_entry->server)
-      silc_free(client_entry->server);
-    if (client_entry->id)
-      silc_free(client_entry->id);
-    if (client_entry->send_key)
-      silc_cipher_free(client_entry->send_key);
-    if (client_entry->receive_key)
-      silc_cipher_free(client_entry->receive_key);
+    silc_client_del_client_entry(client, client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_TOPIC_SET:
@@ -334,7 +328,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!channel_id)
       goto out;
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
@@ -353,8 +347,8 @@ void silc_client_notify_by_server(SilcClient client,
      * application.
      */
 
-    /* Get new Client ID */
-    tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
+    /* Get old Client ID */
+    tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
 
@@ -363,20 +357,17 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     /* Ignore my ID */
-    if (!SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
+    if (SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
       break;
 
-    /* Find Client entry and if not found query it */
-    client_entry2 = 
-      silc_client_get_client_by_id(client, conn, client_id);
-    if (!client_entry2) {
-      silc_client_notify_by_server_resolve(client, conn, packet, client_id);
+    /* Find old Client entry */
+    client_entry = silc_client_get_client_by_id(client, conn, client_id);
+    if (!client_entry)
       goto out;
-    }
     silc_free(client_id);
 
-    /* Get old Client ID */
-    tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
+    /* Get new Client ID */
+    tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
     if (!tmp)
       goto out;
 
@@ -384,15 +375,15 @@ void silc_client_notify_by_server(SilcClient client,
     if (!client_id)
       goto out;
 
-    /* Find old Client entry */
-    client_entry = 
-      silc_client_get_client_by_id(client, conn, client_id);
-    if (!client_entry)
+    /* Find Client entry and if not found resolve it */
+    client_entry2 = silc_client_get_client_by_id(client, conn, client_id);
+    if (!client_entry2) {
+      silc_client_notify_by_server_resolve(client, conn, packet, client_id);
       goto out;
+    }
 
     /* Remove the old from cache */
-    silc_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, 
-                          client_entry->id);
+    silc_idcache_del_by_context(conn->client_cache, client_entry);
 
     /* Replace old ID entry with new one on all channels. */
     silc_client_replace_from_channels(client, conn, client_entry,
@@ -402,16 +393,7 @@ void silc_client_notify_by_server(SilcClient client,
     client->ops->notify(client, conn, type, client_entry, client_entry2);
 
     /* Free data */
-    if (client_entry->nickname)
-      silc_free(client_entry->nickname);
-    if (client_entry->server)
-      silc_free(client_entry->server);
-    if (client_entry->id)
-      silc_free(client_entry->id);
-    if (client_entry->send_key)
-      silc_cipher_free(client_entry->send_key);
-    if (client_entry->receive_key)
-      silc_cipher_free(client_entry->receive_key);
+    silc_client_del_client_entry(client, client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
@@ -424,41 +406,93 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
-    if (!client_id)
+    idp = silc_id_payload_parse_data(tmp, tmp_len);
+    if (!idp)
       goto out;
 
     /* Find Client entry */
-    client_entry = 
-      silc_client_get_client_by_id(client, conn, client_id);
-    if (!client_entry)
-      goto out;
+    if (silc_id_payload_get_type(idp) == SILC_ID_CLIENT) {
+      client_id = silc_id_payload_parse_id(tmp, tmp_len);
+      if (!client_id) {
+       silc_id_payload_free(idp);
+       goto out;
+      }
+
+      client_entry = silc_client_get_client_by_id(client, conn, client_id);
+      if (!client_entry) {
+       silc_id_payload_free(idp);
+       goto out;
+      }
+    } else {
+      server_id = silc_id_payload_parse_id(tmp, tmp_len);
+      if (!server_id) {
+       silc_id_payload_free(idp);
+       goto out;
+      }
+      
+      server = silc_client_get_server_by_id(client, conn, server_id);
+      if (!server) {
+       silc_id_payload_free(idp);
+       silc_free(server_id);
+       goto out;
+      }
+      
+      /* Save the pointer to the client_entry pointer */
+      client_entry = (SilcClientEntry)server;
+      silc_free(server_id);
+    }
 
     /* Get the mode */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
-    if (!tmp)
+    if (!tmp) {
+      silc_id_payload_free(idp);
       goto out;
+    }
 
     SILC_GET32_MSB(mode, tmp);
 
     /* Get channel entry */
     channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
                                SILC_ID_CHANNEL);
-    if (!channel_id)
+    if (!channel_id) {
+      silc_id_payload_free(idp);
       goto out;
+    }
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
-      break;
+                                    &id_cache)) {
+      silc_id_payload_free(idp);
+      goto out;
+    }
 
     channel = (SilcChannelEntry)id_cache->context;
 
     /* Save the new mode */
     channel->mode = mode;
 
+    /* Get the hmac */
+    tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
+    if (tmp) {
+      unsigned char hash[32];
+
+      if (channel->hmac)
+       silc_hmac_free(channel->hmac);
+      if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
+       goto out;
+
+      silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
+                    hash);
+      silc_hmac_set_key(channel->hmac, hash, 
+                       silc_hash_len(channel->hmac->hash));
+      memset(hash, 0, sizeof(hash));
+    }
+
     /* Notify application. The channel entry is sent last as this notify
        is for channel but application don't know it from the arguments
        sent by server. */
-    client->ops->notify(client, conn, type, client_entry, mode, channel);
+    client->ops->notify(client, conn, type, silc_id_payload_get_type(idp), 
+                       client_entry, mode, NULL, tmp, channel);
+
+    silc_id_payload_free(idp);
     break;
 
   case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
@@ -476,10 +510,11 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     /* Find Client entry */
-    client_entry = 
-      silc_client_get_client_by_id(client, conn, client_id);
-    if (!client_entry)
+    client_entry = silc_client_get_client_by_id(client, conn, client_id);
+    if (!client_entry) {
+      silc_client_notify_by_server_resolve(client, conn, packet, client_id);
       goto out;
+    }
 
     /* Get the mode */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
@@ -510,7 +545,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!channel_id)
       goto out;
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
@@ -561,13 +596,15 @@ void silc_client_notify_by_server(SilcClient client,
     
     /* Get the channel entry */
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                    &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
 
+    SILC_LOG_DEBUG(("Old Channel ID id(%s)", 
+                   silc_id_render(channel->id, SILC_ID_CHANNEL)));
+
     /* Free the old ID */
-    silc_free(channel_id);
     silc_free(channel->id);
 
     /* Get the new ID */
@@ -578,7 +615,13 @@ void silc_client_notify_by_server(SilcClient client,
     if (!channel->id)
       goto out;
 
-    id_cache->id = (void *)channel->id;
+    SILC_LOG_DEBUG(("New Channel ID id(%s)", 
+                   silc_id_render(channel->id, SILC_ID_CHANNEL)));
+
+    /* Remove the old cache entry and create a new one */
+    silc_idcache_del_by_context(conn->channel_cache, channel);
+    silc_idcache_add(conn->channel_cache, channel->channel_name, 
+                    channel->id, channel, FALSE);
 
     /* Notify application */
     client->ops->notify(client, conn, type, channel, channel);
@@ -599,8 +642,7 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     /* Find Client entry */
-    client_entry = 
-      silc_client_get_client_by_id(client, conn, client_id);
+    client_entry = silc_client_get_client_by_id(client, conn, client_id);
     if (!client_entry)
       goto out;
 
@@ -610,7 +652,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!channel_id)
       goto out;
     if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                &id_cache))
       break;
 
     channel = (SilcChannelEntry)id_cache->context;
@@ -627,8 +669,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (client_entry == conn->local_entry) {
       if (conn->current_channel == channel)
        conn->current_channel = NULL;
-      silc_idcache_del_by_id(conn->channel_cache, 
-                            SILC_ID_CHANNEL, channel->id);
+      silc_idcache_del_by_id(conn->channel_cache, channel->id);
       silc_free(channel->channel_name);
       silc_free(channel->id);
       silc_free(channel->key);
@@ -636,7 +677,90 @@ void silc_client_notify_by_server(SilcClient client,
       silc_free(channel);
     }
     break;
+
+  case SILC_NOTIFY_TYPE_KILLED:
+    /*
+     * A client (maybe me) was killed from the network.
+     */
+
+    /* Get Client ID */
+    tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
+    if (!tmp)
+      goto out;
+
+    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    if (!client_id)
+      goto out;
+
+    /* Find Client entry */
+    client_entry = silc_client_get_client_by_id(client, conn, client_id);
+    if (!client_entry)
+      goto out;
+
+    /* Get comment */
+    tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
+
+    /* Notify application. */
+    client->ops->notify(client, conn, type, client_entry, tmp);
+
+    if (client_entry != conn->local_entry) {
+      /* Remove client from all channels */
+      silc_client_remove_from_channels(client, conn, client_entry);
+      silc_client_del_client(client, conn, client_entry);
+    }
+
+    break;
     
+  case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
+    {
+      /*
+       * A server quit the SILC network and some clients must be removed
+       * from channels as they quit as well.
+       */
+      SilcClientEntry *clients = NULL;
+      uint32 clients_count = 0;
+      int i;
+
+      for (i = 1; i < silc_argument_get_arg_num(args); i++) {
+       /* Get Client ID */
+       tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
+       if (tmp) {
+         client_id = silc_id_payload_parse_id(tmp, tmp_len);
+         if (!client_id)
+           goto out;
+         
+         /* Get the client entry */
+         client_entry = silc_client_get_client_by_id(client, conn, client_id);
+         if (client_entry) {
+           clients = silc_realloc(clients, sizeof(*clients) * 
+                                  (clients_count + 1));
+           clients[clients_count] = client_entry;
+           clients_count++;
+         }
+         silc_free(client_id);
+       }
+      }
+      client_id = NULL;
+
+      /* Notify application. We don't keep server entries so the server
+        entry is returned as NULL. The client's are returned as array
+        of SilcClientEntry pointers. */
+      client->ops->notify(client, conn, type, NULL, clients, clients_count);
+
+      for (i = 0; i < clients_count; i++) {
+       /* Remove client from all channels */
+       client_entry = clients[i];
+       if (client_entry == conn->local_entry)
+         continue;
+
+       silc_client_remove_from_channels(client, conn, client_entry);
+       silc_client_del_client(client, conn, client_entry);
+      }
+      silc_free(clients);
+
+    }
+    break;
+
   default:
     break;
   }