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