updates.
[silc.git] / apps / silcd / command.h
1 /*
2
3   servercommand.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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; either version 2 of the License, or
12   (at your option) any later version.
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   SilcSocketConnection sock;
54   SilcCommandPayload payload;
55   SilcArgumentPayload args;
56   SilcPacketContext *packet;
57   int pending;                  /* Command is being re-processed when TRUE */
58   int users;                    /* Reference counter */
59 } *SilcServerCommandContext;
60
61 /* Pending Command callback destructor. This is called after calling the
62    pending callback or if error occurs while processing the pending command.
63    If error occurs then the callback won't be called at all, and only this
64    destructor is called. The `context' is the context given for the function
65    silc_server_command_pending. */
66 typedef void (*SilcServerPendingDestructor)(void *context);
67
68 /* Structure holding pending commands. If command is pending it will be
69    executed after command reply has been received and executed. */
70 typedef struct SilcServerCommandPendingStruct {
71   SilcServer server;
72   SilcCommand reply_cmd;
73   SilcCommandCb callback;
74   SilcServerPendingDestructor destructor;
75   void *context;
76   uint16 ident;
77   struct SilcServerCommandPendingStruct *next;
78 } SilcServerCommandPending;
79
80 #include "command_reply.h"
81
82 /* Macros */
83
84 /* Macro used for command declaration in command list structure */
85 #define SILC_SERVER_CMD(func, cmd, flags) \
86 { silc_server_command_##func, SILC_COMMAND_##cmd, flags }
87
88 /* Macro used to declare command functions. The `context' will be the
89    SilcServerCommandContext and the `context2' is the 
90    SilcServerCommandReplyContext if this function is called from the
91    command reply as pending command callback. Otherwise `context2' 
92    is NULL. */
93 #define SILC_SERVER_CMD_FUNC(func) \
94 void silc_server_command_##func(void *context, void *context2)
95
96 /* Executed pending command. The first argument to the callback function
97    is the user specified context. The second argument is always the
98    SilcServerCommandReply context. */
99 #define SILC_SERVER_PENDING_EXEC(ctx, cmd)                              \
100 do {                                                                    \
101   int _i;                                                               \
102   for (_i = 0; _i < ctx->callbacks_count; _i++)                         \
103     if (ctx->callbacks[_i].callback)                                    \
104       (*ctx->callbacks[_i].callback)(ctx->callbacks[_i].context, ctx);  \
105 } while(0)
106
107 /* Execute destructor for pending command */
108 #define SILC_SERVER_PENDING_DESTRUCTOR(ctx, cmd)                           \
109 do {                                                                       \
110   int _i;                                                                  \
111   silc_server_command_pending_del(ctx->server, cmd, ctx->ident);           \
112   for (_i = 0; _i < ctx->callbacks_count; _i++)                            \
113     if (ctx->callbacks[_i].destructor)                                     \
114       (*ctx->callbacks[_i].destructor)(ctx->callbacks[_i].context);        \
115 } while(0)
116
117 /* Prototypes */
118 void silc_server_command_process(SilcServer server,
119                                  SilcSocketConnection sock,
120                                  SilcPacketContext *packet);
121 SilcServerCommandContext silc_server_command_alloc();
122 void silc_server_command_free(SilcServerCommandContext ctx);
123 SilcServerCommandContext 
124 silc_server_command_dup(SilcServerCommandContext ctx);
125 bool silc_server_command_pending(SilcServer server,
126                                  SilcCommand reply_cmd,
127                                  uint16 ident,
128                                  SilcServerPendingDestructor destructor,
129                                  SilcCommandCb callback,
130                                  void *context);
131 void silc_server_command_pending_del(SilcServer server,
132                                      SilcCommand reply_cmd,
133                                      uint16 ident);
134 SilcServerCommandPendingCallbacks
135 silc_server_command_pending_check(SilcServer server,
136                                   SilcServerCommandReplyContext ctx,
137                                   SilcCommand command, 
138                                   uint16 ident,
139                                   uint32 *callbacks_count);
140 SILC_SERVER_CMD_FUNC(whois);
141 SILC_SERVER_CMD_FUNC(whowas);
142 SILC_SERVER_CMD_FUNC(identify);
143 SILC_SERVER_CMD_FUNC(newuser);
144 SILC_SERVER_CMD_FUNC(nick);
145 SILC_SERVER_CMD_FUNC(list);
146 SILC_SERVER_CMD_FUNC(topic);
147 SILC_SERVER_CMD_FUNC(invite);
148 SILC_SERVER_CMD_FUNC(quit);
149 SILC_SERVER_CMD_FUNC(kill);
150 SILC_SERVER_CMD_FUNC(info);
151 SILC_SERVER_CMD_FUNC(connect);
152 SILC_SERVER_CMD_FUNC(ping);
153 SILC_SERVER_CMD_FUNC(oper);
154 SILC_SERVER_CMD_FUNC(pass);
155 SILC_SERVER_CMD_FUNC(admin);
156 SILC_SERVER_CMD_FUNC(join);
157 SILC_SERVER_CMD_FUNC(motd);
158 SILC_SERVER_CMD_FUNC(umode);
159 SILC_SERVER_CMD_FUNC(cmode);
160 SILC_SERVER_CMD_FUNC(cumode);
161 SILC_SERVER_CMD_FUNC(kick);
162 SILC_SERVER_CMD_FUNC(ignore);
163 SILC_SERVER_CMD_FUNC(ban);
164 SILC_SERVER_CMD_FUNC(close);
165 SILC_SERVER_CMD_FUNC(shutdown);
166 SILC_SERVER_CMD_FUNC(silcoper);
167 SILC_SERVER_CMD_FUNC(leave);
168 SILC_SERVER_CMD_FUNC(users);
169 SILC_SERVER_CMD_FUNC(getkey);
170
171 #endif