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