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