Added Requested Attributes sending and receiving support to
[silc.git] / lib / silcclient / client_notify.c
index 12db4c73a2f08c3983da2085178811ae9cf9232a..bd3edd3ff3f9638411756962e20637c7058c1cfc 100644 (file)
@@ -35,11 +35,28 @@ 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_client_get_client_by_id_resolve(client, conn, client_id,
+                                      NULL, NULL, NULL);
   silc_free(client_id);
+  silc_socket_free(res->sock);
+  silc_free(res);
+}
+
+SILC_TASK_CALLBACK(silc_client_notify_del_client_cb)
+{
+  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+  SilcClient client = res->context;
+  SilcClientConnection conn = res->sock->user_data;
+  SilcClientID *client_id = res->packet;
+  SilcClientEntry client_entry;
+  client_entry = silc_client_get_client_by_id(client, conn, client_id);
+  if (client_entry)
+    silc_client_del_client(client, conn, client_entry);
+  silc_free(client_id);
+  silc_socket_free(res->sock);
   silc_free(res);
 }
 
@@ -66,6 +83,68 @@ static void silc_client_notify_by_server_pending(void *context, void *context2)
   silc_free(res);
 }
 
+/* Resets the channel entry's resolve_cmd_ident after whatever-thing
+   was resolved is completed. */
+
+static void silc_client_channel_cond(void *context, void *context2)
+{
+  SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
+  SilcClient client = res->context;
+  SilcClientConnection conn = res->sock->user_data;
+  SilcChannelID *channel_id = res->packet;
+  SilcChannelEntry channel;
+  channel = silc_client_get_channel_by_id(client, conn, channel_id);
+  if (channel)
+    channel->resolve_cmd_ident = 0;
+  silc_free(channel_id);
+  silc_socket_free(res->sock);
+  silc_free(res);
+}
+
+/* Function that starts waiting for the `cmd_ident' to arrive and
+   marks the channel info being resolved.  */
+
+static void silc_client_channel_set_wait(SilcClient client,
+                                        SilcClientConnection conn,
+                                        SilcChannelEntry channel,
+                                        SilcUInt16 cmd_ident)
+{
+  SilcClientNotifyResolve res;
+
+  if (!channel->resolve_cmd_ident) {
+    res = silc_calloc(1, sizeof(*res));
+    res->context = client;
+    res->sock = silc_socket_dup(conn->sock);
+    res->packet = silc_id_dup(channel->id, SILC_ID_CHANNEL);
+    silc_client_command_pending(conn, SILC_COMMAND_NONE, cmd_ident,
+                               silc_client_channel_cond, res);
+    channel->resolve_cmd_ident = cmd_ident;
+  }
+}
+
+/* Attaches to the channel's resolving cmd ident and calls the 
+   notify handling with `packet' after it's received. */
+
+static void silc_client_channel_wait(SilcClient client,
+                                    SilcClientConnection conn,
+                                    SilcChannelEntry channel,
+                                    SilcPacketContext *packet)
+{
+  SilcClientNotifyResolve res;
+
+  if (!channel->resolve_cmd_ident)
+    return;
+
+  res = silc_calloc(1, sizeof(*res));
+  res->packet = silc_packet_context_dup(packet);
+  res->context = client;
+  res->sock = silc_socket_dup(conn->sock);
+
+  silc_client_command_pending(conn, SILC_COMMAND_NONE,
+                             channel->resolve_cmd_ident,
+                             silc_client_notify_by_server_pending, res);
+}
+
 /* Resolve client, channel or server information. */
 
 static void silc_client_notify_by_server_resolve(SilcClient client,
@@ -87,7 +166,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 {
@@ -308,10 +387,11 @@ void silc_client_notify_by_server(SilcClient client,
        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,
+      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);
     }
@@ -369,6 +449,15 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: TOPIC_SET"));
 
+    /* Get channel entry */
+    channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
+                               SILC_ID_CHANNEL);
+    if (!channel_id)
+      goto out;
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
+      break;
+
     /* Get ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -383,6 +472,8 @@ void silc_client_notify_by_server(SilcClient client,
       client_id = id;
       client_entry = silc_client_get_client_by_id(client, conn, client_id);
       if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_CLIENT, client_id);
        goto out;
@@ -392,8 +483,28 @@ void silc_client_notify_by_server(SilcClient client,
       server_id = id;
       server = silc_client_get_server_by_id(client, conn, server_id);
       if (!server) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_SERVER, server_id);
+       server = silc_client_add_server(client, conn, NULL, NULL, server_id);
+       if (!server)
+         goto out;
+
+       server->resolve_cmd_ident = conn->cmd_ident;
+       server_id = NULL;
+       goto out;
+      }
+
+      /* If entry being resoled, wait for it before processing this notify */
+      if (server->resolve_cmd_ident) {
+       SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+       res->packet = silc_packet_context_dup(packet);
+       res->context = client;
+       res->sock = silc_socket_dup(conn->sock);
+       silc_client_command_pending(conn, SILC_COMMAND_NONE, 
+                                   server->resolve_cmd_ident,
+                                   silc_client_notify_by_server_pending, res);
        goto out;
       }
       
@@ -401,18 +512,17 @@ void silc_client_notify_by_server(SilcClient client,
       client_entry = (SilcClientEntry)server;
     } else {
       /* Find Channel entry */
+      silc_free(channel_id);
       channel_id = id;
-      channel = silc_client_get_channel_by_id(client, conn, channel_id);
-      if (!channel) {
+      client_entry = (SilcClientEntry)
+       silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        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 */
@@ -420,14 +530,11 @@ void silc_client_notify_by_server(SilcClient client,
     if (!tmp)
       goto out;
 
-    /* Get channel entry */
-    channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
-                               SILC_ID_CHANNEL);
-    if (!channel_id)
+    /* If information is being resolved for this channel, wait for it */
+    if (channel->resolve_cmd_ident) {
+      silc_client_channel_wait(client, conn, channel, packet);
       goto out;
-    channel = silc_client_get_channel_by_id(client, conn, channel_id);
-    if (!channel)
-      break;
+    }
 
     /* Notify application. The channel entry is sent last as this notify
        is for channel but application don't know it from the arguments
@@ -466,6 +573,18 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
     silc_free(client_id);
 
+    /* Wait for resolving if necessary */
+    if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
+      SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+      res->packet = silc_packet_context_dup(packet);
+      res->context = client;
+      res->sock = silc_socket_dup(conn->sock);
+      silc_client_command_pending(conn, SILC_COMMAND_NONE, 
+                                 client_entry->resolve_cmd_ident,
+                                 silc_client_notify_by_server_pending, res);
+      goto out;
+    }
+
     client_entry->valid = FALSE;
 
     /* Get new Client ID */
@@ -576,6 +695,15 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: CMODE_CHANGE"));
 
+    /* Get channel entry */
+    channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
+                               SILC_ID_CHANNEL);
+    if (!channel_id)
+      goto out;
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
+      goto out;
+
     /* Get ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -590,6 +718,8 @@ void silc_client_notify_by_server(SilcClient client,
       client_id = id;
       client_entry = silc_client_get_client_by_id(client, conn, client_id);
       if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_CLIENT, client_id);
        goto out;
@@ -599,27 +729,46 @@ void silc_client_notify_by_server(SilcClient client,
       server_id = id;
       server = silc_client_get_server_by_id(client, conn, server_id);
       if (!server) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_SERVER, server_id);
+       server = silc_client_add_server(client, conn, NULL, NULL, server_id);
+       if (!server)
+         goto out;
+
+       server->resolve_cmd_ident = conn->cmd_ident;
+       server_id = NULL;
        goto out;
       }
 
+      /* If entry being resoled, wait for it before processing this notify */
+      if (server->resolve_cmd_ident) {
+       SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+       res->packet = silc_packet_context_dup(packet);
+       res->context = client;
+       res->sock = silc_socket_dup(conn->sock);
+       silc_client_command_pending(conn, SILC_COMMAND_NONE, 
+                                   server->resolve_cmd_ident,
+                                   silc_client_notify_by_server_pending, res);
+       goto out;
+      }
+      
       /* Save the pointer to the client_entry pointer */
       client_entry = (SilcClientEntry)server;
     } else {
       /* Find Channel entry */
+      silc_free(channel_id);
       channel_id = id;
-      channel = silc_client_get_channel_by_id(client, conn, channel_id);
-      if (!channel) {
+      client_entry = (SilcClientEntry)
+       silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        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 */
@@ -629,14 +778,11 @@ void silc_client_notify_by_server(SilcClient client,
 
     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)
-      goto out;
-    channel = silc_client_get_channel_by_id(client, conn, channel_id);
-    if (!channel)
+    /* If information is being resolved for this channel, wait for it */
+    if (channel->resolve_cmd_ident) {
+      silc_client_channel_wait(client, conn, channel, packet);
       goto out;
+    }
 
     /* Save the new mode */
     channel->mode = mode;
@@ -673,6 +819,15 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE"));
 
+    /* Get channel entry */
+    channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
+                               SILC_ID_CHANNEL);
+    if (!channel_id)
+      goto out;
+    channel = silc_client_get_channel_by_id(client, conn, channel_id);
+    if (!channel)
+      break;
+
     /* Get ID */
     tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
     if (!tmp)
@@ -687,6 +842,8 @@ void silc_client_notify_by_server(SilcClient client,
       client_id = id;
       client_entry = silc_client_get_client_by_id(client, conn, client_id);
       if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_CLIENT, client_id);
        goto out;
@@ -696,8 +853,28 @@ void silc_client_notify_by_server(SilcClient client,
       server_id = id;
       server = silc_client_get_server_by_id(client, conn, server_id);
       if (!server) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        silc_client_notify_by_server_resolve(client, conn, packet, 
                                             SILC_ID_SERVER, server_id);
+       server = silc_client_add_server(client, conn, NULL, NULL, server_id);
+       if (!server)
+         goto out;
+
+       server->resolve_cmd_ident = conn->cmd_ident;
+       server_id = NULL;
+       goto out;
+      }
+
+      /* If entry being resoled, wait for it before processing this notify */
+      if (server->resolve_cmd_ident) {
+       SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+       res->packet = silc_packet_context_dup(packet);
+       res->context = client;
+       res->sock = silc_socket_dup(conn->sock);
+       silc_client_command_pending(conn, SILC_COMMAND_NONE, 
+                                   server->resolve_cmd_ident,
+                                   silc_client_notify_by_server_pending, res);
        goto out;
       }
 
@@ -705,18 +882,17 @@ void silc_client_notify_by_server(SilcClient client,
       client_entry = (SilcClientEntry)server;
     } else {
       /* Find Channel entry */
+      silc_free(channel_id);
       channel_id = id;
-      channel = silc_client_get_channel_by_id(client, conn, channel_id);
-      if (!channel) {
+      client_entry = (SilcClientEntry)
+       silc_client_get_channel_by_id(client, conn, channel_id);
+      if (!client_entry) {
+       silc_client_channel_set_wait(client, conn, channel,
+                                    conn->cmd_ident + 1);
        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 */
@@ -726,6 +902,12 @@ void silc_client_notify_by_server(SilcClient client,
 
     SILC_GET32_MSB(mode, tmp);
 
+    /* If information is being resolved for this channel, wait for it */
+    if (channel->resolve_cmd_ident) {
+      silc_client_channel_wait(client, conn, channel, packet);
+      goto out;
+    }
+
     /* Get target Client ID */
     tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
     if (!tmp)
@@ -739,17 +921,11 @@ void silc_client_notify_by_server(SilcClient client,
     /* Find target Client entry */
     client_entry2 = 
       silc_client_get_client_by_id(client, conn, client_id);
-    if (!client_entry2)
-      goto out;
-
-    /* Get channel entry */
-    channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
-                               SILC_ID_CHANNEL);
-    if (!channel_id)
+    if (!client_entry2) {
+      silc_client_notify_by_server_resolve(client, conn, packet, 
+                                          SILC_ID_CLIENT, client_id);
       goto out;
-    channel = silc_client_get_channel_by_id(client, conn, channel_id);
-    if (!channel)
-      break;
+    }
 
     /* Save the mode */
     chu = silc_client_on_channel(channel, client_entry2);
@@ -812,7 +988,8 @@ void silc_client_notify_by_server(SilcClient client,
       goto out;
 
     /* Replace the Channel ID */
-    silc_client_replace_channel_id(client, conn, channel, channel_id);
+    if (silc_client_replace_channel_id(client, conn, channel, channel_id))
+      channel_id = NULL;
 
     /* Notify application */
     client->internal->ops->notify(client, conn, type, channel, channel);
@@ -890,6 +1067,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;
 
@@ -946,6 +1134,25 @@ void silc_client_notify_by_server(SilcClient client,
          if (!server) {
            silc_client_notify_by_server_resolve(client, conn, packet, 
                                                 SILC_ID_SERVER, server_id);
+           server = silc_client_add_server(client, conn, NULL, NULL,
+                                           server_id);
+           if (!server)
+             goto out;
+
+           server->resolve_cmd_ident = conn->cmd_ident;
+           server_id = NULL;
+           goto out;
+         }
+
+         if (server->resolve_cmd_ident) {
+           SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+           res->packet = silc_packet_context_dup(packet);
+           res->context = client;
+           res->sock = silc_socket_dup(conn->sock);
+           silc_client_command_pending(conn, SILC_COMMAND_NONE, 
+                                       server->resolve_cmd_ident,
+                                       silc_client_notify_by_server_pending,
+                                       res);
            goto out;
          }
       
@@ -1063,6 +1270,93 @@ void silc_client_notify_by_server(SilcClient client,
     }
     break;
 
+  case SILC_NOTIFY_TYPE_WATCH:
+    {
+      /*
+       * Received notify about some client we are watching
+       */
+      SilcNotifyType notify = 0;
+      bool del_client = FALSE;
+
+      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 (tmp_nick && !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))
+       del_client = TRUE;
+      else if (notify == SILC_NOTIFY_TYPE_SIGNOFF ||
+              notify == SILC_NOTIFY_TYPE_SERVER_SIGNOFF ||
+              notify == SILC_NOTIFY_TYPE_KILLED)
+       del_client = TRUE;
+
+      if (del_client) {
+       SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
+       res->context = client;
+       res->sock = silc_socket_dup(conn->sock);
+       res->packet = client_id;
+        client_id = NULL;
+       silc_schedule_task_add(client->schedule, conn->sock->sock,
+                              silc_client_notify_del_client_cb, res,
+                              1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+      }
+    }
+    break;
+
   default:
     break;
   }