Merge commit 'origin/silc.1.1.branch'
[silc.git] / apps / silcd / command.h
1
2 /*
3
4   servercommand.h
5
6   Author: Pekka Riikonen <priikone@silcnet.org>
7
8   Copyright (C) 1997 - 2005, 2007 Pekka Riikonen
9
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; version 2 of the License.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #ifndef COMMAND_H
22 #define COMMAND_H
23
24 /*
25    Structure holding one command and pointer to its function.
26
27    SilcCommandCb cb
28
29        Callback function called when this command is executed.
30
31    SilcCommand cmd
32
33        The actual command. Defined in silccore/silccommand.h
34
35    SilcCommandFlag flags
36
37        Flags for the command. These set how command behaves on different
38        situations.
39
40 */
41 typedef struct {
42   SilcCommandCb cb;
43   SilcCommand cmd;
44   SilcCommandFlag flags;
45 } SilcServerCommand;
46
47 /* All server commands */
48 extern SilcServerCommand silc_command_list[];
49
50 /* Context sent as argument to all commands */
51 typedef struct {
52   SilcServer server;
53   SilcPacketStream sock;
54   SilcCommandPayload payload;
55   SilcArgumentPayload args;
56   SilcPacket packet;
57   int pending;                  /* Command is being re-processed when TRUE */
58   int users;                    /* Reference counter */
59 } *SilcServerCommandContext;
60
61 /* Structure holding pending commands. If command is pending it will be
62    executed after command reply has been received and executed. */
63 typedef struct SilcServerCommandPendingStruct {
64   SilcCommand reply_cmd;
65   SilcUInt16 ident;
66   unsigned int reply_check : 8;
67   SilcCommandCb callback;
68   void *context;
69   SilcTask timeout;
70   struct SilcServerCommandPendingStruct *next;
71 } SilcServerCommandPending;
72
73 #include "command_reply.h"
74
75 /* Macros */
76
77 /* Macro used for command declaration in command list structure */
78 #define SILC_SERVER_CMD(func, cmd, flags) \
79 { silc_server_command_##func, SILC_COMMAND_##cmd, flags }
80
81 /* Macro used to declare command functions. The `context' will be the
82    SilcServerCommandContext and the `context2' is the
83    SilcServerCommandReplyContext if this function is called from the
84    command reply as pending command callback. Otherwise `context2'
85    is NULL. */
86 #define SILC_SERVER_CMD_FUNC(func) \
87 void silc_server_command_##func(void *context, void *context2)
88
89 /* Executed pending command. The first argument to the callback function
90    is the user specified context. The second argument is always the
91    SilcServerCommandReply context. */
92 #define SILC_SERVER_PENDING_EXEC(ctx, cmd)                              \
93 do {                                                                    \
94   int _i;                                                               \
95   for (_i = 0; _i < ctx->callbacks_count; _i++)                         \
96     if (ctx->callbacks[_i].callback)                                    \
97       (*ctx->callbacks[_i].callback)(ctx->callbacks[_i].context, ctx);  \
98   silc_server_command_pending_del(ctx->server, cmd, ctx->ident);        \
99 } while(0)
100
101 /* Prototypes */
102 void silc_server_command_process(SilcServer server,
103                                  SilcPacketStream sock,
104                                  SilcPacket packet);
105 SilcServerCommandContext silc_server_command_alloc();
106 void silc_server_command_free(SilcServerCommandContext ctx);
107 SilcServerCommandContext
108 silc_server_command_dup(SilcServerCommandContext ctx);
109 SilcBool silc_server_command_pending(SilcServer server,
110                                  SilcCommand reply_cmd,
111                                  SilcUInt16 ident,
112                                  SilcCommandCb callback,
113                                  void *context);
114 SilcBool silc_server_command_pending_timed(SilcServer server,
115                                        SilcCommand reply_cmd,
116                                        SilcUInt16 ident,
117                                        SilcCommandCb callback,
118                                        void *context,
119                                        SilcUInt16 timeout);
120 void silc_server_command_pending_del(SilcServer server,
121                                      SilcCommand reply_cmd,
122                                      SilcUInt16 ident);
123 SilcServerCommandPendingCallbacks
124 silc_server_command_pending_check(SilcServer server,
125                                   SilcCommand command,
126                                   SilcUInt16 ident,
127                                   SilcUInt32 *callbacks_count);
128 SILC_SERVER_CMD_FUNC(whois);
129 SILC_SERVER_CMD_FUNC(whowas);
130 SILC_SERVER_CMD_FUNC(identify);
131 SILC_SERVER_CMD_FUNC(newuser);
132 SILC_SERVER_CMD_FUNC(nick);
133 SILC_SERVER_CMD_FUNC(list);
134 SILC_SERVER_CMD_FUNC(topic);
135 SILC_SERVER_CMD_FUNC(invite);
136 SILC_SERVER_CMD_FUNC(quit);
137 SILC_SERVER_CMD_FUNC(kill);
138 SILC_SERVER_CMD_FUNC(info);
139 SILC_SERVER_CMD_FUNC(stats);
140 SILC_SERVER_CMD_FUNC(ping);
141 SILC_SERVER_CMD_FUNC(oper);
142 SILC_SERVER_CMD_FUNC(join);
143 SILC_SERVER_CMD_FUNC(motd);
144 SILC_SERVER_CMD_FUNC(umode);
145 SILC_SERVER_CMD_FUNC(cmode);
146 SILC_SERVER_CMD_FUNC(cumode);
147 SILC_SERVER_CMD_FUNC(kick);
148 SILC_SERVER_CMD_FUNC(ban);
149 SILC_SERVER_CMD_FUNC(detach);
150 SILC_SERVER_CMD_FUNC(watch);
151 SILC_SERVER_CMD_FUNC(silcoper);
152 SILC_SERVER_CMD_FUNC(leave);
153 SILC_SERVER_CMD_FUNC(users);
154 SILC_SERVER_CMD_FUNC(getkey);
155 SILC_SERVER_CMD_FUNC(service);
156
157 SILC_SERVER_CMD_FUNC(connect);
158 SILC_SERVER_CMD_FUNC(close);
159 SILC_SERVER_CMD_FUNC(shutdown);
160
161 #endif