]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/channel.c
4eab2726a04393f40bc08ddec9ecfd4a4d5144c0
[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_Type(Target) == CLIENT_SERVICE)
335                     && !Client_HasMode(Origin, 'o')) {
336                         IRC_WriteStrClient(Origin, ERR_KICKDENY_MSG,
337                                            Client_ID(Origin), Name,
338                                            Client_ID(Target));
339                         return;
340                 }
341
342                 /* Check if client has the rights to kick target */
343                 ptr = Channel_UserModes(chan, Peer);
344                 target_modes = Channel_UserModes(chan, Target);
345                 while(*ptr) {
346                         /* Owner can kick everyone */
347                         if ( *ptr == 'q') {
348                                 can_kick = true;
349                                 break;
350                         }
351                         /* Admin can't kick owner */
352                         if ( *ptr == 'a' ) {
353                                 if (!strchr(target_modes, 'q')) {
354                                         can_kick = true;
355                                         break;
356                                 }
357                         }
358                         /* Op can't kick owner | admin */
359                         if ( *ptr == 'o' ) {
360                                 if (!strchr(target_modes, 'q') &&
361                                     !strchr(target_modes, 'a')) {
362                                         can_kick = true;
363                                         break;
364                                 }
365                         }
366                         /* Half Op can't kick owner | admin | op */ 
367                         if ( *ptr == 'h' ) {
368                                 if (!strchr(target_modes, 'q') &&
369                                     !strchr(target_modes, 'a') &&
370                                     !strchr(target_modes, 'o')) {
371                                         can_kick = true;
372                                         break;
373                                 }
374                         }
375                         ptr++;
376                 }
377
378                 if(!can_kick) {
379                         IRC_WriteStrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
380                                 Client_ID(Origin), Name);
381                         return;
382                 }
383         }
384
385         /* Check that the client to be kicked is on the specified channel */
386         if (!Channel_IsMemberOf(chan, Target)) {
387                 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
388                                    Client_ID(Origin), Client_ID(Target), Name );
389                 return;
390         }
391
392         /* Kick Client from channel */
393         Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
394 } /* Channel_Kick */
395
396
397 GLOBAL void
398 Channel_Quit( CLIENT *Client, const char *Reason )
399 {
400         CHANNEL *c, *next_c;
401
402         assert( Client != NULL );
403         assert( Reason != NULL );
404
405         if (Conf_MorePrivacy)
406                 Reason = "";
407
408         IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
409
410         c = My_Channels;
411         while( c )
412         {
413                 next_c = c->next;
414                 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
415                 c = next_c;
416         }
417 } /* Channel_Quit */
418
419
420 /**
421  * Get number of channels this server knows and that are "visible" to
422  * the given client. If no client is given, all channels will be counted.
423  *
424  * @param Client The client to check or NULL.
425  * @return Number of channels visible to the client.
426  */
427 GLOBAL unsigned long
428 Channel_CountVisible (CLIENT *Client)
429 {
430         CHANNEL *c;
431         unsigned long count = 0;
432
433         c = My_Channels;
434         while(c) {
435                 if (Client) {
436                         if (!strchr(Channel_Modes(c), 's')
437                             || Channel_IsMemberOf(c, Client))
438                                 count++;
439                 } else
440                         count++;
441                 c = c->next;
442         }
443         return count;
444 }
445
446
447 GLOBAL unsigned long
448 Channel_MemberCount( CHANNEL *Chan )
449 {
450         CL2CHAN *cl2chan;
451         unsigned long count = 0;
452
453         assert( Chan != NULL );
454
455         cl2chan = My_Cl2Chan;
456         while( cl2chan )
457         {
458                 if( cl2chan->channel == Chan ) count++;
459                 cl2chan = cl2chan->next;
460         }
461         return count;
462 } /* Channel_MemberCount */
463
464
465 GLOBAL int
466 Channel_CountForUser( CLIENT *Client )
467 {
468         /* Count number of channels a user is member of. */
469
470         CL2CHAN *cl2chan;
471         int count = 0;
472
473         assert( Client != NULL );
474
475         cl2chan = My_Cl2Chan;
476         while( cl2chan )
477         {
478                 if( cl2chan->client == Client ) count++;
479                 cl2chan = cl2chan->next;
480         }
481
482         return count;
483 } /* Channel_CountForUser */
484
485
486 GLOBAL const char *
487 Channel_Name( const CHANNEL *Chan )
488 {
489         assert( Chan != NULL );
490         return Chan->name;
491 } /* Channel_Name */
492
493
494 GLOBAL char *
495 Channel_Modes( CHANNEL *Chan )
496 {
497         assert( Chan != NULL );
498         return Chan->modes;
499 } /* Channel_Modes */
500
501
502 GLOBAL char *
503 Channel_Key( CHANNEL *Chan )
504 {
505         assert( Chan != NULL );
506         return Chan->key;
507 } /* Channel_Key */
508
509
510 GLOBAL unsigned long
511 Channel_MaxUsers( CHANNEL *Chan )
512 {
513         assert( Chan != NULL );
514         return Chan->maxusers;
515 } /* Channel_MaxUsers */
516
517
518 GLOBAL CHANNEL *
519 Channel_First( void )
520 {
521         return My_Channels;
522 } /* Channel_First */
523
524
525 GLOBAL CHANNEL *
526 Channel_Next( CHANNEL *Chan )
527 {
528         assert( Chan != NULL );
529         return Chan->next;
530 } /* Channel_Next */
531
532
533 GLOBAL CHANNEL *
534 Channel_Search( const char *Name )
535 {
536         /* Search channel structure */
537
538         CHANNEL *c;
539         UINT32 search_hash;
540
541         assert( Name != NULL );
542
543         search_hash = Hash( Name );
544         c = My_Channels;
545         while( c )
546         {
547                 if( search_hash == c->hash )
548                 {
549                         /* hash hit */
550                         if( strcasecmp( Name, c->name ) == 0 ) return c;
551                 }
552                 c = c->next;
553         }
554         return NULL;
555 } /* Channel_Search */
556
557
558 GLOBAL CL2CHAN *
559 Channel_FirstMember( CHANNEL *Chan )
560 {
561         assert( Chan != NULL );
562         return Get_First_Cl2Chan( NULL, Chan );
563 } /* Channel_FirstMember */
564
565
566 GLOBAL CL2CHAN *
567 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
568 {
569         assert( Chan != NULL );
570         assert( Cl2Chan != NULL );
571         return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
572 } /* Channel_NextMember */
573
574
575 GLOBAL CL2CHAN *
576 Channel_FirstChannelOf( CLIENT *Client )
577 {
578         assert( Client != NULL );
579         return Get_First_Cl2Chan( Client, NULL );
580 } /* Channel_FirstChannelOf */
581
582
583 GLOBAL CL2CHAN *
584 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
585 {
586         assert( Client != NULL );
587         assert( Cl2Chan != NULL );
588         return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
589 } /* Channel_NextChannelOf */
590
591
592 GLOBAL CLIENT *
593 Channel_GetClient( CL2CHAN *Cl2Chan )
594 {
595         assert( Cl2Chan != NULL );
596         return Cl2Chan->client;
597 } /* Channel_GetClient */
598
599
600 GLOBAL CHANNEL *
601 Channel_GetChannel( CL2CHAN *Cl2Chan )
602 {
603         assert( Cl2Chan != NULL );
604         return Cl2Chan->channel;
605 } /* Channel_GetChannel */
606
607
608 GLOBAL bool
609 Channel_IsValidName( const char *Name )
610 {
611         assert( Name != NULL );
612
613 #ifdef STRICT_RFC
614         if (strlen(Name) <= 1)
615                 return false;
616 #endif
617         if (strchr("#&+", Name[0]) == NULL)
618                 return false;
619         if (strlen(Name) >= CHANNEL_NAME_LEN)
620                 return false;
621
622         return Name[strcspn(Name, " ,:\007")] == 0;
623 } /* Channel_IsValidName */
624
625
626 GLOBAL bool
627 Channel_ModeAdd( CHANNEL *Chan, char Mode )
628 {
629         /* set Mode.
630          * If the channel already had this mode, return false.
631          * If the channel mode was newly set return true.
632          */
633
634         char x[2];
635
636         assert( Chan != NULL );
637
638         x[0] = Mode; x[1] = '\0';
639         if( ! strchr( Chan->modes, x[0] ))
640         {
641                 /* Channel does not have this mode yet, set it */
642                 strlcat( Chan->modes, x, sizeof( Chan->modes ));
643                 return true;
644         }
645         else return false;
646 } /* Channel_ModeAdd */
647
648
649 GLOBAL bool
650 Channel_ModeDel( CHANNEL *Chan, char Mode )
651 {
652         /* Delete mode.
653          * if the mode was removed return true.
654          * if the channel did not have the mode, return false.
655         */
656         char *p;
657
658         assert( Chan != NULL );
659
660         p = strchr( Chan->modes, Mode );
661         if( ! p ) return false;
662
663         /* Channel has mode -> delete */
664         while( *p )
665         {
666                 *p = *(p + 1);
667                 p++;
668         }
669         return true;
670 } /* Channel_ModeDel */
671
672
673 GLOBAL bool
674 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
675 {
676         /* Set Channel-User-Mode.
677          * if mode was newly set, return true.
678          * if the User already had this channel-mode, return false.
679          */
680
681         CL2CHAN *cl2chan;
682         char x[2];
683
684         assert( Chan != NULL );
685         assert( Client != NULL );
686
687         cl2chan = Get_Cl2Chan( Chan, Client );
688         assert( cl2chan != NULL );
689
690         x[0] = Mode; x[1] = '\0';
691         if( ! strchr( cl2chan->modes, x[0] ))
692         {
693                 /* mode not set, -> set it */
694                 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
695                 return true;
696         }
697         else return false;
698 } /* Channel_UserModeAdd */
699
700
701 GLOBAL bool
702 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
703 {
704         /* Delete Channel-User-Mode.
705          * If Mode was removed, return true.
706          * If User did not have the Channel-Mode, return false.
707          */
708
709         CL2CHAN *cl2chan;
710         char *p;
711
712         assert( Chan != NULL );
713         assert( Client != NULL );
714
715         cl2chan = Get_Cl2Chan( Chan, Client );
716         assert( cl2chan != NULL );
717
718         p = strchr( cl2chan->modes, Mode );
719         if( ! p ) return false;
720
721         /* Client has Mode -> delete */
722         while( *p )
723         {
724                 *p = *(p + 1);
725                 p++;
726         }
727         return true;
728 } /* Channel_UserModeDel */
729
730
731 GLOBAL char *
732 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
733 {
734         /* return Users' Channel-Modes */
735
736         CL2CHAN *cl2chan;
737
738         assert( Chan != NULL );
739         assert( Client != NULL );
740
741         cl2chan = Get_Cl2Chan( Chan, Client );
742         assert( cl2chan != NULL );
743
744         return cl2chan->modes;
745 } /* Channel_UserModes */
746
747
748 GLOBAL bool
749 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
750 {
751         /* Test if Client is on Channel Chan */
752
753         assert( Chan != NULL );
754         assert( Client != NULL );
755         return Get_Cl2Chan(Chan, Client) != NULL;
756 } /* Channel_IsMemberOf */
757
758
759 GLOBAL char *
760 Channel_Topic( CHANNEL *Chan )
761 {
762         char *ret;
763         assert( Chan != NULL );
764         ret = array_start(&Chan->topic);
765         return ret ? ret : "";
766 } /* Channel_Topic */
767
768
769 #ifndef STRICT_RFC
770
771 GLOBAL unsigned int
772 Channel_TopicTime(CHANNEL *Chan)
773 {
774         assert(Chan != NULL);
775         return (unsigned int) Chan->topic_time;
776 } /* Channel_TopicTime */
777
778
779 GLOBAL char *
780 Channel_TopicWho(CHANNEL *Chan)
781 {
782         assert(Chan != NULL);
783         return Chan->topic_who;
784 } /* Channel_TopicWho */
785
786
787 GLOBAL unsigned int
788 Channel_CreationTime(CHANNEL *Chan)
789 {
790         assert(Chan != NULL);
791         return (unsigned int) Chan->creation_time;
792 } /* Channel_CreationTime */
793
794 #endif
795
796
797 GLOBAL void
798 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
799 {
800         size_t len;
801         assert( Chan != NULL );
802         assert( Topic != NULL );
803
804         len = strlen(Topic);
805         if (len < array_bytes(&Chan->topic))
806                 array_free(&Chan->topic);
807
808         if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
809                 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
810                                         Topic, Chan->name, strerror(errno));
811 #ifndef STRICT_RFC
812         Chan->topic_time = time(NULL);
813         if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
814                 strlcpy(Chan->topic_who, Client_ID(Client),
815                         sizeof Chan->topic_who);
816         else
817                 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
818                         sizeof Chan->topic_who);
819 #else
820         (void) Client;
821 #endif
822 } /* Channel_SetTopic */
823
824
825 GLOBAL void
826 Channel_SetModes( CHANNEL *Chan, const char *Modes )
827 {
828         assert( Chan != NULL );
829         assert( Modes != NULL );
830
831         strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
832 } /* Channel_SetModes */
833
834
835 GLOBAL void
836 Channel_SetKey( CHANNEL *Chan, const char *Key )
837 {
838         assert( Chan != NULL );
839         assert( Key != NULL );
840
841         strlcpy( Chan->key, Key, sizeof( Chan->key ));
842         LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
843 } /* Channel_SetKey */
844
845
846 GLOBAL void
847 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
848 {
849         assert( Chan != NULL );
850
851         Chan->maxusers = Count;
852         LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
853 } /* Channel_SetMaxUsers */
854
855
856 /**
857  * Check if a client is allowed to send to a specific channel.
858  *
859  * @param Chan The channel to check.
860  * @param From The client that wants to send.
861  * @return true if the client is allowed to send, false otherwise.
862  */
863 static bool
864 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
865 {
866         bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
867
868         is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
869
870         /* The server itself always can send messages :-) */
871         if (Client_ThisServer() == From)
872                 return true;
873
874         if (Channel_IsMemberOf(Chan, From)) {
875                 is_member = true;
876                 if (strchr(Channel_UserModes(Chan, From), 'v'))
877                         has_voice = true;
878                 if (strchr(Channel_UserModes(Chan, From), 'h'))
879                         is_halfop = true;
880                 if (strchr(Channel_UserModes(Chan, From), 'o'))
881                         is_op = true;
882                 if (strchr(Channel_UserModes(Chan, From), 'a'))
883                         is_chanadmin = true;
884                 if (strchr(Channel_UserModes(Chan, From), 'q'))
885                         is_owner = true;
886         }
887
888         /*
889          * Is the client allowed to write to channel?
890          *
891          * If channel mode n set: non-members cannot send to channel.
892          * If channel mode m set: need voice.
893          */
894         if (strchr(Channel_Modes(Chan), 'n') && !is_member)
895                 return false;
896
897         if (strchr(Channel_Modes(Chan), 'M') && !Client_HasMode(From, 'R')
898             && !Client_HasMode(From, 'o'))
899                 return false;
900
901         if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
902                 return true;
903
904         if (strchr(Channel_Modes(Chan), 'm'))
905                 return false;
906
907         if (Lists_Check(&Chan->list_excepts, From))
908                 return true;
909
910         return !Lists_Check(&Chan->list_bans, From);
911 }
912
913
914 GLOBAL bool
915 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
916               bool SendErrors, const char *Text)
917 {
918         if (!Can_Send_To_Channel(Chan, From)) {
919                 if (! SendErrors)
920                         return CONNECTED;       /* no error, see RFC 2812 */
921                 if (strchr(Channel_Modes(Chan), 'M'))
922                         return IRC_WriteStrClient(From, ERR_NEEDREGGEDNICK_MSG,
923                                                   Client_ID(From), Channel_Name(Chan));
924                 else
925                         return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
926                                           Client_ID(From), Channel_Name(Chan));
927         }
928
929         if (Client_Conn(From) > NONE)
930                 Conn_UpdateIdle(Client_Conn(From));
931
932         return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
933                         "%s %s :%s", Command, Channel_Name(Chan), Text);
934 }
935
936
937 GLOBAL CHANNEL *
938 Channel_Create( const char *Name )
939 {
940         /* Create new CHANNEL structure and add it to linked list */
941         CHANNEL *c;
942
943         assert( Name != NULL );
944
945         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
946         if( ! c )
947         {
948                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
949                 return NULL;
950         }
951         memset( c, 0, sizeof( CHANNEL ));
952         strlcpy( c->name, Name, sizeof( c->name ));
953         c->hash = Hash( c->name );
954         c->next = My_Channels;
955 #ifndef STRICT_RFC
956         c->creation_time = time(NULL);
957 #endif
958         My_Channels = c;
959         LogDebug("Created new channel structure for \"%s\".", Name);
960         return c;
961 } /* Channel_Create */
962
963
964 static CL2CHAN *
965 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
966 {
967         CL2CHAN *cl2chan;
968
969         assert( Chan != NULL );
970         assert( Client != NULL );
971
972         cl2chan = My_Cl2Chan;
973         while( cl2chan )
974         {
975                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
976                 cl2chan = cl2chan->next;
977         }
978         return NULL;
979 } /* Get_Cl2Chan */
980
981
982 static CL2CHAN *
983 Add_Client( CHANNEL *Chan, CLIENT *Client )
984 {
985         CL2CHAN *cl2chan;
986
987         assert( Chan != NULL );
988         assert( Client != NULL );
989
990         /* Create new CL2CHAN structure */
991         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
992         if( ! cl2chan )
993         {
994                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
995                 return NULL;
996         }
997         cl2chan->channel = Chan;
998         cl2chan->client = Client;
999         strcpy( cl2chan->modes, "" );
1000
1001         /* concatenate */
1002         cl2chan->next = My_Cl2Chan;
1003         My_Cl2Chan = cl2chan;
1004
1005         LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1006
1007         return cl2chan;
1008 } /* Add_Client */
1009
1010
1011 static bool
1012 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1013 {
1014         CL2CHAN *cl2chan, *last_cl2chan;
1015         CHANNEL *c;
1016
1017         assert( Chan != NULL );
1018         assert( Client != NULL );
1019         assert( Origin != NULL );
1020         assert( Reason != NULL );
1021
1022         /* Do not inform other servers if the channel is local to this server,
1023          * regardless of what the caller requested! */
1024         if(InformServer)
1025                 InformServer = !Channel_IsLocal(Chan);
1026
1027         last_cl2chan = NULL;
1028         cl2chan = My_Cl2Chan;
1029         while( cl2chan )
1030         {
1031                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1032                 last_cl2chan = cl2chan;
1033                 cl2chan = cl2chan->next;
1034         }
1035         if( ! cl2chan ) return false;
1036
1037         c = cl2chan->channel;
1038         assert( c != NULL );
1039
1040         /* maintain cl2chan list */
1041         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1042         else My_Cl2Chan = cl2chan->next;
1043         free( cl2chan );
1044
1045         switch( Type )
1046         {
1047                 case REMOVE_QUIT:
1048                         /* QUIT: other servers have already been notified, 
1049                          * see Client_Destroy(); so only inform other clients
1050                          * in same channel. */
1051                         assert( InformServer == false );
1052                         LogDebug("User \"%s\" left channel \"%s\" (%s).",
1053                                         Client_Mask( Client ), c->name, Reason );
1054                         break;
1055                 case REMOVE_KICK:
1056                         /* User was KICKed: inform other servers (public
1057                          * channels) and all users in the channel */
1058                         if( InformServer )
1059                                 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1060                                         Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1061                         IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1062                                                         c->name, Client_ID( Client ), Reason );
1063                         if ((Client_Conn(Client) > NONE) &&
1064                                         (Client_Type(Client) == CLIENT_USER))
1065                         {
1066                                 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1067                                                                 c->name, Client_ID( Client ), Reason);
1068                         }
1069                         LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1070                                 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1071                         break;
1072                 default: /* PART */
1073                         if (Conf_MorePrivacy)
1074                                 Reason = "";
1075
1076                         if (InformServer)
1077                                 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1078
1079                         IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1080                                                                         c->name, Reason);
1081
1082                         if ((Client_Conn(Origin) > NONE) &&
1083                                         (Client_Type(Origin) == CLIENT_USER))
1084                         {
1085                                 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1086                                 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1087                                         Client_Mask(Client), c->name, Reason);
1088                         }
1089         }
1090
1091         /* When channel is empty and is not pre-defined, delete */
1092         if( ! strchr( Channel_Modes( Chan ), 'P' ))
1093         {
1094                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1095         }
1096
1097         return true;
1098 } /* Remove_Client */
1099
1100
1101 GLOBAL bool
1102 Channel_AddBan(CHANNEL *c, const char *mask )
1103 {
1104         struct list_head *h = Channel_GetListBans(c);
1105         LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1106         return Lists_Add(h, mask, false, NULL);
1107 }
1108
1109
1110 GLOBAL bool
1111 Channel_AddExcept(CHANNEL *c, const char *mask )
1112 {
1113         struct list_head *h = Channel_GetListExcepts(c);
1114         LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1115         return Lists_Add(h, mask, false, NULL);
1116 }
1117
1118
1119 GLOBAL bool
1120 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1121 {
1122         struct list_head *h = Channel_GetListInvites(c);
1123         LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1124         return Lists_Add(h, mask, onlyonce, NULL);
1125 }
1126
1127
1128 static bool
1129 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1130                 char *msg, char *msg_end)
1131 {
1132         struct list_elem *e;
1133
1134         assert (Client != NULL);
1135         assert (Channel != NULL);
1136
1137         e = Lists_GetFirst(head);
1138         while (e) {
1139                 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1140                                         Channel_Name(Channel),
1141                                         Lists_GetMask(e)))
1142                         return DISCONNECTED;
1143                 e = Lists_GetNext(e);
1144         }
1145
1146         return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1147                                   Channel_Name(Channel));
1148 }
1149
1150
1151 GLOBAL bool
1152 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1153 {
1154         struct list_head *h;
1155
1156         assert( Channel != NULL );
1157
1158         h = Channel_GetListBans(Channel);
1159         return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1160                                RPL_ENDOFBANLIST_MSG);
1161 }
1162
1163
1164 GLOBAL bool
1165 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1166 {
1167         struct list_head *h;
1168
1169         assert( Channel != NULL );
1170
1171         h = Channel_GetListExcepts(Channel);
1172         return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1173                                RPL_ENDOFEXCEPTLIST_MSG);
1174 }
1175
1176
1177 GLOBAL bool
1178 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1179 {
1180         struct list_head *h;
1181
1182         assert( Channel != NULL );
1183
1184         h = Channel_GetListInvites(Channel);
1185         return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1186                                RPL_ENDOFINVITELIST_MSG);
1187 }
1188
1189
1190 /**
1191  * Log a message to the local &SERVER channel, if it exists.
1192  */
1193 GLOBAL void
1194 Channel_LogServer(const char *msg)
1195 {
1196         CHANNEL *sc;
1197         CLIENT *c;
1198
1199         assert(msg != NULL);
1200
1201         sc = Channel_Search("&SERVER");
1202         if (!sc)
1203                 return;
1204
1205         c = Client_ThisServer();
1206         Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1207 } /* Channel_LogServer */
1208
1209
1210 GLOBAL bool
1211 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1212 {
1213         char *file_name, line[COMMAND_LEN], *nick, *pass;
1214         FILE *fd;
1215
1216         assert(Chan != NULL);
1217         assert(Client != NULL);
1218         assert(Key != NULL);
1219
1220         if (!strchr(Chan->modes, 'k'))
1221                 return true;
1222         if (*Key == '\0')
1223                 return false;
1224         if (strcmp(Chan->key, Key) == 0)
1225                 return true;
1226
1227         file_name = array_start(&Chan->keyfile);
1228         if (!file_name)
1229                 return false;
1230         fd = fopen(file_name, "r");
1231         if (!fd) {
1232                 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1233                     file_name, Chan->name, strerror(errno));
1234                 return false;
1235         }
1236
1237         while (fgets(line, (int)sizeof(line), fd) != NULL) {
1238                 ngt_TrimStr(line);
1239                 if (! (nick = strchr(line, ':')))
1240                         continue;
1241                 *nick++ = '\0';
1242                 if (!Match(line, Client_User(Client)))
1243                         continue;
1244                 if (! (pass = strchr(nick, ':')))
1245                         continue;
1246                 *pass++ = '\0';
1247                 if (!Match(nick, Client_ID(Client)))
1248                         continue;
1249                 if (strcmp(Key, pass) != 0)
1250                         continue;
1251
1252                 fclose(fd);
1253                 return true;
1254         }
1255         fclose(fd);
1256         return false;
1257 } /* Channel_CheckKey */
1258
1259
1260 static CL2CHAN *
1261 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1262 {
1263         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1264 } /* Get_First_Cl2Chan */
1265
1266
1267 static CL2CHAN *
1268 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1269 {
1270         CL2CHAN *cl2chan;
1271
1272         assert( Client != NULL || Channel != NULL );
1273
1274         cl2chan = Start;
1275         while( cl2chan )
1276         {
1277                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1278                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1279                 cl2chan = cl2chan->next;
1280         }
1281         return NULL;
1282 } /* Get_Next_Cl2Chan */
1283
1284
1285 /**
1286  * Remove a channel and free all of its data structures.
1287  */
1288 static void
1289 Delete_Channel(CHANNEL *Chan)
1290 {
1291         CHANNEL *chan, *last_chan;
1292
1293         last_chan = NULL;
1294         chan = My_Channels;
1295         while (chan) {
1296                 if (chan == Chan)
1297                         break;
1298                 last_chan = chan;
1299                 chan = chan->next;
1300         }
1301
1302         assert(chan != NULL);
1303         if (!chan)
1304                 return;
1305
1306         /* maintain channel list */
1307         if (last_chan)
1308                 last_chan->next = chan->next;
1309         else
1310                 My_Channels = chan->next;
1311
1312         LogDebug("Freed channel structure for \"%s\".", Chan->name);
1313         Free_Channel(Chan);
1314 } /* Delete_Channel */
1315
1316
1317 static void
1318 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1319 {
1320         size_t len;
1321
1322         assert(Chan != NULL);
1323         assert(KeyFile != NULL);
1324
1325         len = strlen(KeyFile);
1326         if (len < array_bytes(&Chan->keyfile)) {
1327                 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1328                 array_free(&Chan->keyfile);
1329         }
1330
1331         if (len < 1)
1332                 return;
1333
1334         if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1335                 Log(LOG_WARNING,
1336                     "Could not set new channel key file \"%s\" for %s: %s",
1337                     KeyFile, Chan->name, strerror(errno));
1338         else
1339                 Log(LOG_INFO|LOG_snotice,
1340                     "New local channel key file \"%s\" for %s activated.",
1341                     KeyFile, Chan->name);
1342 } /* Set_KeyFile */
1343
1344
1345 /* -eof- */