]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/channel.c
channel maxusers now unsigned long
[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.59 2006/10/06 21:32:58 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 unsigned long
269 Channel_Count( void )
270 {
271         CHANNEL *c;
272         unsigned 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 unsigned long
285 Channel_MemberCount( CHANNEL *Chan )
286 {
287         CL2CHAN *cl2chan;
288         unsigned 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 const char *
343 Channel_Name( const 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 unsigned 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( const char *Name )
466 {
467         assert( Name != NULL );
468
469         if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
470
471         return Name[strcspn(Name, " ,:\007")] == 0;
472 } /* Channel_IsValidName */
473
474
475 GLOBAL bool
476 Channel_ModeAdd( CHANNEL *Chan, char Mode )
477 {
478         /* set Mode.
479          * If the channel already had this mode, return false.
480          * If the channel mode was newly set return true.
481          */
482
483         char x[2];
484
485         assert( Chan != NULL );
486
487         x[0] = Mode; x[1] = '\0';
488         if( ! strchr( Chan->modes, x[0] ))
489         {
490                 /* Channel does not have this mode yet, set it */
491                 strlcat( Chan->modes, x, sizeof( Chan->modes ));
492                 return true;
493         }
494         else return false;
495 } /* Channel_ModeAdd */
496
497
498 GLOBAL bool
499 Channel_ModeDel( CHANNEL *Chan, char Mode )
500 {
501         /* Delete mode.
502          * if the mode was removed return true.
503          * if the channel did not have the mode, return false.
504         */
505         char *p;
506
507         assert( Chan != NULL );
508
509         p = strchr( Chan->modes, Mode );
510         if( ! p ) return false;
511
512         /* Channel has mode -> delete */
513         while( *p )
514         {
515                 *p = *(p + 1);
516                 p++;
517         }
518         return true;
519 } /* Channel_ModeDel */
520
521
522 GLOBAL bool
523 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
524 {
525         /* Set Channel-User-Mode.
526          * if mode was newly set, return true.
527          * if the User already had this channel-mode, return false.
528          */
529
530         CL2CHAN *cl2chan;
531         char x[2];
532
533         assert( Chan != NULL );
534         assert( Client != NULL );
535
536         cl2chan = Get_Cl2Chan( Chan, Client );
537         assert( cl2chan != NULL );
538
539         x[0] = Mode; x[1] = '\0';
540         if( ! strchr( cl2chan->modes, x[0] ))
541         {
542                 /* mode not set, -> set it */
543                 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
544                 return true;
545         }
546         else return false;
547 } /* Channel_UserModeAdd */
548
549
550 GLOBAL bool
551 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
552 {
553         /* Delete Channel-User-Mode.
554          * If Mode was removed, return true.
555          * If User did not have the Channel-Mode, return false.
556          */
557
558         CL2CHAN *cl2chan;
559         char *p;
560
561         assert( Chan != NULL );
562         assert( Client != NULL );
563
564         cl2chan = Get_Cl2Chan( Chan, Client );
565         assert( cl2chan != NULL );
566
567         p = strchr( cl2chan->modes, Mode );
568         if( ! p ) return false;
569
570         /* Client has Mode -> delete */
571         while( *p )
572         {
573                 *p = *(p + 1);
574                 p++;
575         }
576         return true;
577 } /* Channel_UserModeDel */
578
579
580 GLOBAL char *
581 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
582 {
583         /* return Users' Channel-Modes */
584
585         CL2CHAN *cl2chan;
586
587         assert( Chan != NULL );
588         assert( Client != NULL );
589
590         cl2chan = Get_Cl2Chan( Chan, Client );
591         assert( cl2chan != NULL );
592
593         return cl2chan->modes;
594 } /* Channel_UserModes */
595
596
597 GLOBAL bool
598 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
599 {
600         /* Test if Client is on Channel Chan */
601
602         assert( Chan != NULL );
603         assert( Client != NULL );
604
605         if( Get_Cl2Chan( Chan, Client )) return true;
606         else return false;
607 } /* Channel_IsMemberOf */
608
609
610 GLOBAL char *
611 Channel_Topic( CHANNEL *Chan )
612 {
613         char *ret;
614         assert( Chan != NULL );
615         ret = array_start(&Chan->topic);
616         return ret ? ret : "";
617 } /* Channel_Topic */
618
619         
620 #ifndef STRICT_RFC
621
622 GLOBAL unsigned int
623 Channel_TopicTime(CHANNEL *Chan)
624 {
625         assert(Chan != NULL);
626         return (unsigned int) Chan->topic_time;
627 } /* Channel_TopicTime */
628
629
630 GLOBAL char *
631 Channel_TopicWho(CHANNEL *Chan)
632 {
633         assert(Chan != NULL);
634         return Chan->topic_who;
635 } /* Channel_TopicWho */
636
637 #endif
638
639
640 GLOBAL void
641 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
642 {
643         size_t len;
644         assert( Chan != NULL );
645         assert( Topic != NULL );
646
647         len = strlen(Topic);
648         if (len < array_bytes(&Chan->topic))
649                 array_free(&Chan->topic);
650
651         if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
652                 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
653                                         Topic, Chan->name, strerror(errno));
654 #ifndef STRICT_RFC
655         Chan->topic_time = time(NULL);
656         if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
657                 strlcpy(Chan->topic_who, Client_ID(Client),
658                         sizeof Chan->topic_who);
659         else
660                 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
661                         sizeof Chan->topic_who);
662 #else
663         (void) Client;
664 #endif
665 } /* Channel_SetTopic */
666
667
668 GLOBAL void
669 Channel_SetModes( CHANNEL *Chan, char *Modes )
670 {
671         assert( Chan != NULL );
672         assert( Modes != NULL );
673
674         strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
675 } /* Channel_SetModes */
676
677
678 GLOBAL void
679 Channel_SetKey( CHANNEL *Chan, char *Key )
680 {
681         assert( Chan != NULL );
682         assert( Key != NULL );
683
684         strlcpy( Chan->key, Key, sizeof( Chan->key ));
685         LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
686 } /* Channel_SetKey */
687
688
689 GLOBAL void
690 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
691 {
692         assert( Chan != NULL );
693
694         Chan->maxusers = Count;
695         LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
696 } /* Channel_SetMaxUsers */
697
698
699 GLOBAL bool
700 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
701 {
702         bool is_member, has_voice, is_op, ok;
703
704         /* Okay, target is a channel */
705         is_member = has_voice = is_op = false;
706         if( Channel_IsMemberOf( Chan, From ))
707         {
708                 is_member = true;
709                 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
710                 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
711         }
712
713         /* Is the client allowed to write to channel? */
714         ok = true;
715         if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
716         if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
717
718         /* Is the client banned? */
719         if( Lists_CheckBanned( From, Chan ))
720         {
721                 /* Client is banned, but is he channel operator or has voice? */
722                 if(( ! has_voice ) && ( ! is_op )) ok = false;
723         }
724
725         if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
726
727         /* Send text */
728         if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
729         return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
730 } /* Channel_Write */
731
732
733 GLOBAL CHANNEL *
734 Channel_Create( char *Name )
735 {
736         /* Create new CHANNEL structure and add it to linked list */
737         CHANNEL *c;
738
739         assert( Name != NULL );
740
741         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
742         if( ! c )
743         {
744                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
745                 return NULL;
746         }
747         memset( c, 0, sizeof( CHANNEL ));
748         strlcpy( c->name, Name, sizeof( c->name ));
749         c->hash = Hash( c->name );
750         c->next = My_Channels;
751         My_Channels = c;
752         LogDebug("Created new channel structure for \"%s\".", Name);
753         return c;
754 } /* Channel_Create */
755
756
757 static CL2CHAN *
758 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
759 {
760         CL2CHAN *cl2chan;
761
762         assert( Chan != NULL );
763         assert( Client != NULL );
764
765         cl2chan = My_Cl2Chan;
766         while( cl2chan )
767         {
768                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
769                 cl2chan = cl2chan->next;
770         }
771         return NULL;
772 } /* Get_Cl2Chan */
773
774
775 static CL2CHAN *
776 Add_Client( CHANNEL *Chan, CLIENT *Client )
777 {
778         CL2CHAN *cl2chan;
779
780         assert( Chan != NULL );
781         assert( Client != NULL );
782
783         /* neue CL2CHAN-Struktur anlegen */
784         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
785         if( ! cl2chan )
786         {
787                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
788                 return NULL;
789         }
790         cl2chan->channel = Chan;
791         cl2chan->client = Client;
792         strcpy( cl2chan->modes, "" );
793
794         /* Verketten */
795         cl2chan->next = My_Cl2Chan;
796         My_Cl2Chan = cl2chan;
797
798         Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
799
800         return cl2chan;
801 } /* Add_Client */
802
803
804 static bool
805 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
806 {
807         CL2CHAN *cl2chan, *last_cl2chan;
808         CHANNEL *c;
809         
810         assert( Chan != NULL );
811         assert( Client != NULL );
812         assert( Origin != NULL );
813         assert( Reason != NULL );
814
815         last_cl2chan = NULL;
816         cl2chan = My_Cl2Chan;
817         while( cl2chan )
818         {
819                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
820                 last_cl2chan = cl2chan;
821                 cl2chan = cl2chan->next;
822         }
823         if( ! cl2chan ) return false;
824
825         c = cl2chan->channel;
826         assert( c != NULL );
827
828         /* Aus Verkettung loesen und freigeben */
829         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
830         else My_Cl2Chan = cl2chan->next;
831         free( cl2chan );
832
833         switch( Type )
834         {
835                 case REMOVE_QUIT:
836                         /* QUIT: other servers have already been notified, see Client_Destroy();
837                          * so only inform other clients in same channel. */
838                         assert( InformServer == false );
839                         LogDebug("User \"%s\" left channel \"%s\" (%s).",
840                                         Client_Mask( Client ), c->name, Reason );
841                         break;
842                 case REMOVE_KICK:
843                         /* User was KICKed: inform other servers and all users in channel */
844                         if( InformServer )
845                                 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
846                                         Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
847                         IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
848                                                         c->name, Client_ID( Client ), Reason );
849                         if ((Client_Conn(Client) > NONE) &&
850                                         (Client_Type(Client) == CLIENT_USER))
851                         {
852                                 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
853                                                                 c->name, Client_ID( Client ), Reason);
854                         }
855                         LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
856                                 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
857                         break;
858                 default: /* PART */
859                         if (InformServer)
860                                 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
861
862                         IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
863                                                                         c->name, Reason);
864
865                         if ((Client_Conn(Origin) > NONE) &&
866                                         (Client_Type(Origin) == CLIENT_USER))
867                         {
868                                 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
869                                 LogDebug("User \"%s\" left channel \"%s\" (%s).",
870                                         Client_Mask(Client), c->name, Reason);
871                         }
872         }
873
874         /* Wenn Channel nun leer und nicht pre-defined: loeschen */
875         if( ! strchr( Channel_Modes( Chan ), 'P' ))
876         {
877                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
878         }
879                 
880         return true;
881 } /* Remove_Client */
882
883
884 static CL2CHAN *
885 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
886 {
887         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
888 } /* Get_First_Cl2Chan */
889
890
891 static CL2CHAN *
892 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
893 {
894         CL2CHAN *cl2chan;
895
896         assert( Client != NULL || Channel != NULL );
897         
898         cl2chan = Start;
899         while( cl2chan )
900         {
901                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
902                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
903                 cl2chan = cl2chan->next;
904         }
905         return NULL;
906 } /* Get_Next_Cl2Chan */
907
908
909 static bool
910 Delete_Channel( CHANNEL *Chan )
911 {
912         /* Channel-Struktur loeschen */
913         
914         CHANNEL *chan, *last_chan;
915
916         last_chan = NULL;
917         chan = My_Channels;
918         while( chan )
919         {
920                 if( chan == Chan ) break;
921                 last_chan = chan;
922                 chan = chan->next;
923         }
924         if( ! chan ) return false;
925
926         Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
927
928         /* Invite- und Ban-Lists aufraeumen */
929         Lists_DeleteChannel( chan );
930
931         /* Neu verketten und freigeben */
932         if( last_chan ) last_chan->next = chan->next;
933         else My_Channels = chan->next;
934         free( chan );
935                 
936         return true;
937 } /* Delete_Channel */
938
939
940 /* -eof- */