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