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