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