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