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