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