Splitted SILC core library. Core library includes now only
[silc.git] / apps / silc / command.h
1 /*
2
3   command.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. These are defined in silccore/silccommand.h
34
35    char *name
36
37        Logical name of the command. This is the visible command name
38        that user uses when calling command. Eg. NICK.
39
40    SilcCommandFlag flags
41
42        Flags for the command. These set how command behaves on different
43        situations. Server sets these flags as well, but to be sure
44        that our client never sends wrong commands we preserve the
45        flags on client side as well.
46
47        XXX: We preserve these so that we define them but currently we
48        don't check the flags at all.
49
50 */
51 typedef struct {
52   SilcCommandCb cb;
53   SilcCommand cmd;
54   char *name;
55   SilcCommandFlag flags;
56   unsigned int max_args;
57 } SilcClientCommand;
58
59 /* All client commands */
60 extern SilcClientCommand silc_command_list[];
61
62 /* Client command callback function. This included into Command Context, 
63    and if it is defined it will be executed when executing the command. */
64 typedef void (*SilcClientCommandCallback)(void *context);
65
66 /* Context sent as argument to all commands */
67 typedef struct {
68   SilcClient client;
69   SilcSocketConnection sock;
70   unsigned int argc;
71   unsigned char **argv;
72   unsigned int *argv_lens;
73   unsigned int *argv_types;
74 } *SilcClientCommandContext;
75
76 /* Structure holding pending commands. If command is pending it will be
77    executed after command reply has been executed. */
78 /* XXX This support may added for commands as well and not just command
79    replies, if needed later. */
80 typedef struct SilcClientCommandPendingStruct {
81   SilcCommand reply_cmd;
82   void *context;
83   SilcClientCommandCallback callback;
84
85   struct SilcClientCommandPendingStruct *next;
86 } SilcClientCommandPending;
87
88 /* List of pending commands */
89 extern SilcClientCommandPending *silc_command_pending;
90
91 /* Macros */
92
93 /* Macro used for command declaration in command list structure */
94 #define SILC_CLIENT_CMD(func, cmd, name, flags, args) \
95 { silc_client_command_##func, SILC_COMMAND_##cmd, name, flags, args }
96
97 /* Macro used to declare command functions */
98 #define SILC_CLIENT_CMD_FUNC(func) \
99 void silc_client_command_##func(void *context)
100
101 /* Checks for pending commands */
102 #define SILC_CLIENT_COMMAND_CHECK_PENDING(ctx)          \
103 do {                                                    \
104   if (silc_command_pending) {                           \
105     SilcClientCommandPending *r;                        \
106     SilcCommand cmd;                                    \
107                                                         \
108     cmd = silc_command_get(payload);                    \
109     for (r = silc_command_pending; r; r = r->next) {    \
110       if (r->reply_cmd == cmd) {                        \
111         ctx->context = r->context;                      \
112         ctx->callback = r->callback;                    \
113         break;                                          \
114       }                                                 \
115     }                                                   \
116   }                                                     \
117 } while(0)
118
119 /* Executed pending command */
120 #define SILC_CLIENT_COMMAND_EXEC_PENDING(ctx, cmd)      \
121 do {                                                    \
122   if (ctx->callback) {                                  \
123     (*ctx->callback)(ctx->context);                     \
124     silc_client_command_pending_del((cmd));             \
125   }                                                     \
126 } while(0)
127
128 /* Prototypes */
129 void silc_client_command_pending(SilcCommand reply_cmd,
130                                  SilcClientCommandCallback callback,
131                                  void *context);
132 void silc_client_command_pending_del(SilcCommand reply_cmd);
133 SILC_CLIENT_CMD_FUNC(whois);
134 SILC_CLIENT_CMD_FUNC(whowas);
135 SILC_CLIENT_CMD_FUNC(identify);
136 SILC_CLIENT_CMD_FUNC(nick);
137 SILC_CLIENT_CMD_FUNC(server);
138 SILC_CLIENT_CMD_FUNC(list);
139 SILC_CLIENT_CMD_FUNC(topic);
140 SILC_CLIENT_CMD_FUNC(invite);
141 SILC_CLIENT_CMD_FUNC(quit);
142 SILC_CLIENT_CMD_FUNC(kill);
143 SILC_CLIENT_CMD_FUNC(info);
144 SILC_CLIENT_CMD_FUNC(connect);
145 SILC_CLIENT_CMD_FUNC(ping);
146 SILC_CLIENT_CMD_FUNC(oper);
147 SILC_CLIENT_CMD_FUNC(join);
148 SILC_CLIENT_CMD_FUNC(motd);
149 SILC_CLIENT_CMD_FUNC(umode);
150 SILC_CLIENT_CMD_FUNC(cmode);
151 SILC_CLIENT_CMD_FUNC(kick);
152 SILC_CLIENT_CMD_FUNC(restart);
153 SILC_CLIENT_CMD_FUNC(close);
154 SILC_CLIENT_CMD_FUNC(die);
155 SILC_CLIENT_CMD_FUNC(silcoper);
156 SILC_CLIENT_CMD_FUNC(leave);
157 SILC_CLIENT_CMD_FUNC(names);
158 SILC_CLIENT_CMD_FUNC(help);
159 SILC_CLIENT_CMD_FUNC(clear);
160 SILC_CLIENT_CMD_FUNC(version);
161 SILC_CLIENT_CMD_FUNC(msg);
162 SILC_CLIENT_CMD_FUNC(away);
163
164 #endif