updates.
[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 /* Forward declarations */
24 typedef struct SilcClientCommandStruct *SilcClientCommand;
25 typedef struct SilcClientCommandContextStruct *SilcClientCommandContext;
26
27 #include "silcapi.h"
28 #include "command_reply.h"
29
30 /* Structure holding one command and pointer to its function. This
31    structure is allocate into the commands list, and is returned
32    for example by silc_client_command_find function.
33
34    To call a command: command->command(cmd, NULL);
35    To call a command reply: command->reply(cmd, NULL);
36
37 */
38 struct SilcClientCommandStruct {
39   SilcCommand cmd;                 /* Command type */
40   SilcCommandCb command;           /* Command function */
41   SilcCommandCb reply;             /* Command reply callback */
42   char *name;                      /* Name of the command (optional) */
43   uint8 max_args;                  /* Maximum arguments (optional)  */
44   uint16 ident;                    /* Identifier for command (optional)  */
45   struct SilcClientCommandStruct *next;
46 };
47
48 /* Context sent as argument to all commands. This is used by the library
49    and application should use this as well. However, application may
50    choose to use some own context for its own local command. All library
51    commands, however, must use this context. */
52 struct SilcClientCommandContextStruct {
53   SilcClient client;
54   SilcClientConnection conn;
55   SilcClientCommand command;
56   uint32 argc;
57   unsigned char **argv;
58   uint32 *argv_lens;
59   uint32 *argv_types;
60   int pending;                  /* Command is being re-processed when TRUE */
61   int users;                    /* Reference counter */
62 };
63
64 /* Structure holding pending commands. If command is pending it will be
65    executed after command reply has been executed. */
66 typedef struct SilcClientCommandPendingStruct {
67   SilcCommand reply_cmd;
68   SilcCommandCb callback;
69   SilcClientPendingDestructor destructor;
70   void *context;
71   uint16 ident;
72   struct SilcClientCommandPendingStruct *next;
73 } SilcClientCommandPending;
74
75 /* List of pending commands */
76 extern SilcClientCommandPending *silc_command_pending;
77
78
79 /* Macros */
80
81 /* Macro used for command registering and unregistering */
82 #define SILC_CLIENT_CMD(func, cmd, name, args)                          \
83 silc_client_command_register(client, SILC_COMMAND_##cmd, name,          \
84                              silc_client_command_##func,                \
85                              silc_client_command_reply_##func, args, 0)
86 #define SILC_CLIENT_CMDU(func, cmd, name)                               \
87 silc_client_command_unregister(client, SILC_COMMAND_##cmd,              \
88                                silc_client_command_##func,              \
89                                silc_client_command_reply_##func, 0)
90
91 /* Macro used to declare command functions */
92 #define SILC_CLIENT_CMD_FUNC(func)                              \
93 void silc_client_command_##func(void *context, void *context2)
94
95 /* Executed pending command callback */
96 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd)      \
97 do {                                            \
98   if ((ctx)->callback)                          \
99     (*ctx->callback)(ctx->context, ctx);        \
100 } while(0)
101
102 /* Execute destructor for pending command */
103 #define SILC_CLIENT_PENDING_DESTRUCTOR(ctx, cmd)                        \
104 do {                                                                    \
105   silc_client_command_pending_del((ctx)->sock->user_data, (cmd),        \
106                                   (ctx)->ident);                        \
107   if (ctx->destructor)                                                  \
108     (*ctx->destructor)(ctx->context);                                   \
109 } while(0)
110
111 bool silc_client_command_register(SilcClient client,
112                                   SilcCommand command,
113                                   const char *name,
114                                   SilcCommandCb command_function,
115                                   SilcCommandCb command_reply_function,
116                                   uint8 max_args,
117                                   uint16 ident);
118 bool silc_client_command_unregister(SilcClient client,
119                                     SilcCommand command,
120                                     SilcCommandCb command_function,
121                                     SilcCommandCb command_reply_function,
122                                     uint16 ident);
123 void silc_client_commands_register(SilcClient client);
124 void silc_client_commands_unregister(SilcClient client);
125 void silc_client_command_pending_del(SilcClientConnection conn,
126                                      SilcCommand reply_cmd,
127                                      uint16 ident);
128 int silc_client_command_pending_check(SilcClientConnection conn,
129                                       SilcClientCommandReplyContext ctx,
130                                       SilcCommand command, 
131                                       uint16 ident);
132
133 SILC_CLIENT_CMD_FUNC(whois);
134 SILC_CLIENT_CMD_FUNC(whowas);
135 SILC_CLIENT_CMD_FUNC(identify);
136 SILC_CLIENT_CMD_FUNC(nick);
137 SILC_CLIENT_CMD_FUNC(list);
138 SILC_CLIENT_CMD_FUNC(topic);
139 SILC_CLIENT_CMD_FUNC(invite);
140 SILC_CLIENT_CMD_FUNC(quit);
141 SILC_CLIENT_CMD_FUNC(kill);
142 SILC_CLIENT_CMD_FUNC(info);
143 SILC_CLIENT_CMD_FUNC(connect);
144 SILC_CLIENT_CMD_FUNC(ping);
145 SILC_CLIENT_CMD_FUNC(oper);
146 SILC_CLIENT_CMD_FUNC(join);
147 SILC_CLIENT_CMD_FUNC(motd);
148 SILC_CLIENT_CMD_FUNC(umode);
149 SILC_CLIENT_CMD_FUNC(cmode);
150 SILC_CLIENT_CMD_FUNC(cumode);
151 SILC_CLIENT_CMD_FUNC(kick);
152 SILC_CLIENT_CMD_FUNC(ban);
153 SILC_CLIENT_CMD_FUNC(close);
154 SILC_CLIENT_CMD_FUNC(shutdown);
155 SILC_CLIENT_CMD_FUNC(silcoper);
156 SILC_CLIENT_CMD_FUNC(leave);
157 SILC_CLIENT_CMD_FUNC(users);
158 SILC_CLIENT_CMD_FUNC(getkey);
159
160 #endif