A LOT updates. Cannot separate. :)
[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 - 2000 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;
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    Pending commands are used in cases where the original command request
63    had to be forwarded to router. After router replies the pending
64    command is re-executed. */
65 typedef struct SilcServerCommandPendingStruct {
66   SilcCommand reply_cmd;
67   void *context;
68   SilcCommandCb callback;
69
70   struct SilcServerCommandPendingStruct *next;
71 } SilcServerCommandPending;
72
73 /* List of pending commands */
74 extern SilcServerCommandPending *silc_command_pending;
75
76 /* Macros */
77
78 /* Macro used for command declaration in command list structure */
79 #define SILC_SERVER_CMD(func, cmd, flags) \
80 { silc_server_command_##func, SILC_COMMAND_##cmd, flags }
81
82 /* Macro used to declare command functions */
83 #define SILC_SERVER_CMD_FUNC(func) \
84 void silc_server_command_##func(void *context)
85
86 /* Checks for pending commands */
87 #define SILC_SERVER_COMMAND_CHECK_PENDING(ctx)          \
88 do {                                                    \
89   if (silc_command_pending) {                           \
90     SilcServerCommandPending *r;                        \
91     SilcCommand cmd;                                    \
92                                                         \
93     cmd = silc_command_get(payload);                    \
94     for (r = silc_command_pending; r; r = r->next) {    \
95       if (r->reply_cmd == cmd) {                        \
96         ctx->context = r->context;                      \
97         ctx->callback = r->callback;                    \
98         break;                                          \
99       }                                                 \
100     }                                                   \
101   }                                                     \
102 } while(0)
103
104 /* Executed pending command */
105 #define SILC_SERVER_COMMAND_EXEC_PENDING(ctx, cmd)      \
106 do {                                                    \
107   if (ctx->callback) {                                  \
108     (*ctx->callback)(ctx->context);                     \
109     silc_server_command_pending_del(cmd);               \
110   }                                                     \
111 } while(0)
112
113 /* Prototypes */
114 void silc_server_command_process(SilcServer server,
115                                  SilcSocketConnection sock,
116                                  SilcPacketContext *packet);
117 void silc_server_command_pending(SilcCommand reply_cmd,
118                                  SilcCommandCb callback,
119                                  void *context);
120 void silc_server_command_pending_del(SilcCommand reply_cmd);
121 SILC_SERVER_CMD_FUNC(whois);
122 SILC_SERVER_CMD_FUNC(whowas);
123 SILC_SERVER_CMD_FUNC(identify);
124 SILC_SERVER_CMD_FUNC(newuser);
125 SILC_SERVER_CMD_FUNC(nick);
126 SILC_SERVER_CMD_FUNC(list);
127 SILC_SERVER_CMD_FUNC(topic);
128 SILC_SERVER_CMD_FUNC(invite);
129 SILC_SERVER_CMD_FUNC(quit);
130 SILC_SERVER_CMD_FUNC(kill);
131 SILC_SERVER_CMD_FUNC(info);
132 SILC_SERVER_CMD_FUNC(connect);
133 SILC_SERVER_CMD_FUNC(ping);
134 SILC_SERVER_CMD_FUNC(oper);
135 SILC_SERVER_CMD_FUNC(pass);
136 SILC_SERVER_CMD_FUNC(admin);
137 SILC_SERVER_CMD_FUNC(join);
138 SILC_SERVER_CMD_FUNC(motd);
139 SILC_SERVER_CMD_FUNC(umode);
140 SILC_SERVER_CMD_FUNC(cmode);
141 SILC_SERVER_CMD_FUNC(cumode);
142 SILC_SERVER_CMD_FUNC(kick);
143 SILC_SERVER_CMD_FUNC(ignore);
144 SILC_SERVER_CMD_FUNC(restart);
145 SILC_SERVER_CMD_FUNC(close);
146 SILC_SERVER_CMD_FUNC(die);
147 SILC_SERVER_CMD_FUNC(silcoper);
148 SILC_SERVER_CMD_FUNC(leave);
149 SILC_SERVER_CMD_FUNC(names);
150
151 #endif