]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/channel.c
moved invite/ban lists to channel structure
[ngircd.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.60 2006/12/07 17:57:20 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 GLOBAL int
340 Channel_PCount( void )
341 {
342         /* Count the number of persistent (mode 'P') channels */
343
344         CHANNEL *chan;
345         int count = 0;
346
347         chan = My_Channels;
348         while( chan )
349         {
350                 if( strchr( chan->modes, 'P' )) count++;
351                 chan = chan->next;
352         }
353
354         return count;
355 } /* Channel_PCount */
356
357
358 GLOBAL const char *
359 Channel_Name( const CHANNEL *Chan )
360 {
361         assert( Chan != NULL );
362         return Chan->name;
363 } /* Channel_Name */
364
365
366 GLOBAL char *
367 Channel_Modes( CHANNEL *Chan )
368 {
369         assert( Chan != NULL );
370         return Chan->modes;
371 } /* Channel_Modes */
372
373
374 GLOBAL char *
375 Channel_Key( CHANNEL *Chan )
376 {
377         assert( Chan != NULL );
378         return Chan->key;
379 } /* Channel_Key */
380
381
382 GLOBAL unsigned long
383 Channel_MaxUsers( CHANNEL *Chan )
384 {
385         assert( Chan != NULL );
386         return Chan->maxusers;
387 } /* Channel_MaxUsers */
388
389
390 GLOBAL CHANNEL *
391 Channel_First( void )
392 {
393         return My_Channels;
394 } /* Channel_First */
395
396
397 GLOBAL CHANNEL *
398 Channel_Next( CHANNEL *Chan )
399 {
400         assert( Chan != NULL );
401         return Chan->next;
402 } /* Channel_Next */
403
404
405 GLOBAL CHANNEL *
406 Channel_Search( char *Name )
407 {
408         /* Channel-Struktur suchen */
409         
410         CHANNEL *c;
411         UINT32 search_hash;
412
413         assert( Name != NULL );
414
415         search_hash = Hash( Name );
416         c = My_Channels;
417         while( c )
418         {
419                 if( search_hash == c->hash )
420                 {
421                         /* lt. Hash-Wert: Treffer! */
422                         if( strcasecmp( Name, c->name ) == 0 ) return c;
423                 }
424                 c = c->next;
425         }
426         return NULL;
427 } /* Channel_Search */
428
429
430 GLOBAL CL2CHAN *
431 Channel_FirstMember( CHANNEL *Chan )
432 {
433         assert( Chan != NULL );
434         return Get_First_Cl2Chan( NULL, Chan );
435 } /* Channel_FirstMember */
436
437
438 GLOBAL CL2CHAN *
439 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
440 {
441         assert( Chan != NULL );
442         assert( Cl2Chan != NULL );
443         return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
444 } /* Channel_NextMember */
445
446
447 GLOBAL CL2CHAN *
448 Channel_FirstChannelOf( CLIENT *Client )
449 {
450         assert( Client != NULL );
451         return Get_First_Cl2Chan( Client, NULL );
452 } /* Channel_FirstChannelOf */
453
454
455 GLOBAL CL2CHAN *
456 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
457 {
458         assert( Client != NULL );
459         assert( Cl2Chan != NULL );
460         return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
461 } /* Channel_NextChannelOf */
462
463
464 GLOBAL CLIENT *
465 Channel_GetClient( CL2CHAN *Cl2Chan )
466 {
467         assert( Cl2Chan != NULL );
468         return Cl2Chan->client;
469 } /* Channel_GetClient */
470
471
472 GLOBAL CHANNEL *
473 Channel_GetChannel( CL2CHAN *Cl2Chan )
474 {
475         assert( Cl2Chan != NULL );
476         return Cl2Chan->channel;
477 } /* Channel_GetChannel */
478
479
480 GLOBAL bool
481 Channel_IsValidName( const char *Name )
482 {
483         assert( Name != NULL );
484
485         if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
486
487         return Name[strcspn(Name, " ,:\007")] == 0;
488 } /* Channel_IsValidName */
489
490
491 GLOBAL bool
492 Channel_ModeAdd( CHANNEL *Chan, char Mode )
493 {
494         /* set Mode.
495          * If the channel already had this mode, return false.
496          * If the channel mode was newly set return true.
497          */
498
499         char x[2];
500
501         assert( Chan != NULL );
502
503         x[0] = Mode; x[1] = '\0';
504         if( ! strchr( Chan->modes, x[0] ))
505         {
506                 /* Channel does not have this mode yet, set it */
507                 strlcat( Chan->modes, x, sizeof( Chan->modes ));
508                 return true;
509         }
510         else return false;
511 } /* Channel_ModeAdd */
512
513
514 GLOBAL bool
515 Channel_ModeDel( CHANNEL *Chan, char Mode )
516 {
517         /* Delete mode.
518          * if the mode was removed return true.
519          * if the channel did not have the mode, return false.
520         */
521         char *p;
522
523         assert( Chan != NULL );
524
525         p = strchr( Chan->modes, Mode );
526         if( ! p ) return false;
527
528         /* Channel has mode -> delete */
529         while( *p )
530         {
531                 *p = *(p + 1);
532                 p++;
533         }
534         return true;
535 } /* Channel_ModeDel */
536
537
538 GLOBAL bool
539 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
540 {
541         /* Set Channel-User-Mode.
542          * if mode was newly set, return true.
543          * if the User already had this channel-mode, return false.
544          */
545
546         CL2CHAN *cl2chan;
547         char x[2];
548
549         assert( Chan != NULL );
550         assert( Client != NULL );
551
552         cl2chan = Get_Cl2Chan( Chan, Client );
553         assert( cl2chan != NULL );
554
555         x[0] = Mode; x[1] = '\0';
556         if( ! strchr( cl2chan->modes, x[0] ))
557         {
558                 /* mode not set, -> set it */
559                 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
560                 return true;
561         }
562         else return false;
563 } /* Channel_UserModeAdd */
564
565
566 GLOBAL bool
567 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
568 {
569         /* Delete Channel-User-Mode.
570          * If Mode was removed, return true.
571          * If User did not have the Channel-Mode, return false.
572          */
573
574         CL2CHAN *cl2chan;
575         char *p;
576
577         assert( Chan != NULL );
578         assert( Client != NULL );
579
580         cl2chan = Get_Cl2Chan( Chan, Client );
581         assert( cl2chan != NULL );
582
583         p = strchr( cl2chan->modes, Mode );
584         if( ! p ) return false;
585
586         /* Client has Mode -> delete */
587         while( *p )
588         {
589                 *p = *(p + 1);
590                 p++;
591         }
592         return true;
593 } /* Channel_UserModeDel */
594
595
596 GLOBAL char *
597 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
598 {
599         /* return Users' Channel-Modes */
600
601         CL2CHAN *cl2chan;
602
603         assert( Chan != NULL );
604         assert( Client != NULL );
605
606         cl2chan = Get_Cl2Chan( Chan, Client );
607         assert( cl2chan != NULL );
608
609         return cl2chan->modes;
610 } /* Channel_UserModes */
611
612
613 GLOBAL bool
614 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
615 {
616         /* Test if Client is on Channel Chan */
617
618         assert( Chan != NULL );
619         assert( Client != NULL );
620
621         if( Get_Cl2Chan( Chan, Client )) return true;
622         else return false;
623 } /* Channel_IsMemberOf */
624
625
626 GLOBAL char *
627 Channel_Topic( CHANNEL *Chan )
628 {
629         char *ret;
630         assert( Chan != NULL );
631         ret = array_start(&Chan->topic);
632         return ret ? ret : "";
633 } /* Channel_Topic */
634
635         
636 #ifndef STRICT_RFC
637
638 GLOBAL unsigned int
639 Channel_TopicTime(CHANNEL *Chan)
640 {
641         assert(Chan != NULL);
642         return (unsigned int) Chan->topic_time;
643 } /* Channel_TopicTime */
644
645
646 GLOBAL char *
647 Channel_TopicWho(CHANNEL *Chan)
648 {
649         assert(Chan != NULL);
650         return Chan->topic_who;
651 } /* Channel_TopicWho */
652
653 #endif
654
655
656 GLOBAL void
657 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
658 {
659         size_t len;
660         assert( Chan != NULL );
661         assert( Topic != NULL );
662
663         len = strlen(Topic);
664         if (len < array_bytes(&Chan->topic))
665                 array_free(&Chan->topic);
666
667         if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
668                 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
669                                         Topic, Chan->name, strerror(errno));
670 #ifndef STRICT_RFC
671         Chan->topic_time = time(NULL);
672         if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
673                 strlcpy(Chan->topic_who, Client_ID(Client),
674                         sizeof Chan->topic_who);
675         else
676                 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
677                         sizeof Chan->topic_who);
678 #else
679         (void) Client;
680 #endif
681 } /* Channel_SetTopic */
682
683
684 GLOBAL void
685 Channel_SetModes( CHANNEL *Chan, char *Modes )
686 {
687         assert( Chan != NULL );
688         assert( Modes != NULL );
689
690         strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
691 } /* Channel_SetModes */
692
693
694 GLOBAL void
695 Channel_SetKey( CHANNEL *Chan, char *Key )
696 {
697         assert( Chan != NULL );
698         assert( Key != NULL );
699
700         strlcpy( Chan->key, Key, sizeof( Chan->key ));
701         LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
702 } /* Channel_SetKey */
703
704
705 GLOBAL void
706 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
707 {
708         assert( Chan != NULL );
709
710         Chan->maxusers = Count;
711         LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
712 } /* Channel_SetMaxUsers */
713
714
715 GLOBAL bool
716 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
717 {
718         bool is_member, has_voice, is_op, ok;
719
720         /* Okay, target is a channel */
721         is_member = has_voice = is_op = false;
722         if( Channel_IsMemberOf( Chan, From ))
723         {
724                 is_member = true;
725                 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
726                 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
727         }
728
729         /* Is the client allowed to write to channel? */
730         ok = true;
731         if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
732         if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
733
734         /* Is the client banned? */
735         if( Lists_Check(&Chan->list_bans, From))
736         {
737                 /* Client is banned, but is he channel operator or has voice? */
738                 if(( ! has_voice ) && ( ! is_op )) ok = false;
739         }
740
741         if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
742
743         /* Send text */
744         if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
745         return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
746 } /* Channel_Write */
747
748
749 GLOBAL CHANNEL *
750 Channel_Create( char *Name )
751 {
752         /* Create new CHANNEL structure and add it to linked list */
753         CHANNEL *c;
754
755         assert( Name != NULL );
756
757         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
758         if( ! c )
759         {
760                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
761                 return NULL;
762         }
763         memset( c, 0, sizeof( CHANNEL ));
764         strlcpy( c->name, Name, sizeof( c->name ));
765         c->hash = Hash( c->name );
766         c->next = My_Channels;
767         My_Channels = c;
768         LogDebug("Created new channel structure for \"%s\".", Name);
769         return c;
770 } /* Channel_Create */
771
772
773 static CL2CHAN *
774 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
775 {
776         CL2CHAN *cl2chan;
777
778         assert( Chan != NULL );
779         assert( Client != NULL );
780
781         cl2chan = My_Cl2Chan;
782         while( cl2chan )
783         {
784                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
785                 cl2chan = cl2chan->next;
786         }
787         return NULL;
788 } /* Get_Cl2Chan */
789
790
791 static CL2CHAN *
792 Add_Client( CHANNEL *Chan, CLIENT *Client )
793 {
794         CL2CHAN *cl2chan;
795
796         assert( Chan != NULL );
797         assert( Client != NULL );
798
799         /* neue CL2CHAN-Struktur anlegen */
800         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
801         if( ! cl2chan )
802         {
803                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
804                 return NULL;
805         }
806         cl2chan->channel = Chan;
807         cl2chan->client = Client;
808         strcpy( cl2chan->modes, "" );
809
810         /* Verketten */
811         cl2chan->next = My_Cl2Chan;
812         My_Cl2Chan = cl2chan;
813
814         Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
815
816         return cl2chan;
817 } /* Add_Client */
818
819
820 static bool
821 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
822 {
823         CL2CHAN *cl2chan, *last_cl2chan;
824         CHANNEL *c;
825         
826         assert( Chan != NULL );
827         assert( Client != NULL );
828         assert( Origin != NULL );
829         assert( Reason != NULL );
830
831         last_cl2chan = NULL;
832         cl2chan = My_Cl2Chan;
833         while( cl2chan )
834         {
835                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
836                 last_cl2chan = cl2chan;
837                 cl2chan = cl2chan->next;
838         }
839         if( ! cl2chan ) return false;
840
841         c = cl2chan->channel;
842         assert( c != NULL );
843
844         /* Aus Verkettung loesen und freigeben */
845         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
846         else My_Cl2Chan = cl2chan->next;
847         free( cl2chan );
848
849         switch( Type )
850         {
851                 case REMOVE_QUIT:
852                         /* QUIT: other servers have already been notified, see Client_Destroy();
853                          * so only inform other clients in same channel. */
854                         assert( InformServer == false );
855                         LogDebug("User \"%s\" left channel \"%s\" (%s).",
856                                         Client_Mask( Client ), c->name, Reason );
857                         break;
858                 case REMOVE_KICK:
859                         /* User was KICKed: inform other servers and all users in channel */
860                         if( InformServer )
861                                 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
862                                         Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
863                         IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
864                                                         c->name, Client_ID( Client ), Reason );
865                         if ((Client_Conn(Client) > NONE) &&
866                                         (Client_Type(Client) == CLIENT_USER))
867                         {
868                                 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
869                                                                 c->name, Client_ID( Client ), Reason);
870                         }
871                         LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
872                                 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
873                         break;
874                 default: /* PART */
875                         if (InformServer)
876                                 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
877
878                         IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
879                                                                         c->name, Reason);
880
881                         if ((Client_Conn(Origin) > NONE) &&
882                                         (Client_Type(Origin) == CLIENT_USER))
883                         {
884                                 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
885                                 LogDebug("User \"%s\" left channel \"%s\" (%s).",
886                                         Client_Mask(Client), c->name, Reason);
887                         }
888         }
889
890         /* Wenn Channel nun leer und nicht pre-defined: loeschen */
891         if( ! strchr( Channel_Modes( Chan ), 'P' ))
892         {
893                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
894         }
895                 
896         return true;
897 } /* Remove_Client */
898
899
900 GLOBAL bool
901 Channel_AddBan(CHANNEL *c, const char *mask )
902 {
903         struct list_head *h = Channel_GetListBans(c);
904         return Lists_Add(h, mask, false);
905 }
906
907
908 GLOBAL bool
909 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
910 {
911         struct list_head *h = Channel_GetListInvites(c);
912         return Lists_Add(h, mask, onlyonce);
913 }
914
915
916 static bool
917 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
918 {
919         struct list_elem *e;
920         char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
921         char *msg_end;
922
923         assert( Client != NULL );
924         assert( Channel != NULL );
925
926         e = Lists_GetFirst(head);
927         while (e) {
928                 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
929                                 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
930                 e = Lists_GetNext(e);
931         }
932
933         msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
934         return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
935 }
936
937
938 GLOBAL bool
939 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
940 {
941         struct list_head *h;
942
943         assert( Channel != NULL );
944
945         h = Channel_GetListBans(Channel);
946         return ShowInvitesBans(h, Client, Channel, false);
947 }
948
949
950 GLOBAL bool
951 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
952 {
953         struct list_head *h;
954
955         assert( Channel != NULL );
956
957         h = Channel_GetListInvites(Channel);
958         return ShowInvitesBans(h, Client, Channel, true);
959 }
960
961
962 static CL2CHAN *
963 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
964 {
965         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
966 } /* Get_First_Cl2Chan */
967
968
969 static CL2CHAN *
970 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
971 {
972         CL2CHAN *cl2chan;
973
974         assert( Client != NULL || Channel != NULL );
975         
976         cl2chan = Start;
977         while( cl2chan )
978         {
979                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
980                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
981                 cl2chan = cl2chan->next;
982         }
983         return NULL;
984 } /* Get_Next_Cl2Chan */
985
986
987 static bool
988 Delete_Channel( CHANNEL *Chan )
989 {
990         /* Channel-Struktur loeschen */
991
992         CHANNEL *chan, *last_chan;
993
994         last_chan = NULL;
995         chan = My_Channels;
996         while( chan )
997         {
998                 if( chan == Chan ) break;
999                 last_chan = chan;
1000                 chan = chan->next;
1001         }
1002         if( ! chan ) return false;
1003
1004         Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1005
1006         /* Invite- und Ban-Lists aufraeumen */
1007         Lists_Free( &chan->list_bans );
1008         Lists_Free( &chan->list_invites );
1009
1010         /* Neu verketten und freigeben */
1011         if( last_chan ) last_chan->next = chan->next;
1012         else My_Channels = chan->next;
1013         free( chan );
1014
1015         return true;
1016 } /* Delete_Channel */
1017
1018
1019 /* -eof- */