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