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