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