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