X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcclient%2Fclient_notify.c;h=862d957a3631430ff3be933a4588088bfceb0401;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=5b508e5c3e6938645aefbd020e5f520b9d9879a4;hpb=2dc218143c7859f7529396dc121ae08e2fd78da0;p=silc.git diff --git a/lib/silcclient/client_notify.c b/lib/silcclient/client_notify.c index 5b508e5c..862d957a 100644 --- a/lib/silcclient/client_notify.c +++ b/lib/silcclient/client_notify.c @@ -1,16 +1,15 @@ /* - client_notify.c + client_notify.c Author: Pekka Riikonen - Copyright (C) 1997 - 2001 Pekka Riikonen + Copyright (C) 1997 - 2002 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - + the Free Software Foundation; version 2 of the License. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -22,7 +21,8 @@ important packets sent by the server. They tell different things to the client such as nick changes, mode changes etc. */ -#include "clientlibincludes.h" +#include "silcincludes.h" +#include "silcclient.h" #include "client_internal.h" typedef struct { @@ -41,25 +41,20 @@ static void silc_client_notify_by_server_pending(void *context, void *context2) SilcClientCommandReplyContext reply = (SilcClientCommandReplyContext)context2; + SILC_LOG_DEBUG(("Start")); + if (reply) { SilcCommandStatus status; unsigned char *tmp = silc_argument_get_arg_type(reply->args, 1, NULL); SILC_GET16_MSB(status, tmp); - if (status != SILC_STATUS_OK) { - silc_socket_free(res->sock); - return; - } + if (status != SILC_STATUS_OK) + goto out; } silc_client_notify_by_server(res->context, res->sock, res->packet); - silc_socket_free(res->sock); -} - -/* Destructor for the pending command callback */ -static void silc_client_notify_by_server_destructor(void *context) -{ - SilcClientNotifyResolve res = (SilcClientNotifyResolve)context; + out: + silc_socket_free(res->sock); silc_packet_context_free(res->packet); silc_free(res); } @@ -78,10 +73,12 @@ static void silc_client_notify_by_server_resolve(SilcClient client, res->context = client; res->sock = silc_socket_dup(conn->sock); - silc_client_send_command(client, conn, SILC_COMMAND_WHOIS, ++conn->cmd_ident, + silc_client_command_register(client, SILC_COMMAND_WHOIS, NULL, NULL, + 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); silc_client_command_pending(conn, SILC_COMMAND_WHOIS, conn->cmd_ident, - silc_client_notify_by_server_destructor, silc_client_notify_by_server_pending, res); silc_buffer_free(idp); } @@ -107,11 +104,12 @@ void silc_client_notify_by_server(SilcClient client, SilcChannelEntry channel; SilcChannelUser chu; SilcServerEntry server; - SilcIDCacheEntry id_cache = NULL; unsigned char *tmp; - uint32 tmp_len, mode; + SilcUInt32 tmp_len, mode; - payload = silc_notify_payload_parse(buffer); + SILC_LOG_DEBUG(("Start")); + + payload = silc_notify_payload_parse(buffer->data, buffer->len); if (!payload) goto out; @@ -123,8 +121,8 @@ void silc_client_notify_by_server(SilcClient client, switch(type) { case SILC_NOTIFY_TYPE_NONE: /* Notify application */ - client->ops->notify(client, conn, type, - silc_argument_get_arg_type(args, 1, NULL)); + client->internal->ops->notify(client, conn, type, + silc_argument_get_arg_type(args, 1, NULL)); break; case SILC_NOTIFY_TYPE_INVITE: @@ -133,6 +131,8 @@ void silc_client_notify_by_server(SilcClient client, * for the application. */ + SILC_LOG_DEBUG(("Notify: INVITE")); + /* Get Channel ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -143,10 +143,7 @@ void silc_client_notify_by_server(SilcClient client, goto out; /* Get the channel entry */ - channel = NULL; - if (silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) - channel = (SilcChannelEntry)id_cache->context; + channel = silc_client_get_channel_by_id(client, conn, channel_id); /* Get sender Client ID */ tmp = silc_argument_get_arg_type(args, 3, &tmp_len); @@ -170,7 +167,8 @@ void silc_client_notify_by_server(SilcClient client, goto out; /* Notify application */ - client->ops->notify(client, conn, type, channel, tmp, client_entry); + client->internal->ops->notify(client, conn, type, channel, tmp, + client_entry); break; case SILC_NOTIFY_TYPE_JOIN: @@ -179,6 +177,8 @@ void silc_client_notify_by_server(SilcClient client, * cache them for later use. */ + SILC_LOG_DEBUG(("Notify: JOIN")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -204,6 +204,9 @@ void silc_client_notify_by_server(SilcClient client, client_entry->status |= SILC_CLIENT_STATUS_RESOLVING; silc_client_notify_by_server_resolve(client, conn, packet, client_id); goto out; + } else { + if (client_entry != conn->local_entry) + silc_client_nickname_format(client, conn, client_entry); } /* Get Channel ID */ @@ -216,23 +219,23 @@ void silc_client_notify_by_server(SilcClient client, goto out; /* Get channel entry */ - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) break; - channel = (SilcChannelEntry)id_cache->context; - - /* Add client to channel */ - if (client_entry != conn->local_entry) { + /* Join the client to channel */ + if (!silc_client_on_channel(channel, client_entry)) { chu = silc_calloc(1, sizeof(*chu)); chu->client = client_entry; - silc_list_add(channel->clients, chu); + chu->channel = channel; + silc_hash_table_add(channel->user_list, client_entry, chu); + silc_hash_table_add(client_entry->channels, channel, chu); } /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, client_entry, channel); + client->internal->ops->notify(client, conn, type, client_entry, channel); break; case SILC_NOTIFY_TYPE_LEAVE: @@ -241,6 +244,8 @@ void silc_client_notify_by_server(SilcClient client, * we'll keep it in the cache in case we'll need it later. */ + SILC_LOG_DEBUG(("Notify: LEAVE")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -261,26 +266,22 @@ void silc_client_notify_by_server(SilcClient client, SILC_ID_CHANNEL); if (!channel_id) goto out; - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) break; - channel = (SilcChannelEntry)id_cache->context; - /* Remove client from channel */ - silc_list_start(channel->clients); - while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) { - if (chu->client == client_entry) { - silc_list_del(channel->clients, chu); - silc_free(chu); - break; - } + chu = silc_client_on_channel(channel, client_entry); + if (chu) { + silc_hash_table_del(client_entry->channels, channel); + silc_hash_table_del(channel->user_list, client_entry); + silc_free(chu); } /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, client_entry, channel); + client->internal->ops->notify(client, conn, type, client_entry, channel); break; case SILC_NOTIFY_TYPE_SIGNOFF: @@ -288,6 +289,8 @@ void silc_client_notify_by_server(SilcClient client, * Someone left SILC. We'll remove it from all channels and from cache. */ + SILC_LOG_DEBUG(("Notify: SIGNOFF")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -315,7 +318,7 @@ void silc_client_notify_by_server(SilcClient client, tmp = NULL; /* Notify application */ - client->ops->notify(client, conn, type, client_entry, tmp); + client->internal->ops->notify(client, conn, type, client_entry, tmp); /* Free data */ silc_client_del_client_entry(client, conn, client_entry); @@ -326,20 +329,65 @@ void silc_client_notify_by_server(SilcClient client, * Someone set the topic on a channel. */ + SILC_LOG_DEBUG(("Notify: TOPIC_SET")); + /* 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); - if (!client_id) + idp = silc_id_payload_parse(tmp, tmp_len); + if (!idp) goto out; /* Find Client entry */ - client_entry = - silc_client_get_client_by_id(client, conn, client_id); - if (!client_entry) - goto out; + if (silc_id_payload_get_type(idp) == SILC_ID_CLIENT) { + client_id = silc_id_payload_parse_id(tmp, tmp_len); + if (!client_id) { + silc_id_payload_free(idp); + goto out; + } + + /* Find Client entry */ + client_entry = + silc_client_get_client_by_id(client, conn, client_id); + if (!client_entry) + goto out; + } else if (silc_id_payload_get_type(idp) == SILC_ID_SERVER) { + server_id = silc_id_payload_parse_id(tmp, tmp_len); + if (!server_id) { + silc_id_payload_free(idp); + goto out; + } + + server = silc_client_get_server_by_id(client, conn, server_id); + if (!server) { + silc_id_payload_free(idp); + silc_free(server_id); + goto out; + } + + /* Save the pointer to the client_entry pointer */ + client_entry = (SilcClientEntry)server; + silc_free(server_id); + } else { + channel_id = silc_id_payload_parse_id(tmp, tmp_len); + if (!channel_id) { + silc_id_payload_free(idp); + goto out; + } + + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) { + silc_id_payload_free(idp); + silc_free(channel_id); + goto out; + } + + /* Save the pointer to the client_entry pointer */ + client_entry = (SilcClientEntry)channel; + silc_free(channel_id); + } /* Get topic */ tmp = silc_argument_get_arg_type(args, 2, &tmp_len); @@ -351,16 +399,18 @@ void silc_client_notify_by_server(SilcClient client, SILC_ID_CHANNEL); if (!channel_id) goto out; - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) break; - channel = (SilcChannelEntry)id_cache->context; - /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, client_entry, tmp, channel); + client->internal->ops->notify(client, conn, type, + silc_id_payload_get_type(idp), + client_entry, tmp, channel); + + silc_id_payload_free(idp); break; case SILC_NOTIFY_TYPE_NICK_CHANGE: @@ -371,6 +421,8 @@ void silc_client_notify_by_server(SilcClient client, * application. */ + SILC_LOG_DEBUG(("Notify: NICK_CHANGE")); + /* Get old Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -404,22 +456,37 @@ void silc_client_notify_by_server(SilcClient client, /* Find Client entry and if not found resolve it */ client_entry2 = silc_client_get_client_by_id(client, conn, client_id); if (!client_entry2) { + /* Resolve the entry information */ silc_client_notify_by_server_resolve(client, conn, packet, client_id); - goto out; - } - /* Remove the old from cache */ - silc_idcache_del_by_context(conn->client_cache, client_entry); + /* Add the new entry even though we resolved it. This is because we + want to replace the old entry with the new entry here right now. */ + client_entry2 = + silc_client_add_client(client, conn, NULL, NULL, NULL, + silc_id_dup(client_id, SILC_ID_CLIENT), + client_entry->mode); - /* Replace old ID entry with new one on all channels. */ - silc_client_replace_from_channels(client, conn, client_entry, - client_entry2); + /* Replace old ID entry with new one on all channels. */ + silc_client_replace_from_channels(client, conn, client_entry, + client_entry2); + } else { + if (client_entry2 != conn->local_entry) + silc_client_nickname_format(client, conn, client_entry2); - /* Notify application */ - client->ops->notify(client, conn, type, client_entry, client_entry2); + /* Remove the old from cache */ + silc_idcache_del_by_context(conn->client_cache, client_entry); - /* Free data */ - silc_client_del_client_entry(client, conn, client_entry); + /* Replace old ID entry with new one on all channels. */ + silc_client_replace_from_channels(client, conn, client_entry, + client_entry2); + + /* Notify application */ + client->internal->ops->notify(client, conn, type, + client_entry, client_entry2); + + /* Free data */ + silc_client_del_client_entry(client, conn, client_entry); + } break; case SILC_NOTIFY_TYPE_CMODE_CHANGE: @@ -427,12 +494,14 @@ void silc_client_notify_by_server(SilcClient client, * Someone changed a channel mode */ + SILC_LOG_DEBUG(("Notify: CMODE_CHANGE")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) goto out; - idp = silc_id_payload_parse_data(tmp, tmp_len); + idp = silc_id_payload_parse(tmp, tmp_len); if (!idp) goto out; @@ -484,14 +553,12 @@ void silc_client_notify_by_server(SilcClient client, silc_id_payload_free(idp); goto out; } - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) { + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) { silc_id_payload_free(idp); goto out; } - channel = (SilcChannelEntry)id_cache->context; - /* Save the new mode */ channel->mode = mode; @@ -516,8 +583,9 @@ void silc_client_notify_by_server(SilcClient client, /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, silc_id_payload_get_type(idp), - client_entry, mode, NULL, tmp, channel); + client->internal->ops->notify(client, conn, type, + silc_id_payload_get_type(idp), + client_entry, mode, NULL, tmp, channel); silc_id_payload_free(idp); break; @@ -527,6 +595,8 @@ void silc_client_notify_by_server(SilcClient client, * Someone changed user's mode on a channel */ + SILC_LOG_DEBUG(("Notify: CUMODE_CHANGE")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -571,26 +641,21 @@ void silc_client_notify_by_server(SilcClient client, SILC_ID_CHANNEL); if (!channel_id) goto out; - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) break; - channel = (SilcChannelEntry)id_cache->context; - /* Save the mode */ - silc_list_start(channel->clients); - while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) { - if (chu->client == client_entry) { - chu->mode = mode; - break; - } - } + chu = silc_client_on_channel(channel, client_entry2); + if (chu) + chu->mode = mode; /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, client_entry, mode, - client_entry2, channel); + client->internal->ops->notify(client, conn, type, + client_entry, mode, + client_entry2, channel); break; case SILC_NOTIFY_TYPE_MOTD: @@ -598,13 +663,15 @@ void silc_client_notify_by_server(SilcClient client, * Received Message of the day */ + SILC_LOG_DEBUG(("Notify: MOTD")); + /* Get motd */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) goto out; /* Notify application */ - client->ops->notify(client, conn, type, tmp); + client->internal->ops->notify(client, conn, type, tmp); break; case SILC_NOTIFY_TYPE_CHANNEL_CHANGE: @@ -613,6 +680,8 @@ void silc_client_notify_by_server(SilcClient client, * ID to the one provided here. */ + SILC_LOG_DEBUG(("Notify: CHANNEL_CHANGE")); + /* Get the old ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -620,38 +689,27 @@ void silc_client_notify_by_server(SilcClient client, channel_id = silc_id_payload_parse_id(tmp, tmp_len); if (!channel_id) goto out; - - /* Get the channel entry */ - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) - break; - channel = (SilcChannelEntry)id_cache->context; - - SILC_LOG_DEBUG(("Old Channel ID id(%s)", - silc_id_render(channel->id, SILC_ID_CHANNEL))); + /* Get the channel entry */ + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) + goto out; - /* Free the old ID */ - silc_free(channel->id); + silc_free(channel_id); /* Get the new ID */ tmp = silc_argument_get_arg_type(args, 2, &tmp_len); if (!tmp) goto out; - channel->id = silc_id_payload_parse_id(tmp, tmp_len); - if (!channel->id) + channel_id = silc_id_payload_parse_id(tmp, tmp_len); + if (!channel_id) goto out; - SILC_LOG_DEBUG(("New Channel ID id(%s)", - silc_id_render(channel->id, SILC_ID_CHANNEL))); - - /* Remove the old cache entry and create a new one */ - silc_idcache_del_by_context(conn->channel_cache, channel); - silc_idcache_add(conn->channel_cache, channel->channel_name, - channel->id, channel, FALSE); + /* Replace the Channel ID */ + silc_client_replace_channel_id(client, conn, channel, channel_id); /* Notify application */ - client->ops->notify(client, conn, type, channel, channel); + client->internal->ops->notify(client, conn, type, channel, channel); break; case SILC_NOTIFY_TYPE_KICKED: @@ -659,6 +717,8 @@ void silc_client_notify_by_server(SilcClient client, * A client (maybe me) was kicked from a channel */ + SILC_LOG_DEBUG(("Notify: KICKED")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -678,11 +738,28 @@ void silc_client_notify_by_server(SilcClient client, SILC_ID_CHANNEL); if (!channel_id) goto out; - if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id, - &id_cache)) + channel = silc_client_get_channel_by_id(client, conn, channel_id); + if (!channel) break; - channel = (SilcChannelEntry)id_cache->context; + /* Get the kicker */ + tmp = silc_argument_get_arg_type(args, 3, &tmp_len); + if (!tmp) + goto out; + + client_id = silc_id_payload_parse_id(tmp, tmp_len); + if (!client_id) + goto out; + + /* Find kicker's client entry and if not found resolve it */ + client_entry2 = silc_client_get_client_by_id(client, conn, client_id); + if (!client_entry2) { + silc_client_notify_by_server_resolve(client, conn, packet, client_id); + goto out; + } else { + if (client_entry2 != conn->local_entry) + silc_client_nickname_format(client, conn, client_entry2); + } /* Get comment */ tmp = silc_argument_get_arg_type(args, 2, &tmp_len); @@ -690,7 +767,8 @@ void silc_client_notify_by_server(SilcClient client, /* Notify application. The channel entry is sent last as this notify is for channel but application don't know it from the arguments sent by server. */ - client->ops->notify(client, conn, type, client_entry, tmp, channel); + client->internal->ops->notify(client, conn, type, client_entry, tmp, + client_entry2, channel); /* If I was kicked from channel, remove the channel */ if (client_entry == conn->local_entry) { @@ -710,6 +788,8 @@ void silc_client_notify_by_server(SilcClient client, * A client (maybe me) was killed from the network. */ + SILC_LOG_DEBUG(("Notify: KILLED")); + /* Get Client ID */ tmp = silc_argument_get_arg_type(args, 1, &tmp_len); if (!tmp) @@ -728,13 +808,11 @@ void silc_client_notify_by_server(SilcClient client, tmp = silc_argument_get_arg_type(args, 2, &tmp_len); /* Notify application. */ - client->ops->notify(client, conn, type, client_entry, tmp); + client->internal->ops->notify(client, conn, type, client_entry, tmp); - if (client_entry != conn->local_entry) { - /* Remove client from all channels */ - silc_client_remove_from_channels(client, conn, client_entry); + if (client_entry != conn->local_entry) + /* Remove the client from all channels and free it */ silc_client_del_client(client, conn, client_entry); - } break; @@ -745,9 +823,11 @@ void silc_client_notify_by_server(SilcClient client, * from channels as they quit as well. */ SilcClientEntry *clients = NULL; - uint32 clients_count = 0; + 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); @@ -772,7 +852,8 @@ void silc_client_notify_by_server(SilcClient client, /* 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->ops->notify(client, conn, type, NULL, clients, clients_count); + client->internal->ops->notify(client, conn, type, NULL, + clients, clients_count); for (i = 0; i < clients_count; i++) { /* Remove client from all channels */ @@ -780,7 +861,7 @@ void silc_client_notify_by_server(SilcClient client, if (client_entry == conn->local_entry) continue; - silc_client_remove_from_channels(client, conn, client_entry); + /* Remove the client from all channels and free it */ silc_client_del_client(client, conn, client_entry); } silc_free(clients);