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