+typedef struct {
+ SilcClient client;
+ SilcClientConnection conn;
+ SilcChannelID channel_id;
+ SilcGetClientCallback completion;
+ void *context;
+ int res_count;
+} *GetClientsByChannelInternal;
+
+SILC_CLIENT_CMD_FUNC(get_clients_by_channel_cb)
+{
+ GetClientsByChannelInternal i = context;
+ SilcClientEntry *clients = NULL;
+ SilcUInt32 clients_count = 0;
+ bool found = FALSE;
+ SilcChannelEntry channel;
+ SilcHashTableList htl;
+ SilcChannelUser chu;
+
+ channel = silc_client_get_channel_by_id(i->client, i->conn, &i->channel_id);
+ if (channel && !silc_hash_table_count(channel->user_list)) {
+ clients = silc_calloc(silc_hash_table_count(channel->user_list),
+ sizeof(*clients));
+ silc_hash_table_list(channel->user_list, &htl);
+ while (silc_hash_table_get(&htl, NULL, (void **)&chu))
+ clients[clients_count++] = chu->client;
+ silc_hash_table_list_reset(&htl);
+ found = TRUE;
+ }
+
+ if (found) {
+ i->completion(i->client, i->conn, clients, clients_count, i->context);
+ silc_free(clients);
+ } else {
+ i->completion(i->client, i->conn, NULL, 0, i->context);
+ }
+
+ silc_free(i);
+}
+
+/* Gets client entries by the channel entry indicated by `channel'. Thus,
+ it resolves the clients currently on that channel. */
+
+void silc_client_get_clients_by_channel(SilcClient client,
+ SilcClientConnection conn,
+ SilcChannelEntry channel,
+ SilcGetClientCallback completion,
+ void *context)
+{
+ GetClientsByChannelInternal in;
+ SilcHashTableList htl;
+ SilcChannelUser chu;
+ SilcClientEntry entry;
+ unsigned char **res_argv = NULL;
+ SilcUInt32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
+ SilcBuffer idp;
+ bool wait_res = FALSE;
+
+ assert(client && conn && channel);
+
+ SILC_LOG_DEBUG(("Start"));
+
+ in = silc_calloc(1, sizeof(*in));
+ in->client = client;
+ in->conn = conn;
+ in->channel_id = *channel->id;
+ in->completion = completion;
+ in->context = context;
+
+ /* If user list does not exist, send USERS command. */
+ if (!channel->user_list || !silc_hash_table_count(channel->user_list)) {
+ SILC_LOG_DEBUG(("Sending USERS"));
+ silc_client_command_register(client, SILC_COMMAND_USERS, NULL, NULL,
+ silc_client_command_reply_users_i, 0,
+ ++conn->cmd_ident);
+ silc_client_command_send(client, conn, SILC_COMMAND_USERS,
+ conn->cmd_ident, 1, 2, channel->channel_name,
+ strlen(channel->channel_name));
+ silc_client_command_pending(conn, SILC_COMMAND_USERS, conn->cmd_ident,
+ silc_client_command_get_clients_by_channel_cb,
+ in);
+ return;
+ }
+
+ silc_hash_table_list(channel->user_list, &htl);
+ while (silc_hash_table_get(&htl, NULL, (void **)&chu)) {
+ entry = chu->client;
+
+ /* If the entry has incomplete info, then resolve it from the server. */
+ if (!entry->nickname || !entry->realname) {
+ if (entry->status & SILC_CLIENT_STATUS_RESOLVING) {
+ /* Attach to this resolving and wait until it finishes */
+ silc_client_command_pending(
+ conn, SILC_COMMAND_NONE,
+ entry->resolve_cmd_ident,
+ silc_client_command_get_clients_by_channel_cb,
+ (void *)in);
+ wait_res = TRUE;
+ in->res_count++;
+ continue;
+ }
+ entry->status |= SILC_CLIENT_STATUS_RESOLVING;
+ entry->resolve_cmd_ident = conn->cmd_ident + 1;
+
+ idp = silc_id_payload_encode(entry->id, SILC_ID_CLIENT);
+
+ /* No we don't have it, query it from the server. Assemble argument
+ table that will be sent for the WHOIS command later. */
+ res_argv = silc_realloc(res_argv, sizeof(*res_argv) *
+ (res_argc + 1));
+ res_argv_lens = silc_realloc(res_argv_lens, sizeof(*res_argv_lens) *
+ (res_argc + 1));
+ res_argv_types = silc_realloc(res_argv_types, sizeof(*res_argv_types) *
+ (res_argc + 1));
+ res_argv[res_argc] = silc_memdup(idp->data, idp->len);
+ res_argv_lens[res_argc] = idp->len;
+ res_argv_types[res_argc] = res_argc + 4;
+ res_argc++;
+
+ silc_buffer_free(idp);
+ }
+ }
+ silc_hash_table_list_reset(&htl);
+
+ /* Query the client information from server if the list included clients
+ that we don't know about. */
+ if (res_argc) {
+ SilcBuffer res_cmd;
+
+ /* Send the WHOIS command to server */
+ res_cmd = silc_command_payload_encode(SILC_COMMAND_WHOIS,
+ res_argc, res_argv, res_argv_lens,
+ res_argv_types, ++conn->cmd_ident);
+ silc_client_packet_send(client, conn->sock, SILC_PACKET_COMMAND,
+ NULL, 0, NULL, NULL, res_cmd->data, res_cmd->len,
+ TRUE);
+
+ /* Register our own command reply for this command */
+ silc_client_command_register(client, SILC_COMMAND_WHOIS, NULL, NULL,
+ silc_client_command_reply_whois_i, 0,
+ conn->cmd_ident);
+
+ /* Process the applications request after reply has been received */
+ silc_client_command_pending(
+ conn, SILC_COMMAND_WHOIS, conn->cmd_ident,
+ silc_client_command_get_clients_by_channel_cb,
+ (void *)in);
+ in->res_count++;
+
+ silc_buffer_free(res_cmd);
+ silc_free(res_argv);
+ silc_free(res_argv_lens);
+ silc_free(res_argv_types);
+ return;
+ }
+
+ if (wait_res)
+ return;
+
+ /* We have the clients in cache, get them and call the completion */
+ silc_client_command_get_clients_by_channel_cb((void *)in, NULL);
+}
+