CONNECT, CLOSE and SHUTDOWN commands.
[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 /* 
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 /* Context sent as argument to all commands */
63 typedef struct {
64   SilcClient client;
65   SilcClientConnection conn;
66   SilcClientCommand *command;
67   unsigned int argc;
68   unsigned char **argv;
69   unsigned int *argv_lens;
70   unsigned int *argv_types;
71   int pending;                  /* Command is being re-processed when TRUE */
72   int users;                    /* Reference counter */
73 } *SilcClientCommandContext;
74
75 /* Pending Command callback destructor. This is called after calling the
76    pending callback or if error occurs while processing the pending command.
77    If error occurs then the callback won't be called at all, and only this
78    destructor is called. The `context' is the context given for the function
79    silc_client_command_pending. */
80 typedef void (*SilcClientPendingDestructor)(void *context);
81
82 /* Structure holding pending commands. If command is pending it will be
83    executed after command reply has been executed. */
84 typedef struct SilcClientCommandPendingStruct {
85   SilcCommand reply_cmd;
86   SilcCommandCb callback;
87   SilcClientPendingDestructor destructor;
88   void *context;
89   unsigned short ident;
90   struct SilcClientCommandPendingStruct *next;
91 } SilcClientCommandPending;
92
93 /* List of pending commands */
94 extern SilcClientCommandPending *silc_command_pending;
95
96 #include "command_reply.h"
97
98 /* Macros */
99
100 /* Macro used for command declaration in command list structure */
101 #define SILC_CLIENT_CMD(func, cmd, name, flags, args) \
102 { silc_client_command_##func, SILC_COMMAND_##cmd, name, flags, args }
103
104 /* Macro used to declare command functions */
105 #define SILC_CLIENT_CMD_FUNC(func) \
106 void silc_client_command_##func(void *context)
107
108 /* Executed pending command callback */
109 #define SILC_CLIENT_PENDING_EXEC(ctx, cmd)      \
110 do {                                            \
111   if ((ctx)->callback)                          \
112     (*ctx->callback)(ctx->context);             \
113 } while(0)
114
115 /* Execute destructor for pending command */
116 #define SILC_CLIENT_PENDING_DESTRUCTOR(ctx, cmd)                        \
117 do {                                                                    \
118   silc_client_command_pending_del((ctx)->sock->user_data, (cmd),        \
119                                   (ctx)->ident);                        \
120   if (ctx->destructor)                                                  \
121     (*ctx->destructor)(ctx->context);                                   \
122 } while(0)
123
124 /* Prototypes */
125 void silc_client_send_command(SilcClient client, SilcClientConnection conn,
126                               SilcCommand command, unsigned short ident,
127                               unsigned int argc, ...);
128 SilcClientCommand *silc_client_command_find(const char *name);
129 SilcClientCommandContext silc_client_command_alloc();
130 void silc_client_command_free(SilcClientCommandContext ctx);
131 SilcClientCommandContext 
132 silc_client_command_dup(SilcClientCommandContext ctx);
133 void silc_client_command_pending(SilcClientConnection conn,
134                                  SilcCommand reply_cmd,
135                                  unsigned short ident,
136                                  SilcClientPendingDestructor destructor,
137                                  SilcCommandCb callback,
138                                  void *context);
139 void silc_client_command_pending_del(SilcClientConnection conn,
140                                      SilcCommand reply_cmd,
141                                      unsigned short ident);
142 int silc_client_command_pending_check(SilcClientConnection conn,
143                                       SilcClientCommandReplyContext ctx,
144                                       SilcCommand command, 
145                                       unsigned short ident);
146 SILC_CLIENT_CMD_FUNC(whois);
147 SILC_CLIENT_CMD_FUNC(whowas);
148 SILC_CLIENT_CMD_FUNC(identify);
149 SILC_CLIENT_CMD_FUNC(nick);
150 SILC_CLIENT_CMD_FUNC(list);
151 SILC_CLIENT_CMD_FUNC(topic);
152 SILC_CLIENT_CMD_FUNC(invite);
153 SILC_CLIENT_CMD_FUNC(quit);
154 SILC_CLIENT_CMD_FUNC(kill);
155 SILC_CLIENT_CMD_FUNC(info);
156 SILC_CLIENT_CMD_FUNC(connect);
157 SILC_CLIENT_CMD_FUNC(ping);
158 SILC_CLIENT_CMD_FUNC(oper);
159 SILC_CLIENT_CMD_FUNC(join);
160 SILC_CLIENT_CMD_FUNC(motd);
161 SILC_CLIENT_CMD_FUNC(umode);
162 SILC_CLIENT_CMD_FUNC(cmode);
163 SILC_CLIENT_CMD_FUNC(cumode);
164 SILC_CLIENT_CMD_FUNC(kick);
165 SILC_CLIENT_CMD_FUNC(restart);
166 SILC_CLIENT_CMD_FUNC(close);
167 SILC_CLIENT_CMD_FUNC(shutdown);
168 SILC_CLIENT_CMD_FUNC(silcoper);
169 SILC_CLIENT_CMD_FUNC(leave);
170 SILC_CLIENT_CMD_FUNC(users);
171
172 #endif