Created SILC Client Libary by moving stuff from silc/ directory.
[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 /* 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   SilcClientConnection conn;
70   SilcClientCommand *command;
71   unsigned int argc;
72   unsigned char **argv;
73   unsigned int *argv_lens;
74   unsigned int *argv_types;
75 } *SilcClientCommandContext;
76
77 /* Structure holding pending commands. If command is pending it will be
78    executed after command reply has been executed. */
79 /* XXX This support may added for commands as well and not just command
80    replies, if needed later. */
81 typedef struct SilcClientCommandPendingStruct {
82   SilcCommand reply_cmd;
83   void *context;
84   SilcClientCommandCallback callback;
85
86   struct SilcClientCommandPendingStruct *next;
87 } SilcClientCommandPending;
88
89 /* List of pending commands */
90 extern SilcClientCommandPending *silc_command_pending;
91
92 /* Macros */
93
94 /* Macro used for command declaration in command list structure */
95 #define SILC_CLIENT_CMD(func, cmd, name, flags, args) \
96 { silc_client_command_##func, SILC_COMMAND_##cmd, name, flags, args }
97
98 /* Macro used to declare command functions */
99 #define SILC_CLIENT_CMD_FUNC(func) \
100 void silc_client_command_##func(void *context)
101
102 /* Checks for pending commands */
103 #define SILC_CLIENT_COMMAND_CHECK_PENDING(ctx)          \
104 do {                                                    \
105   if (silc_command_pending) {                           \
106     SilcClientCommandPending *r;                        \
107     SilcCommand cmd;                                    \
108                                                         \
109     cmd = silc_command_get(payload);                    \
110     for (r = silc_command_pending; r; r = r->next) {    \
111       if (r->reply_cmd == cmd) {                        \
112         ctx->context = r->context;                      \
113         ctx->callback = r->callback;                    \
114         break;                                          \
115       }                                                 \
116     }                                                   \
117   }                                                     \
118 } while(0)
119
120 /* Executed pending command */
121 #define SILC_CLIENT_COMMAND_EXEC_PENDING(ctx, cmd)      \
122 do {                                                    \
123   if (ctx->callback) {                                  \
124     (*ctx->callback)(ctx->context);                     \
125     silc_client_command_pending_del((cmd));             \
126   }                                                     \
127 } while(0)
128
129 /* Prototypes */
130 void silc_client_command_free(SilcClientCommandContext cmd);
131 SilcClientCommand *silc_client_command_find(const char *name);
132 void silc_client_command_pending(SilcCommand reply_cmd,
133                                  SilcClientCommandCallback callback,
134                                  void *context);
135 void silc_client_command_pending_del(SilcCommand reply_cmd);
136 SILC_CLIENT_CMD_FUNC(whois);
137 SILC_CLIENT_CMD_FUNC(whowas);
138 SILC_CLIENT_CMD_FUNC(identify);
139 SILC_CLIENT_CMD_FUNC(nick);
140 SILC_CLIENT_CMD_FUNC(list);
141 SILC_CLIENT_CMD_FUNC(topic);
142 SILC_CLIENT_CMD_FUNC(invite);
143 SILC_CLIENT_CMD_FUNC(quit);
144 SILC_CLIENT_CMD_FUNC(kill);
145 SILC_CLIENT_CMD_FUNC(info);
146 SILC_CLIENT_CMD_FUNC(connect);
147 SILC_CLIENT_CMD_FUNC(ping);
148 SILC_CLIENT_CMD_FUNC(oper);
149 SILC_CLIENT_CMD_FUNC(join);
150 SILC_CLIENT_CMD_FUNC(motd);
151 SILC_CLIENT_CMD_FUNC(umode);
152 SILC_CLIENT_CMD_FUNC(cmode);
153 SILC_CLIENT_CMD_FUNC(kick);
154 SILC_CLIENT_CMD_FUNC(restart);
155 SILC_CLIENT_CMD_FUNC(close);
156 SILC_CLIENT_CMD_FUNC(die);
157 SILC_CLIENT_CMD_FUNC(silcoper);
158 SILC_CLIENT_CMD_FUNC(leave);
159 SILC_CLIENT_CMD_FUNC(names);
160
161 #endif