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