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