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