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