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