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