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