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