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