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