]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
Implement RFC 2812 handling of "0" argument to JOIN
[ngircd-alex.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.42 2008/02/05 13:31:50 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 /*
44  * RFC 2812, (3.2.1 Join message Command):
45  *  Note that this message
46  *  accepts a special argument ("0"), which is a special request to leave all
47  *  channels the user is currently a member of. The server will process this
48  *  message as if the user had sent a PART command (See Section 3.2.2) for
49  *  each channel he is a member of.
50  */
51 static bool
52 part_from_all_channels(CLIENT* client, CLIENT *target)
53 {
54         CL2CHAN *cl2chan = Channel_FirstChannelOf(target);
55         CHANNEL *chan;
56
57         while (cl2chan) {
58                 chan = Channel_GetChannel(cl2chan);
59                 assert( chan != NULL );
60                 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
61
62                 /* next */
63                 cl2chan = Channel_FirstChannelOf(target);
64         }
65         return CONNECTED;
66 }
67
68
69 GLOBAL bool
70 IRC_JOIN( CLIENT *Client, REQUEST *Req )
71 {
72         char *channame, *channame_ptr, *key, *key_ptr, *flags, *topic, modes[8];
73         bool is_new_chan, is_invited, is_banned;
74         CLIENT *target;
75         CHANNEL *chan;
76
77         assert( Client != NULL );
78         assert( Req != NULL );
79
80         /* Bad number of arguments? */
81         if (Req->argc < 1 || Req->argc > 2)
82                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
83                                           Client_ID(Client), Req->command);
84
85         /* Who is the sender? */
86         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
87         else target = Client;
88         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
89
90         /* Is argument "0"? */
91         if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
92                 return part_from_all_channels(Client, target);
93
94         /* Are channel keys given? */
95         if (Req->argc > 1) {
96                 key = Req->argv[1];
97                 key_ptr = strchr(key, ',');
98                 if (key_ptr) *key_ptr = '\0';
99         }
100         else
101                 key = key_ptr = NULL;
102
103         channame = Req->argv[0];
104         channame_ptr = strchr(channame, ',');
105         if (channame_ptr) *channame_ptr = '\0';
106
107         /* Channel-Namen durchgehen */
108         while (channame)
109         {
110                 chan = NULL; flags = NULL;
111
112                 /* wird der Channel neu angelegt? */
113                 if( Channel_Search( channame )) {
114                         is_new_chan = false;
115                 } else {
116                         if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
117                                 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
118                                 /* Try next name, if any */
119                                 channame = strchr(channame, ',');
120                                 continue;
121                         }
122                         is_new_chan = true;
123                 }
124
125                 /* Hat ein Server Channel-User-Modes uebergeben? */
126                 if( Client_Type( Client ) == CLIENT_SERVER )
127                 {
128                         /* Channel-Flags extrahieren */
129                         flags = strchr( channame, 0x7 );
130                         if( flags )
131                         {
132                                 *flags = '\0';
133                                 flags++;
134                         }
135                 }
136
137                 /* Local client? */
138                 if( Client_Type( Client ) == CLIENT_USER )
139                 {
140                         /* Test if the user has reached his maximum channel count */
141                         if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
142                                 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
143                                                         Client_ID( Client ), channame );
144
145                         /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
146                         if( is_new_chan )
147                         {
148                                 /* Erster User im Channel: Operator-Flag setzen */
149                                 flags = "o";
150                         }
151                         else
152                         {
153                                 /* Existierenden Channel suchen */
154                                 chan = Channel_Search( channame );
155                                 assert( chan != NULL );
156
157                                 is_banned = Lists_Check(Channel_GetListBans(chan), target );
158                                 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
159
160                                 /* Testen, ob Client gebanned ist */
161                                 if(( is_banned == true) &&  ( is_invited == false ))
162                                 {
163                                         /* Client ist gebanned (und nicht invited): */
164                                         IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
165
166                                         /* Try next name, if any */
167                                         channame = strchr(channame, ',');
168                                         continue;
169                                 }
170
171                                 /* Ist der Channel "invite-only"? */
172                                 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
173                                 {
174                                         /* Channel ist "invite-only" und Client wurde nicht invited: */
175                                         IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
176
177                                         /* Try next name, if any */
178                                         channame = strchr(channame, ',');
179                                         continue;
180                                 }
181
182                                 /* Is the channel protected by a key? */
183                                 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
184                                 {
185                                         /* Bad channel key! */
186                                         IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
187
188                                         /* Try next name, if any */
189                                         channame = strchr(channame, ',');
190                                         continue;
191                                 }
192
193                                 /* Are there already too many members? */
194                                 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
195                                 {
196                                         /* Bad channel key! */
197                                         IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
198
199                                         /* Try next name, if any */
200                                         channame = strchr(channame, ',');
201                                         continue;
202                                 }
203                         }
204                 }
205                 else
206                 {
207                         /* Remote server: we don't need to know whether the
208                          * client is invited or not, but we have to make sure
209                          * that the "one shot" entries (generated by INVITE
210                          * commands) in this list become deleted when a user
211                          * joins a channel this way. */
212                         chan = Channel_Search( channame );
213                         if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
214                 }
215
216                 /* Channel joinen (und ggf. anlegen) */
217                 if( ! Channel_Join( target, channame ))
218                 {
219                         /* naechsten Namen ermitteln */
220                         channame = strchr(channame, ',');
221                         continue;
222                 }
223                 if( ! chan ) chan = Channel_Search( channame );
224                 assert( chan != NULL );
225
226                 /* Modes setzen (wenn vorhanden) */
227                 while( flags && *flags )
228                 {
229                         Channel_UserModeAdd( chan, target, *flags );
230                         flags++;
231                 }
232
233                 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
234                 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
235
236                 /* Muessen Modes an andere Server gemeldet werden? */
237                 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
238                 if( modes[1] ) modes[0] = 0x7;
239                 else modes[0] = '\0';
240
241                 /* An andere Server weiterleiten */
242                 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
243
244                 /* im Channel bekannt machen */
245                 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
246                 if( modes[1] )
247                 {
248                         /* Modes im Channel bekannt machen */
249                         IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
250                 }
251
252                 if( Client_Type( Client ) == CLIENT_USER )
253                 {
254                         /* an Client bestaetigen */
255                         IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
256
257                         /* Send topic to client, if any */
258                         topic = Channel_Topic(chan);
259                         if (*topic) {
260                                 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
261                                         Client_ID(Client), channame, topic);
262 #ifndef STRICT_RFC
263                                 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
264                                         Client_ID(Client), channame,
265                                         Channel_TopicWho(chan),
266                                         Channel_TopicTime(chan));
267 #endif
268                         }
269
270                         /* Mitglieder an Client Melden */
271                         IRC_Send_NAMES( Client, chan );
272                         IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
273                 }
274
275                 /* next channel? */
276                 channame = channame_ptr;
277                 if (channame) {
278                         channame++;
279                         channame_ptr = strchr(channame, ',');
280                         if (channame_ptr) *channame_ptr = '\0';
281
282                         if (key_ptr) {
283                                 key = ++key_ptr;
284                                 key_ptr = strchr(key, ',');
285                                 if (key_ptr) *key_ptr = '\0';
286                         }
287                 }
288         }
289         return CONNECTED;
290 } /* IRC_JOIN */
291
292
293 GLOBAL bool
294 IRC_PART( CLIENT *Client, REQUEST *Req )
295 {
296         CLIENT *target;
297         char *chan;
298
299         assert( Client != NULL );
300         assert( Req != NULL );
301
302         if (Req->argc < 1 || Req->argc > 2)
303                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
304                                         Client_ID(Client), Req->command);
305
306         /* Wer ist der Absender? */
307         if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
308         else target = Client;
309         if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
310
311         /* Channel-Namen durchgehen */
312         chan = strtok(Req->argv[0], ",");
313         while (chan) {
314                 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
315
316                 chan = strtok(NULL, ",");
317         }
318         return CONNECTED;
319 } /* IRC_PART */
320
321
322 GLOBAL bool
323 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
324 {
325         CHANNEL *chan;
326         CLIENT *from;
327         char *topic;
328         bool r;
329
330         assert( Client != NULL );
331         assert( Req != NULL );
332
333         /* Falsche Anzahl Parameter? */
334         if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
335
336         if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
337         else from = Client;
338         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
339
340         /* Welcher Channel? */
341         chan = Channel_Search( Req->argv[0] );
342         if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
343
344         /* Ist der User Mitglied in dem Channel? */
345         if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
346
347         if( Req->argc == 1 )
348         {
349                 /* Request actual topic */
350                 topic = Channel_Topic(chan);
351                 if (*topic) {
352                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
353                                 Client_ID(Client), Channel_Name(chan), topic);
354 #ifndef STRICT_RFC
355                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
356                                 Client_ID(Client), Channel_Name(chan),
357                                 Channel_TopicWho(chan),
358                                 Channel_TopicTime(chan));
359 #endif
360                         return r;
361                 }
362                 else
363                          return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
364                                         Client_ID(from), Channel_Name(chan));
365         }
366
367         if( strchr( Channel_Modes( chan ), 't' ))
368         {
369                 /* Topic Lock. Ist der User ein Channel Operator? */
370                 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
371         }
372
373         /* Set new topic */
374         Channel_SetTopic(chan, from, Req->argv[1]);
375         Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
376                 Client_Mask(from), Channel_Name(chan),
377                 Req->argv[1][0] ? Req->argv[1] : "<none>");
378
379         /* im Channel bekannt machen und an Server weiterleiten */
380         IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
381         IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
382
383         if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
384         else return CONNECTED;
385 } /* IRC_TOPIC */
386
387
388 /**
389  * Handler for the IRC "LIST" command.
390  * This implementation handles the local case as well as the forwarding of the
391  * LIST command to other servers in the IRC network.
392  */
393 GLOBAL bool
394 IRC_LIST( CLIENT *Client, REQUEST *Req )
395 {
396         char *pattern;
397         CHANNEL *chan;
398         CLIENT *from, *target;
399
400         assert( Client != NULL );
401         assert( Req != NULL );
402
403         /* Bad number of prameters? */
404         if( Req->argc > 2 )
405                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
406                         Client_ID( Client ), Req->command );
407
408         if( Req->argc > 0 )
409                 pattern = strtok( Req->argv[0], "," );
410         else
411                 pattern = "*";
412
413         /* Get sender from prefix, if any */
414         if( Client_Type( Client ) == CLIENT_SERVER )
415                 from = Client_Search( Req->prefix );
416         else
417                 from = Client;
418
419         if( ! from )
420                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
421                                 Client_ID( Client ), Req->prefix );
422
423         if( Req->argc == 2 )
424         {
425                 /* Forward to other server? */
426                 target = Client_Search( Req->argv[1] );
427                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
428                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
429                                         Client_ID( Client ), Req->argv[1] );
430
431                 if( target != Client_ThisServer( ))
432                 {
433                         /* Target is indeed an other server, forward it! */
434                         return IRC_WriteStrClientPrefix( target, from,
435                                         "LIST %s :%s", Client_ID( from ),
436                                         Req->argv[1] );
437                 }
438         }
439         
440         while( pattern )
441         {
442                 /* Loop through all the channels */
443                 chan = Channel_First( );
444                 while( chan )
445                 {
446                         /* Check search pattern */
447                         if( Match( pattern, Channel_Name( chan )))
448                         {
449                                 /* Gotcha! */
450                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
451                                     Channel_IsMemberOf( chan, from ))
452                                 {
453                                         if( ! IRC_WriteStrClient( from,
454                                             RPL_LIST_MSG, Client_ID( from ),
455                                             Channel_Name( chan ),
456                                             Channel_MemberCount( chan ),
457                                             Channel_Topic( chan )))
458                                                 return DISCONNECTED;
459                                 }
460                         }
461                         chan = Channel_Next( chan );
462                 }
463                 
464                 /* Get next name ... */
465                 if( Req->argc > 0 )
466                         pattern = strtok( NULL, "," );
467                 else
468                         pattern = NULL;
469         }
470         
471         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
472 } /* IRC_LIST */
473
474
475 GLOBAL bool
476 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
477 {
478         char modes_add[COMMAND_LEN], l[16], *ptr;
479         CLIENT *from;
480         CHANNEL *chan;
481         int arg_topic;
482
483         assert( Client != NULL );
484         assert( Req != NULL );
485
486         /* Bad number of parameters? */
487         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
488
489         /* Compatibility kludge */
490         if( Req->argc == 5 ) arg_topic = 4;
491         else if( Req->argc == 3 ) arg_topic = 2;
492         else arg_topic = -1;
493
494         /* Search origin */
495         from = Client_Search( Req->prefix );
496         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
497
498         /* Search or create channel */
499         chan = Channel_Search( Req->argv[0] );
500         if( ! chan ) chan = Channel_Create( Req->argv[0] );
501         if( ! chan ) return CONNECTED;
502
503         if( Req->argv[1][0] == '+' )
504         {
505                 ptr = Channel_Modes( chan );
506                 if( ! *ptr )
507                 {
508                         /* OK, this channel doesn't have modes jet, set the received ones: */
509                         Channel_SetModes( chan, &Req->argv[1][1] );
510
511                         if( Req->argc == 5 )
512                         {
513                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
514                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
515                         }
516                         else
517                         {
518                                 /* Delete modes which we never want to inherit */
519                                 Channel_ModeDel( chan, 'l' );
520                                 Channel_ModeDel( chan, 'k' );
521                         }
522
523                         strcpy( modes_add, "" );
524                         ptr = Channel_Modes( chan );
525                         while( *ptr )
526                         {
527                                 if( *ptr == 'l' )
528                                 {
529                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
530                                         strlcat( modes_add, l, sizeof( modes_add ));
531                                 }
532                                 if( *ptr == 'k' )
533                                 {
534                                         strlcat( modes_add, " ", sizeof( modes_add ));
535                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
536                                 }
537                                 ptr++;
538                         }
539                         
540                         /* Inform members of this channel */
541                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
542                 }
543         }
544         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
545
546         if( arg_topic > 0 )
547         {
548                 /* We got a topic */
549                 ptr = Channel_Topic( chan );
550                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
551                 {
552                         /* OK, there is no topic jet */
553                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
554                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
555                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
556                 }
557         }
558
559         /* Forward CHANINFO to other serevrs */
560         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] );
561         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
562         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
563
564         return CONNECTED;
565 } /* IRC_CHANINFO */
566
567
568 /* -eof- */