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