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