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