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