]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/channel.c
e31b268f509da533bbe888d65e9157fd817e78da
[ngircd-alex.git] / src / ngircd / channel.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 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  * Channel management
12  */
13
14
15 #define __channel_c__
16
17
18 #include "portab.h"
19
20 static char UNUSED id[] = "$Id: channel.c,v 1.61 2006/12/07 22:23:39 fw Exp $";
21
22 #include "imp.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <strings.h>
28
29 #include "defines.h"
30 #include "conn-func.h"
31 #include "client.h"
32
33 #include "exp.h"
34 #include "channel.h"
35
36 #include "imp.h"
37 #include "irc-write.h"
38 #include "resolve.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "lists.h"
42 #include "log.h"
43 #include "messages.h"
44
45 #include "exp.h"
46
47
48 #define REMOVE_PART 0
49 #define REMOVE_QUIT 1
50 #define REMOVE_KICK 2
51
52
53 static CHANNEL *My_Channels;
54 static CL2CHAN *My_Cl2Chan;
55
56
57 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
59 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
60 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
61 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
62 static bool Delete_Channel PARAMS(( CHANNEL *Chan ));
63
64
65 GLOBAL void
66 Channel_Init( void )
67 {
68         My_Channels = NULL;
69         My_Cl2Chan = NULL;
70 } /* Channel_Init */
71
72
73 GLOBAL struct list_head *
74 Channel_GetListBans(CHANNEL *c)
75 {
76         assert(c != NULL);
77         return &c->list_bans;
78 }
79
80
81 GLOBAL struct list_head *
82 Channel_GetListInvites(CHANNEL *c)
83 {
84         assert(c != NULL);
85         return &c->list_invites;
86 }
87
88
89 GLOBAL void
90 Channel_InitPredefined( void )
91 {
92         /* Vordefinierte persistente Channels erzeugen */
93
94         CHANNEL *chan;
95         char *c;
96         unsigned int i;
97         
98         for( i = 0; i < Conf_Channel_Count; i++ )
99         {
100                 /* Ist ein Name konfiguriert? */
101                 if( ! Conf_Channel[i].name[0] ) continue;
102
103                 /* Gueltiger Channel-Name? */
104                 if( ! Channel_IsValidName( Conf_Channel[i].name ))
105                 {
106                         Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
107                         array_free(&Conf_Channel[i].topic);
108                         continue;
109                 }
110
111                 /* Gibt es den Channel bereits? */
112                 chan = Channel_Search( Conf_Channel[i].name );
113                 if( chan )
114                 {
115                         Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
116                         array_free(&Conf_Channel[i].topic);
117                         continue;
118                 }
119
120                 /* Create channel */
121                 chan = Channel_Create(Conf_Channel[i].name);
122                 if (chan) {
123                         Channel_ModeAdd(chan, 'P');
124
125                         if (array_start(&Conf_Channel[i].topic) != NULL)
126                                 Channel_SetTopic(chan, NULL,
127                                          array_start(&Conf_Channel[i].topic));
128                         array_free(&Conf_Channel[i].topic);
129
130                         c = Conf_Channel[i].modes;
131                         while (*c)
132                                 Channel_ModeAdd(chan, *c++);
133
134                         Log(LOG_INFO, "Created pre-defined channel \"%s\".",
135                                                         Conf_Channel[i].name );
136                 }
137                 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
138                                                         Conf_Channel[i].name );
139         }
140 } /* Channel_InitPredefined */
141
142
143 GLOBAL void
144 Channel_Exit( void )
145 {
146         CHANNEL *c, *c_next;
147         CL2CHAN *cl2chan, *cl2chan_next;
148         
149         /* Channel-Strukturen freigeben */
150         c = My_Channels;
151         while( c )
152         {
153                 c_next = c->next;
154                 array_free(&c->topic);
155                 free( c );
156                 c = c_next;
157         }
158
159         /* Channel-Zuordnungstabelle freigeben */
160         cl2chan = My_Cl2Chan;
161         while( c )
162         {
163                 cl2chan_next = cl2chan->next;
164                 free( cl2chan );
165                 cl2chan = cl2chan_next;
166         }
167 } /* Channel_Exit */
168
169
170 GLOBAL bool
171 Channel_Join( CLIENT *Client, char *Name )
172 {
173         CHANNEL *chan;
174         
175         assert( Client != NULL );
176         assert( Name != NULL );
177
178         if( ! Channel_IsValidName( Name )) {
179                 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
180                 return false;
181         }
182
183         chan = Channel_Search( Name );
184         if( chan ) {
185                 /* Ist der Client bereits Mitglied? */
186                 if( Get_Cl2Chan( chan, Client )) return false;
187         }
188         else
189         {
190                 /* Gibt es noch nicht? Dann neu anlegen: */
191                 chan = Channel_Create( Name );
192                 if (!chan) return false;
193         }
194
195         /* User dem Channel hinzufuegen */
196         if( ! Add_Client( chan, Client )) return false;
197         else return true;
198 } /* Channel_Join */
199
200
201 GLOBAL bool
202 Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
203 {
204         CHANNEL *chan;
205
206         assert( Client != NULL );
207         assert( Name != NULL );
208         assert( Reason != NULL );
209
210         chan = Channel_Search( Name );
211         if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
212         {
213                 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
214                 return false;
215         }
216
217         /* User aus Channel entfernen */
218         if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
219         else return true;
220 } /* Channel_Part */
221
222
223 GLOBAL void
224 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
225 {
226         CHANNEL *chan;
227
228         assert( Client != NULL );
229         assert( Origin != NULL );
230         assert( Name != NULL );
231         assert( Reason != NULL );
232
233         chan = Channel_Search( Name );
234         if( ! chan )
235         {
236                 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
237                 return;
238         }
239
240         if( ! Channel_IsMemberOf( chan, Origin ))
241         {
242                 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
243                 return;
244         }
245
246         /* Is User Channel-Operator? */
247         if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
248         {
249                 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
250                 return;
251         }
252
253         /* Ist the kickED User member of channel? */
254         if( ! Channel_IsMemberOf( chan, Client ))
255         {
256                 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
257                 return;
258         }
259
260         Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
261 } /* Channel_Kick */
262
263
264 GLOBAL void
265 Channel_Quit( CLIENT *Client, char *Reason )
266 {
267         CHANNEL *c, *next_c;
268
269         assert( Client != NULL );
270         assert( Reason != NULL );
271
272         IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
273
274         c = My_Channels;
275         while( c )
276         {
277                 next_c = c->next;
278                 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
279                 c = next_c;
280         }
281 } /* Channel_Quit */
282
283
284 GLOBAL unsigned long
285 Channel_Count( void )
286 {
287         CHANNEL *c;
288         unsigned long count = 0;
289         
290         c = My_Channels;
291         while( c )
292         {
293                 count++;
294                 c = c->next;
295         }
296         return count;
297 } /* Channel_Count */
298
299
300 GLOBAL unsigned long
301 Channel_MemberCount( CHANNEL *Chan )
302 {
303         CL2CHAN *cl2chan;
304         unsigned long count = 0;
305
306         assert( Chan != NULL );
307
308         cl2chan = My_Cl2Chan;
309         while( cl2chan )
310         {
311                 if( cl2chan->channel == Chan ) count++;
312                 cl2chan = cl2chan->next;
313         }
314         return count;
315 } /* Channel_MemberCount */
316
317
318 GLOBAL int
319 Channel_CountForUser( CLIENT *Client )
320 {
321         /* Count number of channels a user is member of. */
322
323         CL2CHAN *cl2chan;
324         int count = 0;
325         
326         assert( Client != NULL );
327         
328         cl2chan = My_Cl2Chan;
329         while( cl2chan )
330         {
331                 if( cl2chan->client == Client ) count++;
332                 cl2chan = cl2chan->next;
333         }
334
335         return count;
336 } /* Channel_CountForUser */
337
338
339
340 GLOBAL const char *
341 Channel_Name( const CHANNEL *Chan )
342 {
343         assert( Chan != NULL );
344         return Chan->name;
345 } /* Channel_Name */
346
347
348 GLOBAL char *
349 Channel_Modes( CHANNEL *Chan )
350 {
351         assert( Chan != NULL );
352         return Chan->modes;
353 } /* Channel_Modes */
354
355
356 GLOBAL char *
357 Channel_Key( CHANNEL *Chan )
358 {
359         assert( Chan != NULL );
360         return Chan->key;
361 } /* Channel_Key */
362
363
364 GLOBAL unsigned long
365 Channel_MaxUsers( CHANNEL *Chan )
366 {
367         assert( Chan != NULL );
368         return Chan->maxusers;
369 } /* Channel_MaxUsers */
370
371
372 GLOBAL CHANNEL *
373 Channel_First( void )
374 {
375         return My_Channels;
376 } /* Channel_First */
377
378
379 GLOBAL CHANNEL *
380 Channel_Next( CHANNEL *Chan )
381 {
382         assert( Chan != NULL );
383         return Chan->next;
384 } /* Channel_Next */
385
386
387 GLOBAL CHANNEL *
388 Channel_Search( char *Name )
389 {
390         /* Channel-Struktur suchen */
391         
392         CHANNEL *c;
393         UINT32 search_hash;
394
395         assert( Name != NULL );
396
397         search_hash = Hash( Name );
398         c = My_Channels;
399         while( c )
400         {
401                 if( search_hash == c->hash )
402                 {
403                         /* lt. Hash-Wert: Treffer! */
404                         if( strcasecmp( Name, c->name ) == 0 ) return c;
405                 }
406                 c = c->next;
407         }
408         return NULL;
409 } /* Channel_Search */
410
411
412 GLOBAL CL2CHAN *
413 Channel_FirstMember( CHANNEL *Chan )
414 {
415         assert( Chan != NULL );
416         return Get_First_Cl2Chan( NULL, Chan );
417 } /* Channel_FirstMember */
418
419
420 GLOBAL CL2CHAN *
421 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
422 {
423         assert( Chan != NULL );
424         assert( Cl2Chan != NULL );
425         return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
426 } /* Channel_NextMember */
427
428
429 GLOBAL CL2CHAN *
430 Channel_FirstChannelOf( CLIENT *Client )
431 {
432         assert( Client != NULL );
433         return Get_First_Cl2Chan( Client, NULL );
434 } /* Channel_FirstChannelOf */
435
436
437 GLOBAL CL2CHAN *
438 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
439 {
440         assert( Client != NULL );
441         assert( Cl2Chan != NULL );
442         return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
443 } /* Channel_NextChannelOf */
444
445
446 GLOBAL CLIENT *
447 Channel_GetClient( CL2CHAN *Cl2Chan )
448 {
449         assert( Cl2Chan != NULL );
450         return Cl2Chan->client;
451 } /* Channel_GetClient */
452
453
454 GLOBAL CHANNEL *
455 Channel_GetChannel( CL2CHAN *Cl2Chan )
456 {
457         assert( Cl2Chan != NULL );
458         return Cl2Chan->channel;
459 } /* Channel_GetChannel */
460
461
462 GLOBAL bool
463 Channel_IsValidName( const char *Name )
464 {
465         assert( Name != NULL );
466
467         if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
468
469         return Name[strcspn(Name, " ,:\007")] == 0;
470 } /* Channel_IsValidName */
471
472
473 GLOBAL bool
474 Channel_ModeAdd( CHANNEL *Chan, char Mode )
475 {
476         /* set Mode.
477          * If the channel already had this mode, return false.
478          * If the channel mode was newly set return true.
479          */
480
481         char x[2];
482
483         assert( Chan != NULL );
484
485         x[0] = Mode; x[1] = '\0';
486         if( ! strchr( Chan->modes, x[0] ))
487         {
488                 /* Channel does not have this mode yet, set it */
489                 strlcat( Chan->modes, x, sizeof( Chan->modes ));
490                 return true;
491         }
492         else return false;
493 } /* Channel_ModeAdd */
494
495
496 GLOBAL bool
497 Channel_ModeDel( CHANNEL *Chan, char Mode )
498 {
499         /* Delete mode.
500          * if the mode was removed return true.
501          * if the channel did not have the mode, return false.
502         */
503         char *p;
504
505         assert( Chan != NULL );
506
507         p = strchr( Chan->modes, Mode );
508         if( ! p ) return false;
509
510         /* Channel has mode -> delete */
511         while( *p )
512         {
513                 *p = *(p + 1);
514                 p++;
515         }
516         return true;
517 } /* Channel_ModeDel */
518
519
520 GLOBAL bool
521 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
522 {
523         /* Set Channel-User-Mode.
524          * if mode was newly set, return true.
525          * if the User already had this channel-mode, return false.
526          */
527
528         CL2CHAN *cl2chan;
529         char x[2];
530
531         assert( Chan != NULL );
532         assert( Client != NULL );
533
534         cl2chan = Get_Cl2Chan( Chan, Client );
535         assert( cl2chan != NULL );
536
537         x[0] = Mode; x[1] = '\0';
538         if( ! strchr( cl2chan->modes, x[0] ))
539         {
540                 /* mode not set, -> set it */
541                 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
542                 return true;
543         }
544         else return false;
545 } /* Channel_UserModeAdd */
546
547
548 GLOBAL bool
549 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
550 {
551         /* Delete Channel-User-Mode.
552          * If Mode was removed, return true.
553          * If User did not have the Channel-Mode, return false.
554          */
555
556         CL2CHAN *cl2chan;
557         char *p;
558
559         assert( Chan != NULL );
560         assert( Client != NULL );
561
562         cl2chan = Get_Cl2Chan( Chan, Client );
563         assert( cl2chan != NULL );
564
565         p = strchr( cl2chan->modes, Mode );
566         if( ! p ) return false;
567
568         /* Client has Mode -> delete */
569         while( *p )
570         {
571                 *p = *(p + 1);
572                 p++;
573         }
574         return true;
575 } /* Channel_UserModeDel */
576
577
578 GLOBAL char *
579 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
580 {
581         /* return Users' Channel-Modes */
582
583         CL2CHAN *cl2chan;
584
585         assert( Chan != NULL );
586         assert( Client != NULL );
587
588         cl2chan = Get_Cl2Chan( Chan, Client );
589         assert( cl2chan != NULL );
590
591         return cl2chan->modes;
592 } /* Channel_UserModes */
593
594
595 GLOBAL bool
596 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
597 {
598         /* Test if Client is on Channel Chan */
599
600         assert( Chan != NULL );
601         assert( Client != NULL );
602         return Get_Cl2Chan(Chan, Client);
603 } /* Channel_IsMemberOf */
604
605
606 GLOBAL char *
607 Channel_Topic( CHANNEL *Chan )
608 {
609         char *ret;
610         assert( Chan != NULL );
611         ret = array_start(&Chan->topic);
612         return ret ? ret : "";
613 } /* Channel_Topic */
614
615         
616 #ifndef STRICT_RFC
617
618 GLOBAL unsigned int
619 Channel_TopicTime(CHANNEL *Chan)
620 {
621         assert(Chan != NULL);
622         return (unsigned int) Chan->topic_time;
623 } /* Channel_TopicTime */
624
625
626 GLOBAL char *
627 Channel_TopicWho(CHANNEL *Chan)
628 {
629         assert(Chan != NULL);
630         return Chan->topic_who;
631 } /* Channel_TopicWho */
632
633 #endif
634
635
636 GLOBAL void
637 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
638 {
639         size_t len;
640         assert( Chan != NULL );
641         assert( Topic != NULL );
642
643         len = strlen(Topic);
644         if (len < array_bytes(&Chan->topic))
645                 array_free(&Chan->topic);
646
647         if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
648                 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
649                                         Topic, Chan->name, strerror(errno));
650 #ifndef STRICT_RFC
651         Chan->topic_time = time(NULL);
652         if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
653                 strlcpy(Chan->topic_who, Client_ID(Client),
654                         sizeof Chan->topic_who);
655         else
656                 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
657                         sizeof Chan->topic_who);
658 #else
659         (void) Client;
660 #endif
661 } /* Channel_SetTopic */
662
663
664 GLOBAL void
665 Channel_SetModes( CHANNEL *Chan, char *Modes )
666 {
667         assert( Chan != NULL );
668         assert( Modes != NULL );
669
670         strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
671 } /* Channel_SetModes */
672
673
674 GLOBAL void
675 Channel_SetKey( CHANNEL *Chan, char *Key )
676 {
677         assert( Chan != NULL );
678         assert( Key != NULL );
679
680         strlcpy( Chan->key, Key, sizeof( Chan->key ));
681         LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
682 } /* Channel_SetKey */
683
684
685 GLOBAL void
686 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
687 {
688         assert( Chan != NULL );
689
690         Chan->maxusers = Count;
691         LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
692 } /* Channel_SetMaxUsers */
693
694
695 GLOBAL bool
696 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
697 {
698         bool is_member, has_voice, is_op, ok;
699
700         /* Okay, target is a channel */
701         is_member = has_voice = is_op = false;
702         if( Channel_IsMemberOf( Chan, From ))
703         {
704                 is_member = true;
705                 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
706                 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
707         }
708
709         /* Is the client allowed to write to channel? */
710         ok = true;
711         if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
712         if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
713
714         /* Is the client banned? */
715         if( Lists_Check(&Chan->list_bans, From))
716         {
717                 /* Client is banned, but is he channel operator or has voice? */
718                 if(( ! has_voice ) && ( ! is_op )) ok = false;
719         }
720
721         if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
722
723         /* Send text */
724         if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
725         return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
726 } /* Channel_Write */
727
728
729 GLOBAL CHANNEL *
730 Channel_Create( char *Name )
731 {
732         /* Create new CHANNEL structure and add it to linked list */
733         CHANNEL *c;
734
735         assert( Name != NULL );
736
737         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
738         if( ! c )
739         {
740                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
741                 return NULL;
742         }
743         memset( c, 0, sizeof( CHANNEL ));
744         strlcpy( c->name, Name, sizeof( c->name ));
745         c->hash = Hash( c->name );
746         c->next = My_Channels;
747         My_Channels = c;
748         LogDebug("Created new channel structure for \"%s\".", Name);
749         return c;
750 } /* Channel_Create */
751
752
753 static CL2CHAN *
754 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
755 {
756         CL2CHAN *cl2chan;
757
758         assert( Chan != NULL );
759         assert( Client != NULL );
760
761         cl2chan = My_Cl2Chan;
762         while( cl2chan )
763         {
764                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
765                 cl2chan = cl2chan->next;
766         }
767         return NULL;
768 } /* Get_Cl2Chan */
769
770
771 static CL2CHAN *
772 Add_Client( CHANNEL *Chan, CLIENT *Client )
773 {
774         CL2CHAN *cl2chan;
775
776         assert( Chan != NULL );
777         assert( Client != NULL );
778
779         /* neue CL2CHAN-Struktur anlegen */
780         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
781         if( ! cl2chan )
782         {
783                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
784                 return NULL;
785         }
786         cl2chan->channel = Chan;
787         cl2chan->client = Client;
788         strcpy( cl2chan->modes, "" );
789
790         /* Verketten */
791         cl2chan->next = My_Cl2Chan;
792         My_Cl2Chan = cl2chan;
793
794         Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
795
796         return cl2chan;
797 } /* Add_Client */
798
799
800 static bool
801 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
802 {
803         CL2CHAN *cl2chan, *last_cl2chan;
804         CHANNEL *c;
805         
806         assert( Chan != NULL );
807         assert( Client != NULL );
808         assert( Origin != NULL );
809         assert( Reason != NULL );
810
811         last_cl2chan = NULL;
812         cl2chan = My_Cl2Chan;
813         while( cl2chan )
814         {
815                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
816                 last_cl2chan = cl2chan;
817                 cl2chan = cl2chan->next;
818         }
819         if( ! cl2chan ) return false;
820
821         c = cl2chan->channel;
822         assert( c != NULL );
823
824         /* Aus Verkettung loesen und freigeben */
825         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
826         else My_Cl2Chan = cl2chan->next;
827         free( cl2chan );
828
829         switch( Type )
830         {
831                 case REMOVE_QUIT:
832                         /* QUIT: other servers have already been notified, see Client_Destroy();
833                          * so only inform other clients in same channel. */
834                         assert( InformServer == false );
835                         LogDebug("User \"%s\" left channel \"%s\" (%s).",
836                                         Client_Mask( Client ), c->name, Reason );
837                         break;
838                 case REMOVE_KICK:
839                         /* User was KICKed: inform other servers and all users in channel */
840                         if( InformServer )
841                                 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
842                                         Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
843                         IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
844                                                         c->name, Client_ID( Client ), Reason );
845                         if ((Client_Conn(Client) > NONE) &&
846                                         (Client_Type(Client) == CLIENT_USER))
847                         {
848                                 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
849                                                                 c->name, Client_ID( Client ), Reason);
850                         }
851                         LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
852                                 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
853                         break;
854                 default: /* PART */
855                         if (InformServer)
856                                 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
857
858                         IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
859                                                                         c->name, Reason);
860
861                         if ((Client_Conn(Origin) > NONE) &&
862                                         (Client_Type(Origin) == CLIENT_USER))
863                         {
864                                 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
865                                 LogDebug("User \"%s\" left channel \"%s\" (%s).",
866                                         Client_Mask(Client), c->name, Reason);
867                         }
868         }
869
870         /* Wenn Channel nun leer und nicht pre-defined: loeschen */
871         if( ! strchr( Channel_Modes( Chan ), 'P' ))
872         {
873                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
874         }
875                 
876         return true;
877 } /* Remove_Client */
878
879
880 GLOBAL bool
881 Channel_AddBan(CHANNEL *c, const char *mask )
882 {
883         struct list_head *h = Channel_GetListBans(c);
884         return Lists_Add(h, mask, false);
885 }
886
887
888 GLOBAL bool
889 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
890 {
891         struct list_head *h = Channel_GetListInvites(c);
892         return Lists_Add(h, mask, onlyonce);
893 }
894
895
896 static bool
897 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
898 {
899         struct list_elem *e;
900         char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
901         char *msg_end;
902
903         assert( Client != NULL );
904         assert( Channel != NULL );
905
906         e = Lists_GetFirst(head);
907         while (e) {
908                 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
909                                 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
910                 e = Lists_GetNext(e);
911         }
912
913         msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
914         return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
915 }
916
917
918 GLOBAL bool
919 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
920 {
921         struct list_head *h;
922
923         assert( Channel != NULL );
924
925         h = Channel_GetListBans(Channel);
926         return ShowInvitesBans(h, Client, Channel, false);
927 }
928
929
930 GLOBAL bool
931 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
932 {
933         struct list_head *h;
934
935         assert( Channel != NULL );
936
937         h = Channel_GetListInvites(Channel);
938         return ShowInvitesBans(h, Client, Channel, true);
939 }
940
941
942 static CL2CHAN *
943 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
944 {
945         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
946 } /* Get_First_Cl2Chan */
947
948
949 static CL2CHAN *
950 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
951 {
952         CL2CHAN *cl2chan;
953
954         assert( Client != NULL || Channel != NULL );
955         
956         cl2chan = Start;
957         while( cl2chan )
958         {
959                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
960                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
961                 cl2chan = cl2chan->next;
962         }
963         return NULL;
964 } /* Get_Next_Cl2Chan */
965
966
967 static bool
968 Delete_Channel( CHANNEL *Chan )
969 {
970         /* Channel-Struktur loeschen */
971
972         CHANNEL *chan, *last_chan;
973
974         last_chan = NULL;
975         chan = My_Channels;
976         while( chan )
977         {
978                 if( chan == Chan ) break;
979                 last_chan = chan;
980                 chan = chan->next;
981         }
982         if( ! chan ) return false;
983
984         Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
985
986         /* Invite- und Ban-Lists aufraeumen */
987         Lists_Free( &chan->list_bans );
988         Lists_Free( &chan->list_invites );
989
990         /* Neu verketten und freigeben */
991         if( last_chan ) last_chan->next = chan->next;
992         else My_Channels = chan->next;
993         free( chan );
994
995         return true;
996 } /* Delete_Channel */
997
998
999 /* -eof- */