5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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, 2, &tmp_len);
287 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
291 /* Get channel entry */
292 channel = silc_client_get_channel_by_id(client, conn, channel_id);
297 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
301 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
305 /* Find Client entry and if not found query it */
306 client_entry = silc_client_get_client_by_id(client, conn, client_id);
308 silc_client_channel_set_wait(client, conn, channel,
309 conn->cmd_ident + 1);
310 silc_client_notify_by_server_resolve(client, conn, packet,
311 SILC_ID_CLIENT, client_id);
315 /* If nickname or username hasn't been resolved, do so */
316 if (!client_entry->nickname || !client_entry->username) {
317 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
318 /* Attach to existing resolving */
319 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
320 res->packet = silc_packet_context_dup(packet);
321 res->context = client;
322 res->sock = silc_socket_dup(conn->sock);
323 silc_client_command_pending(conn, SILC_COMMAND_NONE,
324 client_entry->resolve_cmd_ident,
325 silc_client_notify_by_server_pending,
330 /* Do new resolving */
331 silc_client_channel_set_wait(client, conn, channel,
332 conn->cmd_ident + 1);
333 silc_client_notify_by_server_resolve(client, conn, packet,
334 SILC_ID_CLIENT, client_id);
335 client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
336 client_entry->resolve_cmd_ident = conn->cmd_ident;
339 if (client_entry != conn->local_entry)
340 silc_client_nickname_format(client, conn, client_entry);
343 /* If information is being resolved for this channel, wait for it */
344 if (channel->resolve_cmd_ident) {
345 silc_client_channel_wait(client, conn, channel, packet);
349 /* Join the client to channel */
350 if (!silc_client_on_channel(channel, client_entry)) {
351 chu = silc_calloc(1, sizeof(*chu));
352 chu->client = client_entry;
353 chu->channel = channel;
354 silc_hash_table_add(channel->user_list, client_entry, chu);
355 silc_hash_table_add(client_entry->channels, channel, chu);
358 /* Notify application. The channel entry is sent last as this notify
359 is for channel but application don't know it from the arguments
361 client->internal->ops->notify(client, conn, type, client_entry, channel);
364 case SILC_NOTIFY_TYPE_LEAVE:
366 * Someone has left a channel. We will remove it from the channel but
367 * we'll keep it in the cache in case we'll need it later.
370 SILC_LOG_DEBUG(("Notify: LEAVE"));
373 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
377 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
381 /* Find Client entry */
383 silc_client_get_client_by_id(client, conn, client_id);
387 /* Get channel entry */
388 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
392 channel = silc_client_get_channel_by_id(client, conn, channel_id);
396 /* Remove client from channel */
397 chu = silc_client_on_channel(channel, client_entry);
399 silc_hash_table_del(client_entry->channels, channel);
400 silc_hash_table_del(channel->user_list, client_entry);
404 /* Some client implementations actually quit network by first doing
405 LEAVE and then immediately SIGNOFF. We'll check for this by doing
406 check for the client after 5 - 34 seconds. If it is not valid after
407 that we'll remove the client from cache. */
408 if (!silc_hash_table_count(client_entry->channels)) {
409 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
410 res->context = client;
411 res->sock = silc_socket_dup(conn->sock);
412 res->packet = silc_id_dup(client_id, SILC_ID_CLIENT);
413 silc_schedule_task_add(client->schedule, conn->sock->sock,
414 silc_client_notify_check_client, res,
415 (5 + (silc_rng_get_rn16(client->rng) % 29)),
416 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
419 /* Notify application. The channel entry is sent last as this notify
420 is for channel but application don't know it from the arguments
422 client->internal->ops->notify(client, conn, type, client_entry, channel);
425 case SILC_NOTIFY_TYPE_SIGNOFF:
427 * Someone left SILC. We'll remove it from all channels and from cache.
430 SILC_LOG_DEBUG(("Notify: SIGNOFF"));
433 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
437 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
441 /* Find Client entry */
443 silc_client_get_client_by_id(client, conn, client_id);
447 /* Get signoff message */
448 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
452 /* Notify application */
453 client->internal->ops->notify(client, conn, type, client_entry, tmp);
455 /* Remove from all channels */
456 silc_client_remove_from_channels(client, conn, client_entry);
458 /* Remove from cache */
459 silc_idcache_del_by_context(conn->internal->client_cache, client_entry);
462 silc_client_del_client_entry(client, conn, client_entry);
465 case SILC_NOTIFY_TYPE_TOPIC_SET:
467 * Someone set the topic on a channel.
470 SILC_LOG_DEBUG(("Notify: TOPIC_SET"));
472 /* Get channel entry */
473 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
477 channel = silc_client_get_channel_by_id(client, conn, channel_id);
482 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
485 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
489 /* Find Client entry */
490 if (id_type == SILC_ID_CLIENT) {
491 /* Find Client entry */
493 client_entry = silc_client_get_client_by_id(client, conn, client_id);
495 silc_client_channel_set_wait(client, conn, channel,
496 conn->cmd_ident + 1);
497 silc_client_notify_by_server_resolve(client, conn, packet,
498 SILC_ID_CLIENT, client_id);
501 } else if (id_type == SILC_ID_SERVER) {
502 /* Find Server entry */
504 server = silc_client_get_server_by_id(client, conn, server_id);
506 silc_client_channel_set_wait(client, conn, channel,
507 conn->cmd_ident + 1);
508 silc_client_notify_by_server_resolve(client, conn, packet,
509 SILC_ID_SERVER, server_id);
510 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
514 server->resolve_cmd_ident = conn->cmd_ident;
519 /* If entry being resoled, wait for it before processing this notify */
520 if (server->resolve_cmd_ident) {
521 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
522 res->packet = silc_packet_context_dup(packet);
523 res->context = client;
524 res->sock = silc_socket_dup(conn->sock);
525 silc_client_command_pending(conn, SILC_COMMAND_NONE,
526 server->resolve_cmd_ident,
527 silc_client_notify_by_server_pending, res);
531 /* Save the pointer to the client_entry pointer */
532 client_entry = (SilcClientEntry)server;
534 /* Find Channel entry */
535 silc_free(channel_id);
537 client_entry = (SilcClientEntry)
538 silc_client_get_channel_by_id(client, conn, channel_id);
540 silc_client_channel_set_wait(client, conn, channel,
541 conn->cmd_ident + 1);
542 silc_client_notify_by_server_resolve(client, conn, packet,
543 SILC_ID_CHANNEL, channel_id);
549 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
553 /* If information is being resolved for this channel, wait for it */
554 if (channel->resolve_cmd_ident) {
555 silc_client_channel_wait(client, conn, channel, packet);
560 silc_free(channel->topic);
561 channel->topic = silc_memdup(tmp, strlen(tmp));
564 /* Notify application. The channel entry is sent last as this notify
565 is for channel but application don't know it from the arguments
567 client->internal->ops->notify(client, conn, type, id_type,
568 client_entry, tmp, channel);
572 case SILC_NOTIFY_TYPE_NICK_CHANGE:
574 * Someone changed their nickname. If we don't have entry for the new
575 * ID we will query it and return here after it's done. After we've
576 * returned we fetch the old entry and free it and notify the
580 SILC_LOG_DEBUG(("Notify: NICK_CHANGE"));
582 /* Get old Client ID */
583 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
587 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
592 if (conn->local_id && SILC_ID_CLIENT_COMPARE(client_id, conn->local_id))
595 /* Find old Client entry */
596 client_entry = silc_client_get_client_by_id(client, conn, client_id);
599 silc_free(client_id);
602 /* Wait for resolving if necessary */
603 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
604 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
605 res->packet = silc_packet_context_dup(packet);
606 res->context = client;
607 res->sock = silc_socket_dup(conn->sock);
608 silc_client_command_pending(conn, SILC_COMMAND_NONE,
609 client_entry->resolve_cmd_ident,
610 silc_client_notify_by_server_pending, res);
614 client_entry->valid = FALSE;
616 /* Get new Client ID */
617 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
621 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
625 /* Take the nickname */
626 tmp = silc_argument_get_arg_type(args, 3, NULL);
630 /* Check whether nickname changed at all. It is possible that nick
631 change notify is received but nickname didn't change, only the
632 ID changes. Check whether the hashes in the Client ID match, if
633 they do nickname didn't change. */
634 if (SILC_ID_COMPARE_HASH(client_entry->id, client_id) &&
635 silc_utf8_strcasecmp(tmp, client_entry->nickname)) {
636 /* Nickname didn't change. Update only Client ID. */
637 silc_idcache_del_by_context(conn->internal->client_cache,
639 silc_free(client_entry->id);
640 client_entry->id = silc_id_dup(client_id, SILC_ID_CLIENT);
641 silc_idcache_add(conn->internal->client_cache, strdup(tmp),
642 client_entry->id, client_entry, 0, NULL);
644 /* Notify application */
645 client->internal->ops->notify(client, conn, type,
646 client_entry, client_entry);
650 /* Create new client entry, and save all old information with the
651 new nickname and client ID */
652 client_entry2 = silc_client_add_client(client, conn, NULL, NULL,
653 client_entry->realname,
654 silc_id_dup(client_id,
659 if (client_entry->server)
660 client_entry2->server = strdup(client_entry->server);
661 if (client_entry->username)
662 client_entry2->username = strdup(client_entry->username);
663 if (client_entry->hostname)
664 client_entry2->hostname = strdup(client_entry->hostname);
665 client_entry2->fingerprint = client_entry->fingerprint;
666 client_entry2->fingerprint_len = client_entry->fingerprint_len;
667 client_entry->fingerprint = NULL;
668 client_entry->fingerprint_len = 0;
669 silc_client_update_client(client, conn, client_entry2, tmp, NULL, NULL,
672 /* Remove the old from cache */
673 silc_idcache_del_by_context(conn->internal->client_cache, client_entry);
675 /* Replace old ID entry with new one on all channels. */
676 silc_client_replace_from_channels(client, conn, client_entry,
679 /* Notify application */
680 client->internal->ops->notify(client, conn, type,
681 client_entry, client_entry2);
683 /* Free old client entry */
684 silc_client_del_client_entry(client, conn, client_entry);
688 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
691 * Someone changed a channel mode
693 char *passphrase, *cipher, *hmac;
694 SilcPublicKey founder_key = NULL;
695 SilcBufferStruct chpks;
697 SILC_LOG_DEBUG(("Notify: CMODE_CHANGE"));
699 /* Get channel entry */
700 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
704 channel = silc_client_get_channel_by_id(client, conn, channel_id);
709 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
712 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
716 /* Find Client entry */
717 if (id_type == SILC_ID_CLIENT) {
718 /* Find Client entry */
720 client_entry = silc_client_get_client_by_id(client, conn, client_id);
722 silc_client_channel_set_wait(client, conn, channel,
723 conn->cmd_ident + 1);
724 silc_client_notify_by_server_resolve(client, conn, packet,
725 SILC_ID_CLIENT, client_id);
729 if (!client_entry->nickname) {
730 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
731 /* Attach to existing resolving */
732 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
733 res->packet = silc_packet_context_dup(packet);
734 res->context = client;
735 res->sock = silc_socket_dup(conn->sock);
736 silc_client_command_pending(conn, SILC_COMMAND_NONE,
737 client_entry->resolve_cmd_ident,
738 silc_client_notify_by_server_pending,
743 /* Do new resolving */
744 silc_client_channel_set_wait(client, conn, channel,
745 conn->cmd_ident + 1);
746 silc_client_notify_by_server_resolve(client, conn, packet,
747 SILC_ID_CLIENT, client_id);
748 client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
749 client_entry->resolve_cmd_ident = conn->cmd_ident;
752 } else if (id_type == SILC_ID_SERVER) {
753 /* Find Server entry */
755 server = silc_client_get_server_by_id(client, conn, server_id);
757 silc_client_channel_set_wait(client, conn, channel,
758 conn->cmd_ident + 1);
759 silc_client_notify_by_server_resolve(client, conn, packet,
760 SILC_ID_SERVER, server_id);
761 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
765 server->resolve_cmd_ident = conn->cmd_ident;
770 /* If entry being resoled, wait for it before processing this notify */
771 if (server->resolve_cmd_ident) {
772 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
773 res->packet = silc_packet_context_dup(packet);
774 res->context = client;
775 res->sock = silc_socket_dup(conn->sock);
776 silc_client_command_pending(conn, SILC_COMMAND_NONE,
777 server->resolve_cmd_ident,
778 silc_client_notify_by_server_pending,
783 /* Save the pointer to the client_entry pointer */
784 client_entry = (SilcClientEntry)server;
786 /* Find Channel entry */
787 silc_free(channel_id);
789 client_entry = (SilcClientEntry)
790 silc_client_get_channel_by_id(client, conn, channel_id);
792 silc_client_channel_set_wait(client, conn, channel,
793 conn->cmd_ident + 1);
794 silc_client_notify_by_server_resolve(client, conn, packet,
795 SILC_ID_CHANNEL, channel_id);
801 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
805 SILC_GET32_MSB(mode, tmp);
807 /* If information is being resolved for this channel, wait for it */
808 if (channel->resolve_cmd_ident) {
809 silc_client_channel_wait(client, conn, channel, packet);
813 /* Save the new mode */
814 channel->mode = mode;
817 cipher = silc_argument_get_arg_type(args, 3, &tmp_len);
820 hmac = silc_argument_get_arg_type(args, 4, &tmp_len);
822 unsigned char hash[32];
825 silc_hmac_free(channel->hmac);
826 if (!silc_hmac_alloc(hmac, NULL, &channel->hmac))
829 silc_hash_make(silc_hmac_get_hash(channel->hmac),
830 channel->key, channel->key_len / 8,
832 silc_hmac_set_key(channel->hmac, hash,
833 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
834 memset(hash, 0, sizeof(hash));
837 /* Get the passphrase if it was set */
838 passphrase = silc_argument_get_arg_type(args, 5, &tmp_len);
840 /* Get the channel founder key if it was set */
841 tmp = silc_argument_get_arg_type(args, 6, &tmp_len);
843 if (!silc_pkcs_public_key_payload_decode(tmp, tmp_len, &founder_key))
848 tmp = silc_argument_get_arg_type(args, 8, &tmp_len);
849 if (tmp && tmp_len == 4)
850 SILC_GET32_MSB(channel->user_limit, tmp);
851 if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
852 channel->user_limit = 0;
854 /* Get the channel public key that was added or removed */
855 tmp = silc_argument_get_arg_type(args, 7, &tmp_len);
857 silc_buffer_set(&chpks, tmp, tmp_len);
859 /* Notify application. The channel entry is sent last as this notify
860 is for channel but application don't know it from the arguments
862 client->internal->ops->notify(client, conn, type, id_type,
863 client_entry, mode, cipher, hmac,
864 passphrase, founder_key,
865 tmp ? &chpks : NULL, channel);
868 silc_pkcs_public_key_free(founder_key);
872 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
874 * Someone changed user's mode on a channel
877 SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE"));
879 /* Get channel entry */
880 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
884 channel = silc_client_get_channel_by_id(client, conn, channel_id);
889 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
892 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
896 /* Find Client entry */
897 if (id_type == SILC_ID_CLIENT) {
898 /* Find Client entry */
900 client_entry = silc_client_get_client_by_id(client, conn, client_id);
902 silc_client_channel_set_wait(client, conn, channel,
903 conn->cmd_ident + 1);
904 silc_client_notify_by_server_resolve(client, conn, packet,
905 SILC_ID_CLIENT, client_id);
909 if (!client_entry->nickname) {
910 if (client_entry->status & SILC_CLIENT_STATUS_RESOLVING) {
911 /* Attach to existing resolving */
912 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
913 res->packet = silc_packet_context_dup(packet);
914 res->context = client;
915 res->sock = silc_socket_dup(conn->sock);
916 silc_client_command_pending(conn, SILC_COMMAND_NONE,
917 client_entry->resolve_cmd_ident,
918 silc_client_notify_by_server_pending,
923 /* Do new resolving */
924 silc_client_channel_set_wait(client, conn, channel,
925 conn->cmd_ident + 1);
926 silc_client_notify_by_server_resolve(client, conn, packet,
927 SILC_ID_CLIENT, client_id);
928 client_entry->status |= SILC_CLIENT_STATUS_RESOLVING;
929 client_entry->resolve_cmd_ident = conn->cmd_ident;
932 } else if (id_type == SILC_ID_SERVER) {
933 /* Find Server entry */
935 server = silc_client_get_server_by_id(client, conn, server_id);
937 silc_client_channel_set_wait(client, conn, channel,
938 conn->cmd_ident + 1);
939 silc_client_notify_by_server_resolve(client, conn, packet,
940 SILC_ID_SERVER, server_id);
941 server = silc_client_add_server(client, conn, NULL, NULL, server_id);
945 server->resolve_cmd_ident = conn->cmd_ident;
950 /* If entry being resoled, wait for it before processing this notify */
951 if (server->resolve_cmd_ident) {
952 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
953 res->packet = silc_packet_context_dup(packet);
954 res->context = client;
955 res->sock = silc_socket_dup(conn->sock);
956 silc_client_command_pending(conn, SILC_COMMAND_NONE,
957 server->resolve_cmd_ident,
958 silc_client_notify_by_server_pending, res);
962 /* Save the pointer to the client_entry pointer */
963 client_entry = (SilcClientEntry)server;
965 /* Find Channel entry */
966 silc_free(channel_id);
968 client_entry = (SilcClientEntry)
969 silc_client_get_channel_by_id(client, conn, channel_id);
971 silc_client_channel_set_wait(client, conn, channel,
972 conn->cmd_ident + 1);
973 silc_client_notify_by_server_resolve(client, conn, packet,
974 SILC_ID_CHANNEL, channel_id);
980 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
984 SILC_GET32_MSB(mode, tmp);
986 /* If information is being resolved for this channel, wait for it */
987 if (channel->resolve_cmd_ident) {
988 silc_client_channel_wait(client, conn, channel, packet);
992 /* Get target Client ID */
993 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
997 silc_free(client_id);
998 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1002 /* Find target Client entry */
1004 silc_client_get_client_by_id(client, conn, client_id);
1005 if (!client_entry2) {
1006 silc_client_notify_by_server_resolve(client, conn, packet,
1007 SILC_ID_CLIENT, client_id);
1012 chu = silc_client_on_channel(channel, client_entry2);
1016 /* Notify application. The channel entry is sent last as this notify
1017 is for channel but application don't know it from the arguments
1019 client->internal->ops->notify(client, conn, type,
1020 id_type, client_entry, mode,
1021 client_entry2, channel);
1024 case SILC_NOTIFY_TYPE_MOTD:
1026 * Received Message of the day
1029 SILC_LOG_DEBUG(("Notify: MOTD"));
1032 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1036 /* Notify application */
1037 client->internal->ops->notify(client, conn, type, tmp);
1040 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
1042 * Router has enforced a new ID to a channel. Let's change the old
1043 * ID to the one provided here.
1046 SILC_LOG_DEBUG(("Notify: CHANNEL_CHANGE"));
1048 /* Get the old ID */
1049 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1052 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1056 /* Get the channel entry */
1057 channel = silc_client_get_channel_by_id(client, conn, channel_id);
1061 silc_free(channel_id);
1064 /* Get the new ID */
1065 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1068 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1072 /* Replace the Channel ID */
1073 if (silc_client_replace_channel_id(client, conn, channel, channel_id))
1076 /* Notify application */
1077 client->internal->ops->notify(client, conn, type, channel, channel);
1080 case SILC_NOTIFY_TYPE_KICKED:
1082 * A client (maybe me) was kicked from a channel
1085 SILC_LOG_DEBUG(("Notify: KICKED"));
1088 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1092 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1096 /* Find Client entry */
1097 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1101 /* Get channel entry */
1102 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1106 channel = silc_client_get_channel_by_id(client, conn, channel_id);
1110 /* From protocol version 1.1 we get the kicker's client ID as well */
1111 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1113 silc_free(client_id);
1114 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1118 /* Find kicker's client entry and if not found resolve it */
1119 client_entry2 = silc_client_get_client_by_id(client, conn, client_id);
1120 if (!client_entry2) {
1121 silc_client_notify_by_server_resolve(client, conn, packet,
1122 SILC_ID_CLIENT, client_id);
1125 if (client_entry2 != conn->local_entry)
1126 silc_client_nickname_format(client, conn, client_entry2);
1131 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1133 /* Remove kicked client from channel */
1134 if (client_entry != conn->local_entry) {
1135 chu = silc_client_on_channel(channel, client_entry);
1137 silc_hash_table_del(client_entry->channels, channel);
1138 silc_hash_table_del(channel->user_list, client_entry);
1143 /* Notify application. The channel entry is sent last as this notify
1144 is for channel but application don't know it from the arguments
1146 client->internal->ops->notify(client, conn, type, client_entry, tmp,
1147 client_entry2, channel);
1149 /* Remove kicked client (us) from channel */
1150 if (client_entry == conn->local_entry) {
1151 /* If I was kicked from channel, remove the channel */
1152 if (conn->current_channel == channel)
1153 conn->current_channel = NULL;
1154 silc_client_del_channel(client, conn, channel);
1156 if (!silc_hash_table_count(client_entry->channels)) {
1157 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1158 res->context = client;
1159 res->sock = silc_socket_dup(conn->sock);
1160 res->packet = silc_id_dup(client_entry->id, SILC_ID_CLIENT);
1161 silc_schedule_task_add(client->schedule, conn->sock->sock,
1162 silc_client_notify_check_client, res,
1163 (5 + (silc_rng_get_rn16(client->rng) % 529)),
1164 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1169 case SILC_NOTIFY_TYPE_KILLED:
1172 * A client (maybe me) was killed from the network.
1175 SilcUInt32 comment_len;
1177 SILC_LOG_DEBUG(("Notify: KILLED"));
1180 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1184 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1188 /* Find Client entry */
1189 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1194 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1196 /* From protocol version 1.1 we get killer's client ID as well */
1197 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1199 silc_free(client_id);
1201 id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
1205 /* Find Client entry */
1206 if (id_type == SILC_ID_CLIENT) {
1207 /* Find Client entry */
1209 client_entry2 = silc_client_get_client_by_id(client, conn,
1211 if (!client_entry) {
1212 silc_client_notify_by_server_resolve(client, conn, packet,
1213 SILC_ID_CLIENT, client_id);
1216 } else if (id_type == SILC_ID_SERVER) {
1217 /* Find Server entry */
1219 server = silc_client_get_server_by_id(client, conn, server_id);
1221 silc_client_notify_by_server_resolve(client, conn, packet,
1222 SILC_ID_SERVER, server_id);
1223 server = silc_client_add_server(client, conn, NULL, NULL,
1228 server->resolve_cmd_ident = conn->cmd_ident;
1233 if (server->resolve_cmd_ident) {
1234 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1235 res->packet = silc_packet_context_dup(packet);
1236 res->context = client;
1237 res->sock = silc_socket_dup(conn->sock);
1238 silc_client_command_pending(conn, SILC_COMMAND_NONE,
1239 server->resolve_cmd_ident,
1240 silc_client_notify_by_server_pending,
1245 /* Save the pointer to the client_entry pointer */
1246 client_entry2 = (SilcClientEntry)server;
1248 /* Find Channel entry */
1250 channel = silc_client_get_channel_by_id(client, conn, channel_id);
1252 silc_client_notify_by_server_resolve(client, conn, packet,
1253 SILC_ID_CHANNEL, channel_id);
1257 /* Save the pointer to the client_entry pointer */
1258 client_entry2 = (SilcClientEntry)channel;
1259 silc_free(channel_id);
1264 /* Notify application. */
1265 client->internal->ops->notify(client, conn, type, client_entry,
1266 comment, id_type, client_entry2);
1268 if (client_entry != conn->local_entry)
1269 /* Remove the client from all channels and free it */
1270 silc_client_del_client(client, conn, client_entry);
1274 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
1277 * A server quit the SILC network and some clients must be removed
1278 * from channels as they quit as well.
1280 SilcClientEntry *clients = NULL;
1281 SilcUInt32 clients_count = 0;
1284 SILC_LOG_DEBUG(("Notify: SIGNOFF"));
1286 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1288 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1290 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1294 /* Get the client entry */
1295 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1297 clients = silc_realloc(clients, sizeof(*clients) *
1298 (clients_count + 1));
1299 clients[clients_count] = client_entry;
1302 silc_free(client_id);
1307 /* Notify application. We don't keep server entries so the server
1308 entry is returned as NULL. The client's are returned as array
1309 of SilcClientEntry pointers. */
1310 client->internal->ops->notify(client, conn, type, NULL,
1311 clients, clients_count);
1313 for (i = 0; i < clients_count; i++) {
1314 /* Remove client from all channels */
1315 client_entry = clients[i];
1316 if (client_entry == conn->local_entry)
1319 /* Remove the client from all channels and free it */
1320 silc_client_del_client(client, conn, client_entry);
1327 case SILC_NOTIFY_TYPE_ERROR:
1330 * Some has occurred and server is notifying us about it.
1334 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1335 if (!tmp && tmp_len != 1)
1337 error = (SilcStatus)tmp[0];
1339 SILC_LOG_DEBUG(("Notify: ERROR (%d)", error));
1341 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
1342 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1344 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1347 client_entry = silc_client_get_client_by_id(client, conn,
1350 silc_client_del_client(client, conn, client_entry);
1354 /* Notify application. */
1355 client->internal->ops->notify(client, conn, type, error);
1359 case SILC_NOTIFY_TYPE_WATCH:
1362 * Received notify about some client we are watching
1364 SilcNotifyType notify = 0;
1365 bool del_client = FALSE;
1368 SilcPublicKey public_key = NULL;
1370 SILC_LOG_DEBUG(("Notify: WATCH"));
1372 /* Get sender Client ID */
1373 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1376 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1380 /* Find Client entry and if not found query it */
1381 client_entry = silc_client_get_client_by_id(client, conn, client_id);
1382 if (!client_entry) {
1383 silc_client_notify_by_server_resolve(client, conn, packet,
1384 SILC_ID_CLIENT, client_id);
1389 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1390 if (!tmp || tmp_len != 4)
1392 SILC_GET32_MSB(mode, tmp);
1394 /* Get notify type */
1395 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
1396 if (tmp && tmp_len != 2)
1399 SILC_GET16_MSB(notify, tmp);
1402 tmp = silc_argument_get_arg_type(args, 2, NULL);
1404 char *tmp_nick = NULL;
1406 if (client->internal->params->nickname_parse)
1407 client->internal->params->nickname_parse(client_entry->nickname,
1410 tmp_nick = strdup(tmp);
1412 /* If same nick, the client was new to us and has become "present"
1413 to network. Send NULL as nick to application. */
1414 if (tmp_nick && silc_utf8_strcasecmp(tmp, tmp_nick))
1417 silc_free(tmp_nick);
1420 /* Get public key, if present */
1421 pk = silc_argument_get_arg_type(args, 5, &pk_len);
1422 if (pk && !client_entry->public_key) {
1423 if (silc_pkcs_public_key_payload_decode(pk, pk_len, &public_key)) {
1424 client_entry->public_key = public_key;
1429 /* Notify application. */
1430 client->internal->ops->notify(client, conn, type, client_entry,
1432 client_entry->public_key);
1434 client_entry->mode = mode;
1436 /* If nickname was changed, remove the client entry unless the
1437 client is on some channel */
1438 if (tmp && notify == SILC_NOTIFY_TYPE_NICK_CHANGE &&
1439 !silc_hash_table_count(client_entry->channels))
1441 else if (notify == SILC_NOTIFY_TYPE_SIGNOFF ||
1442 notify == SILC_NOTIFY_TYPE_SERVER_SIGNOFF ||
1443 notify == SILC_NOTIFY_TYPE_KILLED)
1447 SilcClientNotifyResolve res = silc_calloc(1, sizeof(*res));
1448 res->context = client;
1449 res->sock = silc_socket_dup(conn->sock);
1450 res->packet = client_id;
1452 silc_schedule_task_add(client->schedule, conn->sock->sock,
1453 silc_client_notify_del_client_cb, res,
1454 1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1457 silc_pkcs_public_key_free(public_key);
1466 silc_notify_payload_free(payload);
1467 silc_free(client_id);
1468 silc_free(channel_id);
1469 silc_free(server_id);