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