New silcconfig library and server parser. Merged silc-newconfig-final.patch.
[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   void *context;
70   uint16 ident;
71   struct SilcClientCommandPendingStruct *next;
72 } SilcClientCommandPending;
73
74 /* List of pending commands */
75 extern SilcClientCommandPending *silc_command_pending;
76
77
78 /* Macros */
79
80 /* Macro used for command registering and unregistering */
81 #define SILC_CLIENT_CMD(func, cmd, name, args)                          \
82 silc_client_command_register(client, SILC_COMMAND_##cmd, name,          \
83                              silc_client_command_##func,                \
84                              silc_client_command_reply_##func, args, 0)
85 #define SILC_CLIENT_CMDU(func, cmd, name)                               \
86 silc_client_command_unregister(client, SILC_COMMAND_##cmd,              \
87                                silc_client_command_##func,              \
88                                silc_client_command_reply_##func, 0)
89
90 /* Macro used to declare command functions */
91 #define SILC_CLIENT_CMD_FUNC(func)                              \
92 void silc_client_command_##func(void *context, void *context2)
93
94 /* Executed pending command callback */
95 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd)                              \
96 do {                                                                    \
97   if ((ctx)->callback)                                                  \
98     (*ctx->callback)(ctx->context, ctx);                                \
99   silc_client_command_pending_del((ctx)->sock->user_data, (cmd),        \
100                                   (ctx)->ident);                        \
101 } while(0)
102
103 bool silc_client_command_register(SilcClient client,
104                                   SilcCommand command,
105                                   const char *name,
106                                   SilcCommandCb command_function,
107                                   SilcCommandCb command_reply_function,
108                                   uint8 max_args,
109                                   uint16 ident);
110 bool silc_client_command_unregister(SilcClient client,
111                                     SilcCommand command,
112                                     SilcCommandCb command_function,
113                                     SilcCommandCb command_reply_function,
114                                     uint16 ident);
115 void silc_client_commands_register(SilcClient client);
116 void silc_client_commands_unregister(SilcClient client);
117 void silc_client_command_pending_del(SilcClientConnection conn,
118                                      SilcCommand reply_cmd,
119                                      uint16 ident);
120 int silc_client_command_pending_check(SilcClientConnection conn,
121                                       SilcClientCommandReplyContext ctx,
122                                       SilcCommand command, 
123                                       uint16 ident);
124
125 SILC_CLIENT_CMD_FUNC(whois);
126 SILC_CLIENT_CMD_FUNC(whowas);
127 SILC_CLIENT_CMD_FUNC(identify);
128 SILC_CLIENT_CMD_FUNC(nick);
129 SILC_CLIENT_CMD_FUNC(list);
130 SILC_CLIENT_CMD_FUNC(topic);
131 SILC_CLIENT_CMD_FUNC(invite);
132 SILC_CLIENT_CMD_FUNC(quit);
133 SILC_CLIENT_CMD_FUNC(kill);
134 SILC_CLIENT_CMD_FUNC(info);
135 SILC_CLIENT_CMD_FUNC(connect);
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(close);
146 SILC_CLIENT_CMD_FUNC(shutdown);
147 SILC_CLIENT_CMD_FUNC(silcoper);
148 SILC_CLIENT_CMD_FUNC(leave);
149 SILC_CLIENT_CMD_FUNC(users);
150 SILC_CLIENT_CMD_FUNC(getkey);
151
152 #endif