Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcclient / command_reply.h
1 /*
2
3   command_reply.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2003 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef COMMAND_REPLY_H
21 #define COMMAND_REPLY_H
22
23 /* Structure holding one command reply and pointer to its function. */
24 typedef struct {
25   SilcCommandCb cb;
26   SilcCommand cmd;
27 } SilcClientCommandReply;
28
29 /* Context holding pending command callbacks. */
30 typedef struct {
31   SilcCommandCb callback;
32   void *context;
33 } *SilcClientCommandPendingCallbacks;
34
35 /* Context sent as argument to all command reply functions */
36 struct SilcClientCommandReplyContextStruct {
37   SilcClient client;
38   SilcSocketConnection sock;
39   SilcCommandPayload payload;
40   SilcStatus status;
41   SilcStatus error;
42   SilcArgumentPayload args;
43   SilcPacketContext *packet;
44
45   /* If defined this executes the pending command. */
46   SilcClientCommandPendingCallbacks callbacks;
47   SilcUInt32 callbacks_count;
48   SilcUInt16 ident;
49   SilcUInt8 users;
50 };
51
52 /* Macros */
53
54 /* Command reply operation that is called at the end of all command replys.
55    Usage: COMMAND_REPLY((ARGS, argument1, argument2, etc...)), */
56 #define COMMAND_REPLY(args) cmd->client->internal->ops->command_reply args
57 #define SILC_ARGS cmd->client, cmd->sock->user_data,                         \
58              cmd->payload, TRUE, silc_command_get(cmd->payload), cmd->status
59
60 /* Error reply to application. Usage: COMMAND_REPLY_ERROR; */
61 #define COMMAND_REPLY_ERROR(error)                                      \
62 do {                                                                    \
63   if (cmd->status == SILC_STATUS_OK) {                                  \
64     cmd->client->internal->ops->                                        \
65       command_reply(cmd->client, cmd->sock->user_data, cmd->payload,    \
66                     FALSE, silc_command_get(cmd->payload), error);      \
67   } else {                                                              \
68     void *arg1 = NULL, *arg2 = NULL;                                    \
69     silc_status_get_args(cmd->status, cmd->args, &arg1, &arg2);         \
70     cmd->client->internal->ops->                                        \
71       command_reply(cmd->client, cmd->sock->user_data, cmd->payload,    \
72                     FALSE, silc_command_get(cmd->payload), cmd->status, \
73                     arg1, arg2);                                        \
74     silc_free(arg1);                                                    \
75     silc_free(arg2);                                                    \
76   }                                                                     \
77 } while(0)
78
79 /* Macro used to declare command reply functions */
80 #define SILC_CLIENT_CMD_REPLY_FUNC(func)                                \
81 void silc_client_command_reply_##func(void *context, void *context2)
82
83 /* Prototypes */
84
85 void silc_client_command_reply_process(SilcClient client,
86                                        SilcSocketConnection sock,
87                                        SilcPacketContext *packet);
88 void silc_client_command_reply_free(SilcClientCommandReplyContext cmd);
89 SILC_CLIENT_CMD_REPLY_FUNC(whois);
90 SILC_CLIENT_CMD_REPLY_FUNC(whowas);
91 SILC_CLIENT_CMD_REPLY_FUNC(identify);
92 SILC_CLIENT_CMD_REPLY_FUNC(nick);
93 SILC_CLIENT_CMD_REPLY_FUNC(list);
94 SILC_CLIENT_CMD_REPLY_FUNC(topic);
95 SILC_CLIENT_CMD_REPLY_FUNC(invite);
96 SILC_CLIENT_CMD_REPLY_FUNC(kill);
97 SILC_CLIENT_CMD_REPLY_FUNC(info);
98 SILC_CLIENT_CMD_REPLY_FUNC(stats);
99 SILC_CLIENT_CMD_REPLY_FUNC(ping);
100 SILC_CLIENT_CMD_REPLY_FUNC(oper);
101 SILC_CLIENT_CMD_REPLY_FUNC(join);
102 SILC_CLIENT_CMD_REPLY_FUNC(motd);
103 SILC_CLIENT_CMD_REPLY_FUNC(umode);
104 SILC_CLIENT_CMD_REPLY_FUNC(cmode);
105 SILC_CLIENT_CMD_REPLY_FUNC(cumode);
106 SILC_CLIENT_CMD_REPLY_FUNC(kick);
107 SILC_CLIENT_CMD_REPLY_FUNC(ban);
108 SILC_CLIENT_CMD_REPLY_FUNC(detach);
109 SILC_CLIENT_CMD_REPLY_FUNC(watch);
110 SILC_CLIENT_CMD_REPLY_FUNC(silcoper);
111 SILC_CLIENT_CMD_REPLY_FUNC(leave);
112 SILC_CLIENT_CMD_REPLY_FUNC(users);
113 SILC_CLIENT_CMD_REPLY_FUNC(getkey);
114 SILC_CLIENT_CMD_REPLY_FUNC(quit);
115
116 /* Internal command reply functions */
117 SILC_CLIENT_CMD_REPLY_FUNC(whois_i);
118 SILC_CLIENT_CMD_REPLY_FUNC(identify_i);
119 SILC_CLIENT_CMD_REPLY_FUNC(info_i);
120 SILC_CLIENT_CMD_REPLY_FUNC(users_i);
121
122 SILC_CLIENT_CMD_REPLY_FUNC(connect);
123 SILC_CLIENT_CMD_REPLY_FUNC(close);
124 SILC_CLIENT_CMD_REPLY_FUNC(shutdown);
125
126 #endif