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