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