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