Initial revision
[silc.git] / apps / silcd / command_reply.c
1 /*
2
3   command_reply.c
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  * $Id$
22  * $Log$
23  * Revision 1.1  2000/06/27 11:36:56  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #include "serverincludes.h"
30 #include "server_internal.h"
31 #include "command_reply.h"
32
33 /* Server command reply list. Not all commands have reply function as
34    they are never sent as forwarded command packets by server. More
35    maybe added later if need appears. */
36 SilcServerCommandReply silc_command_reply_list[] =
37 {
38   SILC_SERVER_CMD_REPLY(join, JOIN),
39
40   { NULL, 0 },
41 };
42
43 /* Process received command reply. */
44
45 void silc_server_command_reply_process(SilcServer server,
46                                        SilcSocketConnection sock,
47                                        SilcBuffer buffer)
48 {
49   SilcServerCommandReplyContext ctx;
50   SilcCommandPayload payload;
51
52   /* Get command reply payload from packet */
53   payload = silc_command_parse_payload(buffer);
54   if (!payload) {
55     /* Silently ignore bad reply packet */
56     SILC_LOG_DEBUG(("Bad command reply packet"));
57     return;
58   }
59   
60   /* Allocate command reply context. This must be free'd by the
61      command reply routine receiving it. */
62   ctx = silc_calloc(1, sizeof(*ctx));
63   ctx->server = server;
64   ctx->sock = sock;
65   ctx->payload = payload;
66       
67   /* Check for pending commands and mark to be exeucted */
68   SILC_SERVER_COMMAND_CHECK_PENDING(ctx);
69   
70   /* Execute command reply */
71   SILC_SERVER_COMMAND_REPLY_EXEC(ctx);
72 }
73
74 /* Free command reply context and its internals. */
75
76 void silc_server_command_reply_free(SilcServerCommandReplyContext cmd)
77 {
78   if (cmd) {
79     silc_command_free_payload(cmd->payload);
80     silc_free(cmd);
81   }
82 }
83
84 /* Received reply for forwarded JOIN command. Router has created or joined
85    the client to the channel. We save some channel information locally
86    for future use. */
87
88 SILC_SERVER_CMD_REPLY_FUNC(join)
89 {
90   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
91   SilcServer server = cmd->server;
92   SilcCommandStatus status;
93   SilcChannelID *id;
94   SilcChannelList *entry;
95   unsigned int argc;
96   unsigned char *id_string;
97   char *channel_name, *tmp;
98
99 #define LCC(x) server->local_list->channel_cache[(x) - 32]
100 #define LCCC(x) server->local_list->channel_cache_count[(x) - 32]
101
102   SILC_LOG_DEBUG(("Start"));
103
104   tmp = silc_command_get_arg_type(cmd->payload, 1, NULL);
105   SILC_GET16_MSB(status, tmp);
106   if (status != SILC_STATUS_OK)
107     goto out;
108
109   /* Get channel name */
110   tmp = silc_command_get_arg_type(cmd->payload, 2, NULL);
111   if (!tmp)
112     goto out;
113
114   /* Get channel ID */
115   id_string = silc_command_get_arg_type(cmd->payload, 3, NULL);
116   if (!id_string)
117     goto out;
118
119   channel_name = strdup(tmp);
120
121   /* Add the channel to our local list. */
122   id = silc_id_str2id(id_string, SILC_ID_CHANNEL);
123   silc_idlist_add_channel(&server->local_list->channels, channel_name, 
124                           SILC_CHANNEL_MODE_NONE, id, 
125                           server->id_entry->router, NULL, &entry);
126   LCCC(channel_name[0]) = silc_idcache_add(&LCC(channel_name[0]), 
127                                            LCCC(channel_name[0]),
128                                            channel_name, SILC_ID_CHANNEL, 
129                                            (void *)id, (void *)entry);
130   entry->global_users = TRUE;
131
132   /* Execute pending JOIN command so that the client who originally
133      wanted to join the channel will be joined after all. */
134   SILC_SERVER_COMMAND_EXEC_PENDING(cmd, SILC_COMMAND_JOIN);
135
136  out:
137   silc_server_command_reply_free(cmd);
138 #undef LCC
139 #undef LCCC
140 }