79b4213028c4fdc904313209f082405ef5d28d4b
[silc.git] / lib / silcclient / command.h
1 /*
2
3   command.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. 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 (actually we preserve them but
46        ignore them :)).
47
48 */
49 typedef struct {
50   SilcCommandCb cb;
51   SilcCommand cmd;
52   char *name;
53   SilcCommandFlag flags;
54   uint32 max_args;
55 } SilcClientCommand;
56
57 /* All client commands */
58 extern SilcClientCommand silc_command_list[];
59
60 /* Context sent as argument to all commands. This is used by the library
61    and application should use this as well. However, application may
62    choose to use some own context for its own local command. All library
63    commands, however, must use this context. */
64 typedef struct {
65   SilcClient client;
66   SilcClientConnection conn;
67   SilcClientCommand *command;
68   uint32 argc;
69   unsigned char **argv;
70   uint32 *argv_lens;
71   uint32 *argv_types;
72   int pending;                  /* Command is being re-processed when TRUE */
73   int users;                    /* Reference counter */
74 } *SilcClientCommandContext;
75
76 #include "silcapi.h"
77
78 /* Structure holding pending commands. If command is pending it will be
79    executed after command reply has been executed. */
80 typedef struct SilcClientCommandPendingStruct {
81   SilcCommand reply_cmd;
82   SilcCommandCb callback;
83   SilcClientPendingDestructor destructor;
84   void *context;
85   uint16 ident;
86   struct SilcClientCommandPendingStruct *next;
87 } SilcClientCommandPending;
88
89 /* List of pending commands */
90 extern SilcClientCommandPending *silc_command_pending;
91
92 #include "command_reply.h"
93
94 /* Macros */
95
96 /* Macro used for command declaration in command list structure */
97 #define SILC_CLIENT_CMD(func, cmd, name, flags, args) \
98 { silc_client_command_##func, SILC_COMMAND_##cmd, name, flags, args }
99
100 /* Macro used to declare command functions */
101 #define SILC_CLIENT_CMD_FUNC(func) \
102 void silc_client_command_##func(void *context, void *context2)
103
104 /* Executed pending command callback */
105 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd)      \
106 do {                                            \
107   if ((ctx)->callback)                          \
108     (*ctx->callback)(ctx->context, ctx);        \
109 } while(0)
110
111 /* Execute destructor for pending command */
112 #define SILC_CLIENT_PENDING_DESTRUCTOR(ctx, cmd)                        \
113 do {                                                                    \
114   silc_client_command_pending_del((ctx)->sock->user_data, (cmd),        \
115                                   (ctx)->ident);                        \
116   if (ctx->destructor)                                                  \
117     (*ctx->destructor)(ctx->context);                                   \
118 } while(0)
119
120 /* Prototypes (some prototypes are in the silcapi.h file) */
121 void silc_client_command_pending_del(SilcClientConnection conn,
122                                      SilcCommand reply_cmd,
123                                      uint16 ident);
124 int silc_client_command_pending_check(SilcClientConnection conn,
125                                       SilcClientCommandReplyContext ctx,
126                                       SilcCommand command, 
127                                       uint16 ident);
128
129 SILC_CLIENT_CMD_FUNC(whois);
130 SILC_CLIENT_CMD_FUNC(whowas);
131 SILC_CLIENT_CMD_FUNC(identify);
132 SILC_CLIENT_CMD_FUNC(nick);
133 SILC_CLIENT_CMD_FUNC(list);
134 SILC_CLIENT_CMD_FUNC(topic);
135 SILC_CLIENT_CMD_FUNC(invite);
136 SILC_CLIENT_CMD_FUNC(quit);
137 SILC_CLIENT_CMD_FUNC(kill);
138 SILC_CLIENT_CMD_FUNC(info);
139 SILC_CLIENT_CMD_FUNC(connect);
140 SILC_CLIENT_CMD_FUNC(ping);
141 SILC_CLIENT_CMD_FUNC(oper);
142 SILC_CLIENT_CMD_FUNC(join);
143 SILC_CLIENT_CMD_FUNC(motd);
144 SILC_CLIENT_CMD_FUNC(umode);
145 SILC_CLIENT_CMD_FUNC(cmode);
146 SILC_CLIENT_CMD_FUNC(cumode);
147 SILC_CLIENT_CMD_FUNC(kick);
148 SILC_CLIENT_CMD_FUNC(ban);
149 SILC_CLIENT_CMD_FUNC(close);
150 SILC_CLIENT_CMD_FUNC(shutdown);
151 SILC_CLIENT_CMD_FUNC(silcoper);
152 SILC_CLIENT_CMD_FUNC(leave);
153 SILC_CLIENT_CMD_FUNC(users);
154 SILC_CLIENT_CMD_FUNC(getkey);
155
156 #endif