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