Merged silc_1_0_branch to trunk.
[silc.git] / apps / silcd / command.h
1 /*
2
3   servercommand.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 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 /*
24    Structure holding one command and pointer to its function.
25
26    SilcCommandCb cb
27
28        Callback function called when this command is executed.
29
30    SilcCommand cmd
31
32        The actual command. Defined in silccore/silccommand.h
33
34    SilcCommandFlag flags
35
36        Flags for the command. These set how command behaves on different
37        situations.
38
39 */
40 typedef struct {
41   SilcCommandCb cb;
42   SilcCommand cmd;
43   SilcCommandFlag flags;
44 } SilcServerCommand;
45
46 /* All server commands */
47 extern SilcServerCommand silc_command_list[];
48
49 /* Context sent as argument to all commands */
50 typedef struct {
51   SilcServer server;
52   SilcSocketConnection sock;
53   SilcCommandPayload payload;
54   SilcArgumentPayload args;
55   SilcPacketContext *packet;
56   int pending;                  /* Command is being re-processed when TRUE */
57   int users;                    /* Reference counter */
58 } *SilcServerCommandContext;
59
60 /* Structure holding pending commands. If command is pending it will be
61    executed after command reply has been received and executed. */
62 typedef struct SilcServerCommandPendingStruct {
63   SilcCommand reply_cmd;
64   SilcUInt16 ident;
65   unsigned int reply_check : 8;
66   SilcCommandCb callback;
67   void *context;
68   SilcTask timeout;
69   struct SilcServerCommandPendingStruct *next;
70 } SilcServerCommandPending;
71
72 #include "command_reply.h"
73
74 /* Macros */
75
76 /* Macro used for command declaration in command list structure */
77 #define SILC_SERVER_CMD(func, cmd, flags) \
78 { silc_server_command_##func, SILC_COMMAND_##cmd, flags }
79
80 /* Macro used to declare command functions. The `context' will be the
81    SilcServerCommandContext and the `context2' is the
82    SilcServerCommandReplyContext if this function is called from the
83    command reply as pending command callback. Otherwise `context2'
84    is NULL. */
85 #define SILC_SERVER_CMD_FUNC(func) \
86 void silc_server_command_##func(void *context, void *context2)
87
88 /* Executed pending command. The first argument to the callback function
89    is the user specified context. The second argument is always the
90    SilcServerCommandReply context. */
91 #define SILC_SERVER_PENDING_EXEC(ctx, cmd)                              \
92 do {                                                                    \
93   int _i;                                                               \
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_server_command_pending_del(ctx->server, cmd, ctx->ident);        \
98 } while(0)
99
100 /* Prototypes */
101 void silc_server_command_process(SilcServer server,
102                                  SilcSocketConnection sock,
103                                  SilcPacketContext *packet);
104 SilcServerCommandContext silc_server_command_alloc();
105 void silc_server_command_free(SilcServerCommandContext ctx);
106 SilcServerCommandContext
107 silc_server_command_dup(SilcServerCommandContext ctx);
108 bool silc_server_command_pending(SilcServer server,
109                                  SilcCommand reply_cmd,
110                                  SilcUInt16 ident,
111                                  SilcCommandCb callback,
112                                  void *context);
113 bool silc_server_command_pending_timed(SilcServer server,
114                                        SilcCommand reply_cmd,
115                                        SilcUInt16 ident,
116                                        SilcCommandCb callback,
117                                        void *context,
118                                        SilcUInt16 timeout);
119 void silc_server_command_pending_del(SilcServer server,
120                                      SilcCommand reply_cmd,
121                                      SilcUInt16 ident);
122 SilcServerCommandPendingCallbacks
123 silc_server_command_pending_check(SilcServer server,
124                                   SilcCommand command,
125                                   SilcUInt16 ident,
126                                   SilcUInt32 *callbacks_count);
127 SILC_SERVER_CMD_FUNC(whois);
128 SILC_SERVER_CMD_FUNC(whowas);
129 SILC_SERVER_CMD_FUNC(identify);
130 SILC_SERVER_CMD_FUNC(newuser);
131 SILC_SERVER_CMD_FUNC(nick);
132 SILC_SERVER_CMD_FUNC(list);
133 SILC_SERVER_CMD_FUNC(topic);
134 SILC_SERVER_CMD_FUNC(invite);
135 SILC_SERVER_CMD_FUNC(quit);
136 SILC_SERVER_CMD_FUNC(kill);
137 SILC_SERVER_CMD_FUNC(info);
138 SILC_SERVER_CMD_FUNC(stats);
139 SILC_SERVER_CMD_FUNC(ping);
140 SILC_SERVER_CMD_FUNC(oper);
141 SILC_SERVER_CMD_FUNC(join);
142 SILC_SERVER_CMD_FUNC(motd);
143 SILC_SERVER_CMD_FUNC(umode);
144 SILC_SERVER_CMD_FUNC(cmode);
145 SILC_SERVER_CMD_FUNC(cumode);
146 SILC_SERVER_CMD_FUNC(kick);
147 SILC_SERVER_CMD_FUNC(ban);
148 SILC_SERVER_CMD_FUNC(detach);
149 SILC_SERVER_CMD_FUNC(watch);
150 SILC_SERVER_CMD_FUNC(silcoper);
151 SILC_SERVER_CMD_FUNC(leave);
152 SILC_SERVER_CMD_FUNC(users);
153 SILC_SERVER_CMD_FUNC(getkey);
154 SILC_SERVER_CMD_FUNC(service);
155
156 SILC_SERVER_CMD_FUNC(connect);
157 SILC_SERVER_CMD_FUNC(close);
158 SILC_SERVER_CMD_FUNC(shutdown);
159
160 #endif