]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/channel.c
Implementec numeric "333": Time and user name who set a channel topic.
[ngircd-alex.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.54 2005/09/02 12:50:25 alex 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, 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 void
74 Channel_InitPredefined( void )
75 {
76         /* Vordefinierte persistente Channels erzeugen */
77
78         CHANNEL *chan;
79         char *c;
80         unsigned int i;
81         
82         for( i = 0; i < Conf_Channel_Count; i++ )
83         {
84                 /* Ist ein Name konfiguriert? */
85                 if( ! Conf_Channel[i].name[0] ) continue;
86
87                 /* Gueltiger Channel-Name? */
88                 if( ! Channel_IsValidName( Conf_Channel[i].name ))
89                 {
90                         Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
91                         array_free(&Conf_Channel[i].topic);
92                         continue;
93                 }
94
95                 /* Gibt es den Channel bereits? */
96                 chan = Channel_Search( Conf_Channel[i].name );
97                 if( chan )
98                 {
99                         Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
100                         array_free(&Conf_Channel[i].topic);
101                         continue;
102                 }
103
104                 /* Create channel */
105                 chan = Channel_Create(Conf_Channel[i].name);
106                 if (chan) {
107                         Channel_ModeAdd(chan, 'P');
108
109                         Channel_SetTopic(chan, NULL,
110                                          array_start(&Conf_Channel[i].topic));
111                         array_free(&Conf_Channel[i].topic);
112
113                         c = Conf_Channel[i].modes;
114                         while (*c)
115                                 Channel_ModeAdd(chan, *c++);
116                 
117                         Log(LOG_INFO, "Created pre-defined channel \"%s\".",
118                                                         Conf_Channel[i].name );
119                 }
120                 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
121                                                         Conf_Channel[i].name );
122         }
123 } /* Channel_InitPredefined */
124
125
126 GLOBAL void
127 Channel_Exit( void )
128 {
129         CHANNEL *c, *c_next;
130         CL2CHAN *cl2chan, *cl2chan_next;
131         
132         /* Channel-Strukturen freigeben */
133         c = My_Channels;
134         while( c )
135         {
136                 c_next = c->next;
137                 array_free(&c->topic);
138                 free( c );
139                 c = c_next;
140         }
141
142         /* Channel-Zuordnungstabelle freigeben */
143         cl2chan = My_Cl2Chan;
144         while( c )
145         {
146                 cl2chan_next = cl2chan->next;
147                 free( cl2chan );
148                 cl2chan = cl2chan_next;
149         }
150 } /* Channel_Exit */
151
152
153 GLOBAL bool
154 Channel_Join( CLIENT *Client, char *Name )
155 {
156         CHANNEL *chan;
157         
158         assert( Client != NULL );
159         assert( Name != NULL );
160
161         /* Valider Channel-Name? */
162         if( ! Channel_IsValidName( Name ))
163         {
164                 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
165                 return false;
166         }
167
168         /* Channel suchen */
169         chan = Channel_Search( Name );
170         if( chan )
171         {
172                 /* Ist der Client bereits Mitglied? */
173                 if( Get_Cl2Chan( chan, Client )) return false;
174         }
175         else
176         {
177                 /* Gibt es noch nicht? Dann neu anlegen: */
178                 chan = Channel_Create( Name );
179                 if( ! chan ) return false;
180         }
181
182         /* User dem Channel hinzufuegen */
183         if( ! Add_Client( chan, Client )) return false;
184         else return true;
185 } /* Channel_Join */
186
187
188 GLOBAL bool
189 Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
190 {
191         CHANNEL *chan;
192
193         assert( Client != NULL );
194         assert( Name != NULL );
195         assert( Reason != NULL );
196
197         /* Channel suchen */
198         chan = Channel_Search( Name );
199         if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
200         {
201                 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
202                 return false;
203         }
204
205         /* User aus Channel entfernen */
206         if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
207         else return true;
208 } /* Channel_Part */
209
210
211 GLOBAL void
212 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
213 {
214         CHANNEL *chan;
215
216         assert( Client != NULL );
217         assert( Origin != NULL );
218         assert( Name != NULL );
219         assert( Reason != NULL );
220
221         /* Channel suchen */
222         chan = Channel_Search( Name );
223         if( ! chan )
224         {
225                 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
226                 return;
227         }
228
229         /* Ist der User Mitglied in dem Channel? */
230         if( ! Channel_IsMemberOf( chan, Origin ))
231         {
232                 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
233                 return;
234         }
235
236         /* Ist der User Channel-Operator? */
237         if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
238         {
239                 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
240                 return;
241         }
242
243         /* Ist der Ziel-User Mitglied im Channel? */
244         if( ! Channel_IsMemberOf( chan, Client ))
245         {
246                 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
247                 return;
248         }
249
250         Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
251 } /* Channel_Kick */
252
253
254 GLOBAL void
255 Channel_Quit( CLIENT *Client, char *Reason )
256 {
257         CHANNEL *c, *next_c;
258
259         assert( Client != NULL );
260         assert( Reason != NULL );
261
262         IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
263
264         c = My_Channels;
265         while( c )
266         {
267                 next_c = c->next;
268                 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
269                 c = next_c;
270         }
271 } /* Channel_Quit */
272
273
274 GLOBAL long
275 Channel_Count( void )
276 {
277         CHANNEL *c;
278         long count = 0;
279         
280         c = My_Channels;
281         while( c )
282         {
283                 count++;
284                 c = c->next;
285         }
286         return count;
287 } /* Channel_Count */
288
289
290 GLOBAL long
291 Channel_MemberCount( CHANNEL *Chan )
292 {
293         CL2CHAN *cl2chan;
294         long count = 0;
295
296         assert( Chan != NULL );
297
298         cl2chan = My_Cl2Chan;
299         while( cl2chan )
300         {
301                 if( cl2chan->channel == Chan ) count++;
302                 cl2chan = cl2chan->next;
303         }
304         return count;
305 } /* Channel_MemberCount */
306
307
308 GLOBAL int
309 Channel_CountForUser( CLIENT *Client )
310 {
311         /* Count number of channels a user is member of. */
312
313         CL2CHAN *cl2chan;
314         int count = 0;
315         
316         assert( Client != NULL );
317         
318         cl2chan = My_Cl2Chan;
319         while( cl2chan )
320         {
321                 if( cl2chan->client == Client ) count++;
322                 cl2chan = cl2chan->next;
323         }
324
325         return count;
326 } /* Channel_CountForUser */
327
328
329 GLOBAL int
330 Channel_PCount( void )
331 {
332         /* Count the number of persistent (mode 'P') channels */
333
334         CHANNEL *chan;
335         int count = 0;
336
337         chan = My_Channels;
338         while( chan )
339         {
340                 if( strchr( chan->modes, 'P' )) count++;
341                 chan = chan->next;
342         }
343
344         return count;
345 } /* Channel_PCount */
346
347
348 GLOBAL char *
349 Channel_Name( CHANNEL *Chan )
350 {
351         assert( Chan != NULL );
352         return Chan->name;
353 } /* Channel_Name */
354
355
356 GLOBAL char *
357 Channel_Modes( CHANNEL *Chan )
358 {
359         assert( Chan != NULL );
360         return Chan->modes;
361 } /* Channel_Modes */
362
363
364 GLOBAL char *
365 Channel_Key( CHANNEL *Chan )
366 {
367         assert( Chan != NULL );
368         return Chan->key;
369 } /* Channel_Key */
370
371
372 GLOBAL long
373 Channel_MaxUsers( CHANNEL *Chan )
374 {
375         assert( Chan != NULL );
376         return Chan->maxusers;
377 } /* Channel_MaxUsers */
378
379
380 GLOBAL CHANNEL *
381 Channel_First( void )
382 {
383         return My_Channels;
384 } /* Channel_First */
385
386
387 GLOBAL CHANNEL *
388 Channel_Next( CHANNEL *Chan )
389 {
390         assert( Chan != NULL );
391         return Chan->next;
392 } /* Channel_Next */
393
394
395 GLOBAL CHANNEL *
396 Channel_Search( char *Name )
397 {
398         /* Channel-Struktur suchen */
399         
400         CHANNEL *c;
401         UINT32 search_hash;
402
403         assert( Name != NULL );
404
405         search_hash = Hash( Name );
406         c = My_Channels;
407         while( c )
408         {
409                 if( search_hash == c->hash )
410                 {
411                         /* lt. Hash-Wert: Treffer! */
412                         if( strcasecmp( Name, c->name ) == 0 ) return c;
413                 }
414                 c = c->next;
415         }
416         return NULL;
417 } /* Channel_Search */
418
419
420 GLOBAL CL2CHAN *
421 Channel_FirstMember( CHANNEL *Chan )
422 {
423         assert( Chan != NULL );
424         return Get_First_Cl2Chan( NULL, Chan );
425 } /* Channel_FirstMember */
426
427
428 GLOBAL CL2CHAN *
429 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
430 {
431         assert( Chan != NULL );
432         assert( Cl2Chan != NULL );
433         return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
434 } /* Channel_NextMember */
435
436
437 GLOBAL CL2CHAN *
438 Channel_FirstChannelOf( CLIENT *Client )
439 {
440         assert( Client != NULL );
441         return Get_First_Cl2Chan( Client, NULL );
442 } /* Channel_FirstChannelOf */
443
444
445 GLOBAL CL2CHAN *
446 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
447 {
448         assert( Client != NULL );
449         assert( Cl2Chan != NULL );
450         return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
451 } /* Channel_NextChannelOf */
452
453
454 GLOBAL CLIENT *
455 Channel_GetClient( CL2CHAN *Cl2Chan )
456 {
457         assert( Cl2Chan != NULL );
458         return Cl2Chan->client;
459 } /* Channel_GetClient */
460
461
462 GLOBAL CHANNEL *
463 Channel_GetChannel( CL2CHAN *Cl2Chan )
464 {
465         assert( Cl2Chan != NULL );
466         return Cl2Chan->channel;
467 } /* Channel_GetChannel */
468
469
470 GLOBAL bool
471 Channel_IsValidName( char *Name )
472 {
473         /* Pruefen, ob Name als Channelname gueltig */
474
475         char *ptr, badchars[10];
476         
477         assert( Name != NULL );
478
479         if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
480
481         ptr = Name;
482         strcpy( badchars, " ,:\007" );
483         while( *ptr )
484         {
485                 if( strchr( badchars, *ptr )) return false;
486                 ptr++;
487         }
488         
489         return true;
490 } /* Channel_IsValidName */
491
492
493 GLOBAL bool
494 Channel_ModeAdd( CHANNEL *Chan, char Mode )
495 {
496         /* set Mode.
497          * If the channel already had this mode, return false.
498          * If the channel mode was newly set return true.
499          */
500
501         char x[2];
502
503         assert( Chan != NULL );
504
505         x[0] = Mode; x[1] = '\0';
506         if( ! strchr( Chan->modes, x[0] ))
507         {
508                 /* Channel does not have this mode yet, set it */
509                 strlcat( Chan->modes, x, sizeof( Chan->modes ));
510                 return true;
511         }
512         else return false;
513 } /* Channel_ModeAdd */
514
515
516 GLOBAL bool
517 Channel_ModeDel( CHANNEL *Chan, char Mode )
518 {
519         /* Delete mode.
520          * if the mode was removed return true.
521          * if the channel did not have the mode, return false.
522         */
523         char x[2], *p;
524
525         assert( Chan != NULL );
526
527         x[0] = Mode; x[1] = '\0';
528
529         p = strchr( Chan->modes, x[0] );
530         if( ! p ) return false;
531
532         /* Channel has mode -> delete */
533         while( *p )
534         {
535                 *p = *(p + 1);
536                 p++;
537         }
538         return true;
539 } /* Channel_ModeDel */
540
541
542 GLOBAL bool
543 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
544 {
545         /* Set Channel-User-Mode.
546          * if mode was newly set, return true.
547          * if the User already had this channel-mode, return false.
548          */
549
550         CL2CHAN *cl2chan;
551         char x[2];
552
553         assert( Chan != NULL );
554         assert( Client != NULL );
555
556         cl2chan = Get_Cl2Chan( Chan, Client );
557         assert( cl2chan != NULL );
558         
559         x[0] = Mode; x[1] = '\0';
560         if( ! strchr( cl2chan->modes, x[0] ))
561         {
562                 /* mode not set, -> set it */
563                 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
564                 return true;
565         }
566         else return false;
567 } /* Channel_UserModeAdd */
568
569
570 GLOBAL bool
571 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
572 {
573         /* Delete Channel-User-Mode.
574          * If Mode was removed, return true.
575          * If User did not have the Channel-Mode, return false.
576          */
577
578         CL2CHAN *cl2chan;
579         char x[2], *p;
580
581         assert( Chan != NULL );
582         assert( Client != NULL );
583
584         cl2chan = Get_Cl2Chan( Chan, Client );
585         assert( cl2chan != NULL );
586
587         x[0] = Mode; x[1] = '\0';
588
589         p = strchr( cl2chan->modes, x[0] );
590         if( ! p ) return false;
591
592         /* Client has Mode -> delete */
593         while( *p )
594         {
595                 *p = *(p + 1);
596                 p++;
597         }
598         return true;
599 } /* Channel_UserModeDel */
600
601
602 GLOBAL char *
603 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
604 {
605         /* return Users' Channel-Modes */
606         
607         CL2CHAN *cl2chan;
608
609         assert( Chan != NULL );
610         assert( Client != NULL );
611
612         cl2chan = Get_Cl2Chan( Chan, Client );
613         assert( cl2chan != NULL );
614
615         return cl2chan->modes;
616 } /* Channel_UserModes */
617
618
619 GLOBAL bool
620 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
621 {
622         /* Test if Client is on Channel Chan */
623
624         assert( Chan != NULL );
625         assert( Client != NULL );
626
627         if( Get_Cl2Chan( Chan, Client )) return true;
628         else return false;
629 } /* Channel_IsMemberOf */
630
631
632 GLOBAL char *
633 Channel_Topic( CHANNEL *Chan )
634 {
635         char *ret;
636         assert( Chan != NULL );
637         ret = array_start(&Chan->topic);
638         return ret ? ret : "";
639 } /* Channel_Topic */
640
641         
642 #ifndef STRICT_RFC
643
644 GLOBAL unsigned int
645 Channel_TopicTime(CHANNEL *Chan)
646 {
647         assert(Chan != NULL);
648         return (unsigned int) Chan->topic_time;
649 } /* Channel_TopicTime */
650
651
652 GLOBAL char *
653 Channel_TopicWho(CHANNEL *Chan)
654 {
655         assert(Chan != NULL);
656         return Chan->topic_who;
657 } /* Channel_TopicWho */
658
659 #endif
660
661
662 GLOBAL void
663 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
664 {
665         size_t len;
666         assert( Chan != NULL );
667         assert( Topic != NULL );
668
669         len = strlen(Topic);
670         if (len < array_bytes(&Chan->topic))
671                 array_free(&Chan->topic);
672
673         if (!array_copyb(&Chan->topic, Topic, len))
674                 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
675                                         Topic, Chan->name, strerror(errno));
676
677         array_cat0(&Chan->topic);
678
679 #ifndef STRICT_RFC
680         Chan->topic_time = time(NULL);
681         if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
682                 strlcpy(Chan->topic_who, Client_ID(Client),
683                         sizeof Chan->topic_who);
684         else
685                 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
686                         sizeof Chan->topic_who);
687 #else
688         (void) Client;
689 #endif
690 } /* Channel_SetTopic */
691
692
693 GLOBAL void
694 Channel_SetModes( CHANNEL *Chan, char *Modes )
695 {
696         assert( Chan != NULL );
697         assert( Modes != NULL );
698
699         strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
700 } /* Channel_SetModes */
701
702
703 GLOBAL void
704 Channel_SetKey( CHANNEL *Chan, char *Key )
705 {
706         assert( Chan != NULL );
707         assert( Key != NULL );
708
709         strlcpy( Chan->key, Key, sizeof( Chan->key ));
710         Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
711 } /* Channel_SetKey */
712
713
714 GLOBAL void
715 Channel_SetMaxUsers( CHANNEL *Chan, long Count )
716 {
717         assert( Chan != NULL );
718
719         Chan->maxusers = Count;
720         Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
721 } /* Channel_SetMaxUsers */
722
723
724 GLOBAL bool
725 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
726 {
727         bool is_member, has_voice, is_op, ok;
728
729         /* Okay, target is a channel */
730         is_member = has_voice = is_op = false;
731         if( Channel_IsMemberOf( Chan, From ))
732         {
733                 is_member = true;
734                 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
735                 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
736         }
737
738         /* Is the client allowed to write to channel? */
739         ok = true;
740         if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
741         if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
742         
743         /* Is the client banned? */
744         if( Lists_CheckBanned( From, Chan ))
745         {
746                 /* Client is banned, bus is he channel operator or has voice? */
747                 if(( ! has_voice ) && ( ! is_op )) ok = false;
748         }
749
750         if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
751
752         /* Send text */
753         if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
754         return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
755 } /* Channel_Write */
756
757
758 GLOBAL CHANNEL *
759 Channel_Create( char *Name )
760 {
761         /* Create new CHANNEL structure and add it to linked list */
762         CHANNEL *c;
763
764         assert( Name != NULL );
765         
766         c = (CHANNEL *)malloc( sizeof( CHANNEL ));
767         if( ! c )
768         {
769                 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
770                 return NULL;
771         }
772         memset( c, 0, sizeof( CHANNEL ));
773         strlcpy( c->name, Name, sizeof( c->name ));
774         c->hash = Hash( c->name );
775         c->next = My_Channels;
776         My_Channels = c;
777 #ifdef DEBUG    
778         Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
779 #endif
780         return c;
781 } /* Channel_Create */
782
783
784 static CL2CHAN *
785 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
786 {
787         CL2CHAN *cl2chan;
788
789         assert( Chan != NULL );
790         assert( Client != NULL );
791
792         cl2chan = My_Cl2Chan;
793         while( cl2chan )
794         {
795                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
796                 cl2chan = cl2chan->next;
797         }
798         return NULL;
799 } /* Get_Cl2Chan */
800
801
802 static CL2CHAN *
803 Add_Client( CHANNEL *Chan, CLIENT *Client )
804 {
805         CL2CHAN *cl2chan;
806
807         assert( Chan != NULL );
808         assert( Client != NULL );
809
810         /* neue CL2CHAN-Struktur anlegen */
811         cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
812         if( ! cl2chan )
813         {
814                 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
815                 return NULL;
816         }
817         cl2chan->channel = Chan;
818         cl2chan->client = Client;
819         strcpy( cl2chan->modes, "" );
820
821         /* Verketten */
822         cl2chan->next = My_Cl2Chan;
823         My_Cl2Chan = cl2chan;
824
825         Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
826
827         return cl2chan;
828 } /* Add_Client */
829
830
831 static bool
832 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
833 {
834         CL2CHAN *cl2chan, *last_cl2chan;
835         CHANNEL *c;
836         
837         assert( Chan != NULL );
838         assert( Client != NULL );
839         assert( Origin != NULL );
840         assert( Reason != NULL );
841
842         last_cl2chan = NULL;
843         cl2chan = My_Cl2Chan;
844         while( cl2chan )
845         {
846                 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
847                 last_cl2chan = cl2chan;
848                 cl2chan = cl2chan->next;
849         }
850         if( ! cl2chan ) return false;
851
852         c = cl2chan->channel;
853         assert( c != NULL );
854
855         /* Aus Verkettung loesen und freigeben */
856         if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
857         else My_Cl2Chan = cl2chan->next;
858         free( cl2chan );
859
860         switch( Type )
861         {
862                 case REMOVE_QUIT:
863                         /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
864                          * hier also "nur" noch alle User in betroffenen Channeln infomieren */
865                         assert( InformServer == false );
866                         Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
867                         break;
868                 case REMOVE_KICK:
869                         /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
870                          * im entsprechenden Channel informieren */
871                         if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
872                         IRC_WriteStrChannelPrefix( Client, c, Origin, false, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
873                         if(( Client_Conn( Client ) > NONE ) && ( Client_Type( Client ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Client, Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
874                         Log( LOG_DEBUG, "User \"%s\" has been kicked of \"%s\" by \"%s\": %s.", Client_Mask( Client ), c->name, Client_ID( Origin ), Reason );
875                         break;
876                 default:
877                         /* PART */
878                         if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
879                         IRC_WriteStrChannelPrefix( Origin, c, Client, false, "PART %s :%s", c->name, Reason );
880                         if(( Client_Conn( Origin ) > NONE ) && ( Client_Type( Origin ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
881                         Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
882         }
883
884         /* Wenn Channel nun leer und nicht pre-defined: loeschen */
885         if( ! strchr( Channel_Modes( Chan ), 'P' ))
886         {
887                 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
888         }
889                 
890         return true;
891 } /* Remove_Client */
892
893
894 static CL2CHAN *
895 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
896 {
897         return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
898 } /* Get_First_Cl2Chan */
899
900
901 static CL2CHAN *
902 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
903 {
904         CL2CHAN *cl2chan;
905
906         assert( Client != NULL || Channel != NULL );
907         
908         cl2chan = Start;
909         while( cl2chan )
910         {
911                 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
912                 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
913                 cl2chan = cl2chan->next;
914         }
915         return NULL;
916 } /* Get_Next_Cl2Chan */
917
918
919 static bool
920 Delete_Channel( CHANNEL *Chan )
921 {
922         /* Channel-Struktur loeschen */
923         
924         CHANNEL *chan, *last_chan;
925
926         last_chan = NULL;
927         chan = My_Channels;
928         while( chan )
929         {
930                 if( chan == Chan ) break;
931                 last_chan = chan;
932                 chan = chan->next;
933         }
934         if( ! chan ) return false;
935
936         Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
937
938         /* Invite- und Ban-Lists aufraeumen */
939         Lists_DeleteChannel( chan );
940
941         /* Neu verketten und freigeben */
942         if( last_chan ) last_chan->next = chan->next;
943         else My_Channels = chan->next;
944         free( chan );
945                 
946         return true;
947 } /* Delete_Channel */
948
949
950 /* -eof- */