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