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