Blame


1 2ee05c9a 2002-03-03 alex /*
2 2ee05c9a 2002-03-03 alex * ngIRCd -- The Next Generation IRC Daemon
3 ec0b405d 2008-09-23 alex * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4 2ee05c9a 2002-03-03 alex *
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 2ee05c9a 2002-03-03 alex *
11 490f28ff 2002-12-12 alex * IRC channel commands
12 2ee05c9a 2002-03-03 alex */
13 2ee05c9a 2002-03-03 alex
14 2ee05c9a 2002-03-03 alex
15 ca33cbda 2002-03-12 alex #include "portab.h"
16 490f28ff 2002-12-12 alex
17 ca33cbda 2002-03-12 alex #include "imp.h"
18 2ee05c9a 2002-03-03 alex #include <assert.h>
19 43d9a624 2003-01-08 alex #include <stdlib.h>
20 43d9a624 2003-01-08 alex #include <stdio.h>
21 2ee05c9a 2002-03-03 alex #include <string.h>
22 2ee05c9a 2002-03-03 alex
23 ca33cbda 2002-03-12 alex #include "defines.h"
24 c2f60abe 2002-05-27 alex #include "conn.h"
25 c2f60abe 2002-05-27 alex #include "client.h"
26 c2f60abe 2002-05-27 alex #include "channel.h"
27 956bbe2c 2008-05-24 alex #include "conn-func.h"
28 c2f60abe 2002-05-27 alex #include "lists.h"
29 2ee05c9a 2002-03-03 alex #include "log.h"
30 39b9f65d 2002-06-26 alex #include "match.h"
31 2ee05c9a 2002-03-03 alex #include "messages.h"
32 c2f60abe 2002-05-27 alex #include "parse.h"
33 0c471b84 2002-11-30 alex #include "irc-info.h"
34 c2f60abe 2002-05-27 alex #include "irc-write.h"
35 5b8b3b83 2002-12-13 alex #include "resolve.h"
36 5b8b3b83 2002-12-13 alex #include "conf.h"
37 2ee05c9a 2002-03-03 alex
38 ca33cbda 2002-03-12 alex #include "exp.h"
39 2ee05c9a 2002-03-03 alex #include "irc-channel.h"
40 6bd35bf0 2008-02-26 fw
41 6bd35bf0 2008-02-26 fw
42 6bd35bf0 2008-02-26 fw /*
43 6bd35bf0 2008-02-26 fw * RFC 2812, (3.2.1 Join message Command):
44 6bd35bf0 2008-02-26 fw * Note that this message
45 6bd35bf0 2008-02-26 fw * accepts a special argument ("0"), which is a special request to leave all
46 6bd35bf0 2008-02-26 fw * channels the user is currently a member of. The server will process this
47 6bd35bf0 2008-02-26 fw * message as if the user had sent a PART command (See Section 3.2.2) for
48 6bd35bf0 2008-02-26 fw * each channel he is a member of.
49 6bd35bf0 2008-02-26 fw */
50 6bd35bf0 2008-02-26 fw static bool
51 6bd35bf0 2008-02-26 fw part_from_all_channels(CLIENT* client, CLIENT *target)
52 6bd35bf0 2008-02-26 fw {
53 c6343037 2008-02-26 fw CL2CHAN *cl2chan;
54 6bd35bf0 2008-02-26 fw CHANNEL *chan;
55 6bd35bf0 2008-02-26 fw
56 c6343037 2008-02-26 fw while ((cl2chan = Channel_FirstChannelOf(target))) {
57 6bd35bf0 2008-02-26 fw chan = Channel_GetChannel(cl2chan);
58 6bd35bf0 2008-02-26 fw assert( chan != NULL );
59 6bd35bf0 2008-02-26 fw Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
60 6bd35bf0 2008-02-26 fw }
61 6bd35bf0 2008-02-26 fw return CONNECTED;
62 6bd35bf0 2008-02-26 fw }
63 2ee05c9a 2002-03-03 alex
64 2ee05c9a 2002-03-03 alex
65 634ef8c1 2008-02-26 fw static bool
66 634ef8c1 2008-02-26 fw join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
67 634ef8c1 2008-02-26 fw const char *channame, const char *key)
68 634ef8c1 2008-02-26 fw {
69 634ef8c1 2008-02-26 fw bool is_invited, is_banned;
70 634ef8c1 2008-02-26 fw const char *channel_modes;
71 83bfdddf 2008-05-05 alex
72 83bfdddf 2008-05-05 alex /* Allow IRC operators to overwrite channel limits */
73 83bfdddf 2008-05-05 alex if (strchr(Client_Modes(Client), 'o'))
74 83bfdddf 2008-05-05 alex return true;
75 634ef8c1 2008-02-26 fw
76 634ef8c1 2008-02-26 fw is_banned = Lists_Check(Channel_GetListBans(chan), target);
77 634ef8c1 2008-02-26 fw is_invited = Lists_Check(Channel_GetListInvites(chan), target);
78 634ef8c1 2008-02-26 fw
79 634ef8c1 2008-02-26 fw if (is_banned && !is_invited) {
80 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
81 634ef8c1 2008-02-26 fw return false;
82 634ef8c1 2008-02-26 fw }
83 634ef8c1 2008-02-26 fw
84 634ef8c1 2008-02-26 fw channel_modes = Channel_Modes(chan);
85 634ef8c1 2008-02-26 fw if ((strchr(channel_modes, 'i')) && !is_invited) {
86 634ef8c1 2008-02-26 fw /* Channel is "invite-only" (and Client wasn't invited) */
87 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
88 634ef8c1 2008-02-26 fw return false;
89 634ef8c1 2008-02-26 fw }
90 634ef8c1 2008-02-26 fw
91 634ef8c1 2008-02-26 fw /* Is the channel protected by a key? */
92 634ef8c1 2008-02-26 fw if (strchr(channel_modes, 'k') &&
93 634ef8c1 2008-02-26 fw strcmp(Channel_Key(chan), key ? key : ""))
94 634ef8c1 2008-02-26 fw {
95 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
96 634ef8c1 2008-02-26 fw return false;
97 634ef8c1 2008-02-26 fw }
98 634ef8c1 2008-02-26 fw /* Are there already too many members? */
99 634ef8c1 2008-02-26 fw if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
100 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
101 634ef8c1 2008-02-26 fw return false;
102 634ef8c1 2008-02-26 fw }
103 634ef8c1 2008-02-26 fw return true;
104 634ef8c1 2008-02-26 fw }
105 634ef8c1 2008-02-26 fw
106 634ef8c1 2008-02-26 fw
107 634ef8c1 2008-02-26 fw static void
108 634ef8c1 2008-02-26 fw join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
109 634ef8c1 2008-02-26 fw {
110 634ef8c1 2008-02-26 fw if (flags) {
111 634ef8c1 2008-02-26 fw while (*flags) {
112 634ef8c1 2008-02-26 fw Channel_UserModeAdd(chan, target, *flags);
113 634ef8c1 2008-02-26 fw flags++;
114 634ef8c1 2008-02-26 fw }
115 634ef8c1 2008-02-26 fw }
116 634ef8c1 2008-02-26 fw
117 634ef8c1 2008-02-26 fw /* If channel persistent and client is ircop: make client chanop */
118 634ef8c1 2008-02-26 fw if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
119 634ef8c1 2008-02-26 fw Channel_UserModeAdd(chan, target, 'o');
120 634ef8c1 2008-02-26 fw }
121 634ef8c1 2008-02-26 fw
122 634ef8c1 2008-02-26 fw
123 634ef8c1 2008-02-26 fw static void
124 ec0b405d 2008-09-23 alex cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
125 ec0b405d 2008-09-23 alex {
126 ec0b405d 2008-09-23 alex CONN_ID conn;
127 ec0b405d 2008-09-23 alex char str[COMMAND_LEN], *ptr = NULL;
128 ec0b405d 2008-09-23 alex
129 ec0b405d 2008-09-23 alex strlcpy(str, (char *)Data, sizeof(str));
130 ec0b405d 2008-09-23 alex conn = Client_Conn(To);
131 ec0b405d 2008-09-23 alex
132 ec0b405d 2008-09-23 alex if (Conn_Options(conn) & CONN_RFC1459) {
133 ec0b405d 2008-09-23 alex /* RFC 1459 compatibility mode, appended modes are NOT
134 ec0b405d 2008-09-23 alex * supported, so strip them off! */
135 ec0b405d 2008-09-23 alex ptr = strchr(str, 0x7);
136 ec0b405d 2008-09-23 alex if (ptr)
137 ec0b405d 2008-09-23 alex *ptr++ = '\0';
138 ec0b405d 2008-09-23 alex }
139 ec0b405d 2008-09-23 alex
140 ec0b405d 2008-09-23 alex IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
141 ec0b405d 2008-09-23 alex if (ptr && *ptr)
142 ec0b405d 2008-09-23 alex IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
143 ec0b405d 2008-09-23 alex Client_ID(Prefix));
144 ec0b405d 2008-09-23 alex } /* cb_join_forward */
145 ec0b405d 2008-09-23 alex
146 ec0b405d 2008-09-23 alex
147 ec0b405d 2008-09-23 alex static void
148 634ef8c1 2008-02-26 fw join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
149 634ef8c1 2008-02-26 fw const char *channame)
150 634ef8c1 2008-02-26 fw {
151 ec0b405d 2008-09-23 alex char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
152 634ef8c1 2008-02-26 fw
153 634ef8c1 2008-02-26 fw strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
154 634ef8c1 2008-02-26 fw if (modes[1])
155 634ef8c1 2008-02-26 fw modes[0] = 0x7;
156 634ef8c1 2008-02-26 fw else
157 634ef8c1 2008-02-26 fw modes[0] = '\0';
158 ec0b405d 2008-09-23 alex
159 634ef8c1 2008-02-26 fw /* forward to other servers */
160 ec0b405d 2008-09-23 alex snprintf(str, sizeof(str), "%s%s", channame, modes);
161 ec0b405d 2008-09-23 alex IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0', cb_join_forward, str);
162 634ef8c1 2008-02-26 fw
163 634ef8c1 2008-02-26 fw /* tell users in this channel about the new client */
164 634ef8c1 2008-02-26 fw IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
165 634ef8c1 2008-02-26 fw if (modes[1])
166 634ef8c1 2008-02-26 fw IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
167 634ef8c1 2008-02-26 fw channame, &modes[1], Client_ID(target));
168 634ef8c1 2008-02-26 fw }
169 634ef8c1 2008-02-26 fw
170 634ef8c1 2008-02-26 fw
171 634ef8c1 2008-02-26 fw static bool
172 634ef8c1 2008-02-26 fw join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
173 634ef8c1 2008-02-26 fw const char *channame)
174 634ef8c1 2008-02-26 fw {
175 634ef8c1 2008-02-26 fw const char *topic;
176 634ef8c1 2008-02-26 fw
177 634ef8c1 2008-02-26 fw if (Client_Type(Client) != CLIENT_USER)
178 634ef8c1 2008-02-26 fw return true;
179 634ef8c1 2008-02-26 fw /* acknowledge join */
180 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
181 634ef8c1 2008-02-26 fw return false;
182 634ef8c1 2008-02-26 fw
183 634ef8c1 2008-02-26 fw /* Send topic to client, if any */
184 634ef8c1 2008-02-26 fw topic = Channel_Topic(chan);
185 634ef8c1 2008-02-26 fw assert(topic != NULL);
186 634ef8c1 2008-02-26 fw if (*topic) {
187 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
188 634ef8c1 2008-02-26 fw Client_ID(Client), channame, topic))
189 634ef8c1 2008-02-26 fw return false;
190 634ef8c1 2008-02-26 fw #ifndef STRICT_RFC
191 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
192 634ef8c1 2008-02-26 fw Client_ID(Client), channame,
193 634ef8c1 2008-02-26 fw Channel_TopicWho(chan),
194 634ef8c1 2008-02-26 fw Channel_TopicTime(chan)))
195 634ef8c1 2008-02-26 fw return false;
196 634ef8c1 2008-02-26 fw #endif
197 634ef8c1 2008-02-26 fw }
198 634ef8c1 2008-02-26 fw /* send list of channel members to client */
199 634ef8c1 2008-02-26 fw if (!IRC_Send_NAMES(Client, chan))
200 634ef8c1 2008-02-26 fw return false;
201 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
202 634ef8c1 2008-02-26 fw }
203 634ef8c1 2008-02-26 fw
204 634ef8c1 2008-02-26 fw
205 8adff592 2005-03-19 fw GLOBAL bool
206 c2f60abe 2002-05-27 alex IRC_JOIN( CLIENT *Client, REQUEST *Req )
207 2ee05c9a 2002-03-03 alex {
208 258e39e8 2008-06-16 alex char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
209 2ee05c9a 2002-03-03 alex CLIENT *target;
210 2ee05c9a 2002-03-03 alex CHANNEL *chan;
211 6bd35bf0 2008-02-26 fw
212 2ee05c9a 2002-03-03 alex assert( Client != NULL );
213 2ee05c9a 2002-03-03 alex assert( Req != NULL );
214 2ee05c9a 2002-03-03 alex
215 69ad0e38 2002-12-16 alex /* Bad number of arguments? */
216 69081851 2007-07-31 alex if (Req->argc < 1 || Req->argc > 2)
217 69081851 2007-07-31 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
218 69081851 2007-07-31 alex Client_ID(Client), Req->command);
219 2ee05c9a 2002-03-03 alex
220 69ad0e38 2002-12-16 alex /* Who is the sender? */
221 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_SERVER)
222 634ef8c1 2008-02-26 fw target = Client_Search(Req->prefix);
223 634ef8c1 2008-02-26 fw else
224 634ef8c1 2008-02-26 fw target = Client;
225 2ee05c9a 2002-03-03 alex
226 634ef8c1 2008-02-26 fw if (!target)
227 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
228 634ef8c1 2008-02-26 fw
229 6bd35bf0 2008-02-26 fw /* Is argument "0"? */
230 6bd35bf0 2008-02-26 fw if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
231 6bd35bf0 2008-02-26 fw return part_from_all_channels(Client, target);
232 6bd35bf0 2008-02-26 fw
233 69ad0e38 2002-12-16 alex /* Are channel keys given? */
234 b90f71ca 2008-05-26 fw if (Req->argc > 1)
235 b90f71ca 2008-05-26 fw key = strtok_r(Req->argv[1], ",", &lastkey);
236 b90f71ca 2008-05-26 fw
237 e7087905 2005-09-02 alex channame = Req->argv[0];
238 b90f71ca 2008-05-26 fw channame = strtok_r(channame, ",", &lastchan);
239 e7087905 2005-09-02 alex
240 25814389 2008-07-22 alex /* Make sure that "channame" is not the empty string ("JOIN :") */
241 25814389 2008-07-22 alex if (! channame)
242 25814389 2008-07-22 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
243 25814389 2008-07-22 alex Client_ID(Client), Req->command);
244 25814389 2008-07-22 alex
245 634ef8c1 2008-02-26 fw while (channame) {
246 634ef8c1 2008-02-26 fw flags = NULL;
247 2ee05c9a 2002-03-03 alex
248 634ef8c1 2008-02-26 fw /* Did the server include channel-user-modes? */
249 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_SERVER) {
250 634ef8c1 2008-02-26 fw flags = strchr(channame, 0x7);
251 634ef8c1 2008-02-26 fw if (flags) {
252 e876e210 2002-06-10 alex *flags = '\0';
253 e876e210 2002-06-10 alex flags++;
254 e876e210 2002-06-10 alex }
255 2ee05c9a 2002-03-03 alex }
256 2ee05c9a 2002-03-03 alex
257 44cdf1b1 2008-02-26 fw chan = Channel_Search(channame);
258 44cdf1b1 2008-02-26 fw if (!chan && Conf_PredefChannelsOnly) {
259 44cdf1b1 2008-02-26 fw /* channel must be created, but server does not allow this */
260 44cdf1b1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
261 44cdf1b1 2008-02-26 fw break;
262 44cdf1b1 2008-02-26 fw }
263 44cdf1b1 2008-02-26 fw
264 233210b9 2004-04-09 alex /* Local client? */
265 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_USER) {
266 00e75ccd 2002-12-14 alex /* Test if the user has reached his maximum channel count */
267 634ef8c1 2008-02-26 fw if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
268 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
269 634ef8c1 2008-02-26 fw Client_ID(Client), channame);
270 530112b1 2008-05-01 alex if (!chan) {
271 530112b1 2008-05-01 alex /*
272 530112b1 2008-05-01 alex * New Channel: first user will be channel operator
273 f7c2e822 2008-05-01 alex * unless this is a modeless channel.
274 f7c2e822 2008-05-01 alex */
275 530112b1 2008-05-01 alex if (*channame != '+')
276 530112b1 2008-05-01 alex flags = "o";
277 530112b1 2008-05-01 alex } else
278 634ef8c1 2008-02-26 fw if (!join_allowed(Client, target, chan, channame, key))
279 634ef8c1 2008-02-26 fw break;
280 956bbe2c 2008-05-24 alex
281 956bbe2c 2008-05-24 alex /* Local client: update idle time */
282 956bbe2c 2008-05-24 alex Conn_UpdateIdle(Client_Conn(Client));
283 634ef8c1 2008-02-26 fw } else {
284 233210b9 2004-04-09 alex /* Remote server: we don't need to know whether the
285 233210b9 2004-04-09 alex * client is invited or not, but we have to make sure
286 233210b9 2004-04-09 alex * that the "one shot" entries (generated by INVITE
287 233210b9 2004-04-09 alex * commands) in this list become deleted when a user
288 233210b9 2004-04-09 alex * joins a channel this way. */
289 634ef8c1 2008-02-26 fw if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
290 2ee05c9a 2002-03-03 alex }
291 2ee05c9a 2002-03-03 alex
292 634ef8c1 2008-02-26 fw /* Join channel (and create channel if it doesn't exist) */
293 634ef8c1 2008-02-26 fw if (!Channel_Join(target, channame))
294 634ef8c1 2008-02-26 fw break;
295 2ee05c9a 2002-03-03 alex
296 530112b1 2008-05-01 alex if (!chan) { /* channel is new; it has been created above */
297 634ef8c1 2008-02-26 fw chan = Channel_Search(channame);
298 530112b1 2008-05-01 alex assert(chan != NULL);
299 530112b1 2008-05-01 alex if (*channame == '+') { /* modeless channel... */
300 530112b1 2008-05-01 alex Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
301 530112b1 2008-05-01 alex Channel_ModeAdd(chan, 'n'); /* no external msgs */
302 530112b1 2008-05-01 alex }
303 530112b1 2008-05-01 alex }
304 634ef8c1 2008-02-26 fw assert(chan != NULL);
305 2ee05c9a 2002-03-03 alex
306 634ef8c1 2008-02-26 fw join_set_channelmodes(chan, target, flags);
307 040f5422 2002-05-21 alex
308 634ef8c1 2008-02-26 fw join_forward(Client, target, chan, channame);
309 2ee05c9a 2002-03-03 alex
310 634ef8c1 2008-02-26 fw if (!join_send_topic(Client, target, chan, channame))
311 634ef8c1 2008-02-26 fw break; /* write error */
312 2ee05c9a 2002-03-03 alex
313 e7087905 2005-09-02 alex /* next channel? */
314 b90f71ca 2008-05-26 fw channame = strtok_r(NULL, ",", &lastchan);
315 b90f71ca 2008-05-26 fw if (channame && key)
316 b90f71ca 2008-05-26 fw key = strtok_r(NULL, ",", &lastkey);
317 2ee05c9a 2002-03-03 alex }
318 2ee05c9a 2002-03-03 alex return CONNECTED;
319 2ee05c9a 2002-03-03 alex } /* IRC_JOIN */
320 2ee05c9a 2002-03-03 alex
321 2ee05c9a 2002-03-03 alex
322 25f48a2a 2008-04-24 alex /**
323 25f48a2a 2008-04-24 alex * Handler for the IRC "PART" command.
324 25f48a2a 2008-04-24 alex */
325 8adff592 2005-03-19 fw GLOBAL bool
326 25f48a2a 2008-04-24 alex IRC_PART(CLIENT * Client, REQUEST * Req)
327 2ee05c9a 2002-03-03 alex {
328 2ee05c9a 2002-03-03 alex CLIENT *target;
329 8adff592 2005-03-19 fw char *chan;
330 2ee05c9a 2002-03-03 alex
331 25f48a2a 2008-04-24 alex assert(Client != NULL);
332 25f48a2a 2008-04-24 alex assert(Req != NULL);
333 2ee05c9a 2002-03-03 alex
334 463c5cb7 2008-01-07 fw if (Req->argc < 1 || Req->argc > 2)
335 463c5cb7 2008-01-07 fw return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
336 25f48a2a 2008-04-24 alex Client_ID(Client), Req->command);
337 2ee05c9a 2002-03-03 alex
338 25f48a2a 2008-04-24 alex /* Get the sender */
339 25f48a2a 2008-04-24 alex if (Client_Type(Client) == CLIENT_SERVER)
340 25f48a2a 2008-04-24 alex target = Client_Search(Req->prefix);
341 25f48a2a 2008-04-24 alex else
342 25f48a2a 2008-04-24 alex target = Client;
343 25f48a2a 2008-04-24 alex if (!target)
344 25f48a2a 2008-04-24 alex return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
345 25f48a2a 2008-04-24 alex Client_ID(Client), Req->prefix);
346 2ee05c9a 2002-03-03 alex
347 25f48a2a 2008-04-24 alex /* Loop over all the given channel names */
348 463c5cb7 2008-01-07 fw chan = strtok(Req->argv[0], ",");
349 25814389 2008-07-22 alex
350 25814389 2008-07-22 alex /* Make sure that "chan" is not the empty string ("PART :") */
351 25814389 2008-07-22 alex if (! chan)
352 25814389 2008-07-22 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
353 25814389 2008-07-22 alex Client_ID(Client), Req->command);
354 25814389 2008-07-22 alex
355 463c5cb7 2008-01-07 fw while (chan) {
356 25f48a2a 2008-04-24 alex Channel_Part(target, Client, chan,
357 25f48a2a 2008-04-24 alex Req->argc > 1 ? Req->argv[1] : Client_ID(target));
358 463c5cb7 2008-01-07 fw chan = strtok(NULL, ",");
359 2ee05c9a 2002-03-03 alex }
360 956bbe2c 2008-05-24 alex
361 956bbe2c 2008-05-24 alex /* Update idle time, if local client */
362 956bbe2c 2008-05-24 alex if (Client_Conn(Client) > NONE)
363 956bbe2c 2008-05-24 alex Conn_UpdateIdle(Client_Conn(Client));
364 956bbe2c 2008-05-24 alex
365 2ee05c9a 2002-03-03 alex return CONNECTED;
366 2ee05c9a 2002-03-03 alex } /* IRC_PART */
367 2ee05c9a 2002-03-03 alex
368 2ee05c9a 2002-03-03 alex
369 8adff592 2005-03-19 fw GLOBAL bool
370 c2f60abe 2002-05-27 alex IRC_TOPIC( CLIENT *Client, REQUEST *Req )
371 2ee05c9a 2002-03-03 alex {
372 2ee05c9a 2002-03-03 alex CHANNEL *chan;
373 2ee05c9a 2002-03-03 alex CLIENT *from;
374 8adff592 2005-03-19 fw char *topic;
375 ca32c1b3 2005-09-02 alex bool r;
376 2ee05c9a 2002-03-03 alex
377 2ee05c9a 2002-03-03 alex assert( Client != NULL );
378 2ee05c9a 2002-03-03 alex assert( Req != NULL );
379 2ee05c9a 2002-03-03 alex
380 530112b1 2008-05-01 alex if ((Req->argc < 1) || (Req->argc > 2))
381 530112b1 2008-05-01 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
382 2ee05c9a 2002-03-03 alex
383 bc4ed226 2002-03-25 alex if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
384 2ee05c9a 2002-03-03 alex else from = Client;
385 2ee05c9a 2002-03-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
386 2ee05c9a 2002-03-03 alex
387 2ee05c9a 2002-03-03 alex /* Welcher Channel? */
388 2ee05c9a 2002-03-03 alex chan = Channel_Search( Req->argv[0] );
389 a2119a66 2002-06-01 alex if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
390 2ee05c9a 2002-03-03 alex
391 2ee05c9a 2002-03-03 alex /* Ist der User Mitglied in dem Channel? */
392 2ee05c9a 2002-03-03 alex if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
393 2ee05c9a 2002-03-03 alex
394 2ee05c9a 2002-03-03 alex if( Req->argc == 1 )
395 2ee05c9a 2002-03-03 alex {
396 ca32c1b3 2005-09-02 alex /* Request actual topic */
397 ca32c1b3 2005-09-02 alex topic = Channel_Topic(chan);
398 ca32c1b3 2005-09-02 alex if (*topic) {
399 ca32c1b3 2005-09-02 alex r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
400 ca32c1b3 2005-09-02 alex Client_ID(Client), Channel_Name(chan), topic);
401 ca32c1b3 2005-09-02 alex #ifndef STRICT_RFC
402 ca32c1b3 2005-09-02 alex r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
403 ca32c1b3 2005-09-02 alex Client_ID(Client), Channel_Name(chan),
404 ca32c1b3 2005-09-02 alex Channel_TopicWho(chan),
405 ca32c1b3 2005-09-02 alex Channel_TopicTime(chan));
406 ca32c1b3 2005-09-02 alex #endif
407 ca32c1b3 2005-09-02 alex return r;
408 ca32c1b3 2005-09-02 alex }
409 ca32c1b3 2005-09-02 alex else
410 ca32c1b3 2005-09-02 alex return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
411 ca32c1b3 2005-09-02 alex Client_ID(from), Channel_Name(chan));
412 2ee05c9a 2002-03-03 alex }
413 2ee05c9a 2002-03-03 alex
414 2ee05c9a 2002-03-03 alex if( strchr( Channel_Modes( chan ), 't' ))
415 2ee05c9a 2002-03-03 alex {
416 2ee05c9a 2002-03-03 alex /* Topic Lock. Ist der User ein Channel Operator? */
417 2ee05c9a 2002-03-03 alex if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
418 2ee05c9a 2002-03-03 alex }
419 2ee05c9a 2002-03-03 alex
420 ca32c1b3 2005-09-02 alex /* Set new topic */
421 ca32c1b3 2005-09-02 alex Channel_SetTopic(chan, from, Req->argv[1]);
422 d93030ad 2008-09-23 alex LogDebug("%s \"%s\" set topic on \"%s\": %s",
423 d93030ad 2008-09-23 alex Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
424 d93030ad 2008-09-23 alex Req->argv[1][0] ? Req->argv[1] : "<none>");
425 2ee05c9a 2002-03-03 alex
426 2ee05c9a 2002-03-03 alex /* im Channel bekannt machen und an Server weiterleiten */
427 2ee05c9a 2002-03-03 alex IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
428 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
429 2ee05c9a 2002-03-03 alex
430 2ee05c9a 2002-03-03 alex if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
431 2ee05c9a 2002-03-03 alex else return CONNECTED;
432 2ee05c9a 2002-03-03 alex } /* IRC_TOPIC */
433 2ee05c9a 2002-03-03 alex
434 2ee05c9a 2002-03-03 alex
435 94dd7fa7 2005-06-12 alex /**
436 94dd7fa7 2005-06-12 alex * Handler for the IRC "LIST" command.
437 94dd7fa7 2005-06-12 alex * This implementation handles the local case as well as the forwarding of the
438 94dd7fa7 2005-06-12 alex * LIST command to other servers in the IRC network.
439 94dd7fa7 2005-06-12 alex */
440 8adff592 2005-03-19 fw GLOBAL bool
441 c2f60abe 2002-05-27 alex IRC_LIST( CLIENT *Client, REQUEST *Req )
442 3c0c3c3c 2002-04-23 alex {
443 8adff592 2005-03-19 fw char *pattern;
444 3c0c3c3c 2002-04-23 alex CHANNEL *chan;
445 005391ca 2002-09-16 alex CLIENT *from, *target;
446 3c0c3c3c 2002-04-23 alex
447 3c0c3c3c 2002-04-23 alex assert( Client != NULL );
448 3c0c3c3c 2002-04-23 alex assert( Req != NULL );
449 3c0c3c3c 2002-04-23 alex
450 94dd7fa7 2005-06-12 alex /* Bad number of prameters? */
451 94dd7fa7 2005-06-12 alex if( Req->argc > 2 )
452 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
453 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->command );
454 3c0c3c3c 2002-04-23 alex
455 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
456 94dd7fa7 2005-06-12 alex pattern = strtok( Req->argv[0], "," );
457 94dd7fa7 2005-06-12 alex else
458 94dd7fa7 2005-06-12 alex pattern = "*";
459 005391ca 2002-09-16 alex
460 94dd7fa7 2005-06-12 alex /* Get sender from prefix, if any */
461 94dd7fa7 2005-06-12 alex if( Client_Type( Client ) == CLIENT_SERVER )
462 94dd7fa7 2005-06-12 alex from = Client_Search( Req->prefix );
463 94dd7fa7 2005-06-12 alex else
464 94dd7fa7 2005-06-12 alex from = Client;
465 005391ca 2002-09-16 alex
466 94dd7fa7 2005-06-12 alex if( ! from )
467 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
468 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->prefix );
469 94dd7fa7 2005-06-12 alex
470 005391ca 2002-09-16 alex if( Req->argc == 2 )
471 005391ca 2002-09-16 alex {
472 94dd7fa7 2005-06-12 alex /* Forward to other server? */
473 005391ca 2002-09-16 alex target = Client_Search( Req->argv[1] );
474 94dd7fa7 2005-06-12 alex if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
475 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
476 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->argv[1] );
477 005391ca 2002-09-16 alex
478 005391ca 2002-09-16 alex if( target != Client_ThisServer( ))
479 005391ca 2002-09-16 alex {
480 94dd7fa7 2005-06-12 alex /* Target is indeed an other server, forward it! */
481 94dd7fa7 2005-06-12 alex return IRC_WriteStrClientPrefix( target, from,
482 94dd7fa7 2005-06-12 alex "LIST %s :%s", Client_ID( from ),
483 94dd7fa7 2005-06-12 alex Req->argv[1] );
484 005391ca 2002-09-16 alex }
485 005391ca 2002-09-16 alex }
486 ec0b405d 2008-09-23 alex
487 3c0c3c3c 2002-04-23 alex while( pattern )
488 3c0c3c3c 2002-04-23 alex {
489 4ef172d6 2005-03-02 alex /* Loop through all the channels */
490 3c0c3c3c 2002-04-23 alex chan = Channel_First( );
491 3c0c3c3c 2002-04-23 alex while( chan )
492 3c0c3c3c 2002-04-23 alex {
493 4ef172d6 2005-03-02 alex /* Check search pattern */
494 39b9f65d 2002-06-26 alex if( Match( pattern, Channel_Name( chan )))
495 3c0c3c3c 2002-04-23 alex {
496 4ef172d6 2005-03-02 alex /* Gotcha! */
497 4ef172d6 2005-03-02 alex if( ! strchr( Channel_Modes( chan ), 's' ) ||
498 4ef172d6 2005-03-02 alex Channel_IsMemberOf( chan, from ))
499 4ef172d6 2005-03-02 alex {
500 94dd7fa7 2005-06-12 alex if( ! IRC_WriteStrClient( from,
501 94dd7fa7 2005-06-12 alex RPL_LIST_MSG, Client_ID( from ),
502 94dd7fa7 2005-06-12 alex Channel_Name( chan ),
503 94dd7fa7 2005-06-12 alex Channel_MemberCount( chan ),
504 94dd7fa7 2005-06-12 alex Channel_Topic( chan )))
505 94dd7fa7 2005-06-12 alex return DISCONNECTED;
506 4ef172d6 2005-03-02 alex }
507 3c0c3c3c 2002-04-23 alex }
508 3c0c3c3c 2002-04-23 alex chan = Channel_Next( chan );
509 3c0c3c3c 2002-04-23 alex }
510 ec0b405d 2008-09-23 alex
511 94dd7fa7 2005-06-12 alex /* Get next name ... */
512 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
513 94dd7fa7 2005-06-12 alex pattern = strtok( NULL, "," );
514 94dd7fa7 2005-06-12 alex else
515 94dd7fa7 2005-06-12 alex pattern = NULL;
516 3c0c3c3c 2002-04-23 alex }
517 ec0b405d 2008-09-23 alex
518 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
519 3c0c3c3c 2002-04-23 alex } /* IRC_LIST */
520 1f9ba7b3 2002-09-03 alex
521 1f9ba7b3 2002-09-03 alex
522 8adff592 2005-03-19 fw GLOBAL bool
523 1f9ba7b3 2002-09-03 alex IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
524 1f9ba7b3 2002-09-03 alex {
525 8adff592 2005-03-19 fw char modes_add[COMMAND_LEN], l[16], *ptr;
526 1f9ba7b3 2002-09-03 alex CLIENT *from;
527 1f9ba7b3 2002-09-03 alex CHANNEL *chan;
528 8adff592 2005-03-19 fw int arg_topic;
529 1f9ba7b3 2002-09-03 alex
530 1f9ba7b3 2002-09-03 alex assert( Client != NULL );
531 1f9ba7b3 2002-09-03 alex assert( Req != NULL );
532 1f9ba7b3 2002-09-03 alex
533 43d9a624 2003-01-08 alex /* Bad number of parameters? */
534 43d9a624 2003-01-08 alex if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
535 43d9a624 2003-01-08 alex
536 43d9a624 2003-01-08 alex /* Compatibility kludge */
537 43d9a624 2003-01-08 alex if( Req->argc == 5 ) arg_topic = 4;
538 43d9a624 2003-01-08 alex else if( Req->argc == 3 ) arg_topic = 2;
539 43d9a624 2003-01-08 alex else arg_topic = -1;
540 1f9ba7b3 2002-09-03 alex
541 43d9a624 2003-01-08 alex /* Search origin */
542 1f9ba7b3 2002-09-03 alex from = Client_Search( Req->prefix );
543 1f9ba7b3 2002-09-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
544 1f9ba7b3 2002-09-03 alex
545 43d9a624 2003-01-08 alex /* Search or create channel */
546 1f9ba7b3 2002-09-03 alex chan = Channel_Search( Req->argv[0] );
547 1f9ba7b3 2002-09-03 alex if( ! chan ) chan = Channel_Create( Req->argv[0] );
548 1f9ba7b3 2002-09-03 alex if( ! chan ) return CONNECTED;
549 1f9ba7b3 2002-09-03 alex
550 1f9ba7b3 2002-09-03 alex if( Req->argv[1][0] == '+' )
551 1f9ba7b3 2002-09-03 alex {
552 1f9ba7b3 2002-09-03 alex ptr = Channel_Modes( chan );
553 1f9ba7b3 2002-09-03 alex if( ! *ptr )
554 1f9ba7b3 2002-09-03 alex {
555 bb94d181 2003-01-08 alex /* OK, this channel doesn't have modes jet, set the received ones: */
556 1f9ba7b3 2002-09-03 alex Channel_SetModes( chan, &Req->argv[1][1] );
557 bb94d181 2003-01-08 alex
558 43d9a624 2003-01-08 alex if( Req->argc == 5 )
559 43d9a624 2003-01-08 alex {
560 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
561 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
562 43d9a624 2003-01-08 alex }
563 43d9a624 2003-01-08 alex else
564 43d9a624 2003-01-08 alex {
565 43d9a624 2003-01-08 alex /* Delete modes which we never want to inherit */
566 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'l' );
567 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'k' );
568 43d9a624 2003-01-08 alex }
569 3c0c3c3c 2002-04-23 alex
570 43d9a624 2003-01-08 alex strcpy( modes_add, "" );
571 43d9a624 2003-01-08 alex ptr = Channel_Modes( chan );
572 43d9a624 2003-01-08 alex while( *ptr )
573 43d9a624 2003-01-08 alex {
574 43d9a624 2003-01-08 alex if( *ptr == 'l' )
575 43d9a624 2003-01-08 alex {
576 6e105bf8 2006-10-06 fw snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
577 43d9a624 2003-01-08 alex strlcat( modes_add, l, sizeof( modes_add ));
578 43d9a624 2003-01-08 alex }
579 43d9a624 2003-01-08 alex if( *ptr == 'k' )
580 43d9a624 2003-01-08 alex {
581 43d9a624 2003-01-08 alex strlcat( modes_add, " ", sizeof( modes_add ));
582 43d9a624 2003-01-08 alex strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
583 43d9a624 2003-01-08 alex }
584 43d9a624 2003-01-08 alex ptr++;
585 43d9a624 2003-01-08 alex }
586 ec0b405d 2008-09-23 alex
587 43d9a624 2003-01-08 alex /* Inform members of this channel */
588 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
589 43d9a624 2003-01-08 alex }
590 43d9a624 2003-01-08 alex }
591 43d9a624 2003-01-08 alex else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
592 43d9a624 2003-01-08 alex
593 43d9a624 2003-01-08 alex if( arg_topic > 0 )
594 43d9a624 2003-01-08 alex {
595 43d9a624 2003-01-08 alex /* We got a topic */
596 1f9ba7b3 2002-09-03 alex ptr = Channel_Topic( chan );
597 43d9a624 2003-01-08 alex if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
598 1f9ba7b3 2002-09-03 alex {
599 43d9a624 2003-01-08 alex /* OK, there is no topic jet */
600 ca32c1b3 2005-09-02 alex Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
601 ca32c1b3 2005-09-02 alex IRC_WriteStrChannelPrefix(Client, chan, from, false,
602 ca32c1b3 2005-09-02 alex "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
603 1f9ba7b3 2002-09-03 alex }
604 1f9ba7b3 2002-09-03 alex }
605 3c0c3c3c 2002-04-23 alex
606 43d9a624 2003-01-08 alex /* Forward CHANINFO to other serevrs */
607 43d9a624 2003-01-08 alex if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
608 43d9a624 2003-01-08 alex else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
609 43d9a624 2003-01-08 alex else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
610 43d9a624 2003-01-08 alex
611 1f9ba7b3 2002-09-03 alex return CONNECTED;
612 1f9ba7b3 2002-09-03 alex } /* IRC_CHANINFO */
613 1f9ba7b3 2002-09-03 alex
614 1f9ba7b3 2002-09-03 alex
615 2ee05c9a 2002-03-03 alex /* -eof- */