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