5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 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.
23 #include "command_reply.h"
25 /* Structure holding one command and pointer to its function. This
26 structure is allocate into the commands list, and is returned
27 for example by silc_client_command_find function.
29 To call a command: command->command(cmd, NULL);
30 To call a command reply: command->reply(cmd, NULL);
33 struct SilcClientCommandStruct {
34 SilcCommand cmd; /* Command type */
35 SilcCommandCb command; /* Command function */
36 SilcCommandCb reply; /* Command reply callback */
37 char *name; /* Name of the command (optional) */
38 SilcUInt8 max_args; /* Maximum arguments (optional) */
39 SilcUInt16 ident; /* Identifier for command (optional) */
40 struct SilcClientCommandStruct *next;
43 /* Context sent as argument to all commands. This is used by the library
44 and application should use this as well. However, application may
45 choose to use some own context for its own local command. All library
46 commands, however, must use this context. */
47 struct SilcClientCommandContextStruct {
49 SilcClientConnection conn;
50 SilcClientCommand command;
53 SilcUInt32 *argv_lens;
54 SilcUInt32 *argv_types;
55 int pending; /* Command is being re-processed when TRUE */
56 int users; /* Reference counter */
59 /* Structure holding pending commands. If command is pending it will be
60 executed after command reply has been executed. */
61 typedef struct SilcClientCommandPendingStruct {
62 SilcCommand reply_cmd;
64 unsigned int reply_check : 8;
65 SilcCommandCb callback;
67 struct SilcClientCommandPendingStruct *next;
68 } SilcClientCommandPending;
70 /* List of pending commands */
71 extern SilcClientCommandPending *silc_command_pending;
76 /* Macro used for command registering and unregistering */
77 #define SILC_CLIENT_CMD(func, cmd, name, args) \
78 silc_client_command_register(client, SILC_COMMAND_##cmd, name, \
79 silc_client_command_##func, \
80 silc_client_command_reply_##func, args, 0)
81 #define SILC_CLIENT_CMDU(func, cmd, name) \
82 silc_client_command_unregister(client, SILC_COMMAND_##cmd, \
83 silc_client_command_##func, \
84 silc_client_command_reply_##func, 0)
86 /* Macro used to declare command functions */
87 #define SILC_CLIENT_CMD_FUNC(func) \
88 void silc_client_command_##func(void *context, void *context2)
90 /* Executed pending command callback */
91 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd) \
94 for (_i = 0; _i < ctx->callbacks_count; _i++) \
95 if (ctx->callbacks[_i].callback) \
96 (*ctx->callbacks[_i].callback)(ctx->callbacks[_i].context, ctx); \
97 silc_client_command_pending_del(ctx->sock->user_data, cmd, ctx->ident); \
100 bool silc_client_command_register(SilcClient client,
103 SilcCommandCb command_function,
104 SilcCommandCb command_reply_function,
107 bool silc_client_command_unregister(SilcClient client,
109 SilcCommandCb command_function,
110 SilcCommandCb command_reply_function,
112 void silc_client_commands_register(SilcClient client);
113 void silc_client_commands_unregister(SilcClient client);
114 void silc_client_command_pending_del(SilcClientConnection conn,
115 SilcCommand reply_cmd,
117 SilcClientCommandPendingCallbacks
118 silc_client_command_pending_check(SilcClientConnection conn,
119 SilcClientCommandReplyContext ctx,
122 SilcUInt32 *callbacks_count);
123 void silc_client_command_process(SilcClient client,
124 SilcSocketConnection sock,
125 SilcPacketContext *packet);
126 SILC_CLIENT_CMD_FUNC(whois);
127 SILC_CLIENT_CMD_FUNC(whowas);
128 SILC_CLIENT_CMD_FUNC(identify);
129 SILC_CLIENT_CMD_FUNC(nick);
130 SILC_CLIENT_CMD_FUNC(list);
131 SILC_CLIENT_CMD_FUNC(topic);
132 SILC_CLIENT_CMD_FUNC(invite);
133 SILC_CLIENT_CMD_FUNC(quit);
134 SILC_CLIENT_CMD_FUNC(kill);
135 SILC_CLIENT_CMD_FUNC(info);
136 SILC_CLIENT_CMD_FUNC(ping);
137 SILC_CLIENT_CMD_FUNC(oper);
138 SILC_CLIENT_CMD_FUNC(join);
139 SILC_CLIENT_CMD_FUNC(motd);
140 SILC_CLIENT_CMD_FUNC(umode);
141 SILC_CLIENT_CMD_FUNC(cmode);
142 SILC_CLIENT_CMD_FUNC(cumode);
143 SILC_CLIENT_CMD_FUNC(kick);
144 SILC_CLIENT_CMD_FUNC(ban);
145 SILC_CLIENT_CMD_FUNC(detach);
146 SILC_CLIENT_CMD_FUNC(watch);
147 SILC_CLIENT_CMD_FUNC(silcoper);
148 SILC_CLIENT_CMD_FUNC(leave);
149 SILC_CLIENT_CMD_FUNC(users);
150 SILC_CLIENT_CMD_FUNC(getkey);
152 SILC_CLIENT_CMD_FUNC(shutdown);
153 SILC_CLIENT_CMD_FUNC(close);
154 SILC_CLIENT_CMD_FUNC(connect);