updates.
[silc.git] / lib / silcclient / client_notify.c
index bccd100986430c01d5f4aec8b497cbd96746e62c..49c315c51fc46e5cddd0c3230447885d822776c4 100644 (file)
@@ -2,7 +2,7 @@
 
   client_notify.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
   Copyright (C) 1997 - 2001 Pekka Riikonen
 
 #include "clientlibincludes.h"
 #include "client_internal.h"
 
+typedef struct {
+  SilcPacketContext *packet;
+  void *context;
+  SilcSocketConnection sock;
+} *SilcClientNotifyResolve;
+
 /* 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. */
 
-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);
+  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+  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_client_notify_by_server(res->context, res->sock, res->packet);
+  silc_socket_free(res->sock);
 }
 
 /* Destructor for the pending command callback */
 
 static void silc_client_notify_by_server_destructor(void *context)
 {
-  silc_packet_context_free((SilcPacketContext *)context);
+  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+  silc_packet_context_free(res->packet);
+  silc_free(res);
 }
 
 /* Resolve client information from server by Client ID. */
@@ -49,17 +71,21 @@ static void silc_client_notify_by_server_resolve(SilcClient client,
                                                 SilcPacketContext *packet,
                                                 SilcClientID *client_id)
 {
-  SilcPacketContext *p = silc_packet_context_dup(packet);
+  SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
   SilcBuffer idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
 
-  p->context = (void *)client;
-  p->sock = conn->sock;
+  res->packet = silc_packet_context_dup(packet);
+  res->context = client;
+  res->sock = silc_socket_dup(conn->sock);
 
-  silc_client_send_command(client, conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident,
+  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, p);
+                             silc_client_notify_by_server_pending, res);
   silc_buffer_free(idp);
 }
 
@@ -75,17 +101,22 @@ 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;
+
+  SILC_LOG_DEBUG(("Start"));
 
-  payload = silc_notify_payload_parse(buffer);
+  payload = silc_notify_payload_parse(buffer->data, buffer->len);
   if (!payload)
     goto out;
 
@@ -97,8 +128,8 @@ void silc_client_notify_by_server(SilcClient client,
   switch(type) {
   case SILC_NOTIFY_TYPE_NONE:
     /* Notify application */
-    client->ops->notify(client, conn, type, 
-                       silc_argument_get_arg_type(args, 1, NULL));
+    client->internal->ops->notify(client, conn, type, 
+                                 silc_argument_get_arg_type(args, 1, NULL));
     break;
 
   case SILC_NOTIFY_TYPE_INVITE:
@@ -107,6 +138,8 @@ void silc_client_notify_by_server(SilcClient client,
      * for the application.
      */
     
+    SILC_LOG_DEBUG(("Notify: INVITE"));
+
     /* Get Channel ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -119,7 +152,7 @@ void silc_client_notify_by_server(SilcClient client,
     /* Get the channel entry */
     channel = NULL;
     if (silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
-                                    SILC_ID_CHANNEL, &id_cache))
+                                   &id_cache))
       channel = (SilcChannelEntry)id_cache->context;
 
     /* Get sender Client ID */
@@ -144,7 +177,8 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     /* Notify application */
-    client->ops->notify(client, conn, type, channel, tmp, client_entry);
+    client->internal->ops->notify(client, conn, type, channel, tmp, 
+                                 client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_JOIN:
@@ -153,6 +187,8 @@ void silc_client_notify_by_server(SilcClient client,
      * cache them for later use.
      */
 
+    SILC_LOG_DEBUG(("Notify: JOIN"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -171,8 +207,16 @@ void silc_client_notify_by_server(SilcClient client,
 
     /* If nickname or username hasn't been resolved, do so */
     if (!client_entry->nickname || !client_entry->username) {
+      if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
+       client_entry->status &= ~SILC_CLIENT_STATUS_RESOLVING;
+       goto out;
+      }
+      client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
       silc_client_notify_by_server_resolve(client, conn, packet, client_id);
       goto out;
+    } else {
+      if (client_entry != conn->local_entry)
+       silc_client_nickname_format(client, conn, client_entry);
     }
 
     /* Get Channel ID */
@@ -186,7 +230,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;
@@ -198,13 +242,10 @@ void silc_client_notify_by_server(SilcClient client,
       silc_list_add(channel->clients, chu);
     }
 
-    /* XXX add support for multiple same nicks on same channel. Check
-       for them here */
-
     /* 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, channel);
+    client->internal->ops->notify(client, conn, type, client_entry, channel);
     break;
 
   case SILC_NOTIFY_TYPE_LEAVE:
@@ -213,6 +254,8 @@ void silc_client_notify_by_server(SilcClient client,
      * we'll keep it in the cache in case we'll need it later.
      */
     
+    SILC_LOG_DEBUG(("Notify: LEAVE"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -234,7 +277,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;
@@ -252,7 +295,7 @@ 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->ops->notify(client, conn, type, client_entry, channel);
+    client->internal->ops->notify(client, conn, type, client_entry, channel);
     break;
 
   case SILC_NOTIFY_TYPE_SIGNOFF:
@@ -260,6 +303,8 @@ void silc_client_notify_by_server(SilcClient client,
      * Someone left SILC. We'll remove it from all channels and from cache.
      */
 
+    SILC_LOG_DEBUG(("Notify: SIGNOFF"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -279,8 +324,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);
@@ -288,19 +332,10 @@ void silc_client_notify_by_server(SilcClient client,
       tmp = NULL;
 
     /* Notify application */
-    client->ops->notify(client, conn, type, client_entry, tmp);
+    client->internal->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, conn, client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_TOPIC_SET:
@@ -308,20 +343,65 @@ void silc_client_notify_by_server(SilcClient client,
      * Someone set the topic on a channel.
      */
 
+    SILC_LOG_DEBUG(("Notify: TOPIC_SET"));
+
     /* 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)
+    idp = silc_id_payload_parse(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;
+      }
+
+      /* 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);
+       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);
+    } else {
+      channel_id = silc_id_payload_parse_id(tmp, tmp_len);
+      if (!channel_id) {
+       silc_id_payload_free(idp);
+       goto out;
+      }
+      
+      channel = silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!channel) {
+       silc_id_payload_free(idp);
+       silc_free(channel_id);
+       goto out;
+      }
+      
+      /* Save the pointer to the client_entry pointer */
+      client_entry = (SilcClientEntry)channel;
+      silc_free(channel_id);
+    }
 
     /* Get topic */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
@@ -334,7 +414,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;
@@ -342,7 +422,11 @@ 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->ops->notify(client, conn, type, client_entry, tmp, channel);
+    client->internal->ops->notify(client, conn, type, 
+                                 silc_id_payload_get_type(idp),
+                                 client_entry, tmp, channel);
+
+    silc_id_payload_free(idp);
     break;
 
   case SILC_NOTIFY_TYPE_NICK_CHANGE:
@@ -353,6 +437,8 @@ void silc_client_notify_by_server(SilcClient client,
      * application.
      */
 
+    SILC_LOG_DEBUG(("Notify: NICK_CHANGE"));
+
     /* Get old Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -363,7 +449,7 @@ 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 old Client entry */
@@ -372,6 +458,8 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
     silc_free(client_id);
 
+    client_entry->valid = FALSE;
+
     /* Get new Client ID */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
     if (!tmp)
@@ -386,31 +474,24 @@ void silc_client_notify_by_server(SilcClient client,
     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);
     }
 
     /* 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,
                                      client_entry2);
 
     /* Notify application */
-    client->ops->notify(client, conn, type, client_entry, client_entry2);
+    client->internal->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_free(client_entry);
+    silc_client_del_client_entry(client, conn, client_entry);
     break;
 
   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
@@ -418,46 +499,102 @@ void silc_client_notify_by_server(SilcClient client,
      * Someone changed a channel mode
      */
 
+    SILC_LOG_DEBUG(("Notify: CMODE_CHANGE"));
+
     /* 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)
+    idp = silc_id_payload_parse(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(silc_hmac_get_hash(channel->hmac), 
+                    channel->key, channel->key_len / 8,
+                    hash);
+      silc_hmac_set_key(channel->hmac, hash, 
+                       silc_hash_len(silc_hmac_get_hash(channel->hmac)));
+      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->internal->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:
@@ -465,6 +602,8 @@ void silc_client_notify_by_server(SilcClient client,
      * Someone changed user's mode on a channel
      */
 
+    SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -475,10 +614,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);
@@ -509,7 +649,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;
@@ -526,8 +666,9 @@ 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->ops->notify(client, conn, type, client_entry, mode, 
-                       client_entry2, channel);
+    client->internal->ops->notify(client, conn, type, 
+                                 client_entry, mode, 
+                                 client_entry2, channel);
     break;
 
   case SILC_NOTIFY_TYPE_MOTD:
@@ -535,13 +676,15 @@ void silc_client_notify_by_server(SilcClient client,
      * Received Message of the day
      */
 
+    SILC_LOG_DEBUG(("Notify: MOTD"));
+
     /* Get motd */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
       goto out;
     
     /* Notify application */
-    client->ops->notify(client, conn, type, tmp);
+    client->internal->ops->notify(client, conn, type, tmp);
     break;
 
   case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
@@ -550,6 +693,8 @@ void silc_client_notify_by_server(SilcClient client,
      * ID to the one provided here.
      */
 
+    SILC_LOG_DEBUG(("Notify: CHANNEL_CHANGE"));
+
     /* Get the old ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -560,13 +705,18 @@ 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)));
+
+    /* Remove the old channel entry */
+    silc_idcache_del_by_context(conn->channel_cache, channel);
+
     /* Free the old ID */
-    silc_free(channel_id);
     silc_free(channel->id);
 
     /* Get the new ID */
@@ -577,10 +727,15 @@ 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)));
+
+    /* Add the channel entry again to ID cache */
+    silc_idcache_add(conn->channel_cache, channel->channel_name, 
+                    channel->id, channel, 0, NULL);
 
     /* Notify application */
-    client->ops->notify(client, conn, type, channel, channel);
+    client->internal->ops->notify(client, conn, type, channel, channel);
     break;
 
   case SILC_NOTIFY_TYPE_KICKED:
@@ -588,6 +743,8 @@ void silc_client_notify_by_server(SilcClient client,
      * A client (maybe me) was kicked from a channel
      */
 
+    SILC_LOG_DEBUG(("Notify: KICKED"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -608,25 +765,44 @@ 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;
 
+    /* Get the kicker */
+    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;
+
+    /* 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);
+    }
+
     /* Get comment */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
 
     /* 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, tmp, channel);
+    client->internal->ops->notify(client, conn, type, client_entry, tmp, 
+                                 client_entry2, channel);
 
     /* If I was kicked from channel, remove the channel */
     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);
@@ -640,6 +816,8 @@ void silc_client_notify_by_server(SilcClient client,
      * A client (maybe me) was killed from the network.
      */
 
+    SILC_LOG_DEBUG(("Notify: KILLED"));
+
     /* Get Client ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -657,39 +835,76 @@ void silc_client_notify_by_server(SilcClient client,
     /* Get comment */
     tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
 
-    /* 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, tmp);
+    /* Notify application. */
+    client->internal->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_idcache_del_by_id(conn->client_cache, SILC_ID_CLIENT, 
-                            client_entry->id);
-      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_free(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;
+
+      SILC_LOG_DEBUG(("Notify: SIGNOFF"));
+
+      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->internal->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;
   }
 
  out:
   silc_notify_payload_free(payload);
-  if (client_id)
-    silc_free(client_id);
-  if (channel_id)
-    silc_free(channel_id);
+  silc_free(client_id);
+  silc_free(channel_id);
 }