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