+/* Server side of WATCH command. */
+
+SILC_SERVER_CMD_FUNC(watch)
+{
+ SilcServerCommandContext cmd = (SilcServerCommandContext)context;
+ SilcServer server = cmd->server;
+ char *add_nick, *del_nick;
+ SilcUInt32 add_nick_len, del_nick_len, tmp_len;
+ char nick[128 + 1];
+ unsigned char hash[16], *tmp;
+ SilcClientEntry client;
+ SilcClientID *client_id = NULL;
+
+ SILC_SERVER_COMMAND_CHECK(SILC_COMMAND_WATCH, cmd, 1, 3);
+
+ if (server->server_type == SILC_SERVER && !server->standalone) {
+ if (!cmd->pending) {
+ /* Send the command to router */
+ SilcBuffer tmpbuf;
+ SilcUInt16 old_ident;
+
+ old_ident = silc_command_get_ident(cmd->payload);
+ silc_command_set_ident(cmd->payload, ++server->cmd_ident);
+ tmpbuf = silc_command_payload_encode_payload(cmd->payload);
+
+ silc_server_packet_send(server, server->router->connection,
+ SILC_PACKET_COMMAND, cmd->packet->flags,
+ tmpbuf->data, tmpbuf->len, TRUE);
+
+ /* Reprocess this packet after received reply from router */
+ silc_server_command_pending(server, SILC_COMMAND_WATCH,
+ silc_command_get_ident(cmd->payload),
+ silc_server_command_watch,
+ silc_server_command_dup(cmd));
+ cmd->pending = TRUE;
+ silc_command_set_ident(cmd->payload, old_ident);
+ silc_buffer_free(tmpbuf);
+ } else if (context2) {
+ /* Received reply from router, just send same data to the client. */
+ SilcServerCommandReplyContext reply = context2;
+ SilcStatus status;
+ silc_command_get_status(reply->payload, &status, NULL);
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH, status);
+ }
+
+ goto out;
+ }
+
+ /* We are router and keep the watch list for local cell */
+
+ /* Get the client ID */
+ tmp = silc_argument_get_arg_type(cmd->args, 1, &tmp_len);
+ if (!tmp) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+ goto out;
+ }
+ client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+ if (!client_id) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
+ goto out;
+ }
+
+ /* Get the client entry which must be in local list */
+ client = silc_idlist_find_client_by_id(server->local_list,
+ client_id, TRUE, NULL);
+ if (!client) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NO_SUCH_CLIENT_ID);
+ goto out;
+ }
+
+ /* Take nickname */
+ add_nick = silc_argument_get_arg_type(cmd->args, 2, &add_nick_len);
+ del_nick = silc_argument_get_arg_type(cmd->args, 3, &del_nick_len);
+ if (!add_nick && !del_nick) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+ goto out;
+ }
+
+ if (add_nick && add_nick_len > 128)
+ add_nick[128] = '\0';
+ if (del_nick && del_nick_len > 128)
+ del_nick[128] = '\0';
+
+ memset(nick, 0, sizeof(nick));
+
+ /* Add new nickname to be watched in our cell */
+ if (add_nick) {
+ if (silc_server_name_bad_chars(add_nick, strlen(add_nick)) == TRUE) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_BAD_NICKNAME);
+ goto out;
+ }
+
+ /* Hash the nick, we have the hash saved, not nicks because we can
+ do one to one mapping to the nick from Client ID hash this way. */
+ silc_to_lower(add_nick, nick, sizeof(nick) - 1);
+ silc_hash_make(server->md5hash, nick, strlen(nick), hash);
+
+ /* Check whether this client is already watching this nickname */
+ if (silc_hash_table_find_by_context(server->watcher_list, hash,
+ client, NULL)) {
+ /* Nickname is alredy being watched for this client */
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NICKNAME_IN_USE);
+ goto out;
+ }
+
+ /* Get the nickname from the watcher list and use the same key in
+ new entries as well. If key doesn't exist then create it. */
+ if (!silc_hash_table_find(server->watcher_list, hash, (void **)&tmp, NULL))
+ tmp = silc_memdup(hash, CLIENTID_HASH_LEN);
+
+ /* Add the client to the watcher list with the specified nickname hash. */
+ silc_hash_table_add(server->watcher_list, tmp, client);
+ }
+
+ /* Delete nickname from watch list */
+ if (del_nick) {
+ if (silc_server_name_bad_chars(del_nick, strlen(del_nick)) == TRUE) {
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_BAD_NICKNAME);
+ goto out;
+ }
+
+ /* Hash the nick, we have the hash saved, not nicks because we can
+ do one to one mapping to the nick from Client ID hash this way. */
+ silc_to_lower(del_nick, nick, sizeof(nick) - 1);
+ silc_hash_make(server->md5hash, nick, strlen(nick), hash);
+
+ /* Check that this client is watching for this nickname */
+ if (!silc_hash_table_find_by_context(server->watcher_list, hash,
+ client, (void **)&tmp)) {
+ /* Nickname is alredy being watched for this client */
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_ERR_NO_SUCH_NICK);
+ goto out;
+ }
+
+ /* Delete the nickname from the watcher list. */
+ silc_hash_table_del_by_context(server->watcher_list, hash, client);
+
+ /* Now check whether there still exists entries with this key, if not
+ then free the key to not leak memory. */
+ if (!silc_hash_table_find(server->watcher_list, hash, NULL, NULL))
+ silc_free(tmp);
+ }
+
+ silc_server_command_send_status_reply(cmd, SILC_COMMAND_WATCH,
+ SILC_STATUS_OK);
+
+ out:
+ silc_free(client_id);
+ silc_server_command_free(cmd);
+}
+