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