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