Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
10 *
11 * IRC channel commands
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc-channel.c,v 1.30 2005/06/12 18:23:59 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include "defines.h"
26 #include "conn.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "lists.h"
30 #include "log.h"
31 #include "match.h"
32 #include "messages.h"
33 #include "parse.h"
34 #include "irc-info.h"
35 #include "irc-write.h"
36 #include "resolve.h"
37 #include "conf.h"
39 #include "exp.h"
40 #include "irc-channel.h"
43 GLOBAL bool
44 IRC_JOIN( CLIENT *Client, REQUEST *Req )
45 {
46 char *channame, *key, *flags, *topic, modes[8];
47 bool is_new_chan, is_invited, is_banned;
48 CLIENT *target;
49 CHANNEL *chan;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Bad number of arguments? */
55 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
57 /* Who is the sender? */
58 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
59 else target = Client;
60 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
62 /* Are channel keys given? */
63 if( Req->argc > 1 ) key = Req->argv[1];
64 else key = NULL;
66 /* Channel-Namen durchgehen */
67 chan = NULL;
68 channame = strtok( Req->argv[0], "," );
69 while( channame )
70 {
71 chan = NULL; flags = NULL;
73 /* wird der Channel neu angelegt? */
74 if( Channel_Search( channame )) is_new_chan = false;
75 else is_new_chan = true;
77 /* Hat ein Server Channel-User-Modes uebergeben? */
78 if( Client_Type( Client ) == CLIENT_SERVER )
79 {
80 /* Channel-Flags extrahieren */
81 flags = strchr( channame, 0x7 );
82 if( flags )
83 {
84 *flags = '\0';
85 flags++;
86 }
87 }
89 /* Local client? */
90 if( Client_Type( Client ) == CLIENT_USER )
91 {
92 /* Test if the user has reached his maximum channel count */
93 if( Client_Type( Client ) == CLIENT_USER )
94 {
95 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
96 {
97 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
98 return CONNECTED;
99 }
102 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
103 if( is_new_chan )
105 /* Erster User im Channel: Operator-Flag setzen */
106 flags = "o";
108 else
110 /* Existierenden Channel suchen */
111 chan = Channel_Search( channame );
112 assert( chan != NULL );
114 is_banned = Lists_CheckBanned( target, chan );
115 is_invited = Lists_CheckInvited( target, chan );
117 /* Testen, ob Client gebanned ist */
118 if(( is_banned == true) && ( is_invited == false ))
120 /* Client ist gebanned (und nicht invited): */
121 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
123 /* Try next name, if any */
124 channame = strtok( NULL, "," );
125 continue;
128 /* Ist der Channel "invite-only"? */
129 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
131 /* Channel ist "invite-only" und Client wurde nicht invited: */
132 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
134 /* Try next name, if any */
135 channame = strtok( NULL, "," );
136 continue;
139 /* Is the channel protected by a key? */
140 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
142 /* Bad channel key! */
143 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
145 /* Try next name, if any */
146 channame = strtok( NULL, "," );
147 continue;
150 /* Are there already too many members? */
151 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
153 /* Bad channel key! */
154 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
156 /* Try next name, if any */
157 channame = strtok( NULL, "," );
158 continue;
162 else
164 /* Remote server: we don't need to know whether the
165 * client is invited or not, but we have to make sure
166 * that the "one shot" entries (generated by INVITE
167 * commands) in this list become deleted when a user
168 * joins a channel this way. */
169 chan = Channel_Search( channame );
170 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
173 /* Channel joinen (und ggf. anlegen) */
174 if( ! Channel_Join( target, channame ))
176 /* naechsten Namen ermitteln */
177 channame = strtok( NULL, "," );
178 continue;
180 if( ! chan ) chan = Channel_Search( channame );
181 assert( chan != NULL );
183 /* Modes setzen (wenn vorhanden) */
184 while( flags && *flags )
186 Channel_UserModeAdd( chan, target, *flags );
187 flags++;
190 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
191 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
193 /* Muessen Modes an andere Server gemeldet werden? */
194 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
195 if( modes[1] ) modes[0] = 0x7;
196 else modes[0] = '\0';
198 /* An andere Server weiterleiten */
199 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
201 /* im Channel bekannt machen */
202 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
203 if( modes[1] )
205 /* Modes im Channel bekannt machen */
206 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
209 if( Client_Type( Client ) == CLIENT_USER )
211 /* an Client bestaetigen */
212 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
214 /* Topic an Client schicken */
215 topic = Channel_Topic( chan );
216 if( *topic ) IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), channame, topic );
218 /* Mitglieder an Client Melden */
219 IRC_Send_NAMES( Client, chan );
220 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
223 /* naechsten Namen ermitteln */
224 channame = strtok( NULL, "," );
226 return CONNECTED;
227 } /* IRC_JOIN */
230 GLOBAL bool
231 IRC_PART( CLIENT *Client, REQUEST *Req )
233 CLIENT *target;
234 char *chan;
236 assert( Client != NULL );
237 assert( Req != NULL );
239 /* Falsche Anzahl Parameter? */
240 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
242 /* Wer ist der Absender? */
243 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
244 else target = Client;
245 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
247 /* Channel-Namen durchgehen */
248 chan = strtok( Req->argv[0], "," );
249 while( chan )
251 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
253 /* naechsten Namen ermitteln */
254 chan = strtok( NULL, "," );
255 continue;
258 /* naechsten Namen ermitteln */
259 chan = strtok( NULL, "," );
261 return CONNECTED;
262 } /* IRC_PART */
265 GLOBAL bool
266 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
268 CHANNEL *chan;
269 CLIENT *from;
270 char *topic;
272 assert( Client != NULL );
273 assert( Req != NULL );
275 /* Falsche Anzahl Parameter? */
276 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
278 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
279 else from = Client;
280 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
282 /* Welcher Channel? */
283 chan = Channel_Search( Req->argv[0] );
284 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
286 /* Ist der User Mitglied in dem Channel? */
287 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
289 if( Req->argc == 1 )
291 /* Topic erfragen */
292 topic = Channel_Topic( chan );
293 if( *topic ) return IRC_WriteStrClient( from, RPL_TOPIC_MSG, Client_ID( from ), Channel_Name( chan ), topic );
294 else return IRC_WriteStrClient( from, RPL_NOTOPIC_MSG, Client_ID( from ), Channel_Name( chan ));
297 if( strchr( Channel_Modes( chan ), 't' ))
299 /* Topic Lock. Ist der User ein Channel Operator? */
300 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
303 /* Topic setzen */
304 Channel_SetTopic( chan, Req->argv[1] );
305 Log( LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s", Client_Mask( from ), Channel_Name( chan ), Req->argv[1][0] ? Req->argv[1] : "<none>" );
307 /* im Channel bekannt machen und an Server weiterleiten */
308 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
309 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
311 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
312 else return CONNECTED;
313 } /* IRC_TOPIC */
316 /**
317 * Handler for the IRC "LIST" command.
318 * This implementation handles the local case as well as the forwarding of the
319 * LIST command to other servers in the IRC network.
320 */
321 GLOBAL bool
322 IRC_LIST( CLIENT *Client, REQUEST *Req )
324 char *pattern;
325 CHANNEL *chan;
326 CLIENT *from, *target;
328 assert( Client != NULL );
329 assert( Req != NULL );
331 /* Bad number of prameters? */
332 if( Req->argc > 2 )
333 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
334 Client_ID( Client ), Req->command );
336 if( Req->argc > 0 )
337 pattern = strtok( Req->argv[0], "," );
338 else
339 pattern = "*";
341 /* Get sender from prefix, if any */
342 if( Client_Type( Client ) == CLIENT_SERVER )
343 from = Client_Search( Req->prefix );
344 else
345 from = Client;
347 if( ! from )
348 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
349 Client_ID( Client ), Req->prefix );
351 if( Req->argc == 2 )
353 /* Forward to other server? */
354 target = Client_Search( Req->argv[1] );
355 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
356 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
357 Client_ID( Client ), Req->argv[1] );
359 if( target != Client_ThisServer( ))
361 /* Target is indeed an other server, forward it! */
362 return IRC_WriteStrClientPrefix( target, from,
363 "LIST %s :%s", Client_ID( from ),
364 Req->argv[1] );
368 while( pattern )
370 /* Loop through all the channels */
371 chan = Channel_First( );
372 while( chan )
374 /* Check search pattern */
375 if( Match( pattern, Channel_Name( chan )))
377 /* Gotcha! */
378 if( ! strchr( Channel_Modes( chan ), 's' ) ||
379 Channel_IsMemberOf( chan, from ))
381 if( ! IRC_WriteStrClient( from,
382 RPL_LIST_MSG, Client_ID( from ),
383 Channel_Name( chan ),
384 Channel_MemberCount( chan ),
385 Channel_Topic( chan )))
386 return DISCONNECTED;
389 chan = Channel_Next( chan );
392 /* Get next name ... */
393 if( Req->argc > 0 )
394 pattern = strtok( NULL, "," );
395 else
396 pattern = NULL;
399 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
400 } /* IRC_LIST */
403 GLOBAL bool
404 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
406 char modes_add[COMMAND_LEN], l[16], *ptr;
407 CLIENT *from;
408 CHANNEL *chan;
409 int arg_topic;
411 assert( Client != NULL );
412 assert( Req != NULL );
414 /* Bad number of parameters? */
415 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
417 /* Compatibility kludge */
418 if( Req->argc == 5 ) arg_topic = 4;
419 else if( Req->argc == 3 ) arg_topic = 2;
420 else arg_topic = -1;
422 /* Search origin */
423 from = Client_Search( Req->prefix );
424 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
426 /* Search or create channel */
427 chan = Channel_Search( Req->argv[0] );
428 if( ! chan ) chan = Channel_Create( Req->argv[0] );
429 if( ! chan ) return CONNECTED;
431 if( Req->argv[1][0] == '+' )
433 ptr = Channel_Modes( chan );
434 if( ! *ptr )
436 /* OK, this channel doesn't have modes jet, set the received ones: */
437 Channel_SetModes( chan, &Req->argv[1][1] );
439 if( Req->argc == 5 )
441 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
442 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
444 else
446 /* Delete modes which we never want to inherit */
447 Channel_ModeDel( chan, 'l' );
448 Channel_ModeDel( chan, 'k' );
451 strcpy( modes_add, "" );
452 ptr = Channel_Modes( chan );
453 while( *ptr )
455 if( *ptr == 'l' )
457 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
458 strlcat( modes_add, l, sizeof( modes_add ));
460 if( *ptr == 'k' )
462 strlcat( modes_add, " ", sizeof( modes_add ));
463 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
465 ptr++;
468 /* Inform members of this channel */
469 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
472 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
474 if( arg_topic > 0 )
476 /* We got a topic */
477 ptr = Channel_Topic( chan );
478 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
480 /* OK, there is no topic jet */
481 Channel_SetTopic( chan, Req->argv[arg_topic] );
482 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Channel_Topic( chan ));
486 /* Forward CHANINFO to other serevrs */
487 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 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
489 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
491 return CONNECTED;
492 } /* IRC_CHANINFO */
495 /* -eof- */