updates.
[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 - 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 #include "command_reply.h"
25
26 /* 
27    Structure holding one command and pointer to its function. 
28
29    SilcCommandCb cb
30
31        Callback function called when this command is executed.
32
33    SilcCommand cmd
34
35        The actual command. These are defined in silccore/silccommand.h
36
37    char *name
38
39        Logical name of the command. This is the visible command name
40        that user uses when calling command. Eg. NICK.
41
42    SilcCommandFlag flags
43
44        Flags for the command. These set how command behaves on different
45        situations. Server sets these flags as well, but to be sure
46        that our client never sends wrong commands we preserve the
47        flags on client side as well.
48
49        XXX: We preserve these so that we define them but currently we
50        don't check the flags at all.
51
52 */
53 typedef struct {
54   SilcCommandCb cb;
55   SilcCommand cmd;
56   char *name;
57   SilcCommandFlag flags;
58   unsigned int max_args;
59 } SilcClientCommand;
60
61 /* All client commands */
62 extern SilcClientCommand silc_command_list[];
63
64 /* Context sent as argument to all commands */
65 typedef struct {
66   SilcClient client;
67   SilcClientConnection conn;
68   SilcClientCommand *command;
69   unsigned int argc;
70   unsigned char **argv;
71   unsigned int *argv_lens;
72   unsigned int *argv_types;
73 } *SilcClientCommandContext;
74
75 /* Structure holding pending commands. If command is pending it will be
76    executed after command reply has been executed. */
77 /* XXX This support may added for commands as well and not just command
78    replies, if needed later. */
79 typedef struct SilcClientCommandPendingStruct {
80   SilcCommand reply_cmd;
81   SilcCommandCb callback;
82   void *context;
83   unsigned short ident;
84   struct SilcClientCommandPendingStruct *next;
85 } SilcClientCommandPending;
86
87 /* List of pending commands */
88 extern SilcClientCommandPending *silc_command_pending;
89
90 /* Macros */
91
92 /* Macro used for command declaration in command list structure */
93 #define SILC_CLIENT_CMD(func, cmd, name, flags, args) \
94 { silc_client_command_##func, SILC_COMMAND_##cmd, name, flags, args }
95
96 /* Macro used to declare command functions */
97 #define SILC_CLIENT_CMD_FUNC(func) \
98 void silc_client_command_##func(void *context)
99
100 /* Executed pending command callback */
101 #define SILC_CLIENT_COMMAND_EXEC_PENDING(ctx, cmd)                      \
102 do {                                                                    \
103   if ((ctx)->callback) {                                                \
104     (*ctx->callback)(ctx->context);                                     \
105     silc_client_command_pending_del((ctx)->sock->user_data, (cmd),      \
106                                     (ctx)->ident);                      \
107   }                                                                     \
108 } while(0)
109
110 /* Prototypes */
111 void silc_client_command_free(SilcClientCommandContext cmd);
112 void silc_client_send_command(SilcClient client, SilcClientConnection conn,
113                               SilcCommand command, unsigned int argc, ...);
114 SilcClientCommand *silc_client_command_find(const char *name);
115 void silc_client_command_pending(SilcClientConnection conn,
116                                  SilcCommand reply_cmd,
117                                  unsigned short ident,
118                                  SilcCommandCb callback,
119                                  void *context);
120 void silc_client_command_pending_del(SilcClientConnection conn,
121                                      SilcCommand reply_cmd,
122                                      unsigned short ident);
123 int silc_client_command_pending_check(SilcClientConnection conn,
124                                       SilcClientCommandReplyContext ctx,
125                                       SilcCommand command, 
126                                       unsigned short ident);
127 SILC_CLIENT_CMD_FUNC(whois);
128 SILC_CLIENT_CMD_FUNC(whowas);
129 SILC_CLIENT_CMD_FUNC(identify);
130 SILC_CLIENT_CMD_FUNC(nick);
131 SILC_CLIENT_CMD_FUNC(list);
132 SILC_CLIENT_CMD_FUNC(topic);
133 SILC_CLIENT_CMD_FUNC(invite);
134 SILC_CLIENT_CMD_FUNC(quit);
135 SILC_CLIENT_CMD_FUNC(kill);
136 SILC_CLIENT_CMD_FUNC(info);
137 SILC_CLIENT_CMD_FUNC(connect);
138 SILC_CLIENT_CMD_FUNC(ping);
139 SILC_CLIENT_CMD_FUNC(oper);
140 SILC_CLIENT_CMD_FUNC(join);
141 SILC_CLIENT_CMD_FUNC(motd);
142 SILC_CLIENT_CMD_FUNC(umode);
143 SILC_CLIENT_CMD_FUNC(cmode);
144 SILC_CLIENT_CMD_FUNC(cumode);
145 SILC_CLIENT_CMD_FUNC(kick);
146 SILC_CLIENT_CMD_FUNC(restart);
147 SILC_CLIENT_CMD_FUNC(close);
148 SILC_CLIENT_CMD_FUNC(die);
149 SILC_CLIENT_CMD_FUNC(silcoper);
150 SILC_CLIENT_CMD_FUNC(leave);
151 SILC_CLIENT_CMD_FUNC(users);
152
153 #endif