Blame


1 2ee05c9a 2002-03-03 alex /*
2 2ee05c9a 2002-03-03 alex * ngIRCd -- The Next Generation IRC Daemon
3 2ee05c9a 2002-03-03 alex * Copyright (c)2001,2002 by 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 94dd7fa7 2005-06-12 alex static char UNUSED id[] = "$Id: irc-channel.c,v 1.30 2005/06/12 18:23:59 alex Exp $";
18 2ee05c9a 2002-03-03 alex
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 2ee05c9a 2002-03-03 alex #include <assert.h>
21 43d9a624 2003-01-08 alex #include <stdlib.h>
22 43d9a624 2003-01-08 alex #include <stdio.h>
23 2ee05c9a 2002-03-03 alex #include <string.h>
24 2ee05c9a 2002-03-03 alex
25 ca33cbda 2002-03-12 alex #include "defines.h"
26 c2f60abe 2002-05-27 alex #include "conn.h"
27 c2f60abe 2002-05-27 alex #include "client.h"
28 c2f60abe 2002-05-27 alex #include "channel.h"
29 c2f60abe 2002-05-27 alex #include "lists.h"
30 2ee05c9a 2002-03-03 alex #include "log.h"
31 39b9f65d 2002-06-26 alex #include "match.h"
32 2ee05c9a 2002-03-03 alex #include "messages.h"
33 c2f60abe 2002-05-27 alex #include "parse.h"
34 0c471b84 2002-11-30 alex #include "irc-info.h"
35 c2f60abe 2002-05-27 alex #include "irc-write.h"
36 5b8b3b83 2002-12-13 alex #include "resolve.h"
37 5b8b3b83 2002-12-13 alex #include "conf.h"
38 2ee05c9a 2002-03-03 alex
39 ca33cbda 2002-03-12 alex #include "exp.h"
40 2ee05c9a 2002-03-03 alex #include "irc-channel.h"
41 2ee05c9a 2002-03-03 alex
42 2ee05c9a 2002-03-03 alex
43 8adff592 2005-03-19 fw GLOBAL bool
44 c2f60abe 2002-05-27 alex IRC_JOIN( CLIENT *Client, REQUEST *Req )
45 2ee05c9a 2002-03-03 alex {
46 8adff592 2005-03-19 fw char *channame, *key, *flags, *topic, modes[8];
47 8adff592 2005-03-19 fw bool is_new_chan, is_invited, is_banned;
48 2ee05c9a 2002-03-03 alex CLIENT *target;
49 2ee05c9a 2002-03-03 alex CHANNEL *chan;
50 2ee05c9a 2002-03-03 alex
51 2ee05c9a 2002-03-03 alex assert( Client != NULL );
52 2ee05c9a 2002-03-03 alex assert( Req != NULL );
53 2ee05c9a 2002-03-03 alex
54 69ad0e38 2002-12-16 alex /* Bad number of arguments? */
55 69ad0e38 2002-12-16 alex if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
56 2ee05c9a 2002-03-03 alex
57 69ad0e38 2002-12-16 alex /* Who is the sender? */
58 bc4ed226 2002-03-25 alex if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
59 2ee05c9a 2002-03-03 alex else target = Client;
60 2ee05c9a 2002-03-03 alex if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
61 2ee05c9a 2002-03-03 alex
62 69ad0e38 2002-12-16 alex /* Are channel keys given? */
63 69ad0e38 2002-12-16 alex if( Req->argc > 1 ) key = Req->argv[1];
64 69ad0e38 2002-12-16 alex else key = NULL;
65 69ad0e38 2002-12-16 alex
66 2ee05c9a 2002-03-03 alex /* Channel-Namen durchgehen */
67 c2f60abe 2002-05-27 alex chan = NULL;
68 2ee05c9a 2002-03-03 alex channame = strtok( Req->argv[0], "," );
69 2ee05c9a 2002-03-03 alex while( channame )
70 2ee05c9a 2002-03-03 alex {
71 cb76d96e 2004-03-11 alex chan = NULL; flags = NULL;
72 2ee05c9a 2002-03-03 alex
73 f7567db0 2002-06-02 alex /* wird der Channel neu angelegt? */
74 8adff592 2005-03-19 fw if( Channel_Search( channame )) is_new_chan = false;
75 8adff592 2005-03-19 fw else is_new_chan = true;
76 2ee05c9a 2002-03-03 alex
77 2ee05c9a 2002-03-03 alex /* Hat ein Server Channel-User-Modes uebergeben? */
78 2ee05c9a 2002-03-03 alex if( Client_Type( Client ) == CLIENT_SERVER )
79 2ee05c9a 2002-03-03 alex {
80 2ee05c9a 2002-03-03 alex /* Channel-Flags extrahieren */
81 2ee05c9a 2002-03-03 alex flags = strchr( channame, 0x7 );
82 e876e210 2002-06-10 alex if( flags )
83 e876e210 2002-06-10 alex {
84 e876e210 2002-06-10 alex *flags = '\0';
85 e876e210 2002-06-10 alex flags++;
86 e876e210 2002-06-10 alex }
87 2ee05c9a 2002-03-03 alex }
88 2ee05c9a 2002-03-03 alex
89 233210b9 2004-04-09 alex /* Local client? */
90 c2f60abe 2002-05-27 alex if( Client_Type( Client ) == CLIENT_USER )
91 2ee05c9a 2002-03-03 alex {
92 00e75ccd 2002-12-14 alex /* Test if the user has reached his maximum channel count */
93 00e75ccd 2002-12-14 alex if( Client_Type( Client ) == CLIENT_USER )
94 00e75ccd 2002-12-14 alex {
95 00e75ccd 2002-12-14 alex if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
96 00e75ccd 2002-12-14 alex {
97 00e75ccd 2002-12-14 alex IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
98 00e75ccd 2002-12-14 alex return CONNECTED;
99 00e75ccd 2002-12-14 alex }
100 00e75ccd 2002-12-14 alex }
101 00e75ccd 2002-12-14 alex
102 c2f60abe 2002-05-27 alex /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
103 c2f60abe 2002-05-27 alex if( is_new_chan )
104 c2f60abe 2002-05-27 alex {
105 c2f60abe 2002-05-27 alex /* Erster User im Channel: Operator-Flag setzen */
106 c2f60abe 2002-05-27 alex flags = "o";
107 c2f60abe 2002-05-27 alex }
108 c2f60abe 2002-05-27 alex else
109 c2f60abe 2002-05-27 alex {
110 c2f60abe 2002-05-27 alex /* Existierenden Channel suchen */
111 c2f60abe 2002-05-27 alex chan = Channel_Search( channame );
112 c2f60abe 2002-05-27 alex assert( chan != NULL );
113 c2f60abe 2002-05-27 alex
114 a1ded68a 2002-09-08 alex is_banned = Lists_CheckBanned( target, chan );
115 a1ded68a 2002-09-08 alex is_invited = Lists_CheckInvited( target, chan );
116 a1ded68a 2002-09-08 alex
117 c2f60abe 2002-05-27 alex /* Testen, ob Client gebanned ist */
118 8adff592 2005-03-19 fw if(( is_banned == true) && ( is_invited == false ))
119 c2f60abe 2002-05-27 alex {
120 a1ded68a 2002-09-08 alex /* Client ist gebanned (und nicht invited): */
121 f7567db0 2002-06-02 alex IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
122 f7567db0 2002-06-02 alex
123 69ad0e38 2002-12-16 alex /* Try next name, if any */
124 f7567db0 2002-06-02 alex channame = strtok( NULL, "," );
125 f7567db0 2002-06-02 alex continue;
126 c2f60abe 2002-05-27 alex }
127 c2f60abe 2002-05-27 alex
128 c2f60abe 2002-05-27 alex /* Ist der Channel "invite-only"? */
129 8adff592 2005-03-19 fw if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
130 c2f60abe 2002-05-27 alex {
131 a1ded68a 2002-09-08 alex /* Channel ist "invite-only" und Client wurde nicht invited: */
132 a1ded68a 2002-09-08 alex IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
133 f7567db0 2002-06-02 alex
134 69ad0e38 2002-12-16 alex /* Try next name, if any */
135 69ad0e38 2002-12-16 alex channame = strtok( NULL, "," );
136 69ad0e38 2002-12-16 alex continue;
137 69ad0e38 2002-12-16 alex }
138 69ad0e38 2002-12-16 alex
139 69ad0e38 2002-12-16 alex /* Is the channel protected by a key? */
140 69ad0e38 2002-12-16 alex if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
141 69ad0e38 2002-12-16 alex {
142 69ad0e38 2002-12-16 alex /* Bad channel key! */
143 69ad0e38 2002-12-16 alex IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
144 69ad0e38 2002-12-16 alex
145 69ad0e38 2002-12-16 alex /* Try next name, if any */
146 69ad0e38 2002-12-16 alex channame = strtok( NULL, "," );
147 69ad0e38 2002-12-16 alex continue;
148 69ad0e38 2002-12-16 alex }
149 69ad0e38 2002-12-16 alex
150 69ad0e38 2002-12-16 alex /* Are there already too many members? */
151 69ad0e38 2002-12-16 alex if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
152 69ad0e38 2002-12-16 alex {
153 69ad0e38 2002-12-16 alex /* Bad channel key! */
154 69ad0e38 2002-12-16 alex IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
155 69ad0e38 2002-12-16 alex
156 69ad0e38 2002-12-16 alex /* Try next name, if any */
157 a1ded68a 2002-09-08 alex channame = strtok( NULL, "," );
158 a1ded68a 2002-09-08 alex continue;
159 c2f60abe 2002-05-27 alex }
160 c2f60abe 2002-05-27 alex }
161 233210b9 2004-04-09 alex }
162 233210b9 2004-04-09 alex else
163 233210b9 2004-04-09 alex {
164 233210b9 2004-04-09 alex /* Remote server: we don't need to know whether the
165 233210b9 2004-04-09 alex * client is invited or not, but we have to make sure
166 233210b9 2004-04-09 alex * that the "one shot" entries (generated by INVITE
167 233210b9 2004-04-09 alex * commands) in this list become deleted when a user
168 233210b9 2004-04-09 alex * joins a channel this way. */
169 233210b9 2004-04-09 alex chan = Channel_Search( channame );
170 8adff592 2005-03-19 fw if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
171 2ee05c9a 2002-03-03 alex }
172 2ee05c9a 2002-03-03 alex
173 2ee05c9a 2002-03-03 alex /* Channel joinen (und ggf. anlegen) */
174 2ee05c9a 2002-03-03 alex if( ! Channel_Join( target, channame ))
175 2ee05c9a 2002-03-03 alex {
176 2ee05c9a 2002-03-03 alex /* naechsten Namen ermitteln */
177 2ee05c9a 2002-03-03 alex channame = strtok( NULL, "," );
178 2ee05c9a 2002-03-03 alex continue;
179 2ee05c9a 2002-03-03 alex }
180 c2f60abe 2002-05-27 alex if( ! chan ) chan = Channel_Search( channame );
181 2ee05c9a 2002-03-03 alex assert( chan != NULL );
182 2ee05c9a 2002-03-03 alex
183 2ee05c9a 2002-03-03 alex /* Modes setzen (wenn vorhanden) */
184 2ee05c9a 2002-03-03 alex while( flags && *flags )
185 2ee05c9a 2002-03-03 alex {
186 2ee05c9a 2002-03-03 alex Channel_UserModeAdd( chan, target, *flags );
187 2ee05c9a 2002-03-03 alex flags++;
188 2ee05c9a 2002-03-03 alex }
189 2ee05c9a 2002-03-03 alex
190 040f5422 2002-05-21 alex /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
191 040f5422 2002-05-21 alex if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
192 040f5422 2002-05-21 alex
193 2ee05c9a 2002-03-03 alex /* Muessen Modes an andere Server gemeldet werden? */
194 695631b2 2002-12-26 alex strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
195 2ee05c9a 2002-03-03 alex if( modes[1] ) modes[0] = 0x7;
196 2ee05c9a 2002-03-03 alex else modes[0] = '\0';
197 2ee05c9a 2002-03-03 alex
198 2ee05c9a 2002-03-03 alex /* An andere Server weiterleiten */
199 2ee05c9a 2002-03-03 alex IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
200 2ee05c9a 2002-03-03 alex
201 2ee05c9a 2002-03-03 alex /* im Channel bekannt machen */
202 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
203 2ee05c9a 2002-03-03 alex if( modes[1] )
204 2ee05c9a 2002-03-03 alex {
205 2ee05c9a 2002-03-03 alex /* Modes im Channel bekannt machen */
206 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
207 2ee05c9a 2002-03-03 alex }
208 2ee05c9a 2002-03-03 alex
209 2ee05c9a 2002-03-03 alex if( Client_Type( Client ) == CLIENT_USER )
210 2ee05c9a 2002-03-03 alex {
211 2ee05c9a 2002-03-03 alex /* an Client bestaetigen */
212 2ee05c9a 2002-03-03 alex IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
213 2ee05c9a 2002-03-03 alex
214 2ee05c9a 2002-03-03 alex /* Topic an Client schicken */
215 2ee05c9a 2002-03-03 alex topic = Channel_Topic( chan );
216 2ee05c9a 2002-03-03 alex if( *topic ) IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), channame, topic );
217 2ee05c9a 2002-03-03 alex
218 2ee05c9a 2002-03-03 alex /* Mitglieder an Client Melden */
219 2ee05c9a 2002-03-03 alex IRC_Send_NAMES( Client, chan );
220 2ee05c9a 2002-03-03 alex IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
221 2ee05c9a 2002-03-03 alex }
222 2ee05c9a 2002-03-03 alex
223 2ee05c9a 2002-03-03 alex /* naechsten Namen ermitteln */
224 2ee05c9a 2002-03-03 alex channame = strtok( NULL, "," );
225 2ee05c9a 2002-03-03 alex }
226 2ee05c9a 2002-03-03 alex return CONNECTED;
227 2ee05c9a 2002-03-03 alex } /* IRC_JOIN */
228 2ee05c9a 2002-03-03 alex
229 2ee05c9a 2002-03-03 alex
230 8adff592 2005-03-19 fw GLOBAL bool
231 c2f60abe 2002-05-27 alex IRC_PART( CLIENT *Client, REQUEST *Req )
232 2ee05c9a 2002-03-03 alex {
233 2ee05c9a 2002-03-03 alex CLIENT *target;
234 8adff592 2005-03-19 fw char *chan;
235 2ee05c9a 2002-03-03 alex
236 2ee05c9a 2002-03-03 alex assert( Client != NULL );
237 2ee05c9a 2002-03-03 alex assert( Req != NULL );
238 2ee05c9a 2002-03-03 alex
239 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
240 2ee05c9a 2002-03-03 alex if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
241 2ee05c9a 2002-03-03 alex
242 2ee05c9a 2002-03-03 alex /* Wer ist der Absender? */
243 bc4ed226 2002-03-25 alex if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
244 2ee05c9a 2002-03-03 alex else target = Client;
245 2ee05c9a 2002-03-03 alex if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
246 2ee05c9a 2002-03-03 alex
247 2ee05c9a 2002-03-03 alex /* Channel-Namen durchgehen */
248 2ee05c9a 2002-03-03 alex chan = strtok( Req->argv[0], "," );
249 2ee05c9a 2002-03-03 alex while( chan )
250 2ee05c9a 2002-03-03 alex {
251 2ee05c9a 2002-03-03 alex if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
252 2ee05c9a 2002-03-03 alex {
253 2ee05c9a 2002-03-03 alex /* naechsten Namen ermitteln */
254 2ee05c9a 2002-03-03 alex chan = strtok( NULL, "," );
255 2ee05c9a 2002-03-03 alex continue;
256 2ee05c9a 2002-03-03 alex }
257 2ee05c9a 2002-03-03 alex
258 2ee05c9a 2002-03-03 alex /* naechsten Namen ermitteln */
259 2ee05c9a 2002-03-03 alex chan = strtok( NULL, "," );
260 2ee05c9a 2002-03-03 alex }
261 2ee05c9a 2002-03-03 alex return CONNECTED;
262 2ee05c9a 2002-03-03 alex } /* IRC_PART */
263 2ee05c9a 2002-03-03 alex
264 2ee05c9a 2002-03-03 alex
265 8adff592 2005-03-19 fw GLOBAL bool
266 c2f60abe 2002-05-27 alex IRC_TOPIC( CLIENT *Client, REQUEST *Req )
267 2ee05c9a 2002-03-03 alex {
268 2ee05c9a 2002-03-03 alex CHANNEL *chan;
269 2ee05c9a 2002-03-03 alex CLIENT *from;
270 8adff592 2005-03-19 fw char *topic;
271 2ee05c9a 2002-03-03 alex
272 2ee05c9a 2002-03-03 alex assert( Client != NULL );
273 2ee05c9a 2002-03-03 alex assert( Req != NULL );
274 2ee05c9a 2002-03-03 alex
275 2ee05c9a 2002-03-03 alex /* Falsche Anzahl Parameter? */
276 2ee05c9a 2002-03-03 alex if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
277 2ee05c9a 2002-03-03 alex
278 bc4ed226 2002-03-25 alex if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
279 2ee05c9a 2002-03-03 alex else from = Client;
280 2ee05c9a 2002-03-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
281 2ee05c9a 2002-03-03 alex
282 2ee05c9a 2002-03-03 alex /* Welcher Channel? */
283 2ee05c9a 2002-03-03 alex chan = Channel_Search( Req->argv[0] );
284 a2119a66 2002-06-01 alex if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
285 2ee05c9a 2002-03-03 alex
286 2ee05c9a 2002-03-03 alex /* Ist der User Mitglied in dem Channel? */
287 2ee05c9a 2002-03-03 alex if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
288 2ee05c9a 2002-03-03 alex
289 2ee05c9a 2002-03-03 alex if( Req->argc == 1 )
290 2ee05c9a 2002-03-03 alex {
291 2ee05c9a 2002-03-03 alex /* Topic erfragen */
292 2ee05c9a 2002-03-03 alex topic = Channel_Topic( chan );
293 2ee05c9a 2002-03-03 alex if( *topic ) return IRC_WriteStrClient( from, RPL_TOPIC_MSG, Client_ID( from ), Channel_Name( chan ), topic );
294 2ee05c9a 2002-03-03 alex else return IRC_WriteStrClient( from, RPL_NOTOPIC_MSG, Client_ID( from ), Channel_Name( chan ));
295 2ee05c9a 2002-03-03 alex }
296 2ee05c9a 2002-03-03 alex
297 2ee05c9a 2002-03-03 alex if( strchr( Channel_Modes( chan ), 't' ))
298 2ee05c9a 2002-03-03 alex {
299 2ee05c9a 2002-03-03 alex /* Topic Lock. Ist der User ein Channel Operator? */
300 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 ));
301 2ee05c9a 2002-03-03 alex }
302 2ee05c9a 2002-03-03 alex
303 2ee05c9a 2002-03-03 alex /* Topic setzen */
304 2ee05c9a 2002-03-03 alex Channel_SetTopic( chan, Req->argv[1] );
305 2ee05c9a 2002-03-03 alex Log( LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s", Client_Mask( from ), Channel_Name( chan ), Req->argv[1][0] ? Req->argv[1] : "<none>" );
306 2ee05c9a 2002-03-03 alex
307 2ee05c9a 2002-03-03 alex /* im Channel bekannt machen und an Server weiterleiten */
308 2ee05c9a 2002-03-03 alex IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
309 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
310 2ee05c9a 2002-03-03 alex
311 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] );
312 2ee05c9a 2002-03-03 alex else return CONNECTED;
313 2ee05c9a 2002-03-03 alex } /* IRC_TOPIC */
314 2ee05c9a 2002-03-03 alex
315 2ee05c9a 2002-03-03 alex
316 94dd7fa7 2005-06-12 alex /**
317 94dd7fa7 2005-06-12 alex * Handler for the IRC "LIST" command.
318 94dd7fa7 2005-06-12 alex * This implementation handles the local case as well as the forwarding of the
319 94dd7fa7 2005-06-12 alex * LIST command to other servers in the IRC network.
320 94dd7fa7 2005-06-12 alex */
321 8adff592 2005-03-19 fw GLOBAL bool
322 c2f60abe 2002-05-27 alex IRC_LIST( CLIENT *Client, REQUEST *Req )
323 3c0c3c3c 2002-04-23 alex {
324 8adff592 2005-03-19 fw char *pattern;
325 3c0c3c3c 2002-04-23 alex CHANNEL *chan;
326 005391ca 2002-09-16 alex CLIENT *from, *target;
327 3c0c3c3c 2002-04-23 alex
328 3c0c3c3c 2002-04-23 alex assert( Client != NULL );
329 3c0c3c3c 2002-04-23 alex assert( Req != NULL );
330 3c0c3c3c 2002-04-23 alex
331 94dd7fa7 2005-06-12 alex /* Bad number of prameters? */
332 94dd7fa7 2005-06-12 alex if( Req->argc > 2 )
333 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
334 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->command );
335 3c0c3c3c 2002-04-23 alex
336 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
337 94dd7fa7 2005-06-12 alex pattern = strtok( Req->argv[0], "," );
338 94dd7fa7 2005-06-12 alex else
339 94dd7fa7 2005-06-12 alex pattern = "*";
340 005391ca 2002-09-16 alex
341 94dd7fa7 2005-06-12 alex /* Get sender from prefix, if any */
342 94dd7fa7 2005-06-12 alex if( Client_Type( Client ) == CLIENT_SERVER )
343 94dd7fa7 2005-06-12 alex from = Client_Search( Req->prefix );
344 94dd7fa7 2005-06-12 alex else
345 94dd7fa7 2005-06-12 alex from = Client;
346 005391ca 2002-09-16 alex
347 94dd7fa7 2005-06-12 alex if( ! from )
348 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
349 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->prefix );
350 94dd7fa7 2005-06-12 alex
351 005391ca 2002-09-16 alex if( Req->argc == 2 )
352 005391ca 2002-09-16 alex {
353 94dd7fa7 2005-06-12 alex /* Forward to other server? */
354 005391ca 2002-09-16 alex target = Client_Search( Req->argv[1] );
355 94dd7fa7 2005-06-12 alex if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
356 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
357 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->argv[1] );
358 005391ca 2002-09-16 alex
359 005391ca 2002-09-16 alex if( target != Client_ThisServer( ))
360 005391ca 2002-09-16 alex {
361 94dd7fa7 2005-06-12 alex /* Target is indeed an other server, forward it! */
362 94dd7fa7 2005-06-12 alex return IRC_WriteStrClientPrefix( target, from,
363 94dd7fa7 2005-06-12 alex "LIST %s :%s", Client_ID( from ),
364 94dd7fa7 2005-06-12 alex Req->argv[1] );
365 005391ca 2002-09-16 alex }
366 005391ca 2002-09-16 alex }
367 3c0c3c3c 2002-04-23 alex
368 3c0c3c3c 2002-04-23 alex while( pattern )
369 3c0c3c3c 2002-04-23 alex {
370 4ef172d6 2005-03-02 alex /* Loop through all the channels */
371 3c0c3c3c 2002-04-23 alex chan = Channel_First( );
372 3c0c3c3c 2002-04-23 alex while( chan )
373 3c0c3c3c 2002-04-23 alex {
374 4ef172d6 2005-03-02 alex /* Check search pattern */
375 39b9f65d 2002-06-26 alex if( Match( pattern, Channel_Name( chan )))
376 3c0c3c3c 2002-04-23 alex {
377 4ef172d6 2005-03-02 alex /* Gotcha! */
378 4ef172d6 2005-03-02 alex if( ! strchr( Channel_Modes( chan ), 's' ) ||
379 4ef172d6 2005-03-02 alex Channel_IsMemberOf( chan, from ))
380 4ef172d6 2005-03-02 alex {
381 94dd7fa7 2005-06-12 alex if( ! IRC_WriteStrClient( from,
382 94dd7fa7 2005-06-12 alex RPL_LIST_MSG, Client_ID( from ),
383 94dd7fa7 2005-06-12 alex Channel_Name( chan ),
384 94dd7fa7 2005-06-12 alex Channel_MemberCount( chan ),
385 94dd7fa7 2005-06-12 alex Channel_Topic( chan )))
386 94dd7fa7 2005-06-12 alex return DISCONNECTED;
387 4ef172d6 2005-03-02 alex }
388 3c0c3c3c 2002-04-23 alex }
389 3c0c3c3c 2002-04-23 alex chan = Channel_Next( chan );
390 3c0c3c3c 2002-04-23 alex }
391 3c0c3c3c 2002-04-23 alex
392 94dd7fa7 2005-06-12 alex /* Get next name ... */
393 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
394 94dd7fa7 2005-06-12 alex pattern = strtok( NULL, "," );
395 94dd7fa7 2005-06-12 alex else
396 94dd7fa7 2005-06-12 alex pattern = NULL;
397 3c0c3c3c 2002-04-23 alex }
398 3c0c3c3c 2002-04-23 alex
399 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
400 3c0c3c3c 2002-04-23 alex } /* IRC_LIST */
401 1f9ba7b3 2002-09-03 alex
402 1f9ba7b3 2002-09-03 alex
403 8adff592 2005-03-19 fw GLOBAL bool
404 1f9ba7b3 2002-09-03 alex IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
405 1f9ba7b3 2002-09-03 alex {
406 8adff592 2005-03-19 fw char modes_add[COMMAND_LEN], l[16], *ptr;
407 1f9ba7b3 2002-09-03 alex CLIENT *from;
408 1f9ba7b3 2002-09-03 alex CHANNEL *chan;
409 8adff592 2005-03-19 fw int arg_topic;
410 1f9ba7b3 2002-09-03 alex
411 1f9ba7b3 2002-09-03 alex assert( Client != NULL );
412 1f9ba7b3 2002-09-03 alex assert( Req != NULL );
413 1f9ba7b3 2002-09-03 alex
414 43d9a624 2003-01-08 alex /* Bad number of parameters? */
415 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 );
416 43d9a624 2003-01-08 alex
417 43d9a624 2003-01-08 alex /* Compatibility kludge */
418 43d9a624 2003-01-08 alex if( Req->argc == 5 ) arg_topic = 4;
419 43d9a624 2003-01-08 alex else if( Req->argc == 3 ) arg_topic = 2;
420 43d9a624 2003-01-08 alex else arg_topic = -1;
421 1f9ba7b3 2002-09-03 alex
422 43d9a624 2003-01-08 alex /* Search origin */
423 1f9ba7b3 2002-09-03 alex from = Client_Search( Req->prefix );
424 1f9ba7b3 2002-09-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
425 1f9ba7b3 2002-09-03 alex
426 43d9a624 2003-01-08 alex /* Search or create channel */
427 1f9ba7b3 2002-09-03 alex chan = Channel_Search( Req->argv[0] );
428 1f9ba7b3 2002-09-03 alex if( ! chan ) chan = Channel_Create( Req->argv[0] );
429 1f9ba7b3 2002-09-03 alex if( ! chan ) return CONNECTED;
430 1f9ba7b3 2002-09-03 alex
431 1f9ba7b3 2002-09-03 alex if( Req->argv[1][0] == '+' )
432 1f9ba7b3 2002-09-03 alex {
433 1f9ba7b3 2002-09-03 alex ptr = Channel_Modes( chan );
434 1f9ba7b3 2002-09-03 alex if( ! *ptr )
435 1f9ba7b3 2002-09-03 alex {
436 bb94d181 2003-01-08 alex /* OK, this channel doesn't have modes jet, set the received ones: */
437 1f9ba7b3 2002-09-03 alex Channel_SetModes( chan, &Req->argv[1][1] );
438 bb94d181 2003-01-08 alex
439 43d9a624 2003-01-08 alex if( Req->argc == 5 )
440 43d9a624 2003-01-08 alex {
441 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
442 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
443 43d9a624 2003-01-08 alex }
444 43d9a624 2003-01-08 alex else
445 43d9a624 2003-01-08 alex {
446 43d9a624 2003-01-08 alex /* Delete modes which we never want to inherit */
447 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'l' );
448 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'k' );
449 43d9a624 2003-01-08 alex }
450 3c0c3c3c 2002-04-23 alex
451 43d9a624 2003-01-08 alex strcpy( modes_add, "" );
452 43d9a624 2003-01-08 alex ptr = Channel_Modes( chan );
453 43d9a624 2003-01-08 alex while( *ptr )
454 43d9a624 2003-01-08 alex {
455 43d9a624 2003-01-08 alex if( *ptr == 'l' )
456 43d9a624 2003-01-08 alex {
457 43d9a624 2003-01-08 alex snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
458 43d9a624 2003-01-08 alex strlcat( modes_add, l, sizeof( modes_add ));
459 43d9a624 2003-01-08 alex }
460 43d9a624 2003-01-08 alex if( *ptr == 'k' )
461 43d9a624 2003-01-08 alex {
462 43d9a624 2003-01-08 alex strlcat( modes_add, " ", sizeof( modes_add ));
463 43d9a624 2003-01-08 alex strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
464 43d9a624 2003-01-08 alex }
465 43d9a624 2003-01-08 alex ptr++;
466 43d9a624 2003-01-08 alex }
467 43d9a624 2003-01-08 alex
468 43d9a624 2003-01-08 alex /* Inform members of this channel */
469 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
470 43d9a624 2003-01-08 alex }
471 43d9a624 2003-01-08 alex }
472 43d9a624 2003-01-08 alex else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
473 43d9a624 2003-01-08 alex
474 43d9a624 2003-01-08 alex if( arg_topic > 0 )
475 43d9a624 2003-01-08 alex {
476 43d9a624 2003-01-08 alex /* We got a topic */
477 1f9ba7b3 2002-09-03 alex ptr = Channel_Topic( chan );
478 43d9a624 2003-01-08 alex if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
479 1f9ba7b3 2002-09-03 alex {
480 43d9a624 2003-01-08 alex /* OK, there is no topic jet */
481 43d9a624 2003-01-08 alex Channel_SetTopic( chan, Req->argv[arg_topic] );
482 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Channel_Topic( chan ));
483 1f9ba7b3 2002-09-03 alex }
484 1f9ba7b3 2002-09-03 alex }
485 3c0c3c3c 2002-04-23 alex
486 43d9a624 2003-01-08 alex /* Forward CHANINFO to other serevrs */
487 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] );
488 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] );
489 43d9a624 2003-01-08 alex else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
490 43d9a624 2003-01-08 alex
491 1f9ba7b3 2002-09-03 alex return CONNECTED;
492 1f9ba7b3 2002-09-03 alex } /* IRC_CHANINFO */
493 1f9ba7b3 2002-09-03 alex
494 1f9ba7b3 2002-09-03 alex
495 2ee05c9a 2002-03-03 alex /* -eof- */