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