+ silc_client_del_channel(client, conn, channel);
+ } else {
+ 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;
+
+ case SILC_NOTIFY_TYPE_KILLED:
+ {
+ /*
+ * A client (maybe me) was killed from the network.
+ */
+ char *comment;
+ SilcUInt32 comment_len;
+
+ SILC_LOG_DEBUG(("Notify: KILLED"));
+
+ /* Get 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 */
+ client_entry = silc_client_get_client_by_id(client, conn, client_id);
+ if (!client_entry)
+ goto out;
+
+ /* Get comment */
+ comment = silc_argument_get_arg_type(args, 2, &comment_len);
+
+ /* From protocol version 1.1 we get killer's client ID as well */
+ tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
+ if (tmp) {
+ silc_free(client_id);
+ client_id = NULL;
+ id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
+ if (!id)
+ goto out;
+
+ /* Find Client entry */
+ if (id_type == SILC_ID_CLIENT) {
+ /* Find Client entry */
+ client_id = id;
+ client_entry2 = silc_client_get_client_by_id(client, conn,
+ client_id);
+ if (!client_entry) {
+ silc_client_notify_by_server_resolve(client, conn, packet,
+ SILC_ID_CLIENT, client_id);
+ goto out;
+ }
+ } else if (id_type == SILC_ID_SERVER) {
+ /* Find Server entry */
+ server_id = id;
+ server = silc_client_get_server_by_id(client, conn, server_id);
+ if (!server) {
+ silc_client_notify_by_server_resolve(client, conn, packet,
+ SILC_ID_SERVER, server_id);
+ 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;
+ }
+
+ /* Save the pointer to the client_entry pointer */
+ client_entry2 = (SilcClientEntry)server;
+ } else {
+ /* Find Channel entry */
+ channel_id = id;
+ channel = silc_client_get_channel_by_id(client, conn, channel_id);
+ if (!channel) {
+ silc_client_notify_by_server_resolve(client, conn, packet,
+ SILC_ID_CHANNEL, channel_id);
+ goto out;
+ }
+
+ /* Save the pointer to the client_entry pointer */
+ client_entry2 = (SilcClientEntry)channel;
+ silc_free(channel_id);
+ channel_id = NULL;
+ }
+ }
+
+ /* Notify application. */
+ client->internal->ops->notify(client, conn, type, client_entry,
+ comment, id_type, client_entry2);
+
+ if (client_entry != conn->local_entry)
+ /* Remove the client from all channels and free it */
+ silc_client_del_client(client, conn, client_entry);
+ }
+ break;
+
+ case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
+ {
+ /*
+ * A server quit the SILC network and some clients must be removed
+ * from channels as they quit as well.
+ */
+ SilcClientEntry *clients = NULL;
+ SilcUInt32 clients_count = 0;
+ int i;
+
+ SILC_LOG_DEBUG(("Notify: SIGNOFF"));
+
+ for (i = 1; i < silc_argument_get_arg_num(args); i++) {
+ /* Get Client ID */
+ tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
+ if (tmp) {
+ client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+ if (!client_id)
+ goto out;
+
+ /* Get the client entry */
+ client_entry = silc_client_get_client_by_id(client, conn, client_id);
+ if (client_entry) {
+ clients = silc_realloc(clients, sizeof(*clients) *
+ (clients_count + 1));
+ clients[clients_count] = client_entry;
+ clients_count++;
+ }
+ silc_free(client_id);
+ }
+ }
+ client_id = NULL;
+
+ /* Notify application. We don't keep server entries so the server
+ entry is returned as NULL. The client's are returned as array
+ of SilcClientEntry pointers. */
+ client->internal->ops->notify(client, conn, type, NULL,
+ clients, clients_count);
+
+ for (i = 0; i < clients_count; i++) {
+ /* Remove client from all channels */
+ client_entry = clients[i];
+ if (client_entry == conn->local_entry)
+ continue;
+
+ /* Remove the client from all channels and free it */
+ silc_client_del_client(client, conn, client_entry);
+ }
+ silc_free(clients);
+
+ }
+ break;
+
+ case SILC_NOTIFY_TYPE_ERROR:
+ {
+ /*
+ * Some has occurred and server is notifying us about it.
+ */
+ SilcStatus error;
+
+ tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
+ if (!tmp && tmp_len != 1)
+ goto out;
+ error = (SilcStatus)tmp[0];
+
+ SILC_LOG_DEBUG(("Notify: ERROR (%d)", error));
+
+ if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
+ tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
+ if (tmp) {
+ client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+ if (!client_id)
+ goto out;
+ client_entry = silc_client_get_client_by_id(client, conn,
+ client_id);
+ if (client_entry)
+ silc_client_del_client(client, conn, client_entry);
+ }
+ }
+
+ /* Notify application. */
+ client->internal->ops->notify(client, conn, type, error);
+ }
+ break;
+
+ case SILC_NOTIFY_TYPE_WATCH:
+ {
+ /*
+ * Received notify about some client we are watching
+ */
+ SilcNotifyType notify = 0;
+ bool del_client = FALSE;
+ unsigned char *pk;
+ SilcUInt32 pk_len;
+ SilcPublicKey public_key = NULL;
+
+ 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 && silc_utf8_strcasecmp(tmp, tmp_nick))
+ tmp = NULL;
+
+ silc_free(tmp_nick);
+ }
+
+ /* Get public key, if present */
+ pk = silc_argument_get_arg_type(args, 5, &pk_len);
+ if (pk && !client_entry->public_key) {
+ if (silc_pkcs_public_key_payload_decode(pk, pk_len, &public_key)) {
+ client_entry->public_key = public_key;
+ public_key = NULL;
+ }
+ }
+
+ /* Notify application. */
+ client->internal->ops->notify(client, conn, type, client_entry,
+ tmp, mode, notify,
+ client_entry->public_key);
+
+ 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);
+ }
+
+ silc_pkcs_public_key_free(public_key);