updates.
[silc.git] / lib / silcclient / client_notify.c
index 797b464e8deaba7ce547ae69c48c4285238bd04c..dea2e513510458159d5cc2358fdb97fa31c6be06 100644 (file)
@@ -35,11 +35,12 @@ typedef struct {
 SILC_TASK_CALLBACK(silc_client_notify_check_client)
 { 
   SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
-  SilcClientConnection conn = res->context;
-  SilcClient client = conn->client;
+  SilcClient client = res->context;
+  SilcClientConnection conn = res->sock->user_data;
   SilcClientID *client_id = res->packet;
   silc_client_get_client_by_id_resolve(client, conn, client_id, NULL, NULL);
   silc_free(client_id);
+  silc_socket_free(res->sock);
   silc_free(res);
 }
 
@@ -55,11 +56,8 @@ static void silc_client_notify_by_server_pending(void *context, void *context2)
 
   SILC_LOG_DEBUG(("Start"));
 
-  if (reply) {
-    SilcCommandStatus status = silc_command_get_status(reply->payload);
-    if (status != SILC_STATUS_OK)
-      goto out;
-  }
+  if (reply && !silc_command_get_status(reply->payload, NULL, NULL))
+    goto out;
 
   silc_client_notify_by_server(res->context, res->sock, res->packet);
 
@@ -90,7 +88,7 @@ static void silc_client_notify_by_server_resolve(SilcClient client,
                                 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);
+                            1, 4, idp->data, idp->len);
     silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
                                silc_client_notify_by_server_pending, res);
   } else {
@@ -226,9 +224,10 @@ void silc_client_notify_by_server(SilcClient client,
        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, 
                                           SILC_ID_CLIENT, client_id);
+      client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
+      client_entry->resolve_cmd_ident = conn->cmd_ident;
       goto out;
     } else {
       if (client_entry != conn->local_entry)
@@ -306,15 +305,16 @@ void silc_client_notify_by_server(SilcClient client,
 
     /* 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 - 14 seconds.  If it is not valid after
+       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->context = client;
+      res->sock = silc_socket_dup(conn->sock);
       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) % 9)),
+      silc_schedule_task_add(client->schedule, conn->sock->sock,
+                            silc_client_notify_check_client, res,
+                            (5 + (silc_rng_get_rn16(client->rng) % 29)),
                             0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
     }
 
@@ -459,7 +459,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 (conn->local_id && SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
       break;
 
     /* Find old Client entry */
@@ -485,6 +485,31 @@ void silc_client_notify_by_server(SilcClient client,
     tmp = silc_argument_get_arg_type(args, 3, NULL);
     if (tmp) {
       /* Protocol version 1.1 */
+      char *tmp_nick = NULL;
+
+      /* Check whether nickname changed at all.  It is possible that nick
+        change notify is received but nickname didn't changed, only the
+        ID changes. */
+      if (client->internal->params->nickname_parse)
+       client->internal->params->nickname_parse(client_entry->nickname,
+                                                &tmp_nick);
+      else
+       tmp_nick = strdup(tmp);
+
+      if (tmp_nick && !strcmp(tmp, tmp_nick)) {
+       /* Nickname didn't change. Update only the ID */
+       silc_idcache_del_by_context(conn->client_cache, client_entry);
+       silc_free(client_entry->id);
+       client_entry->id = silc_id_dup(client_id, SILC_ID_CLIENT);
+       silc_idcache_add(conn->client_cache, strdup(tmp),
+                        client_entry->id, client_entry, 0, NULL);
+
+       /* Notify application */
+       client->internal->ops->notify(client, conn, type, 
+                                     client_entry, client_entry);
+       break;
+      }
+      silc_free(tmp_nick);
 
       /* Create new client entry, and save all old information with the
         new nickname and client ID */
@@ -867,6 +892,17 @@ void silc_client_notify_by_server(SilcClient client,
        silc_hash_table_del(channel->user_list, client_entry);
        silc_free(chu);
       }
+
+      if (!silc_hash_table_count(client_entry->channels)) {
+       SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+       res->context = client;
+       res->sock = silc_socket_dup(conn->sock);
+       res->packet = silc_id_dup(client_entry->id, SILC_ID_CLIENT);
+       silc_schedule_task_add(client->schedule, conn->sock->sock,
+                              silc_client_notify_check_client, res,
+                              (5 + (silc_rng_get_rn16(client->rng) % 529)),
+                              0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+      }
     }
     break;
 
@@ -901,25 +937,53 @@ void silc_client_notify_by_server(SilcClient client,
       tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
       if (tmp) {
        silc_free(client_id);
-       client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
-       if (!client_id)
+       id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+       if (!id)
          goto out;
 
-       /* Find killer'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;
+       /* 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 {
-         if (client_entry2 != conn->local_entry)
-           silc_client_nickname_format(client, conn, client_entry2);
+         /* 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, 
-                                   comment, client_entry2);
+                                   comment, id_type, client_entry2);
 
       if (client_entry != conn->local_entry)
        /* Remove the client from all channels and free it */
@@ -980,6 +1044,109 @@ void silc_client_notify_by_server(SilcClient client,
     }
     break;
 
+  case SILC_NOTIFY_TYPE_ERROR:
+    {
+      /*
+       * Some has occurred and server is notifying us about it.
+       */
+      SilcStatus error;
+
+      tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
+      if (!tmp && tmp_len != 1)
+       goto out;
+      error = (SilcStatus)tmp[0];
+
+      SILC_LOG_DEBUG(("Notify: ERROR (%d)", error));
+
+      if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
+       tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
+       if (tmp) {
+         client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+         if (!client_id)
+           goto out;
+         client_entry = silc_client_get_client_by_id(client, conn,
+                                                     client_id);
+         if (client_entry)
+           silc_client_del_client(client, conn, client_entry);
+       }
+      }
+
+      /* Notify application. */
+      client->internal->ops->notify(client, conn, type, error);
+    }
+    break;
+
+  case SILC_NOTIFY_TYPE_WATCH:
+    {
+      /*
+       * Received notify about some client we are watching
+       */
+      SilcNotifyType notify = 0;
+
+      SILC_LOG_DEBUG(("Notify: WATCH"));
+
+      /* Get sender 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, 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, 
+                                            SILC_ID_CLIENT, client_id);
+       goto out;
+      }
+
+      /* Get user mode */
+      tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
+      if (!tmp || tmp_len != 4)
+       goto out;
+      SILC_GET32_MSB(mode, tmp);
+
+      /* Get notify type */
+      tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
+      if (tmp && tmp_len != 2)
+       goto out;
+      if (tmp)
+       SILC_GET16_MSB(notify, tmp);
+
+      /* Get nickname */
+      tmp = silc_argument_get_arg_type(args, 2, NULL);
+      if (tmp) {
+       char *tmp_nick = NULL;
+
+       if (client->internal->params->nickname_parse)
+         client->internal->params->nickname_parse(client_entry->nickname,
+                                                  &tmp_nick);
+       else
+         tmp_nick = strdup(tmp);
+
+       /* If same nick, the client was new to us and has become "present"
+          to network.  Send NULL as nick to application. */
+       if (!strcmp(tmp, tmp_nick))
+         tmp = NULL;
+
+       silc_free(tmp_nick);
+      }
+
+      /* Notify application. */
+      client->internal->ops->notify(client, conn, type, client_entry,
+                                   tmp, mode, notify);
+
+      client_entry->mode = mode;
+
+      /* If nickname was changed, remove the client entry unless the
+        client is on some channel */
+      if (tmp && notify == SILC_NOTIFY_TYPE_NICK_CHANGE &&
+         !silc_hash_table_count(client_entry->channels))
+       silc_client_del_client(client, conn, client_entry);
+    }
+    break;
+
   default:
     break;
   }