]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/channel.c
Implement channel mode "M"
[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 static bool
849 Can_Send_To_Channel_Identified(CHANNEL *Chan, CLIENT *From)
850 {
851         if ((Client_ThisServer() == From) || Client_HasMode(From, 'o'))
852                 return true;
853
854         if (strchr(Channel_Modes(Chan), 'M') && !Client_HasMode(From, 'R'))
855                 return false;
856
857         if (Lists_Check(&Chan->list_excepts, From))
858                 return true;
859
860         return !Lists_Check(&Chan->list_bans, From);
861 }
862
863
864 GLOBAL bool
865 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
866               bool SendErrors, const char *Text)
867 {
868         if (!Can_Send_To_Channel(Chan, From)) {
869                 if (! SendErrors)
870                         return CONNECTED;       /* no error, see RFC 2812 */
871                 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
872                                           Client_ID(From), Channel_Name(Chan));
873         }
874
875         if (!Can_Send_To_Channel_Identified(Chan, From)) {
876                 if (! SendErrors)
877                         return CONNECTED;
878                 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
879                                           Client_ID(From), Channel_Name(Chan));
880         }
881
882         if (Client_Conn(From) > NONE)
883                 Conn_UpdateIdle(Client_Conn(From));
884
885         return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
886                         "%s %s :%s", Command, Channel_Name(Chan), Text);
887 }
888
889
890 GLOBAL CHANNEL *
891 Channel_Create( const char *Name )
892 {
893         /* Create new CHANNEL structure and add it to linked list */
894         CHANNEL *c;
895
896         assert( Name != NULL );
897
898         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
899         if( ! c )
900         {
901                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
902                 return NULL;
903         }
904         memset( c, 0, sizeof( CHANNEL ));
905         strlcpy( c->name, Name, sizeof( c->name ));
906         c->hash = Hash( c->name );
907         c->next = My_Channels;
908 #ifndef STRICT_RFC
909         c->creation_time = time(NULL);
910 #endif
911         My_Channels = c;
912         LogDebug("Created new channel structure for \"%s\".", Name);
913         return c;
914 } /* Channel_Create */
915
916
917 static CL2CHAN *
918 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
919 {
920         CL2CHAN *cl2chan;
921
922         assert( Chan != NULL );
923         assert( Client != NULL );
924
925         cl2chan = My_Cl2Chan;
926         while( cl2chan )
927         {
928                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
929                 cl2chan = cl2chan->next;
930         }
931         return NULL;
932 } /* Get_Cl2Chan */
933
934
935 static CL2CHAN *
936 Add_Client( CHANNEL *Chan, CLIENT *Client )
937 {
938         CL2CHAN *cl2chan;
939
940         assert( Chan != NULL );
941         assert( Client != NULL );
942
943         /* Create new CL2CHAN structure */
944         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
945         if( ! cl2chan )
946         {
947                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
948                 return NULL;
949         }
950         cl2chan->channel = Chan;
951         cl2chan->client = Client;
952         strcpy( cl2chan->modes, "" );
953
954         /* concatenate */
955         cl2chan->next = My_Cl2Chan;
956         My_Cl2Chan = cl2chan;
957
958         LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
959
960         return cl2chan;
961 } /* Add_Client */
962
963
964 static bool
965 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
966 {
967         CL2CHAN *cl2chan, *last_cl2chan;
968         CHANNEL *c;
969
970         assert( Chan != NULL );
971         assert( Client != NULL );
972         assert( Origin != NULL );
973         assert( Reason != NULL );
974
975         /* Do not inform other servers if the channel is local to this server,
976          * regardless of what the caller requested! */
977         if(InformServer)
978                 InformServer = !Channel_IsLocal(Chan);
979
980         last_cl2chan = NULL;
981         cl2chan = My_Cl2Chan;
982         while( cl2chan )
983         {
984                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
985                 last_cl2chan = cl2chan;
986                 cl2chan = cl2chan->next;
987         }
988         if( ! cl2chan ) return false;
989
990         c = cl2chan->channel;
991         assert( c != NULL );
992
993         /* maintain cl2chan list */
994         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
995         else My_Cl2Chan = cl2chan->next;
996         free( cl2chan );
997
998         switch( Type )
999         {
1000                 case REMOVE_QUIT:
1001                         /* QUIT: other servers have already been notified, 
1002                          * see Client_Destroy(); so only inform other clients
1003                          * in same channel. */
1004                         assert( InformServer == false );
1005                         LogDebug("User \"%s\" left channel \"%s\" (%s).",
1006                                         Client_Mask( Client ), c->name, Reason );
1007                         break;
1008                 case REMOVE_KICK:
1009                         /* User was KICKed: inform other servers (public
1010                          * channels) and all users in the channel */
1011                         if( InformServer )
1012                                 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1013                                         Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1014                         IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1015                                                         c->name, Client_ID( Client ), Reason );
1016                         if ((Client_Conn(Client) > NONE) &&
1017                                         (Client_Type(Client) == CLIENT_USER))
1018                         {
1019                                 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1020                                                                 c->name, Client_ID( Client ), Reason);
1021                         }
1022                         LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1023                                 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1024                         break;
1025                 default: /* PART */
1026                         if (Conf_MorePrivacy)
1027                                 Reason = "";
1028
1029                         if (InformServer)
1030                                 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1031
1032                         IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1033                                                                         c->name, Reason);
1034
1035                         if ((Client_Conn(Origin) > NONE) &&
1036                                         (Client_Type(Origin) == CLIENT_USER))
1037                         {
1038                                 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1039                                 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1040                                         Client_Mask(Client), c->name, Reason);
1041                         }
1042         }
1043
1044         /* When channel is empty and is not pre-defined, delete */
1045         if( ! strchr( Channel_Modes( Chan ), 'P' ))
1046         {
1047                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1048         }
1049
1050         return true;
1051 } /* Remove_Client */
1052
1053
1054 GLOBAL bool
1055 Channel_AddBan(CHANNEL *c, const char *mask )
1056 {
1057         struct list_head *h = Channel_GetListBans(c);
1058         LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1059         return Lists_Add(h, mask, false, NULL);
1060 }
1061
1062
1063 GLOBAL bool
1064 Channel_AddExcept(CHANNEL *c, const char *mask )
1065 {
1066         struct list_head *h = Channel_GetListExcepts(c);
1067         LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1068         return Lists_Add(h, mask, false, NULL);
1069 }
1070
1071
1072 GLOBAL bool
1073 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1074 {
1075         struct list_head *h = Channel_GetListInvites(c);
1076         LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1077         return Lists_Add(h, mask, onlyonce, NULL);
1078 }
1079
1080
1081 static bool
1082 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1083                 char *msg, char *msg_end)
1084 {
1085         struct list_elem *e;
1086
1087         assert (Client != NULL);
1088         assert (Channel != NULL);
1089
1090         e = Lists_GetFirst(head);
1091         while (e) {
1092                 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1093                                         Channel_Name(Channel),
1094                                         Lists_GetMask(e)))
1095                         return DISCONNECTED;
1096                 e = Lists_GetNext(e);
1097         }
1098
1099         return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1100                                   Channel_Name(Channel));
1101 }
1102
1103
1104 GLOBAL bool
1105 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1106 {
1107         struct list_head *h;
1108
1109         assert( Channel != NULL );
1110
1111         h = Channel_GetListBans(Channel);
1112         return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1113                                RPL_ENDOFBANLIST_MSG);
1114 }
1115
1116
1117 GLOBAL bool
1118 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1119 {
1120         struct list_head *h;
1121
1122         assert( Channel != NULL );
1123
1124         h = Channel_GetListExcepts(Channel);
1125         return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1126                                RPL_ENDOFEXCEPTLIST_MSG);
1127 }
1128
1129
1130 GLOBAL bool
1131 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1132 {
1133         struct list_head *h;
1134
1135         assert( Channel != NULL );
1136
1137         h = Channel_GetListInvites(Channel);
1138         return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1139                                RPL_ENDOFINVITELIST_MSG);
1140 }
1141
1142
1143 /**
1144  * Log a message to the local &SERVER channel, if it exists.
1145  */
1146 GLOBAL void
1147 Channel_LogServer(const char *msg)
1148 {
1149         CHANNEL *sc;
1150         CLIENT *c;
1151
1152         assert(msg != NULL);
1153
1154         sc = Channel_Search("&SERVER");
1155         if (!sc)
1156                 return;
1157
1158         c = Client_ThisServer();
1159         Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1160 } /* Channel_LogServer */
1161
1162
1163 GLOBAL bool
1164 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1165 {
1166         char *file_name, line[COMMAND_LEN], *nick, *pass;
1167         FILE *fd;
1168
1169         assert(Chan != NULL);
1170         assert(Client != NULL);
1171         assert(Key != NULL);
1172
1173         if (!strchr(Chan->modes, 'k'))
1174                 return true;
1175         if (*Key == '\0')
1176                 return false;
1177         if (strcmp(Chan->key, Key) == 0)
1178                 return true;
1179
1180         file_name = array_start(&Chan->keyfile);
1181         if (!file_name)
1182                 return false;
1183         fd = fopen(file_name, "r");
1184         if (!fd) {
1185                 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1186                     file_name, Chan->name, strerror(errno));
1187                 return false;
1188         }
1189
1190         while (fgets(line, (int)sizeof(line), fd) != NULL) {
1191                 ngt_TrimStr(line);
1192                 if (! (nick = strchr(line, ':')))
1193                         continue;
1194                 *nick++ = '\0';
1195                 if (!Match(line, Client_User(Client)))
1196                         continue;
1197                 if (! (pass = strchr(nick, ':')))
1198                         continue;
1199                 *pass++ = '\0';
1200                 if (!Match(nick, Client_ID(Client)))
1201                         continue;
1202                 if (strcmp(Key, pass) != 0)
1203                         continue;
1204
1205                 fclose(fd);
1206                 return true;
1207         }
1208         fclose(fd);
1209         return false;
1210 } /* Channel_CheckKey */
1211
1212
1213 /**
1214  * Check wether a client is allowed to administer a channel or not.
1215  *
1216  * @param Chan          The channel to test.
1217  * @param Client        The client from which the command has been received.
1218  * @param Origin        The originator of the command (or NULL).
1219  * @param OnChannel     Set to true if the originator is member of the channel.
1220  * @param AdminOk       Set to true if the client is allowed to do
1221  *                      administrative tasks on this channel.
1222  * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1223  *                      that is send commands as if originating from a server
1224  *                      and not the originator of the command.
1225  */
1226 GLOBAL void
1227 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1228                          bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1229 {
1230         assert (Chan != NULL);
1231         assert (Client != NULL);
1232         assert (OnChannel != NULL);
1233         assert (AdminOk != NULL);
1234         assert (UseServerMode != NULL);
1235
1236         /* Use the client as origin, if no origin has been given (no prefix?) */
1237         if (!Origin)
1238                 Origin = Client;
1239
1240         *OnChannel = false;
1241         *AdminOk = false;
1242         *UseServerMode = false;
1243
1244         if (Client_Type(Client) != CLIENT_USER
1245             && Client_Type(Client) != CLIENT_SERVER
1246             && Client_Type(Client) != CLIENT_SERVICE)
1247                 return;
1248
1249         /* Allow channel administration if the client is a server or service */
1250         if (Client_Type(Client) != CLIENT_USER) {
1251                 *AdminOk = true;
1252                 return;
1253         }
1254
1255         *OnChannel = Channel_IsMemberOf(Chan, Origin);
1256
1257         if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1258                 /* User is a channel operator */
1259                 *AdminOk = true;
1260         } else if (Conf_OperCanMode) {
1261                 /* IRC operators are allowed to administer channels as well */
1262                 if (Client_OperByMe(Origin)) {
1263                         *AdminOk = true;
1264                         if (Conf_OperServerMode)
1265                                 *UseServerMode = true;
1266                 }
1267         }
1268 } /* Channel_CheckAdminRights */
1269
1270
1271 static CL2CHAN *
1272 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1273 {
1274         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1275 } /* Get_First_Cl2Chan */
1276
1277
1278 static CL2CHAN *
1279 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1280 {
1281         CL2CHAN *cl2chan;
1282
1283         assert( Client != NULL || Channel != NULL );
1284
1285         cl2chan = Start;
1286         while( cl2chan )
1287         {
1288                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1289                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1290                 cl2chan = cl2chan->next;
1291         }
1292         return NULL;
1293 } /* Get_Next_Cl2Chan */
1294
1295
1296 /**
1297  * Remove a channel and free all of its data structures.
1298  */
1299 static void
1300 Delete_Channel(CHANNEL *Chan)
1301 {
1302         CHANNEL *chan, *last_chan;
1303
1304         last_chan = NULL;
1305         chan = My_Channels;
1306         while (chan) {
1307                 if (chan == Chan)
1308                         break;
1309                 last_chan = chan;
1310                 chan = chan->next;
1311         }
1312
1313         assert(chan != NULL);
1314         if (!chan)
1315                 return;
1316
1317         /* maintain channel list */
1318         if (last_chan)
1319                 last_chan->next = chan->next;
1320         else
1321                 My_Channels = chan->next;
1322
1323         LogDebug("Freed channel structure for \"%s\".", Chan->name);
1324         Free_Channel(Chan);
1325 } /* Delete_Channel */
1326
1327
1328 static void
1329 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1330 {
1331         size_t len;
1332
1333         assert(Chan != NULL);
1334         assert(KeyFile != NULL);
1335
1336         len = strlen(KeyFile);
1337         if (len < array_bytes(&Chan->keyfile)) {
1338                 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1339                 array_free(&Chan->keyfile);
1340         }
1341
1342         if (len < 1)
1343                 return;
1344
1345         if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1346                 Log(LOG_WARNING,
1347                     "Could not set new channel key file \"%s\" for %s: %s",
1348                     KeyFile, Chan->name, strerror(errno));
1349         else
1350                 Log(LOG_INFO|LOG_snotice,
1351                     "New local channel key file \"%s\" for %s activated.",
1352                     KeyFile, Chan->name);
1353 } /* Set_KeyFile */
1354
1355
1356 /* -eof- */