5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Command reply functions are "the otherside" of the command functions.
22 * Reply to a command sent by server is handled by these functions.
24 * The arguments received from server are also passed to the calling
25 * application through command_reply client operation. The arguments are
26 * exactly same and in same order as the server sent it. However, ID's are
27 * not sent to the application. Instead, corresponding ID entry is sent
28 * to the application. For example, instead of sending Client ID the
29 * corresponding SilcClientEntry is sent to the application. The case is
30 * same with for example Channel ID's. This way application has all the
31 * necessary data already in hand without redundant searching. If ID is
32 * received but ID entry does not exist, NULL is sent.
36 #include "clientlibincludes.h"
38 /* Client command reply list. */
39 SilcClientCommandReply silc_command_reply_list[] =
41 SILC_CLIENT_CMD_REPLY(whois, WHOIS),
42 SILC_CLIENT_CMD_REPLY(whowas, WHOWAS),
43 SILC_CLIENT_CMD_REPLY(identify, IDENTIFY),
44 SILC_CLIENT_CMD_REPLY(nick, NICK),
45 SILC_CLIENT_CMD_REPLY(list, LIST),
46 SILC_CLIENT_CMD_REPLY(topic, TOPIC),
47 SILC_CLIENT_CMD_REPLY(invite, INVITE),
48 SILC_CLIENT_CMD_REPLY(quit, QUIT),
49 SILC_CLIENT_CMD_REPLY(kill, KILL),
50 SILC_CLIENT_CMD_REPLY(info, INFO),
51 SILC_CLIENT_CMD_REPLY(connect, CONNECT),
52 SILC_CLIENT_CMD_REPLY(ping, PING),
53 SILC_CLIENT_CMD_REPLY(oper, OPER),
54 SILC_CLIENT_CMD_REPLY(join, JOIN),
55 SILC_CLIENT_CMD_REPLY(motd, MOTD),
56 SILC_CLIENT_CMD_REPLY(umode, UMODE),
57 SILC_CLIENT_CMD_REPLY(cmode, CMODE),
58 SILC_CLIENT_CMD_REPLY(cumode, CUMODE),
59 SILC_CLIENT_CMD_REPLY(kick, KICK),
60 SILC_CLIENT_CMD_REPLY(restart, RESTART),
61 SILC_CLIENT_CMD_REPLY(close, CLOSE),
62 SILC_CLIENT_CMD_REPLY(die, DIE),
63 SILC_CLIENT_CMD_REPLY(silcoper, SILCOPER),
64 SILC_CLIENT_CMD_REPLY(leave, LEAVE),
65 SILC_CLIENT_CMD_REPLY(names, NAMES),
70 /* Status message structure. Messages are defined below. */
72 SilcCommandStatus status;
74 } SilcCommandStatusMessage;
76 /* Status messages returned by the server */
77 #define STAT(x) SILC_STATUS_ERR_##x
78 const SilcCommandStatusMessage silc_command_status_messages[] = {
80 { STAT(NO_SUCH_NICK), "No such nickname" },
81 { STAT(NO_SUCH_CHANNEL), "No such channel" },
82 { STAT(NO_SUCH_SERVER), "No such server" },
83 { STAT(TOO_MANY_TARGETS), "Duplicate recipients. No message delivered" },
84 { STAT(NO_RECIPIENT), "No recipient given" },
85 { STAT(UNKNOWN_COMMAND), "Unknown command" },
86 { STAT(WILDCARDS), "Unknown command" },
87 { STAT(NO_CLIENT_ID), "No Client ID given" },
88 { STAT(NO_CHANNEL_ID), "No Channel ID given" },
89 { STAT(NO_SERVER_ID), "No Server ID given" },
90 { STAT(BAD_CLIENT_ID), "Bad Client ID" },
91 { STAT(BAD_CHANNEL_ID), "Bad Channel ID" },
92 { STAT(NO_SUCH_CLIENT_ID), "No such Client ID" },
93 { STAT(NO_SUCH_CHANNEL_ID),"No such Channel ID" },
94 { STAT(NICKNAME_IN_USE), "Nickname already exists" },
95 { STAT(NOT_ON_CHANNEL), "You are not on that channel" },
96 { STAT(USER_NOT_ON_CHANNEL),"They are not on the channel" },
97 { STAT(USER_ON_CHANNEL), "User already on the channel" },
98 { STAT(NOT_REGISTERED), "You have not registered" },
99 { STAT(NOT_ENOUGH_PARAMS), "Not enough parameters" },
100 { STAT(TOO_MANY_PARAMS), "Too many parameters" },
101 { STAT(PERM_DENIED), "Your host is not among the privileged" },
102 { STAT(BANNED_FROM_SERVER),"You are banned from this server" },
103 { STAT(BAD_PASSWORD), "Cannot join channel. Incorrect password" },
104 { STAT(CHANNEL_IS_FULL), "Cannot join channel. Channel is full" },
105 { STAT(NOT_INVITED), "Cannot join channel. You have not been invited" },
106 { STAT(BANNED_FROM_CHANNEL), "Cannot join channel. You have been banned" },
107 { STAT(UNKNOWN_MODE), "Unknown mode" },
108 { STAT(NOT_YOU), "Cannot change mode for other users" },
109 { STAT(NO_CHANNEL_PRIV), "Permission denied. You are not channel operator" },
110 { STAT(NO_SERVER_PRIV), "Permission denied. You are not server operator" },
111 { STAT(NO_ROUTER_PRIV), "Permission denied. You are not SILC operator" },
112 { STAT(BAD_NICKNAME), "Bad nickname" },
113 { STAT(BAD_CHANNEL), "Bad channel name" },
114 { STAT(AUTH_FAILED), "Authentication failed" },
119 /* Command reply operation that is called at the end of all command replys.
120 Usage: COMMAND_REPLY((ARGS, argument1, argument2, etc...)), */
121 #define COMMAND_REPLY(args) cmd->client->ops->command_reply args
122 #define ARGS cmd->client, cmd->sock->user_data, \
123 cmd->payload, TRUE, silc_command_get(cmd->payload), status
125 /* Error reply to application. Usage: COMMAND_REPLY_ERROR; */
126 #define COMMAND_REPLY_ERROR cmd->client->ops->command_reply(cmd->client, \
127 cmd->sock->user_data, cmd->payload, FALSE, \
128 silc_command_get(cmd->payload), status)
130 /* Process received command reply. */
132 void silc_client_command_reply_process(SilcClient client,
133 SilcSocketConnection sock,
134 SilcPacketContext *packet)
136 SilcBuffer buffer = packet->buffer;
137 SilcClientCommandReplyContext ctx;
138 SilcCommandPayload payload;
140 /* Get command reply payload from packet */
141 payload = silc_command_payload_parse(buffer);
143 /* Silently ignore bad reply packet */
144 SILC_LOG_DEBUG(("Bad command reply packet"));
148 /* Allocate command reply context. This must be free'd by the
149 command reply routine receiving it. */
150 ctx = silc_calloc(1, sizeof(*ctx));
151 ctx->client = client;
153 ctx->payload = payload;
154 ctx->args = silc_command_get_args(ctx->payload);
155 ctx->packet = packet;
157 /* Check for pending commands and mark to be exeucted */
158 SILC_CLIENT_COMMAND_CHECK_PENDING(ctx);
160 /* Execute command reply */
161 SILC_CLIENT_COMMAND_REPLY_EXEC(ctx);
164 /* Returns status message string */
167 silc_client_command_status_message(SilcCommandStatus status)
171 for (i = 0; silc_command_status_messages[i].message; i++) {
172 if (silc_command_status_messages[i].status == status)
176 if (silc_command_status_messages[i].message == NULL)
179 return silc_command_status_messages[i].message;
182 /* Free command reply context and its internals. */
184 void silc_client_command_reply_free(SilcClientCommandReplyContext cmd)
187 silc_command_free_payload(cmd->payload);
193 silc_client_command_reply_whois_print(SilcClientCommandReplyContext cmd,
194 SilcCommandStatus status)
198 unsigned char *id_data;
199 char *nickname = NULL, *username = NULL;
200 char *realname = NULL;
201 SilcClientID *client_id;
202 SilcIDCacheEntry id_cache = NULL;
203 SilcClientEntry client_entry = NULL;
204 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
206 memset(buf, 0, sizeof(buf));
208 argc = silc_argument_get_arg_num(cmd->args);
210 id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
216 client_id = silc_id_payload_parse_id(id_data, len);
218 nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
220 strncat(buf, nickname, len);
221 strncat(buf, " is ", 4);
224 username = silc_argument_get_arg_type(cmd->args, 4, &len);
226 strncat(buf, username, len);
229 realname = silc_argument_get_arg_type(cmd->args, 5, &len);
231 strncat(buf, " (", 2);
232 strncat(buf, realname, len);
233 strncat(buf, ")", 1);
236 /* Check if we have this client cached already. */
237 if (!silc_idcache_find_by_id_one(conn->client_cache, (void *)client_id,
238 SILC_ID_CLIENT, &id_cache)) {
239 client_entry = silc_calloc(1, sizeof(*client_entry));
240 client_entry->id = client_id;
241 silc_parse_nickname(nickname, &client_entry->nickname,
242 &client_entry->server, &client_entry->num);
243 client_entry->username = strdup(username);
245 client_entry->realname = strdup(realname);
247 /* Add client to cache */
248 silc_idcache_add(conn->client_cache, client_entry->nickname,
249 SILC_ID_CLIENT, client_id, (void *)client_entry, TRUE);
251 client_entry = (SilcClientEntry)id_cache->context;
252 if (client_entry->nickname)
253 silc_free(client_entry->nickname);
254 if (client_entry->server)
255 silc_free(client_entry->server);
256 if (client_entry->username)
257 silc_free(client_entry->username);
258 if (client_entry->realname)
259 silc_free(client_entry->realname);
261 silc_parse_nickname(nickname, &client_entry->nickname,
262 &client_entry->server, &client_entry->num);
263 client_entry->username = strdup(username);
265 client_entry->realname = strdup(realname);
267 id_cache->data = client_entry->nickname;
269 silc_free(client_id);
273 cmd->client->ops->say(cmd->client, conn, "%s", buf);
275 /* Notify application */
276 COMMAND_REPLY((ARGS, client_entry, nickname, username, realname,
280 /* Received reply for WHOIS command. This maybe called several times
281 for one WHOIS command as server may reply with list of results. */
283 SILC_CLIENT_CMD_REPLY_FUNC(whois)
285 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
286 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
287 SilcCommandStatus status;
290 SILC_LOG_DEBUG(("Start"));
292 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
293 SILC_GET16_MSB(status, tmp);
294 if (status != SILC_STATUS_OK &&
295 status != SILC_STATUS_LIST_START &&
296 status != SILC_STATUS_LIST_ITEM &&
297 status != SILC_STATUS_LIST_END) {
298 if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
299 /* Take nickname which may be provided */
300 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
302 cmd->client->ops->say(cmd->client, conn, "%s: %s", tmp,
303 silc_client_command_status_message(status));
305 cmd->client->ops->say(cmd->client, conn, "%s",
306 silc_client_command_status_message(status));
310 cmd->client->ops->say(cmd->client, conn,
311 "%s", silc_client_command_status_message(status));
317 /* Display one whois reply */
318 if (status == SILC_STATUS_OK) {
319 silc_client_command_reply_whois_print(cmd, status);
322 /* XXX list should not be displayed untill all items has been received. */
323 if (status == SILC_STATUS_LIST_START) {
324 silc_client_command_reply_whois_print(cmd, status);
327 if (status == SILC_STATUS_LIST_ITEM) {
328 silc_client_command_reply_whois_print(cmd, status);
331 if (status == SILC_STATUS_LIST_END) {
332 silc_client_command_reply_whois_print(cmd, status);
335 SILC_CLIENT_COMMAND_EXEC_PENDING(cmd, SILC_COMMAND_WHOIS);
338 silc_client_command_reply_free(cmd);
341 /* Received reply for WHOWAS command. */
343 SILC_CLIENT_CMD_REPLY_FUNC(whowas)
348 /* Received reply for IDENTIFY command. This maybe called several times
349 for one IDENTIFY command as server may reply with list of results.
350 This is totally silent and does not print anything on screen. */
352 SILC_CLIENT_CMD_REPLY_FUNC(identify)
354 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
355 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
356 SilcClientEntry client_entry;
357 SilcIDCacheEntry id_cache = NULL;
358 SilcCommandStatus status;
361 SILC_LOG_DEBUG(("Start"));
363 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
364 SILC_GET16_MSB(status, tmp);
365 if (status != SILC_STATUS_OK) {
366 if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
367 /* Take nickname which may be provided */
368 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
370 cmd->client->ops->say(cmd->client, conn, "%s: %s", tmp,
371 silc_client_command_status_message(status));
373 cmd->client->ops->say(cmd->client, conn, "%s",
374 silc_client_command_status_message(status));
378 cmd->client->ops->say(cmd->client, conn,
379 "%s", silc_client_command_status_message(status));
385 /* Display one whois reply */
386 if (status == SILC_STATUS_OK) {
388 unsigned char *id_data;
391 SilcClientID *client_id;
393 id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
396 client_id = silc_id_payload_parse_id(id_data, len);
398 nickname = silc_argument_get_arg_type(cmd->args, 3, NULL);
399 username = silc_argument_get_arg_type(cmd->args, 4, NULL);
401 if (!silc_idcache_find_by_id_one(conn->client_cache, (void *)client_id,
402 SILC_ID_CLIENT, &id_cache)) {
403 client_entry = silc_calloc(1, sizeof(*client_entry));
404 client_entry->id = client_id;
405 silc_parse_nickname(nickname, &client_entry->nickname,
406 &client_entry->server, &client_entry->num);
408 client_entry->username = strdup(username);
410 /* Add client to cache */
411 silc_idcache_add(conn->client_cache, client_entry->nickname,
412 SILC_ID_CLIENT, client_id, (void *)client_entry, TRUE);
414 client_entry = (SilcClientEntry)id_cache->context;
415 if (client_entry->nickname)
416 silc_free(client_entry->nickname);
417 if (client_entry->server)
418 silc_free(client_entry->server);
419 if (username && client_entry->username)
420 silc_free(client_entry->username);
422 silc_parse_nickname(nickname, &client_entry->nickname,
423 &client_entry->server, &client_entry->num);
426 client_entry->username = strdup(username);
428 id_cache->data = client_entry->nickname;
430 silc_free(client_id);
434 if (status == SILC_STATUS_LIST_START) {
438 if (status == SILC_STATUS_LIST_END) {
442 SILC_CLIENT_COMMAND_EXEC_PENDING(cmd, SILC_COMMAND_IDENTIFY);
445 silc_client_command_reply_free(cmd);
448 /* Received reply for command NICK. If everything went without errors
449 we just received our new Client ID. */
451 SILC_CLIENT_CMD_REPLY_FUNC(nick)
453 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
454 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
455 SilcCommandStatus status;
458 unsigned int argc, len;
460 SILC_LOG_DEBUG(("Start"));
462 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
463 if (status != SILC_STATUS_OK) {
464 cmd->client->ops->say(cmd->client, conn, "Cannot set nickname: %s",
465 silc_client_command_status_message(status));
470 argc = silc_argument_get_arg_num(cmd->args);
471 if (argc < 2 || argc > 2) {
472 cmd->client->ops->say(cmd->client, conn,
473 "Cannot set nickname: bad reply to command");
478 /* Take received Client ID */
479 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
480 idp = silc_id_payload_parse_data(tmp, len);
481 silc_client_receive_new_id(cmd->client, cmd->sock, idp);
483 /* Notify application */
484 COMMAND_REPLY((ARGS, conn->local_entry));
487 silc_client_command_reply_free(cmd);
490 SILC_CLIENT_CMD_REPLY_FUNC(list)
494 /* Received reply to topic command. */
496 SILC_CLIENT_CMD_REPLY_FUNC(topic)
498 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
499 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
500 SilcCommandStatus status;
501 SilcChannelEntry channel;
502 SilcChannelID *channel_id = NULL;
503 SilcIDCacheEntry id_cache = NULL;
506 unsigned int argc, len;
508 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
509 if (status != SILC_STATUS_OK) {
510 cmd->client->ops->say(cmd->client, conn,
511 "%s", silc_client_command_status_message(status));
513 silc_client_command_reply_free(cmd);
517 argc = silc_argument_get_arg_num(cmd->args);
518 if (argc < 1 || argc > 3) {
523 /* Take Channel ID */
524 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
529 topic = silc_argument_get_arg_type(cmd->args, 3, NULL);
533 channel_id = silc_id_payload_parse_id(tmp, len);
535 /* Get the channel name */
536 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
537 SILC_ID_CHANNEL, &id_cache)) {
538 silc_free(channel_id);
543 channel = (SilcChannelEntry)id_cache->context;
545 cmd->client->ops->say(cmd->client, conn,
546 "Topic on channel %s: %s", channel->channel_name,
549 /* Notify application */
550 COMMAND_REPLY((ARGS, channel, topic));
553 silc_client_command_reply_free(cmd);
556 /* Received reply to invite command. */
558 SILC_CLIENT_CMD_REPLY_FUNC(invite)
560 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
561 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
562 SilcCommandStatus status;
565 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
566 SILC_GET16_MSB(status, tmp);
567 if (status != SILC_STATUS_OK) {
568 cmd->client->ops->say(cmd->client, conn,
569 "%s", silc_client_command_status_message(status));
571 silc_client_command_reply_free(cmd);
575 /* Notify application */
576 COMMAND_REPLY((ARGS));
578 silc_client_command_reply_free(cmd);
581 SILC_CLIENT_CMD_REPLY_FUNC(quit)
585 SILC_CLIENT_CMD_REPLY_FUNC(kill)
589 /* Received reply to INFO command. We receive the server ID and some
590 information about the server user requested. */
592 SILC_CLIENT_CMD_REPLY_FUNC(info)
594 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
595 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
596 SilcClient client = cmd->client;
597 SilcCommandStatus status;
600 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
601 SILC_GET16_MSB(status, tmp);
602 if (status != SILC_STATUS_OK) {
603 cmd->client->ops->say(cmd->client, conn,
604 "%s", silc_client_command_status_message(status));
606 silc_client_command_reply_free(cmd);
611 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
615 /* XXX save server id */
617 /* Get server info */
618 tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
622 client->ops->say(cmd->client, conn, "Info: %s", tmp);
624 /* Notify application */
625 COMMAND_REPLY((ARGS, NULL, (char *)tmp));
628 silc_client_command_reply_free(cmd);
631 SILC_CLIENT_CMD_REPLY_FUNC(connect)
635 /* Received reply to PING command. The reply time is shown to user. */
637 SILC_CLIENT_CMD_REPLY_FUNC(ping)
639 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
640 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
641 SilcCommandStatus status;
644 time_t diff, curtime;
646 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
647 if (status != SILC_STATUS_OK) {
648 cmd->client->ops->say(cmd->client, conn,
649 "%s", silc_client_command_status_message(status));
654 curtime = time(NULL);
655 id = silc_id_str2id(cmd->packet->src_id, cmd->packet->src_id_type);
657 for (i = 0; i < conn->ping_count; i++) {
658 if (!SILC_ID_SERVER_COMPARE(conn->ping[i].dest_id, id)) {
659 diff = curtime - conn->ping[i].start_time;
660 cmd->client->ops->say(cmd->client, conn,
661 "Ping reply from %s: %d second%s",
662 conn->ping[i].dest_name, diff,
663 diff == 1 ? "" : "s");
665 conn->ping[i].start_time = 0;
666 silc_free(conn->ping[i].dest_id);
667 conn->ping[i].dest_id = NULL;
668 silc_free(conn->ping[i].dest_name);
669 conn->ping[i].dest_name = NULL;
671 /* Notify application */
672 COMMAND_REPLY((ARGS));
680 silc_client_command_reply_free(cmd);
683 SILC_CLIENT_CMD_REPLY_FUNC(oper)
687 /* Received reply for JOIN command. */
689 SILC_CLIENT_CMD_REPLY_FUNC(join)
691 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
692 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
693 SilcClient client = cmd->client;
694 SilcCommandStatus status;
696 unsigned int argc, mode, len;
697 char *topic, *tmp, *channel_name;
699 SILC_LOG_DEBUG(("Start"));
701 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
702 if (status != SILC_STATUS_OK) {
703 cmd->client->ops->say(cmd->client, conn,
704 "%s", silc_client_command_status_message(status));
709 argc = silc_argument_get_arg_num(cmd->args);
710 if (argc < 3 || argc > 5) {
711 cmd->client->ops->say(cmd->client, conn,
712 "Cannot join channel: Bad reply packet");
717 /* Get channel name */
718 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
720 cmd->client->ops->say(cmd->client, conn,
721 "Cannot join channel: Bad reply packet");
725 channel_name = strdup(tmp);
728 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
730 cmd->client->ops->say(cmd->client, conn,
731 "Cannot join channel: Bad reply packet");
735 idp = silc_id_payload_parse_data(tmp, len);
737 /* Get channel mode */
738 tmp = silc_argument_get_arg_type(cmd->args, 4, NULL);
740 SILC_GET32_MSB(mode, tmp);
745 topic = silc_argument_get_arg_type(cmd->args, 5, NULL);
747 /* Save received Channel ID */
748 silc_client_new_channel_id(cmd->client, cmd->sock, channel_name,
750 silc_id_payload_free(idp);
753 client->ops->say(cmd->client, conn,
754 "Topic for %s: %s", channel_name, topic);
756 /* Notify application */
757 COMMAND_REPLY((ARGS, channel_name, conn->current_channel, mode,
761 silc_client_command_reply_free(cmd);
764 /* Received reply for MOTD command */
766 SILC_CLIENT_CMD_REPLY_FUNC(motd)
768 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
769 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
770 SilcCommandStatus status;
771 unsigned int argc, i;
773 char *motd = NULL, *cp, line[256];
775 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
776 SILC_GET16_MSB(status, tmp);
777 if (status != SILC_STATUS_OK) {
778 cmd->client->ops->say(cmd->client, conn,
779 "%s", silc_client_command_status_message(status));
784 argc = silc_argument_get_arg_num(cmd->args);
791 motd = silc_argument_get_arg_type(cmd->args, 2, NULL);
800 if (cp[i++] == '\n') {
801 memset(line, 0, sizeof(line));
802 strncat(line, cp, i - 1);
808 cmd->client->ops->say(cmd->client, conn, "%s", line);
817 /* Notify application */
818 COMMAND_REPLY((ARGS, motd));
821 silc_client_command_reply_free(cmd);
824 SILC_CLIENT_CMD_REPLY_FUNC(umode)
828 /* Received reply for CMODE command. */
830 SILC_CLIENT_CMD_REPLY_FUNC(cmode)
832 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
833 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
834 SilcCommandStatus status;
837 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
838 if (status != SILC_STATUS_OK) {
839 cmd->client->ops->say(cmd->client, conn,
840 "%s", silc_client_command_status_message(status));
845 /* Get channel mode */
846 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
852 /* Notify application */
853 COMMAND_REPLY((ARGS, tmp));
856 silc_client_command_reply_free(cmd);
859 /* Received reply for CUMODE command */
861 SILC_CLIENT_CMD_REPLY_FUNC(cumode)
863 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
864 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
865 SilcCommandStatus status;
866 SilcIDCacheEntry id_cache = NULL;
867 SilcClientID *client_id;
868 unsigned char *tmp, *id;
871 SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
872 if (status != SILC_STATUS_OK) {
873 cmd->client->ops->say(cmd->client, conn,
874 "%s", silc_client_command_status_message(status));
879 /* Get channel mode */
880 tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
887 id = silc_argument_get_arg_type(cmd->args, 3, &len);
892 client_id = silc_id_payload_parse_id(id, len);
894 /* Get client entry */
895 if (!silc_idcache_find_by_id_one(conn->client_cache, (void *)client_id,
896 SILC_ID_CLIENT, &id_cache)) {
901 /* Notify application */
902 COMMAND_REPLY((ARGS, tmp, (SilcClientEntry)id_cache->context));
903 silc_free(client_id);
906 silc_client_command_reply_free(cmd);
909 SILC_CLIENT_CMD_REPLY_FUNC(kick)
913 SILC_CLIENT_CMD_REPLY_FUNC(restart)
917 SILC_CLIENT_CMD_REPLY_FUNC(close)
921 SILC_CLIENT_CMD_REPLY_FUNC(die)
925 SILC_CLIENT_CMD_REPLY_FUNC(silcoper)
929 /* Reply to LEAVE command. */
931 SILC_CLIENT_CMD_REPLY_FUNC(leave)
933 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
934 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
935 SilcCommandStatus status;
938 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
939 SILC_GET16_MSB(status, tmp);
940 if (status != SILC_STATUS_OK) {
941 cmd->client->ops->say(cmd->client, conn,
942 "%s", silc_client_command_status_message(status));
947 /* Notify application */
948 COMMAND_REPLY((ARGS));
950 silc_client_command_reply_free(cmd);
953 /* Reply to NAMES command. Received list of client names on the channel
956 SILC_CLIENT_CMD_REPLY_FUNC(names)
958 SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
959 SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
960 SilcCommandStatus status;
961 SilcIDCacheEntry id_cache = NULL;
962 SilcChannelEntry channel;
964 SilcChannelID *channel_id = NULL;
965 SilcBuffer client_id_list;
966 SilcBuffer client_mode_list;
968 char *name_list, *cp;
969 int i, k, len1, len2, list_count = 0;
971 SILC_LOG_DEBUG(("Start"));
973 tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
974 SILC_GET16_MSB(status, tmp);
975 if (status != SILC_STATUS_OK) {
976 cmd->client->ops->say(cmd->client, conn,
977 "%s", silc_client_command_status_message(status));
983 tmp = silc_argument_get_arg_type(cmd->args, 2, &len1);
985 cmd->client->ops->say(cmd->client, conn,
986 "Cannot Channel ID: Bad reply packet");
990 channel_id = silc_id_payload_parse_id(tmp, len1);
992 /* Get the name list of the channel */
993 name_list = silc_argument_get_arg_type(cmd->args, 3, &len1);
995 cmd->client->ops->say(cmd->client, conn,
996 "Cannot get user list: Bad reply packet");
1001 /* Get Client ID list */
1002 tmp = silc_argument_get_arg_type(cmd->args, 4, &len2);
1004 cmd->client->ops->say(cmd->client, conn,
1005 "Cannot get user list: Bad reply packet");
1006 COMMAND_REPLY_ERROR;
1010 client_id_list = silc_buffer_alloc(len2);
1011 silc_buffer_pull_tail(client_id_list, len2);
1012 silc_buffer_put(client_id_list, tmp, len2);
1014 /* Get client mode list */
1015 tmp = silc_argument_get_arg_type(cmd->args, 5, &len2);
1017 cmd->client->ops->say(cmd->client, conn,
1018 "Cannot get user list: Bad reply packet");
1019 COMMAND_REPLY_ERROR;
1023 client_mode_list = silc_buffer_alloc(len2);
1024 silc_buffer_pull_tail(client_mode_list, len2);
1025 silc_buffer_put(client_mode_list, tmp, len2);
1027 /* Get the channel name */
1028 if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)channel_id,
1029 SILC_ID_CHANNEL, &id_cache)) {
1030 COMMAND_REPLY_ERROR;
1034 channel = (SilcChannelEntry)id_cache->context;
1036 /* Remove commas from list */
1037 for (i = 0; i < len1; i++)
1038 if (name_list[i] == ',') {
1044 /* Remove old client list from channel. */
1045 silc_list_start(channel->clients);
1046 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1047 silc_list_del(channel->clients, chu);
1051 /* Cache the received name list, client ID's and modes. This cache expires
1052 whenever server sends notify message to channel. It means two things;
1053 some user has joined or leaved the channel. */
1055 for (i = 0; i < list_count; i++) {
1056 int nick_len = strcspn(name_list, " ");
1057 unsigned short idp_len;
1059 char *nickname = silc_calloc(nick_len + 1, sizeof(*nickname));
1060 SilcClientID *client_id;
1061 SilcClientEntry client;
1063 memcpy(nickname, name_list, nick_len);
1064 SILC_GET16_MSB(idp_len, client_id_list->data + 2);
1066 client_id = silc_id_payload_parse_id(client_id_list->data, idp_len);
1067 silc_buffer_pull(client_id_list, idp_len);
1069 SILC_GET32_MSB(mode, client_mode_list->data);
1070 silc_buffer_pull(client_mode_list, 4);
1072 /* Check if we have this client cached already. */
1073 if (!silc_idcache_find_by_id_one(conn->client_cache, (void *)client_id,
1074 SILC_ID_CLIENT, &id_cache)) {
1075 client = silc_calloc(1, sizeof(*client));
1076 client->id = client_id;
1077 silc_parse_nickname(nickname, &client->nickname, &client->server,
1079 silc_free(nickname);
1081 /* Add client to cache */
1082 silc_idcache_add(conn->client_cache, client->nickname, SILC_ID_CLIENT,
1083 client_id, (void *)client, TRUE);
1085 client = (SilcClientEntry)id_cache->context;
1086 silc_free(client_id);
1087 silc_free(nickname);
1091 chu = silc_calloc(1, sizeof(*chu));
1092 chu->client = client;
1094 silc_list_add(channel->clients, chu);
1096 name_list += nick_len + 1;
1100 for (i = 0; i < list_count; i++) {
1102 int nick_len = strcspn(name_list, " ");
1103 char *nickname = silc_calloc(nick_len + 1, sizeof(*nickname));
1104 memcpy(nickname, name_list, nick_len);
1106 silc_list_start(channel->clients);
1107 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1108 if (!strncmp(chu->client->nickname, nickname,
1109 strlen(chu->client->nickname))) {
1117 memset(t, 0, sizeof(t));
1118 chu->client->nickname = silc_calloc(strlen(nickname) + 8, 1);
1119 snprintf(t, sizeof(t), "[%d]", c++);
1120 strncat(chu->client->nickname, t, strlen(t));
1121 strncat(chu->client->nickname, nickname, strlen(nickname));
1125 silc_free(nickname);
1128 /* XXX hmm... actually it is applications business to display this
1129 information. We should just pass (as we do) the data to application and
1130 let it to parse it and display it the way it wants. */
1131 if (cmd->callback) {
1132 cmd->client->ops->say(cmd->client, conn, "Users on %s",
1133 channel->channel_name);
1135 silc_list_start(channel->clients);
1136 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1137 SilcClientEntry e = chu->client;
1138 char *m, tmp[80], line[80];
1140 memset(line, 0, sizeof(line));
1141 memset(tmp, 0, sizeof(tmp));
1142 m = silc_client_chumode_char(chu->mode);
1145 strcat(line, e->nickname);
1146 strcat(line, e->server ? "@" : "");
1150 len1 = strlen(e->server);
1151 strncat(line, e->server ? e->server : "", len1 > 30 ? 30 : len1);
1153 len1 = strlen(line);
1155 memset(&line[29], 0, len1 - 29);
1157 for (i = 0; i < 30 - len1 - 1; i++)
1162 strcat(tmp, m ? m : "");
1165 if (strlen(tmp) < 5)
1166 for (i = 0; i < 5 - strlen(tmp); i++)
1169 strcat(line, e->username ? e->username : "");
1171 cmd->client->ops->say(cmd->client, conn, "%s", line);
1182 silc_list_start(channel->clients);
1183 while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
1184 char *m, *n = chu->client->nickname;
1188 name_list = silc_realloc(name_list, sizeof(*name_list) * (len1 + 3));
1190 m = silc_client_chumode_char(chu->mode);
1192 memcpy(name_list + (len1 - len2), m, strlen(m));
1197 memcpy(name_list + (len1 - len2), n, len2);
1198 name_list[len1] = 0;
1200 if (k == silc_list_count(channel->clients) - 1)
1202 memcpy(name_list + len1, " ", 1);
1207 cmd->client->ops->say(cmd->client, conn, "Users on %s: %s",
1208 channel->channel_name, name_list);
1209 silc_free(name_list);
1212 name_list = silc_argument_get_arg_type(cmd->args, 3, &len1);
1214 /* Notify application */
1215 COMMAND_REPLY((ARGS, channel, name_list, client_id_list->head,
1216 client_mode_list->head));
1218 silc_buffer_free(client_id_list);
1219 silc_buffer_free(client_mode_list);
1223 silc_free(channel_id);
1224 silc_client_command_reply_free(cmd);