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