Watcher list support added.
[silc.git] / lib / silcclient / command.h
1 /*
2
3   command.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #ifndef COMMAND_H
21 #define COMMAND_H
22
23 #include "command_reply.h"
24
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.
28
29    To call a command: command->command(cmd, NULL);
30    To call a command reply: command->reply(cmd, NULL);
31
32 */
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;
41 };
42
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 {
48   SilcClient client;
49   SilcClientConnection conn;
50   SilcClientCommand command;
51   SilcUInt32 argc;
52   unsigned char **argv;
53   SilcUInt32 *argv_lens;
54   SilcUInt32 *argv_types;
55   int pending;                  /* Command is being re-processed when TRUE */
56   int users;                    /* Reference counter */
57 };
58
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;
63   SilcCommandCb callback;
64   void *context;
65   SilcUInt16 ident;
66   struct SilcClientCommandPendingStruct *next;
67 } SilcClientCommandPending;
68
69 /* List of pending commands */
70 extern SilcClientCommandPending *silc_command_pending;
71
72
73 /* Macros */
74
75 /* Macro used for command registering and unregistering */
76 #define SILC_CLIENT_CMD(func, cmd, name, args)                          \
77 silc_client_command_register(client, SILC_COMMAND_##cmd, name,          \
78                              silc_client_command_##func,                \
79                              silc_client_command_reply_##func, args, 0)
80 #define SILC_CLIENT_CMDU(func, cmd, name)                               \
81 silc_client_command_unregister(client, SILC_COMMAND_##cmd,              \
82                                silc_client_command_##func,              \
83                                silc_client_command_reply_##func, 0)
84
85 /* Macro used to declare command functions */
86 #define SILC_CLIENT_CMD_FUNC(func)                              \
87 void silc_client_command_##func(void *context, void *context2)
88
89 /* Executed pending command callback */
90 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd)                                \
91 do {                                                                      \
92   int _i;                                                                 \
93   for (_i = 0; _i < ctx->callbacks_count; _i++)                           \
94     if (ctx->callbacks[_i].callback)                                      \
95       (*ctx->callbacks[_i].callback)(ctx->callbacks[_i].context, ctx);    \
96   silc_client_command_pending_del(ctx->sock->user_data, cmd, ctx->ident); \
97 } while(0)
98
99 bool silc_client_command_register(SilcClient client,
100                                   SilcCommand command,
101                                   const char *name,
102                                   SilcCommandCb command_function,
103                                   SilcCommandCb command_reply_function,
104                                   SilcUInt8 max_args,
105                                   SilcUInt16 ident);
106 bool silc_client_command_unregister(SilcClient client,
107                                     SilcCommand command,
108                                     SilcCommandCb command_function,
109                                     SilcCommandCb command_reply_function,
110                                     SilcUInt16 ident);
111 void silc_client_commands_register(SilcClient client);
112 void silc_client_commands_unregister(SilcClient client);
113 void silc_client_command_pending_del(SilcClientConnection conn,
114                                      SilcCommand reply_cmd,
115                                      SilcUInt16 ident);
116 SilcClientCommandPendingCallbacks
117 silc_client_command_pending_check(SilcClientConnection conn,
118                                   SilcClientCommandReplyContext ctx,
119                                   SilcCommand command, 
120                                   SilcUInt16 ident,
121                                   SilcUInt32 *callbacks_count);
122 SILC_CLIENT_CMD_FUNC(whois);
123 SILC_CLIENT_CMD_FUNC(whowas);
124 SILC_CLIENT_CMD_FUNC(identify);
125 SILC_CLIENT_CMD_FUNC(nick);
126 SILC_CLIENT_CMD_FUNC(list);
127 SILC_CLIENT_CMD_FUNC(topic);
128 SILC_CLIENT_CMD_FUNC(invite);
129 SILC_CLIENT_CMD_FUNC(quit);
130 SILC_CLIENT_CMD_FUNC(kill);
131 SILC_CLIENT_CMD_FUNC(info);
132 SILC_CLIENT_CMD_FUNC(ping);
133 SILC_CLIENT_CMD_FUNC(oper);
134 SILC_CLIENT_CMD_FUNC(join);
135 SILC_CLIENT_CMD_FUNC(motd);
136 SILC_CLIENT_CMD_FUNC(umode);
137 SILC_CLIENT_CMD_FUNC(cmode);
138 SILC_CLIENT_CMD_FUNC(cumode);
139 SILC_CLIENT_CMD_FUNC(kick);
140 SILC_CLIENT_CMD_FUNC(ban);
141 SILC_CLIENT_CMD_FUNC(detach);
142 SILC_CLIENT_CMD_FUNC(watch);
143 SILC_CLIENT_CMD_FUNC(silcoper);
144 SILC_CLIENT_CMD_FUNC(leave);
145 SILC_CLIENT_CMD_FUNC(users);
146 SILC_CLIENT_CMD_FUNC(getkey);
147
148 SILC_CLIENT_CMD_FUNC(shutdown);
149 SILC_CLIENT_CMD_FUNC(close);
150 SILC_CLIENT_CMD_FUNC(connect);
151
152 #endif