5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /* This file includes the Notify packet handling. Notify packets are
21 important packets sent by the server. They tell different things to the
22 client such as nick changes, mode changes etc. */
24 #include "silcincludes.h"
25 #include "silcclient.h"
26 #include "client_internal.h"
28 /* Context used for resolving client, channel and server info. */
32 SilcSocketConnection sock;
33 } *SilcClientNotifyResolve;
35 SILC_TASK_CALLBACK(silc_client_notify_check_client)
37 SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
38 SilcClient client = res->context;
39 SilcClientConnection conn = res->sock->user_data;
40 SilcClientID *client_id = res->packet;
41 silc_client_get_client_by_id_resolve(client, conn, client_id,
44 silc_socket_free(res->sock);
48 SILC_TASK_CALLBACK(silc_client_notify_del_client_cb)
50 SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
51 SilcClient client = res->context;
52 SilcClientConnection conn = res->sock->user_data;
53 SilcClientID *client_id = res->packet;
54 SilcClientEntry client_entry;
55 client_entry = silc_client_get_client_by_id(client, conn, client_id);
57 silc_client_del_client(client, conn, client_entry);
59 silc_socket_free(res->sock);
63 /* Called when notify is received and some async operation (such as command)
64 is required before processing the notify message. This calls again the
65 silc_client_notify_by_server and reprocesses the original notify packet. */
67 static void silc_client_notify_by_server_pending(void *context, void *context2)
69 SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
70 SilcClientCommandReplyContext reply =
71 (SilcClientCommandReplyContext)context2;
73 SILC_LOG_DEBUG(("Start"));
75 if (reply && !silc_command_get_status(reply->payload, NULL, NULL))
78 silc_client_notify_by_server(res->context, res->sock, res->packet);
81 silc_socket_free(res->sock);
82 silc_packet_context_free(res->packet);
86 /* Resets the channel entry's resolve_cmd_ident after whatever-thing
87 was resolved is completed. */
89 static void silc_client_channel_cond(void *context, void *context2)
91 SilcClientNotifyResolve res = (SilcClientNotifyResolve)context;
92 SilcClient client = res->context;
93 SilcClientConnection conn = res->sock->user_data;
94 SilcChannelID *channel_id = res->packet;
95 SilcChannelEntry channel;
96 channel = silc_client_get_channel_by_id(client, conn, channel_id);
98 channel->resolve_cmd_ident = 0;
99 silc_free(channel_id);
100 silc_socket_free(res->sock);
104 /* Function that starts waiting for the `cmd_ident' to arrive and
105 marks the channel info being resolved. */
107 static void silc_client_channel_set_wait(SilcClient client,
108 SilcClientConnection conn,
109 SilcChannelEntry channel,
110 SilcUInt16 cmd_ident)
112 SilcClientNotifyResolve res;
114 if (!channel->resolve_cmd_ident) {
115 res = silc_calloc(1, sizeof(*res));
116 res->context = client;
117 res->sock = silc_socket_dup(conn->sock);
118 res->packet = silc_id_dup(channel->id, SILC_ID_CHANNEL);
119 silc_client_command_pending(conn, SILC_COMMAND_NONE, cmd_ident,
120 silc_client_channel_cond, res);
121 channel->resolve_cmd_ident = cmd_ident;
125 /* Attaches to the channel's resolving cmd ident and calls the
126 notify handling with `packet' after it's received. */
128 static void silc_client_channel_wait(SilcClient client,
129 SilcClientConnection conn,
130 SilcChannelEntry channel,
131 SilcPacketContext *packet)
133 SilcClientNotifyResolve res;
135 if (!channel->resolve_cmd_ident)
138 res = silc_calloc(1, sizeof(*res));
139 res->packet = silc_packet_context_dup(packet);
140 res->context = client;
141 res->sock = silc_socket_dup(conn->sock);
143 silc_client_command_pending(conn, SILC_COMMAND_NONE,
144 channel->resolve_cmd_ident,
145 silc_client_notify_by_server_pending, res);
148 /* Resolve client, channel or server information. */
150 static void silc_client_notify_by_server_resolve(SilcClient client,
151 SilcClientConnection conn,
152 SilcPacketContext *packet,
156 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
157 SilcBuffer idp = silc_id_payload_encode(id, id_type);
159 res->packet = silc_packet_context_dup(packet);
160 res->context = client;
161 res->sock = silc_socket_dup(conn->sock);
163 /* For client resolving use WHOIS, and otherwise use IDENTIFY */
164 if (id_type == SILC_ID_CLIENT) {
165 silc_client_command_register(client, SILC_COMMAND_WHOIS, NULL, NULL,
166 silc_client_command_reply_whois_i, 0,
168 silc_client_command_send(client, conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
169 1, 4, idp->data, idp->len);
170 silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
171 silc_client_notify_by_server_pending, res);
173 silc_client_command_register(client, SILC_COMMAND_IDENTIFY, NULL, NULL,
174 silc_client_command_reply_identify_i, 0,
176 silc_client_command_send(client, conn, SILC_COMMAND_IDENTIFY,
177 conn->cmd_ident, 1, 5, idp->data, idp->len);
178 silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, conn->cmd_ident,
179 silc_client_notify_by_server_pending, res);
181 silc_buffer_free(idp);
184 /* Received notify message from server */
186 void silc_client_notify_by_server(SilcClient client,
187 SilcSocketConnection sock,
188 SilcPacketContext *packet)
190 SilcBuffer buffer = packet->buffer;
191 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
192 SilcNotifyPayload payload;
194 SilcArgumentPayload args;
198 SilcClientID *client_id = NULL;
199 SilcChannelID *channel_id = NULL;
200 SilcServerID *server_id = NULL;
201 SilcClientEntry client_entry = NULL;
202 SilcClientEntry client_entry2 = NULL;
203 SilcChannelEntry channel;
205 SilcServerEntry server;
207 SilcUInt32 tmp_len, mode;
209 SILC_LOG_DEBUG(("Start"));
211 payload = silc_notify_payload_parse(buffer->data, buffer->len);
215 type = silc_notify_get_type(payload);
216 args = silc_notify_get_args(payload);
221 case SILC_NOTIFY_TYPE_NONE:
222 /* Notify application */
223 client->internal->ops->notify(client, conn, type,
224 silc_argument_get_arg_type(args, 1, NULL));
227 case SILC_NOTIFY_TYPE_INVITE:
229 * Someone invited me to a channel. Find Client and Channel entries
230 * for the application.
233 SILC_LOG_DEBUG(("Notify: INVITE"));
236 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
240 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
244 /* Get the channel entry */
245 channel = silc_client_get_channel_by_id(client, conn, channel_id);
247 /* Get sender Client ID */
248 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
252 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
256 /* Find Client entry and if not found query it */
257 client_entry = silc_client_get_client_by_id(client, conn, client_id);
259 silc_client_notify_by_server_resolve(client, conn, packet,
260 SILC_ID_CLIENT, client_id);
264 /* Get the channel name */
265 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
269 /* Notify application */
270 client->internal->ops->notify(client, conn, type, channel, tmp,
274 case SILC_NOTIFY_TYPE_JOIN:
276 * Someone has joined to a channel. Get their ID and nickname and
277 * cache them for later use.
280 SILC_LOG_DEBUG(("Notify: JOIN"));
283 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
287 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
291 /* Find Client entry and if not found query it */
292 client_entry = silc_client_get_client_by_id(client, conn, client_id);
294 silc_client_notify_by_server_resolve(client, conn, packet,
295 SILC_ID_CLIENT, client_id);
299 /* If nickname or username hasn't been resolved, do so */
300 if (!client_entry->nickname || !client_entry->username) {
301 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
302 client_entry->status &= ~SILC_CLIENT_STATUS_RESOLVING;
305 silc_client_notify_by_server_resolve(client, conn, packet,
306 SILC_ID_CLIENT, client_id);
307 client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
308 client_entry->resolve_cmd_ident = conn->cmd_ident;
311 if (client_entry != conn->local_entry)
312 silc_client_nickname_format(client, conn, client_entry);
316 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
320 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
324 /* Get channel entry */
325 channel = silc_client_get_channel_by_id(client, conn, channel_id);
329 /* Join the client to channel */
330 if (!silc_client_on_channel(channel, client_entry)) {
331 chu = silc_calloc(1, sizeof(*chu));
332 chu->client = client_entry;
333 chu->channel = channel;
334 silc_hash_table_add(channel->user_list, client_entry, chu);
335 silc_hash_table_add(client_entry->channels, channel, chu);
338 /* Notify application. The channel entry is sent last as this notify
339 is for channel but application don't know it from the arguments
341 client->internal->ops->notify(client, conn, type, client_entry, channel);
344 case SILC_NOTIFY_TYPE_LEAVE:
346 * Someone has left a channel. We will remove it from the channel but
347 * we'll keep it in the cache in case we'll need it later.
350 SILC_LOG_DEBUG(("Notify: LEAVE"));
353 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
357 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
361 /* Find Client entry */
363 silc_client_get_client_by_id(client, conn, client_id);
367 /* Get channel entry */
368 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
372 channel = silc_client_get_channel_by_id(client, conn, channel_id);
376 /* Remove client from channel */
377 chu = silc_client_on_channel(channel, client_entry);
379 silc_hash_table_del(client_entry->channels, channel);
380 silc_hash_table_del(channel->user_list, client_entry);
384 /* Some client implementations actually quit network by first doing
385 LEAVE and then immediately SIGNOFF. We'll check for this by doing
386 check for the client after 5 - 34 seconds. If it is not valid after
387 that we'll remove the client from cache. */
388 if (!silc_hash_table_count(client_entry->channels)) {
389 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
390 res->context = client;
391 res->sock = silc_socket_dup(conn->sock);
392 res->packet = silc_id_dup(client_id, SILC_ID_CLIENT);
393 silc_schedule_task_add(client->schedule, conn->sock->sock,
394 silc_client_notify_check_client, res,
395 (5 + (silc_rng_get_rn16(client->rng) % 29)),
396 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
399 /* Notify application. The channel entry is sent last as this notify
400 is for channel but application don't know it from the arguments
402 client->internal->ops->notify(client, conn, type, client_entry, channel);
405 case SILC_NOTIFY_TYPE_SIGNOFF:
407 * Someone left SILC. We'll remove it from all channels and from cache.
410 SILC_LOG_DEBUG(("Notify: SIGNOFF"));
413 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
417 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
421 /* Find Client entry */
423 silc_client_get_client_by_id(client, conn, client_id);
427 /* Remove from all channels */
428 silc_client_remove_from_channels(client, conn, client_entry);
430 /* Remove from cache */
431 silc_idcache_del_by_context(conn->internal->client_cache, client_entry);
433 /* Get signoff message */
434 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
438 /* Notify application */
439 client->internal->ops->notify(client, conn, type, client_entry, tmp);
442 silc_client_del_client_entry(client, conn, client_entry);
445 case SILC_NOTIFY_TYPE_TOPIC_SET:
447 * Someone set the topic on a channel.
450 SILC_LOG_DEBUG(("Notify: TOPIC_SET"));
452 /* Get channel entry */
453 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
457 channel = silc_client_get_channel_by_id(client, conn, channel_id);
462 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
465 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
469 /* Find Client entry */
470 if (id_type == SILC_ID_CLIENT) {
471 /* Find Client entry */
473 client_entry = silc_client_get_client_by_id(client, conn, client_id);
475 silc_client_channel_set_wait(client, conn, channel,
476 conn->cmd_ident + 1);
477 silc_client_notify_by_server_resolve(client, conn, packet,
478 SILC_ID_CLIENT, client_id);
481 } else if (id_type == SILC_ID_SERVER) {
482 /* Find Server entry */
484 server = silc_client_get_server_by_id(client, conn, server_id);
486 silc_client_channel_set_wait(client, conn, channel,
487 conn->cmd_ident + 1);
488 silc_client_notify_by_server_resolve(client, conn, packet,
489 SILC_ID_SERVER, server_id);
490 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
494 server->resolve_cmd_ident = conn->cmd_ident;
499 /* If entry being resoled, wait for it before processing this notify */
500 if (server->resolve_cmd_ident) {
501 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
502 res->packet = silc_packet_context_dup(packet);
503 res->context = client;
504 res->sock = silc_socket_dup(conn->sock);
505 silc_client_command_pending(conn, SILC_COMMAND_NONE,
506 server->resolve_cmd_ident,
507 silc_client_notify_by_server_pending, res);
511 /* Save the pointer to the client_entry pointer */
512 client_entry = (SilcClientEntry)server;
514 /* Find Channel entry */
515 silc_free(channel_id);
517 client_entry = (SilcClientEntry)
518 silc_client_get_channel_by_id(client, conn, channel_id);
520 silc_client_channel_set_wait(client, conn, channel,
521 conn->cmd_ident + 1);
522 silc_client_notify_by_server_resolve(client, conn, packet,
523 SILC_ID_CHANNEL, channel_id);
529 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
533 /* If information is being resolved for this channel, wait for it */
534 if (channel->resolve_cmd_ident) {
535 silc_client_channel_wait(client, conn, channel, packet);
539 /* Notify application. The channel entry is sent last as this notify
540 is for channel but application don't know it from the arguments
542 client->internal->ops->notify(client, conn, type, id_type,
543 client_entry, tmp, channel);
547 case SILC_NOTIFY_TYPE_NICK_CHANGE:
549 * Someone changed their nickname. If we don't have entry for the new
550 * ID we will query it and return here after it's done. After we've
551 * returned we fetch the old entry and free it and notify the
555 SILC_LOG_DEBUG(("Notify: NICK_CHANGE"));
557 /* Get old Client ID */
558 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
562 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
567 if (conn->local_id && SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
570 /* Find old Client entry */
571 client_entry = silc_client_get_client_by_id(client, conn, client_id);
574 silc_free(client_id);
577 /* Wait for resolving if necessary */
578 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
579 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
580 res->packet = silc_packet_context_dup(packet);
581 res->context = client;
582 res->sock = silc_socket_dup(conn->sock);
583 silc_client_command_pending(conn, SILC_COMMAND_NONE,
584 client_entry->resolve_cmd_ident,
585 silc_client_notify_by_server_pending, res);
589 client_entry->valid = FALSE;
591 /* Get new Client ID */
592 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
596 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
600 /* Take the nickname */
601 tmp = silc_argument_get_arg_type(args, 3, NULL);
605 /* Check whether nickname changed at all. It is possible that nick
606 change notify is received but nickname didn't changed, only the
607 ID changes. Check whether the hashes in the Client ID match, if
608 they do nickname didn't change. */
609 if (SILC_ID_COMPARE_HASH(client_entry->id, client_id)) {
610 /* Nickname didn't change. Update only the ID */
611 silc_idcache_del_by_context(conn->internal->client_cache,
613 silc_free(client_entry->id);
614 client_entry->id = silc_id_dup(client_id, SILC_ID_CLIENT);
615 silc_idcache_add(conn->internal->client_cache, strdup(tmp),
616 client_entry->id, client_entry, 0, NULL);
618 /* Notify application */
619 client->internal->ops->notify(client, conn, type,
620 client_entry, client_entry);
624 /* Create new client entry, and save all old information with the
625 new nickname and client ID */
626 client_entry2 = silc_client_add_client(client, conn, NULL, NULL,
627 client_entry->realname,
628 silc_id_dup(client_id,
633 if (client_entry->server)
634 client_entry2->server = strdup(client_entry->server);
635 if (client_entry->username)
636 client_entry2->username = strdup(client_entry->username);
637 if (client_entry->hostname)
638 client_entry2->hostname = strdup(client_entry->hostname);
639 silc_client_update_client(client, conn, client_entry2, tmp, NULL, NULL,
642 /* Remove the old from cache */
643 silc_idcache_del_by_context(conn->internal->client_cache, client_entry);
645 /* Replace old ID entry with new one on all channels. */
646 silc_client_replace_from_channels(client, conn, client_entry,
649 /* Notify application */
650 client->internal->ops->notify(client, conn, type,
651 client_entry, client_entry2);
653 /* Free old client entry */
654 silc_client_del_client_entry(client, conn, client_entry);
658 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
660 * Someone changed a channel mode
663 SILC_LOG_DEBUG(("Notify: CMODE_CHANGE"));
665 /* Get channel entry */
666 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
670 channel = silc_client_get_channel_by_id(client, conn, channel_id);
675 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
678 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
682 /* Find Client entry */
683 if (id_type == SILC_ID_CLIENT) {
684 /* Find Client entry */
686 client_entry = silc_client_get_client_by_id(client, conn, client_id);
688 silc_client_channel_set_wait(client, conn, channel,
689 conn->cmd_ident + 1);
690 silc_client_notify_by_server_resolve(client, conn, packet,
691 SILC_ID_CLIENT, client_id);
695 if (!client_entry->nickname) {
696 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
697 /* Attach to existing resolving */
698 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
699 res->packet = silc_packet_context_dup(packet);
700 res->context = client;
701 res->sock = silc_socket_dup(conn->sock);
702 silc_client_command_pending(conn, SILC_COMMAND_NONE,
703 client_entry->resolve_cmd_ident,
704 silc_client_notify_by_server_pending,
709 /* Do new resolving */
710 silc_client_channel_set_wait(client, conn, channel,
711 conn->cmd_ident + 1);
712 silc_client_notify_by_server_resolve(client, conn, packet,
713 SILC_ID_CLIENT, client_id);
716 } else if (id_type == SILC_ID_SERVER) {
717 /* Find Server entry */
719 server = silc_client_get_server_by_id(client, conn, server_id);
721 silc_client_channel_set_wait(client, conn, channel,
722 conn->cmd_ident + 1);
723 silc_client_notify_by_server_resolve(client, conn, packet,
724 SILC_ID_SERVER, server_id);
725 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
729 server->resolve_cmd_ident = conn->cmd_ident;
734 /* If entry being resoled, wait for it before processing this notify */
735 if (server->resolve_cmd_ident) {
736 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
737 res->packet = silc_packet_context_dup(packet);
738 res->context = client;
739 res->sock = silc_socket_dup(conn->sock);
740 silc_client_command_pending(conn, SILC_COMMAND_NONE,
741 server->resolve_cmd_ident,
742 silc_client_notify_by_server_pending, res);
746 /* Save the pointer to the client_entry pointer */
747 client_entry = (SilcClientEntry)server;
749 /* Find Channel entry */
750 silc_free(channel_id);
752 client_entry = (SilcClientEntry)
753 silc_client_get_channel_by_id(client, conn, channel_id);
755 silc_client_channel_set_wait(client, conn, channel,
756 conn->cmd_ident + 1);
757 silc_client_notify_by_server_resolve(client, conn, packet,
758 SILC_ID_CHANNEL, channel_id);
764 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
768 SILC_GET32_MSB(mode, tmp);
770 /* If information is being resolved for this channel, wait for it */
771 if (channel->resolve_cmd_ident) {
772 silc_client_channel_wait(client, conn, channel, packet);
776 /* Save the new mode */
777 channel->mode = mode;
780 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
782 unsigned char hash[32];
785 silc_hmac_free(channel->hmac);
786 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
789 silc_hash_make(silc_hmac_get_hash(channel->hmac),
790 channel->key, channel->key_len / 8,
792 silc_hmac_set_key(channel->hmac, hash,
793 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
794 memset(hash, 0, sizeof(hash));
797 /* Notify application. The channel entry is sent last as this notify
798 is for channel but application don't know it from the arguments
800 client->internal->ops->notify(client, conn, type, id_type,
801 client_entry, mode, NULL, tmp, channel);
804 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
806 * Someone changed user's mode on a channel
809 SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE"));
811 /* Get channel entry */
812 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
816 channel = silc_client_get_channel_by_id(client, conn, channel_id);
821 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
824 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
828 /* Find Client entry */
829 if (id_type == SILC_ID_CLIENT) {
830 /* Find Client entry */
832 client_entry = silc_client_get_client_by_id(client, conn, client_id);
834 silc_client_channel_set_wait(client, conn, channel,
835 conn->cmd_ident + 1);
836 silc_client_notify_by_server_resolve(client, conn, packet,
837 SILC_ID_CLIENT, client_id);
841 if (!client_entry->nickname) {
842 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
843 /* Attach to existing resolving */
844 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
845 res->packet = silc_packet_context_dup(packet);
846 res->context = client;
847 res->sock = silc_socket_dup(conn->sock);
848 silc_client_command_pending(conn, SILC_COMMAND_NONE,
849 client_entry->resolve_cmd_ident,
850 silc_client_notify_by_server_pending,
855 /* Do new resolving */
856 silc_client_channel_set_wait(client, conn, channel,
857 conn->cmd_ident + 1);
858 silc_client_notify_by_server_resolve(client, conn, packet,
859 SILC_ID_CLIENT, client_id);
862 } else if (id_type == SILC_ID_SERVER) {
863 /* Find Server entry */
865 server = silc_client_get_server_by_id(client, conn, server_id);
867 silc_client_channel_set_wait(client, conn, channel,
868 conn->cmd_ident + 1);
869 silc_client_notify_by_server_resolve(client, conn, packet,
870 SILC_ID_SERVER, server_id);
871 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
875 server->resolve_cmd_ident = conn->cmd_ident;
880 /* If entry being resoled, wait for it before processing this notify */
881 if (server->resolve_cmd_ident) {
882 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
883 res->packet = silc_packet_context_dup(packet);
884 res->context = client;
885 res->sock = silc_socket_dup(conn->sock);
886 silc_client_command_pending(conn, SILC_COMMAND_NONE,
887 server->resolve_cmd_ident,
888 silc_client_notify_by_server_pending, res);
892 /* Save the pointer to the client_entry pointer */
893 client_entry = (SilcClientEntry)server;
895 /* Find Channel entry */
896 silc_free(channel_id);
898 client_entry = (SilcClientEntry)
899 silc_client_get_channel_by_id(client, conn, channel_id);
901 silc_client_channel_set_wait(client, conn, channel,
902 conn->cmd_ident + 1);
903 silc_client_notify_by_server_resolve(client, conn, packet,
904 SILC_ID_CHANNEL, channel_id);
910 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
914 SILC_GET32_MSB(mode, tmp);
916 /* If information is being resolved for this channel, wait for it */
917 if (channel->resolve_cmd_ident) {
918 silc_client_channel_wait(client, conn, channel, packet);
922 /* Get target Client ID */
923 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
927 silc_free(client_id);
928 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
932 /* Find target Client entry */
934 silc_client_get_client_by_id(client, conn, client_id);
935 if (!client_entry2) {
936 silc_client_notify_by_server_resolve(client, conn, packet,
937 SILC_ID_CLIENT, client_id);
942 chu = silc_client_on_channel(channel, client_entry2);
946 /* Notify application. The channel entry is sent last as this notify
947 is for channel but application don't know it from the arguments
949 client->internal->ops->notify(client, conn, type,
950 id_type, client_entry, mode,
951 client_entry2, channel);
954 case SILC_NOTIFY_TYPE_MOTD:
956 * Received Message of the day
959 SILC_LOG_DEBUG(("Notify: MOTD"));
962 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
966 /* Notify application */
967 client->internal->ops->notify(client, conn, type, tmp);
970 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
972 * Router has enforced a new ID to a channel. Let's change the old
973 * ID to the one provided here.
976 SILC_LOG_DEBUG(("Notify: CHANNEL_CHANGE"));
979 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
982 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
986 /* Get the channel entry */
987 channel = silc_client_get_channel_by_id(client, conn, channel_id);
991 silc_free(channel_id);
995 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
998 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1002 /* Replace the Channel ID */
1003 if (silc_client_replace_channel_id(client, conn, channel, channel_id))
1006 /* Notify application */
1007 client->internal->ops->notify(client, conn, type, channel, channel);
1010 case SILC_NOTIFY_TYPE_KICKED:
1012 * A client (maybe me) was kicked from a channel
1015 SILC_LOG_DEBUG(("Notify: KICKED"));
1018 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1022 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1026 /* Find Client entry */
1027 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1031 /* Get channel entry */
1032 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1036 channel = silc_client_get_channel_by_id(client, conn, channel_id);
1040 /* From protocol version 1.1 we get the kicker's client ID as well */
1041 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1043 silc_free(client_id);
1044 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1048 /* Find kicker's client entry and if not found resolve it */
1049 client_entry2 = silc_client_get_client_by_id(client, conn, client_id);
1050 if (!client_entry2) {
1051 silc_client_notify_by_server_resolve(client, conn, packet,
1052 SILC_ID_CLIENT, client_id);
1055 if (client_entry2 != conn->local_entry)
1056 silc_client_nickname_format(client, conn, client_entry2);
1061 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1063 /* Notify application. The channel entry is sent last as this notify
1064 is for channel but application don't know it from the arguments
1066 client->internal->ops->notify(client, conn, type, client_entry, tmp,
1067 client_entry2, channel);
1069 /* Remove kicked client from channel */
1070 if (client_entry == conn->local_entry) {
1071 /* If I was kicked from channel, remove the channel */
1072 if (conn->current_channel == channel)
1073 conn->current_channel = NULL;
1074 silc_client_del_channel(client, conn, channel);
1076 chu = silc_client_on_channel(channel, client_entry);
1078 silc_hash_table_del(client_entry->channels, channel);
1079 silc_hash_table_del(channel->user_list, client_entry);
1083 if (!silc_hash_table_count(client_entry->channels)) {
1084 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1085 res->context = client;
1086 res->sock = silc_socket_dup(conn->sock);
1087 res->packet = silc_id_dup(client_entry->id, SILC_ID_CLIENT);
1088 silc_schedule_task_add(client->schedule, conn->sock->sock,
1089 silc_client_notify_check_client, res,
1090 (5 + (silc_rng_get_rn16(client->rng) % 529)),
1091 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1096 case SILC_NOTIFY_TYPE_KILLED:
1099 * A client (maybe me) was killed from the network.
1102 SilcUInt32 comment_len;
1104 SILC_LOG_DEBUG(("Notify: KILLED"));
1107 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1111 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1115 /* Find Client entry */
1116 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1121 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1123 /* From protocol version 1.1 we get killer's client ID as well */
1124 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1126 silc_free(client_id);
1128 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1132 /* Find Client entry */
1133 if (id_type == SILC_ID_CLIENT) {
1134 /* Find Client entry */
1136 client_entry2 = silc_client_get_client_by_id(client, conn,
1138 if (!client_entry) {
1139 silc_client_notify_by_server_resolve(client, conn, packet,
1140 SILC_ID_CLIENT, client_id);
1143 } else if (id_type == SILC_ID_SERVER) {
1144 /* Find Server entry */
1146 server = silc_client_get_server_by_id(client, conn, server_id);
1148 silc_client_notify_by_server_resolve(client, conn, packet,
1149 SILC_ID_SERVER, server_id);
1150 server = silc_client_add_server(client, conn, NULL, NULL,
1155 server->resolve_cmd_ident = conn->cmd_ident;
1160 if (server->resolve_cmd_ident) {
1161 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1162 res->packet = silc_packet_context_dup(packet);
1163 res->context = client;
1164 res->sock = silc_socket_dup(conn->sock);
1165 silc_client_command_pending(conn, SILC_COMMAND_NONE,
1166 server->resolve_cmd_ident,
1167 silc_client_notify_by_server_pending,
1172 /* Save the pointer to the client_entry pointer */
1173 client_entry2 = (SilcClientEntry)server;
1175 /* Find Channel entry */
1177 channel = silc_client_get_channel_by_id(client, conn, channel_id);
1179 silc_client_notify_by_server_resolve(client, conn, packet,
1180 SILC_ID_CHANNEL, channel_id);
1184 /* Save the pointer to the client_entry pointer */
1185 client_entry2 = (SilcClientEntry)channel;
1186 silc_free(channel_id);
1191 /* Notify application. */
1192 client->internal->ops->notify(client, conn, type, client_entry,
1193 comment, id_type, client_entry2);
1195 if (client_entry != conn->local_entry)
1196 /* Remove the client from all channels and free it */
1197 silc_client_del_client(client, conn, client_entry);
1201 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1204 * A server quit the SILC network and some clients must be removed
1205 * from channels as they quit as well.
1207 SilcClientEntry *clients = NULL;
1208 SilcUInt32 clients_count = 0;
1211 SILC_LOG_DEBUG(("Notify: SIGNOFF"));
1213 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1215 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1217 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1221 /* Get the client entry */
1222 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1224 clients = silc_realloc(clients, sizeof(*clients) *
1225 (clients_count + 1));
1226 clients[clients_count] = client_entry;
1229 silc_free(client_id);
1234 /* Notify application. We don't keep server entries so the server
1235 entry is returned as NULL. The client's are returned as array
1236 of SilcClientEntry pointers. */
1237 client->internal->ops->notify(client, conn, type, NULL,
1238 clients, clients_count);
1240 for (i = 0; i < clients_count; i++) {
1241 /* Remove client from all channels */
1242 client_entry = clients[i];
1243 if (client_entry == conn->local_entry)
1246 /* Remove the client from all channels and free it */
1247 silc_client_del_client(client, conn, client_entry);
1254 case SILC_NOTIFY_TYPE_ERROR:
1257 * Some has occurred and server is notifying us about it.
1261 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1262 if (!tmp && tmp_len != 1)
1264 error = (SilcStatus)tmp[0];
1266 SILC_LOG_DEBUG(("Notify: ERROR (%d)", error));
1268 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
1269 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1271 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1274 client_entry = silc_client_get_client_by_id(client, conn,
1277 silc_client_del_client(client, conn, client_entry);
1281 /* Notify application. */
1282 client->internal->ops->notify(client, conn, type, error);
1286 case SILC_NOTIFY_TYPE_WATCH:
1289 * Received notify about some client we are watching
1291 SilcNotifyType notify = 0;
1292 bool del_client = FALSE;
1294 SILC_LOG_DEBUG(("Notify: WATCH"));
1296 /* Get sender Client ID */
1297 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1300 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1304 /* Find Client entry and if not found query it */
1305 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1306 if (!client_entry) {
1307 silc_client_notify_by_server_resolve(client, conn, packet,
1308 SILC_ID_CLIENT, client_id);
1313 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1314 if (!tmp || tmp_len != 4)
1316 SILC_GET32_MSB(mode, tmp);
1318 /* Get notify type */
1319 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1320 if (tmp && tmp_len != 2)
1323 SILC_GET16_MSB(notify, tmp);
1326 tmp = silc_argument_get_arg_type(args, 2, NULL);
1328 char *tmp_nick = NULL;
1330 if (client->internal->params->nickname_parse)
1331 client->internal->params->nickname_parse(client_entry->nickname,
1334 tmp_nick = strdup(tmp);
1336 /* If same nick, the client was new to us and has become "present"
1337 to network. Send NULL as nick to application. */
1338 if (tmp_nick && !strcmp(tmp, tmp_nick))
1341 silc_free(tmp_nick);
1344 /* Notify application. */
1345 client->internal->ops->notify(client, conn, type, client_entry,
1348 client_entry->mode = mode;
1350 /* If nickname was changed, remove the client entry unless the
1351 client is on some channel */
1352 if (tmp && notify == SILC_NOTIFY_TYPE_NICK_CHANGE &&
1353 !silc_hash_table_count(client_entry->channels))
1355 else if (notify == SILC_NOTIFY_TYPE_SIGNOFF ||
1356 notify == SILC_NOTIFY_TYPE_SERVER_SIGNOFF ||
1357 notify == SILC_NOTIFY_TYPE_KILLED)
1361 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1362 res->context = client;
1363 res->sock = silc_socket_dup(conn->sock);
1364 res->packet = client_id;
1366 silc_schedule_task_add(client->schedule, conn->sock->sock,
1367 silc_client_notify_del_client_cb, res,
1368 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1378 silc_notify_payload_free(payload);
1379 silc_free(client_id);
1380 silc_free(channel_id);
1381 silc_free(server_id);