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