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