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