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