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