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