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