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