updates.
[silc.git] / lib / silcclient / client_notify.c
index 49c315c51fc46e5cddd0c3230447885d822776c4..1e7778583f6e864b8ab307ff57f2484d4bf980c9 100644 (file)
@@ -1,16 +1,15 @@
 /*
 
-  client_notify.c
+  client_notify.c 
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2002 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
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-  
+  the Free Software Foundation; version 2 of the License.
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    important packets sent by the server. They tell different things to the
    client such as nick changes, mode changes etc. */
 
-#include "clientlibincludes.h"
+#include "silcincludes.h"
+#include "silcclient.h"
 #include "client_internal.h"
 
+/* Context used for resolving client, channel and server info. */
 typedef struct {
-  SilcPacketContext *packet;
+  void *packet;
   void *context;
   SilcSocketConnection sock;
 } *SilcClientNotifyResolve;
 
+SILC_TASK_CALLBACK(silc_client_notify_check_client)
+{ 
+  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+  SilcClientConnection conn = res->context;
+  SilcClient client = conn->client;
+  SilcClientID *client_id = res->packet;
+  silc_client_get_client_by_id_resolve(client, conn, client_id, NULL, NULL);
+  silc_free(client_id);
+  silc_free(res);
+}
+
 /* Called when notify is received and some async operation (such as command)
    is required before processing the notify message. This calls again the
    silc_client_notify_by_server and reprocesses the original notify packet. */
@@ -41,51 +53,52 @@ static void silc_client_notify_by_server_pending(void *context, void *context2)
   SilcClientCommandReplyContext reply = 
     (SilcClientCommandReplyContext)context2;
 
-  if (reply) {
-    SilcCommandStatus status;
-    unsigned char *tmp = silc_argument_get_arg_type(reply->args, 1, NULL);
-    SILC_GET16_MSB(status, tmp);
-    if (status != SILC_STATUS_OK) {
-      silc_socket_free(res->sock);
-      return;
-    }
-  }
+  SILC_LOG_DEBUG(("Start"));
 
-  silc_client_notify_by_server(res->context, res->sock, res->packet);
-  silc_socket_free(res->sock);
-}
+  if (reply && !silc_command_get_status(reply->payload, NULL, NULL))
+    goto out;
 
-/* Destructor for the pending command callback */
+  silc_client_notify_by_server(res->context, res->sock, res->packet);
 
-static void silc_client_notify_by_server_destructor(void *context)
-{
-  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+ out:
+  silc_socket_free(res->sock);
   silc_packet_context_free(res->packet);
   silc_free(res);
 }
 
-/* Resolve client information from server by Client ID. */
+/* Resolve client, channel or server information. */
 
 static void silc_client_notify_by_server_resolve(SilcClient client,
                                                 SilcClientConnection conn,
                                                 SilcPacketContext *packet,
-                                                SilcClientID *client_id)
+                                                SilcIdType id_type,
+                                                void *id)
 {
   SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
-  SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
+  SilcBuffer idp = silc_id_payload_encode(id, id_type);
 
   res->packet = silc_packet_context_dup(packet);
   res->context = client;
   res->sock = silc_socket_dup(conn->sock);
 
-  silc_client_command_register(client, SILC_COMMAND_WHOIS, NULL, NULL,
-                              silc_client_command_reply_whois_i, 0,
-                              ++conn->cmd_ident);
-  silc_client_command_send(client, conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
-                          1, 3, idp->data, idp->len);
-  silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
-                             silc_client_notify_by_server_destructor,
-                             silc_client_notify_by_server_pending, res);
+  /* For client resolving use WHOIS, and otherwise use IDENTIFY */
+  if (id_type == SILC_ID_CLIENT) {
+    silc_client_command_register(client, SILC_COMMAND_WHOIS, NULL, NULL,
+                                silc_client_command_reply_whois_i, 0,
+                                ++conn->cmd_ident);
+    silc_client_command_send(client, conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
+                            1, 3, idp->data, idp->len);
+    silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
+                               silc_client_notify_by_server_pending, res);
+  } else {
+    silc_client_command_register(client, SILC_COMMAND_IDENTIFY, NULL, NULL,
+                                silc_client_command_reply_identify_i, 0,
+                                ++conn->cmd_ident);
+    silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY, 
+                            conn->cmd_ident, 1, 5, idp->data, idp->len);
+    silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, conn->cmd_ident,
+                               silc_client_notify_by_server_pending, res);
+  }
   silc_buffer_free(idp);
 }
 
@@ -101,18 +114,18 @@ void silc_client_notify_by_server(SilcClient client,
   SilcNotifyType type;
   SilcArgumentPayload args;
 
-  SilcIDPayload idp;
+  void *id;
+  SilcIdType id_type;
   SilcClientID *client_id = NULL;
   SilcChannelID *channel_id = NULL;
   SilcServerID *server_id = NULL;
-  SilcClientEntry client_entry;
-  SilcClientEntry client_entry2;
+  SilcClientEntry client_entry = NULL;
+  SilcClientEntry client_entry2 = NULL;
   SilcChannelEntry channel;
   SilcChannelUser chu;
   SilcServerEntry server;
-  SilcIDCacheEntry id_cache = NULL;
   unsigned char *tmp;
-  uint32 tmp_len, mode;
+  SilcUInt32 tmp_len, mode;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -145,29 +158,27 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    channel_id = silc_id_payload_parse_id(tmp, tmp_len);
+    channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     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;
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
 
     /* 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);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
     /* Find Client entry and if not found query it */
     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);
+      silc_client_notify_by_server_resolve(client, conn, packet, 
+                                          SILC_ID_CLIENT, client_id);
       goto out;
     }
 
@@ -194,14 +205,15 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
     /* Find Client entry and if not found query it */
     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);
+      silc_client_notify_by_server_resolve(client, conn, packet, 
+                                          SILC_ID_CLIENT, client_id);
       goto out;
     }
 
@@ -212,7 +224,8 @@ void silc_client_notify_by_server(SilcClient client,
        goto out;
       }
       client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
-      silc_client_notify_by_server_resolve(client, conn, packet, client_id);
+      silc_client_notify_by_server_resolve(client, conn, packet, 
+                                          SILC_ID_CLIENT, client_id);
       goto out;
     } else {
       if (client_entry != conn->local_entry)
@@ -224,22 +237,22 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    channel_id = silc_id_payload_parse_id(tmp, tmp_len);
+    channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!channel_id)
       goto out;
 
     /* Get channel entry */
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    &id_cache))
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
-    /* Add client to channel */
-    if (client_entry != conn->local_entry) {
+    /* Join the client to channel */
+    if (!silc_client_on_channel(channel, client_entry)) {
       chu = silc_calloc(1, sizeof(*chu));
       chu->client = client_entry;
-      silc_list_add(channel->clients, chu);
+      chu->channel = channel;
+      silc_hash_table_add(channel->user_list, client_entry, chu);
+      silc_hash_table_add(client_entry->channels, channel, chu);
     }
 
     /* Notify application. The channel entry is sent last as this notify
@@ -261,7 +274,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
@@ -276,20 +289,30 @@ void silc_client_notify_by_server(SilcClient client,
                                SILC_ID_CHANNEL);
     if (!channel_id)
       goto out;
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                &id_cache))
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
     /* Remove client from channel */
-    silc_list_start(channel->clients);
-    while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
-      if (chu->client == client_entry) {
-       silc_list_del(channel->clients, chu);
-       silc_free(chu);
-       break;
-      }
+    chu = silc_client_on_channel(channel, client_entry);
+    if (chu) {
+      silc_hash_table_del(client_entry->channels, channel);
+      silc_hash_table_del(channel->user_list, client_entry);
+      silc_free(chu);
+    }
+
+    /* Some client implementations actually quit network by first doing
+       LEAVE and then immediately SIGNOFF.  We'll check for this by doing 
+       check for the client after 5 - 34 seconds.  If it is not valid after
+       that we'll remove the client from cache. */
+    if (!silc_hash_table_count(client_entry->channels)) {
+      SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+      res->context = conn;
+      res->packet = silc_id_dup(client_id, SILC_ID_CLIENT);
+      silc_schedule_task_add(client->schedule, 0,
+                            silc_client_notify_check_client, conn,
+                            (5 + (silc_rng_get_rn16(client->rng) % 29)),
+                            0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
     }
 
     /* Notify application. The channel entry is sent last as this notify
@@ -310,7 +333,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
@@ -345,62 +368,50 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: TOPIC_SET"));
 
-    /* Get Client ID */
+    /* Get ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
-
-    idp = silc_id_payload_parse(tmp, tmp_len);
-    if (!idp)
+    id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+    if (!id)
       goto out;
 
     /* Find Client entry */
-    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;
-      }
-
+    if (id_type == SILC_ID_CLIENT) {
       /* Find Client entry */
-      client_entry = 
-       silc_client_get_client_by_id(client, conn, client_id);
-      if (!client_entry)
-       goto out;
-    } else if (silc_id_payload_get_type(idp) == SILC_ID_SERVER) {
-      server_id = silc_id_payload_parse_id(tmp, tmp_len);
-      if (!server_id) {
-       silc_id_payload_free(idp);
+      client_id = id;
+      client_entry = silc_client_get_client_by_id(client, conn, client_id);
+      if (!client_entry) {
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CLIENT, client_id);
        goto out;
       }
-      
+    } else if (id_type == SILC_ID_SERVER) {
+      /* Find Server entry */
+      server_id = id;
       server = silc_client_get_server_by_id(client, conn, server_id);
       if (!server) {
-       silc_id_payload_free(idp);
-       silc_free(server_id);
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_SERVER, server_id);
        goto out;
       }
       
       /* Save the pointer to the client_entry pointer */
       client_entry = (SilcClientEntry)server;
-      silc_free(server_id);
     } else {
-      channel_id = silc_id_payload_parse_id(tmp, tmp_len);
-      if (!channel_id) {
-       silc_id_payload_free(idp);
-       goto out;
-      }
-      
+      /* Find Channel entry */
+      channel_id = id;
       channel = silc_client_get_channel_by_id(client, conn, channel_id);
       if (!channel) {
-       silc_id_payload_free(idp);
-       silc_free(channel_id);
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CHANNEL, channel_id);
        goto out;
       }
       
       /* Save the pointer to the client_entry pointer */
       client_entry = (SilcClientEntry)channel;
       silc_free(channel_id);
+      channel_id = NULL;
     }
 
     /* Get topic */
@@ -413,20 +424,16 @@ void silc_client_notify_by_server(SilcClient client,
                                SILC_ID_CHANNEL);
     if (!channel_id)
       goto out;
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                &id_cache))
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
     /* 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->internal->ops->notify(client, conn, type, 
-                                 silc_id_payload_get_type(idp),
+    client->internal->ops->notify(client, conn, type, id_type,
                                  client_entry, tmp, channel);
 
-    silc_id_payload_free(idp);
     break;
 
   case SILC_NOTIFY_TYPE_NICK_CHANGE:
@@ -444,7 +451,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
@@ -465,23 +472,64 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
-    /* 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;
+    /* From protocol version 1.1 we get the new nickname in notify as well,
+       so we don't have to resolve it.  Do it the hard way if server doesn't
+       send it to us. */
+    tmp = silc_argument_get_arg_type(args, 3, NULL);
+    if (tmp) {
+      /* Protocol version 1.1 */
+
+      /* Create new client entry, and save all old information with the
+        new nickname and client ID */
+      client_entry2 = silc_client_add_client(client, conn, NULL, NULL, 
+                                            client_entry->realname,
+                                            silc_id_dup(client_id, 
+                                                        SILC_ID_CLIENT), 0);
+      if (!client_entry2)
+       goto out;
+
+      if (client_entry->server)
+       client_entry2->server = strdup(client_entry->server);
+      if (client_entry->username)
+       client_entry2->username = strdup(client_entry->username);
+      if (client_entry->hostname)
+       client_entry2->hostname = strdup(client_entry->hostname);
+      silc_client_update_client(client, conn, client_entry2, tmp, NULL, NULL,
+                               client_entry->mode);
     } else {
+      /* Protocol version 1.0 */
+
+      /* Find client entry and if not found resolve it */
+      client_entry2 = silc_client_get_client_by_id(client, conn, client_id);
+      if (!client_entry2) {
+       /* Resolve the entry information */
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CLIENT, client_id);
+
+       /* Add the new entry even though we resolved it. This is because we
+          want to replace the old entry with the new entry here right now. */
+       client_entry2 = 
+         silc_client_add_client(client, conn, NULL, NULL, NULL, 
+                                silc_id_dup(client_id, SILC_ID_CLIENT), 
+                                client_entry->mode);
+
+       /* Replace old ID entry with new one on all channels. */
+       silc_client_replace_from_channels(client, conn, client_entry,
+                                         client_entry2);
+       break;
+      }
+
       if (client_entry2 != conn->local_entry)
        silc_client_nickname_format(client, conn, client_entry2);
     }
 
     /* Remove the old from cache */
     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,
                                      client_entry2);
@@ -489,9 +537,10 @@ void silc_client_notify_by_server(SilcClient client,
     /* Notify application */
     client->internal->ops->notify(client, conn, type, 
                                  client_entry, client_entry2);
-
-    /* Free data */
+    
+    /* Free old client entry */
     silc_client_del_client_entry(client, conn, client_entry);
+
     break;
 
   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
@@ -501,70 +550,67 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: CMODE_CHANGE"));
 
-    /* Get Client ID */
+    /* Get ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
-
-    idp = silc_id_payload_parse(tmp, tmp_len);
-    if (!idp)
+    id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+    if (!id)
       goto out;
 
     /* Find Client entry */
-    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;
-      }
-
+    if (id_type == SILC_ID_CLIENT) {
+      /* Find Client entry */
+      client_id = id;
       client_entry = silc_client_get_client_by_id(client, conn, client_id);
       if (!client_entry) {
-       silc_id_payload_free(idp);
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CLIENT, client_id);
        goto out;
       }
-    } else {
-      server_id = silc_id_payload_parse_id(tmp, tmp_len);
-      if (!server_id) {
-       silc_id_payload_free(idp);
-       goto out;
-      }
-      
+    } else if (id_type == SILC_ID_SERVER) {
+      /* Find Server entry */
+      server_id = id;
       server = silc_client_get_server_by_id(client, conn, server_id);
       if (!server) {
-       silc_id_payload_free(idp);
-       silc_free(server_id);
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_SERVER, server_id);
        goto out;
       }
-      
+
       /* Save the pointer to the client_entry pointer */
       client_entry = (SilcClientEntry)server;
-      silc_free(server_id);
+    } else {
+      /* Find Channel entry */
+      channel_id = id;
+      channel = silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!channel) {
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CHANNEL, channel_id);
+       goto out;
+      }
+      
+      /* Save the pointer to the client_entry pointer */
+      client_entry = (SilcClientEntry)channel;
+      silc_free(channel_id);
+      channel_id = NULL;
     }
 
     /* Get the mode */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
-    if (!tmp) {
-      silc_id_payload_free(idp);
+    if (!tmp)
       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) {
-      silc_id_payload_free(idp);
+    if (!channel_id)
       goto out;
-    }
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    &id_cache)) {
-      silc_id_payload_free(idp);
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       goto out;
-    }
-
-    channel = (SilcChannelEntry)id_cache->context;
 
     /* Save the new mode */
     channel->mode = mode;
@@ -590,11 +636,8 @@ void silc_client_notify_by_server(SilcClient client,
     /* 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->internal->ops->notify(client, conn, type, 
-                                 silc_id_payload_get_type(idp), 
+    client->internal->ops->notify(client, conn, type, id_type,
                                  client_entry, mode, NULL, tmp, channel);
-
-    silc_id_payload_free(idp);
     break;
 
   case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
@@ -604,20 +647,50 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE"));
 
-    /* Get Client ID */
+    /* Get 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)
+    id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+    if (!id)
       goto out;
 
     /* Find 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;
+    if (id_type == SILC_ID_CLIENT) {
+      /* Find Client entry */
+      client_id = id;
+      client_entry = silc_client_get_client_by_id(client, conn, client_id);
+      if (!client_entry) {
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CLIENT, client_id);
+       goto out;
+      }
+    } else if (id_type == SILC_ID_SERVER) {
+      /* Find Server entry */
+      server_id = id;
+      server = silc_client_get_server_by_id(client, conn, server_id);
+      if (!server) {
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_SERVER, server_id);
+       goto out;
+      }
+
+      /* Save the pointer to the client_entry pointer */
+      client_entry = (SilcClientEntry)server;
+    } else {
+      /* Find Channel entry */
+      channel_id = id;
+      channel = silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!channel) {
+       silc_client_notify_by_server_resolve(client, conn, packet, 
+                                            SILC_ID_CHANNEL, channel_id);
+       goto out;
+      }
+      
+      /* Save the pointer to the client_entry pointer */
+      client_entry = (SilcClientEntry)channel;
+      silc_free(channel_id);
+      channel_id = NULL;
     }
 
     /* Get the mode */
@@ -633,7 +706,7 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     silc_free(client_id);
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
@@ -648,26 +721,20 @@ void silc_client_notify_by_server(SilcClient client,
                                SILC_ID_CHANNEL);
     if (!channel_id)
       goto out;
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                &id_cache))
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
     /* Save the mode */
-    silc_list_start(channel->clients);
-    while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
-      if (chu->client == client_entry) {
-       chu->mode = mode;
-       break;
-      }
-    }
+    chu = silc_client_on_channel(channel, client_entry2);
+    if (chu)
+      chu->mode = mode;
 
     /* 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->internal->ops->notify(client, conn, type, 
-                                 client_entry, mode, 
+    client->internal->ops->notify(client, conn, type,
+                                 id_type, client_entry, mode, 
                                  client_entry2, channel);
     break;
 
@@ -699,40 +766,27 @@ void silc_client_notify_by_server(SilcClient client,
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
-    channel_id = silc_id_payload_parse_id(tmp, tmp_len);
+    channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!channel_id)
       goto out;
-    
-    /* Get the channel entry */
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    &id_cache))
-      break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
-    SILC_LOG_DEBUG(("Old Channel ID id(%s)", 
-                   silc_id_render(channel->id, SILC_ID_CHANNEL)));
-
-    /* Remove the old channel entry */
-    silc_idcache_del_by_context(conn->channel_cache, channel);
+    /* Get the channel entry */
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
+      goto out;
 
-    /* Free the old ID */
-    silc_free(channel->id);
+    silc_free(channel_id);
 
     /* Get the new ID */
     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)
+    channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+    if (!channel_id)
       goto out;
 
-    SILC_LOG_DEBUG(("New Channel ID id(%s)", 
-                   silc_id_render(channel->id, SILC_ID_CHANNEL)));
-
-    /* Add the channel entry again to ID cache */
-    silc_idcache_add(conn->channel_cache, channel->channel_name, 
-                    channel->id, channel, 0, NULL);
+    /* Replace the Channel ID */
+    silc_client_replace_channel_id(client, conn, channel, channel_id);
 
     /* Notify application */
     client->internal->ops->notify(client, conn, type, channel, channel);
@@ -750,7 +804,7 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    client_id = silc_id_payload_parse_id(tmp, tmp_len);
+    client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
     if (!client_id)
       goto out;
 
@@ -764,29 +818,28 @@ void silc_client_notify_by_server(SilcClient client,
                                SILC_ID_CHANNEL);
     if (!channel_id)
       goto out;
-    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    &id_cache))
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
       break;
 
-    channel = (SilcChannelEntry)id_cache->context;
-
-    /* Get the kicker */
+    /* From protocol version 1.1 we get the kicker's client ID as well */
     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;
+    if (tmp) {
+      silc_free(client_id);
+      client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+      if (!client_id)
+       goto out;
 
-    /* Find kicker's 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;
-    } else {
-      if (client_entry2 != conn->local_entry)
-       silc_client_nickname_format(client, conn, client_entry2);
+      /* Find kicker's 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, 
+                                            SILC_ID_CLIENT, client_id);
+       goto out;
+      } else {
+       if (client_entry2 != conn->local_entry)
+         silc_client_nickname_format(client, conn, client_entry2);
+      }
     }
 
     /* Get comment */
@@ -798,52 +851,105 @@ void silc_client_notify_by_server(SilcClient client,
     client->internal->ops->notify(client, conn, type, client_entry, tmp, 
                                  client_entry2, channel);
 
-    /* If I was kicked from channel, remove the channel */
+    /* Remove kicked client from channel */
     if (client_entry == conn->local_entry) {
+      /* If I was kicked from channel, remove the channel */
       if (conn->current_channel == channel)
        conn->current_channel = NULL;
-      silc_idcache_del_by_id(conn->channel_cache, channel->id);
-      silc_free(channel->channel_name);
-      silc_free(channel->id);
-      silc_free(channel->key);
-      silc_cipher_free(channel->channel_key);
-      silc_free(channel);
+      silc_client_del_channel(client, conn, channel);
+    } else {
+      chu = silc_client_on_channel(channel, client_entry);
+      if (chu) {
+       silc_hash_table_del(client_entry->channels, channel);
+       silc_hash_table_del(channel->user_list, client_entry);
+       silc_free(chu);
+      }
     }
     break;
 
   case SILC_NOTIFY_TYPE_KILLED:
-    /*
-     * A client (maybe me) was killed from the network.
-     */
+    {
+      /*
+       * A client (maybe me) was killed from the network.
+       */
+      char *comment;
+      SilcUInt32 comment_len;
 
-    SILC_LOG_DEBUG(("Notify: KILLED"));
+      SILC_LOG_DEBUG(("Notify: KILLED"));
 
-    /* Get Client ID */
-    tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
-    if (!tmp)
-      goto out;
+      /* 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;
+      client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+      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;
+      /* 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);
+      /* Get comment */
+      comment = silc_argument_get_arg_type(args, 2, &comment_len);
+
+      /* From protocol version 1.1 we get killer's client ID as well */
+      tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
+      if (tmp) {
+       silc_free(client_id);
+       id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+       if (!id)
+         goto out;
+
+       /* Find Client entry */
+       if (id_type == SILC_ID_CLIENT) {
+         /* Find Client entry */
+         client_id = id;
+         client_entry2 = silc_client_get_client_by_id(client, conn, 
+                                                      client_id);
+         if (!client_entry) {
+           silc_client_notify_by_server_resolve(client, conn, packet, 
+                                                SILC_ID_CLIENT, client_id);
+           goto out;
+         }
+       } else if (id_type == SILC_ID_SERVER) {
+         /* Find Server entry */
+         server_id = id;
+         server = silc_client_get_server_by_id(client, conn, server_id);
+         if (!server) {
+           silc_client_notify_by_server_resolve(client, conn, packet, 
+                                                SILC_ID_SERVER, server_id);
+           goto out;
+         }
+      
+         /* Save the pointer to the client_entry pointer */
+         client_entry2 = (SilcClientEntry)server;
+       } else {
+         /* Find Channel entry */
+         channel_id = id;
+         channel = silc_client_get_channel_by_id(client, conn, channel_id);
+         if (!channel) {
+           silc_client_notify_by_server_resolve(client, conn, packet, 
+                                                SILC_ID_CHANNEL, channel_id);
+           goto out;
+         }
+         
+         /* Save the pointer to the client_entry pointer */
+         client_entry2 = (SilcClientEntry)channel;
+         silc_free(channel_id);
+         channel_id = NULL;
+       }
+      }
 
-    /* Notify application. */
-    client->internal->ops->notify(client, conn, type, client_entry, tmp);
+      /* Notify application. */
+      client->internal->ops->notify(client, conn, type, client_entry, 
+                                   comment, id_type, client_entry2);
 
-    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);
+      if (client_entry != conn->local_entry)
+       /* Remove the client from all channels and free it */
+       silc_client_del_client(client, conn, client_entry);
     }
-
     break;
     
   case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
@@ -853,7 +959,7 @@ void silc_client_notify_by_server(SilcClient client,
        * from channels as they quit as well.
        */
       SilcClientEntry *clients = NULL;
-      uint32 clients_count = 0;
+      SilcUInt32 clients_count = 0;
       int i;
 
       SILC_LOG_DEBUG(("Notify: SIGNOFF"));
@@ -862,7 +968,7 @@ void silc_client_notify_by_server(SilcClient client,
        /* 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);
+         client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
          if (!client_id)
            goto out;
          
@@ -891,7 +997,7 @@ void silc_client_notify_by_server(SilcClient client,
        if (client_entry == conn->local_entry)
          continue;
 
-       silc_client_remove_from_channels(client, conn, client_entry);
+       /* Remove the client from all channels and free it */
        silc_client_del_client(client, conn, client_entry);
       }
       silc_free(clients);
@@ -907,4 +1013,5 @@ void silc_client_notify_by_server(SilcClient client,
   silc_notify_payload_free(payload);
   silc_free(client_id);
   silc_free(channel_id);
+  silc_free(server_id);
 }