2 38b9cb88 2001-12-14 alex * ngIRCd -- The Next Generation IRC Daemon
3 1547f76c 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * Channel management
15 8a324b6d 2002-01-26 alex #define __channel_c__
18 ca33cbda 2002-03-12 alex #include "portab.h"
20 cb76d96e 2004-03-11 alex static char UNUSED id[] = "$Id: channel.c,v 1.45 2004/03/11 22:16:31 alex Exp $";
22 ca33cbda 2002-03-12 alex #include "imp.h"
23 38b9cb88 2001-12-14 alex #include <assert.h>
24 bb19cfda 2002-01-21 alex #include <stdlib.h>
25 bb19cfda 2002-01-21 alex #include <string.h>
26 57c7e236 2004-01-17 alex #include <strings.h>
28 b8d7dcec 2002-12-30 alex #include "conn-func.h"
29 bb19cfda 2002-01-21 alex #include "client.h"
31 c2f60abe 2002-05-27 alex #include "exp.h"
32 c2f60abe 2002-05-27 alex #include "channel.h"
34 c2f60abe 2002-05-27 alex #include "imp.h"
35 c2f60abe 2002-05-27 alex #include "irc-write.h"
36 c2f60abe 2002-05-27 alex #include "resolve.h"
37 040f5422 2002-05-21 alex #include "conf.h"
38 0e38d10b 2002-03-25 alex #include "hash.h"
39 1fd7215d 2002-06-09 alex #include "lists.h"
40 bb19cfda 2002-01-21 alex #include "log.h"
41 bb19cfda 2002-01-21 alex #include "messages.h"
43 ca33cbda 2002-03-12 alex #include "exp.h"
46 cc1e8514 2002-06-01 alex #define REMOVE_PART 0
47 cc1e8514 2002-06-01 alex #define REMOVE_QUIT 1
48 cc1e8514 2002-06-01 alex #define REMOVE_KICK 2
51 38b9cb88 2001-12-14 alex LOCAL CHANNEL *My_Channels;
52 bb19cfda 2002-01-21 alex LOCAL CL2CHAN *My_Cl2Chan;
55 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
56 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 cc1e8514 2002-06-01 alex LOCAL BOOLEAN Remove_Client PARAMS(( INT Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, CHAR *Reason, BOOLEAN InformServer ));
58 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
59 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
60 c2f60abe 2002-05-27 alex LOCAL BOOLEAN Delete_Channel PARAMS(( CHANNEL *Chan ));
64 c2f60abe 2002-05-27 alex Channel_Init( VOID )
66 38b9cb88 2001-12-14 alex My_Channels = NULL;
67 bb19cfda 2002-01-21 alex My_Cl2Chan = NULL;
68 04162c39 2002-06-02 alex } /* Channel_Init */
72 04162c39 2002-06-02 alex Channel_InitPredefined( VOID )
74 040f5422 2002-05-21 alex /* Vordefinierte persistente Channels erzeugen */
76 04162c39 2002-06-02 alex CHANNEL *chan;
80 040f5422 2002-05-21 alex for( i = 0; i < Conf_Channel_Count; i++ )
82 040f5422 2002-05-21 alex /* Ist ein Name konfiguriert? */
83 adcd9289 2002-09-02 alex if( ! Conf_Channel[i].name[0] ) continue;
85 040f5422 2002-05-21 alex /* Gueltiger Channel-Name? */
86 040f5422 2002-05-21 alex if( ! Channel_IsValidName( Conf_Channel[i].name ))
88 040f5422 2002-05-21 alex Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
92 7233b721 2002-11-22 alex /* Gibt es den Channel bereits? */
93 7233b721 2002-11-22 alex chan = Channel_Search( Conf_Channel[i].name );
96 7233b721 2002-11-22 alex Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
100 040f5422 2002-05-21 alex /* Channel anlegen */
101 378e511e 2002-09-03 alex chan = Channel_Create( Conf_Channel[i].name );
104 040f5422 2002-05-21 alex Channel_ModeAdd( chan, 'P' );
105 040f5422 2002-05-21 alex Channel_SetTopic( chan, Conf_Channel[i].topic );
106 040f5422 2002-05-21 alex c = Conf_Channel[i].modes;
107 040f5422 2002-05-21 alex while( *c ) Channel_ModeAdd( chan, *c++ );
108 040f5422 2002-05-21 alex Log( LOG_INFO, "Created pre-defined channel \"%s\".", Conf_Channel[i].name );
110 040f5422 2002-05-21 alex else Log( LOG_ERR, "Can't create pre-defined channel \"%s\"!", Conf_Channel[i].name );
112 04162c39 2002-06-02 alex } /* Channel_InitPredefined */
115 c2f60abe 2002-05-27 alex GLOBAL VOID
116 c2f60abe 2002-05-27 alex Channel_Exit( VOID )
118 bb19cfda 2002-01-21 alex CHANNEL *c, *c_next;
119 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan, *cl2chan_next;
121 bb19cfda 2002-01-21 alex /* Channel-Strukturen freigeben */
122 bb19cfda 2002-01-21 alex c = My_Channels;
125 bb19cfda 2002-01-21 alex c_next = c->next;
127 bb19cfda 2002-01-21 alex c = c_next;
130 bb19cfda 2002-01-21 alex /* Channel-Zuordnungstabelle freigeben */
131 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
134 bb19cfda 2002-01-21 alex cl2chan_next = cl2chan->next;
135 bb19cfda 2002-01-21 alex free( cl2chan );
136 bb19cfda 2002-01-21 alex cl2chan = cl2chan_next;
138 38b9cb88 2001-12-14 alex } /* Channel_Exit */
141 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
142 c2f60abe 2002-05-27 alex Channel_Join( CLIENT *Client, CHAR *Name )
144 bb19cfda 2002-01-21 alex CHANNEL *chan;
146 bb19cfda 2002-01-21 alex assert( Client != NULL );
147 bb19cfda 2002-01-21 alex assert( Name != NULL );
149 bb19cfda 2002-01-21 alex /* Valider Channel-Name? */
150 f53914b1 2002-02-06 alex if( ! Channel_IsValidName( Name ))
152 bb19cfda 2002-01-21 alex IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
153 bb19cfda 2002-01-21 alex return FALSE;
156 bb19cfda 2002-01-21 alex /* Channel suchen */
157 8a324b6d 2002-01-26 alex chan = Channel_Search( Name );
160 bb19cfda 2002-01-21 alex /* Ist der Client bereits Mitglied? */
161 bb19cfda 2002-01-21 alex if( Get_Cl2Chan( chan, Client )) return FALSE;
165 bb19cfda 2002-01-21 alex /* Gibt es noch nicht? Dann neu anlegen: */
166 378e511e 2002-09-03 alex chan = Channel_Create( Name );
167 bb19cfda 2002-01-21 alex if( ! chan ) return FALSE;
170 bb19cfda 2002-01-21 alex /* User dem Channel hinzufuegen */
171 bb19cfda 2002-01-21 alex if( ! Add_Client( chan, Client )) return FALSE;
172 bb19cfda 2002-01-21 alex else return TRUE;
173 bb19cfda 2002-01-21 alex } /* Channel_Join */
176 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
177 c2f60abe 2002-05-27 alex Channel_Part( CLIENT *Client, CLIENT *Origin, CHAR *Name, CHAR *Reason )
179 bb19cfda 2002-01-21 alex CHANNEL *chan;
181 bb19cfda 2002-01-21 alex assert( Client != NULL );
182 bb19cfda 2002-01-21 alex assert( Name != NULL );
183 cc1e8514 2002-06-01 alex assert( Reason != NULL );
185 bb19cfda 2002-01-21 alex /* Channel suchen */
186 8a324b6d 2002-01-26 alex chan = Channel_Search( Name );
187 bb19cfda 2002-01-21 alex if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
189 bb19cfda 2002-01-21 alex IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
190 bb19cfda 2002-01-21 alex return FALSE;
193 bb19cfda 2002-01-21 alex /* User aus Channel entfernen */
194 cc1e8514 2002-06-01 alex if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, TRUE )) return FALSE;
195 bb19cfda 2002-01-21 alex else return TRUE;
196 bb19cfda 2002-01-21 alex } /* Channel_Part */
199 c2f60abe 2002-05-27 alex GLOBAL VOID
200 cc1e8514 2002-06-01 alex Channel_Kick( CLIENT *Client, CLIENT *Origin, CHAR *Name, CHAR *Reason )
202 cc1e8514 2002-06-01 alex CHANNEL *chan;
204 cc1e8514 2002-06-01 alex assert( Client != NULL );
205 cc1e8514 2002-06-01 alex assert( Origin != NULL );
206 cc1e8514 2002-06-01 alex assert( Name != NULL );
207 cc1e8514 2002-06-01 alex assert( Reason != NULL );
209 cc1e8514 2002-06-01 alex /* Channel suchen */
210 cc1e8514 2002-06-01 alex chan = Channel_Search( Name );
211 cc1e8514 2002-06-01 alex if( ! chan )
213 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
217 cc1e8514 2002-06-01 alex /* Ist der User Mitglied in dem Channel? */
218 cc1e8514 2002-06-01 alex if( ! Channel_IsMemberOf( chan, Origin ))
220 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
224 cc1e8514 2002-06-01 alex /* Ist der User Channel-Operator? */
225 cc1e8514 2002-06-01 alex if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
227 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
231 cc1e8514 2002-06-01 alex /* Ist der Ziel-User Mitglied im Channel? */
232 cc1e8514 2002-06-01 alex if( ! Channel_IsMemberOf( chan, Client ))
234 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
238 cc1e8514 2002-06-01 alex Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, TRUE );
239 cc1e8514 2002-06-01 alex } /* Channel_Kick */
242 cc1e8514 2002-06-01 alex GLOBAL VOID
243 cc1e8514 2002-06-01 alex Channel_Quit( CLIENT *Client, CHAR *Reason )
245 bb19cfda 2002-01-21 alex CHANNEL *c, *next_c;
247 bb19cfda 2002-01-21 alex assert( Client != NULL );
248 cc1e8514 2002-06-01 alex assert( Reason != NULL );
250 bb19cfda 2002-01-21 alex c = My_Channels;
253 bb19cfda 2002-01-21 alex next_c = c->next;
254 22365b2c 2002-06-01 alex Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, FALSE );
255 bb19cfda 2002-01-21 alex c = next_c;
257 cc1e8514 2002-06-01 alex } /* Channel_Quit */
260 c7b55aa6 2002-10-09 alex GLOBAL LONG
261 c2f60abe 2002-05-27 alex Channel_Count( VOID )
263 bb19cfda 2002-01-21 alex CHANNEL *c;
264 c7b55aa6 2002-10-09 alex LONG count;
267 bb19cfda 2002-01-21 alex c = My_Channels;
271 bb19cfda 2002-01-21 alex c = c->next;
273 bb19cfda 2002-01-21 alex return count;
274 56823924 2002-01-16 alex } /* Channel_Count */
277 c7b55aa6 2002-10-09 alex GLOBAL LONG
278 c2f60abe 2002-05-27 alex Channel_MemberCount( CHANNEL *Chan )
280 f22f1cf4 2002-04-23 alex CL2CHAN *cl2chan;
281 c7b55aa6 2002-10-09 alex LONG count;
283 f22f1cf4 2002-04-23 alex assert( Chan != NULL );
286 f22f1cf4 2002-04-23 alex cl2chan = My_Cl2Chan;
287 f22f1cf4 2002-04-23 alex while( cl2chan )
289 f22f1cf4 2002-04-23 alex if( cl2chan->channel == Chan ) count++;
290 f22f1cf4 2002-04-23 alex cl2chan = cl2chan->next;
292 f22f1cf4 2002-04-23 alex return count;
293 f22f1cf4 2002-04-23 alex } /* Channel_MemberCount */
297 e9078163 2002-12-14 alex Channel_CountForUser( CLIENT *Client )
299 b7eb5f66 2002-12-13 alex /* Count number of channels a user is member of. */
301 b7eb5f66 2002-12-13 alex CL2CHAN *cl2chan;
304 b7eb5f66 2002-12-13 alex assert( Client != NULL );
307 b7eb5f66 2002-12-13 alex cl2chan = My_Cl2Chan;
308 b7eb5f66 2002-12-13 alex while( cl2chan )
310 b7eb5f66 2002-12-13 alex if( cl2chan->client == Client ) count++;
311 b7eb5f66 2002-12-13 alex cl2chan = cl2chan->next;
314 b7eb5f66 2002-12-13 alex return count;
315 b7eb5f66 2002-12-13 alex } /* Channel_CountForUser */
319 e9078163 2002-12-14 alex Channel_PCount( VOID )
321 e9078163 2002-12-14 alex /* Count the number of persistent (mode 'P') channels */
323 e9078163 2002-12-14 alex CHANNEL *chan;
327 e9078163 2002-12-14 alex chan = My_Channels;
328 e9078163 2002-12-14 alex while( chan )
330 e9078163 2002-12-14 alex if( strchr( chan->modes, 'P' )) count++;
331 e9078163 2002-12-14 alex chan = chan->next;
334 e9078163 2002-12-14 alex return count;
335 e9078163 2002-12-14 alex } /* Channel_PCount */
338 c2f60abe 2002-05-27 alex GLOBAL CHAR *
339 c2f60abe 2002-05-27 alex Channel_Name( CHANNEL *Chan )
341 71c7a6fc 2002-01-28 alex assert( Chan != NULL );
342 71c7a6fc 2002-01-28 alex return Chan->name;
343 71c7a6fc 2002-01-28 alex } /* Channel_Name */
346 c2f60abe 2002-05-27 alex GLOBAL CHAR *
347 c2f60abe 2002-05-27 alex Channel_Modes( CHANNEL *Chan )
349 f53914b1 2002-02-06 alex assert( Chan != NULL );
350 f53914b1 2002-02-06 alex return Chan->modes;
351 f53914b1 2002-02-06 alex } /* Channel_Modes */
354 2e88dd72 2002-12-16 alex GLOBAL CHAR *
355 2e88dd72 2002-12-16 alex Channel_Key( CHANNEL *Chan )
357 2e88dd72 2002-12-16 alex assert( Chan != NULL );
358 2e88dd72 2002-12-16 alex return Chan->key;
359 2e88dd72 2002-12-16 alex } /* Channel_Key */
362 2e88dd72 2002-12-16 alex GLOBAL LONG
363 2e88dd72 2002-12-16 alex Channel_MaxUsers( CHANNEL *Chan )
365 2e88dd72 2002-12-16 alex assert( Chan != NULL );
366 2e88dd72 2002-12-16 alex return Chan->maxusers;
367 2e88dd72 2002-12-16 alex } /* Channel_MaxUsers */
370 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
371 c2f60abe 2002-05-27 alex Channel_First( VOID )
373 71c7a6fc 2002-01-28 alex return My_Channels;
374 71c7a6fc 2002-01-28 alex } /* Channel_First */
377 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
378 c2f60abe 2002-05-27 alex Channel_Next( CHANNEL *Chan )
380 71c7a6fc 2002-01-28 alex assert( Chan != NULL );
381 71c7a6fc 2002-01-28 alex return Chan->next;
382 71c7a6fc 2002-01-28 alex } /* Channel_Next */
385 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
386 c2f60abe 2002-05-27 alex Channel_Search( CHAR *Name )
388 bb19cfda 2002-01-21 alex /* Channel-Struktur suchen */
390 bb19cfda 2002-01-21 alex CHANNEL *c;
391 0e38d10b 2002-03-25 alex UINT32 search_hash;
393 bb19cfda 2002-01-21 alex assert( Name != NULL );
395 0e38d10b 2002-03-25 alex search_hash = Hash( Name );
396 bb19cfda 2002-01-21 alex c = My_Channels;
399 0e38d10b 2002-03-25 alex if( search_hash == c->hash )
401 0e38d10b 2002-03-25 alex /* lt. Hash-Wert: Treffer! */
402 0e38d10b 2002-03-25 alex if( strcasecmp( Name, c->name ) == 0 ) return c;
404 bb19cfda 2002-01-21 alex c = c->next;
406 bb19cfda 2002-01-21 alex return NULL;
407 8a324b6d 2002-01-26 alex } /* Channel_Search */
410 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
411 c2f60abe 2002-05-27 alex Channel_FirstMember( CHANNEL *Chan )
413 8a324b6d 2002-01-26 alex assert( Chan != NULL );
414 8a324b6d 2002-01-26 alex return Get_First_Cl2Chan( NULL, Chan );
415 0bb9db1a 2002-01-29 alex } /* Channel_FirstMember */
418 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
419 c2f60abe 2002-05-27 alex Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
421 8a324b6d 2002-01-26 alex assert( Chan != NULL );
422 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
423 8a324b6d 2002-01-26 alex return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
424 8a324b6d 2002-01-26 alex } /* Channel_NextMember */
427 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
428 c2f60abe 2002-05-27 alex Channel_FirstChannelOf( CLIENT *Client )
430 0bb9db1a 2002-01-29 alex assert( Client != NULL );
431 0bb9db1a 2002-01-29 alex return Get_First_Cl2Chan( Client, NULL );
432 0bb9db1a 2002-01-29 alex } /* Channel_FirstChannelOf */
435 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
436 c2f60abe 2002-05-27 alex Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
438 0bb9db1a 2002-01-29 alex assert( Client != NULL );
439 0bb9db1a 2002-01-29 alex assert( Cl2Chan != NULL );
440 0bb9db1a 2002-01-29 alex return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
441 0bb9db1a 2002-01-29 alex } /* Channel_NextChannelOf */
444 c2f60abe 2002-05-27 alex GLOBAL CLIENT *
445 c2f60abe 2002-05-27 alex Channel_GetClient( CL2CHAN *Cl2Chan )
447 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
448 8a324b6d 2002-01-26 alex return Cl2Chan->client;
449 8a324b6d 2002-01-26 alex } /* Channel_GetClient */
452 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
453 c2f60abe 2002-05-27 alex Channel_GetChannel( CL2CHAN *Cl2Chan )
455 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
456 8a324b6d 2002-01-26 alex return Cl2Chan->channel;
457 8a324b6d 2002-01-26 alex } /* Channel_GetChannel */
460 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
461 c2f60abe 2002-05-27 alex Channel_IsValidName( CHAR *Name )
463 f7327524 2002-05-30 alex /* Pruefen, ob Name als Channelname gueltig */
465 f7327524 2002-05-30 alex CHAR *ptr, badchars[10];
467 f53914b1 2002-02-06 alex assert( Name != NULL );
469 f53914b1 2002-02-06 alex if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return FALSE;
471 7157d936 2002-03-02 alex ptr = Name;
472 d9a13b31 2002-12-25 alex strcpy( badchars, " ,:\007" );
473 7157d936 2002-03-02 alex while( *ptr )
475 7157d936 2002-03-02 alex if( strchr( badchars, *ptr )) return FALSE;
479 f53914b1 2002-02-06 alex return TRUE;
480 f53914b1 2002-02-06 alex } /* Channel_IsValidName */
483 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
484 c2f60abe 2002-05-27 alex Channel_ModeAdd( CHANNEL *Chan, CHAR Mode )
486 d72c55a0 2002-02-11 alex /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
487 d72c55a0 2002-02-11 alex * Mode neu gesetzt wurde, FALSE, wenn der Channel den Mode
488 d72c55a0 2002-02-11 alex * bereits hatte. */
492 d72c55a0 2002-02-11 alex assert( Chan != NULL );
494 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
495 d72c55a0 2002-02-11 alex if( ! strchr( Chan->modes, x[0] ))
497 d72c55a0 2002-02-11 alex /* Client hat den Mode noch nicht -> setzen */
498 6626395c 2002-12-26 alex strlcat( Chan->modes, x, sizeof( Chan->modes ));
499 d72c55a0 2002-02-11 alex return TRUE;
501 d72c55a0 2002-02-11 alex else return FALSE;
502 d72c55a0 2002-02-11 alex } /* Channel_ModeAdd */
505 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
506 c2f60abe 2002-05-27 alex Channel_ModeDel( CHANNEL *Chan, CHAR Mode )
508 d72c55a0 2002-02-11 alex /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
509 d72c55a0 2002-02-11 alex * Mode entfernt wurde, FALSE, wenn der Channel den Mode
510 d72c55a0 2002-02-11 alex * ueberhaupt nicht hatte. */
512 d72c55a0 2002-02-11 alex CHAR x[2], *p;
514 d72c55a0 2002-02-11 alex assert( Chan != NULL );
516 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
518 d72c55a0 2002-02-11 alex p = strchr( Chan->modes, x[0] );
519 d72c55a0 2002-02-11 alex if( ! p ) return FALSE;
521 d72c55a0 2002-02-11 alex /* Client hat den Mode -> loeschen */
522 d72c55a0 2002-02-11 alex while( *p )
524 d72c55a0 2002-02-11 alex *p = *(p + 1);
527 d72c55a0 2002-02-11 alex return TRUE;
528 d72c55a0 2002-02-11 alex } /* Channel_ModeDel */
531 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
532 c2f60abe 2002-05-27 alex Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, CHAR Mode )
534 d72c55a0 2002-02-11 alex /* Channel-User-Mode soll gesetzt werden. TRUE wird geliefert,
535 d72c55a0 2002-02-11 alex * wenn der Mode neu gesetzt wurde, FALSE, wenn der User den
536 d72c55a0 2002-02-11 alex * Channel-Mode bereits hatte. */
538 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
541 d72c55a0 2002-02-11 alex assert( Chan != NULL );
542 d72c55a0 2002-02-11 alex assert( Client != NULL );
544 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
545 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
547 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
548 d72c55a0 2002-02-11 alex if( ! strchr( cl2chan->modes, x[0] ))
550 d72c55a0 2002-02-11 alex /* Client hat den Mode noch nicht -> setzen */
551 6626395c 2002-12-26 alex strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
552 d72c55a0 2002-02-11 alex return TRUE;
554 d72c55a0 2002-02-11 alex else return FALSE;
555 d72c55a0 2002-02-11 alex } /* Channel_UserModeAdd */
558 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
559 c2f60abe 2002-05-27 alex Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, CHAR Mode )
561 d72c55a0 2002-02-11 alex /* Channel-User-Mode soll geloescht werden. TRUE wird geliefert,
562 d72c55a0 2002-02-11 alex * wenn der Mode entfernt wurde, FALSE, wenn der User den Channel-Mode
563 d72c55a0 2002-02-11 alex * ueberhaupt nicht hatte. */
565 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
566 d72c55a0 2002-02-11 alex CHAR x[2], *p;
568 d72c55a0 2002-02-11 alex assert( Chan != NULL );
569 d72c55a0 2002-02-11 alex assert( Client != NULL );
571 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
572 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
574 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
576 d72c55a0 2002-02-11 alex p = strchr( cl2chan->modes, x[0] );
577 d72c55a0 2002-02-11 alex if( ! p ) return FALSE;
579 d72c55a0 2002-02-11 alex /* Client hat den Mode -> loeschen */
580 d72c55a0 2002-02-11 alex while( *p )
582 d72c55a0 2002-02-11 alex *p = *(p + 1);
585 d72c55a0 2002-02-11 alex return TRUE;
586 d72c55a0 2002-02-11 alex } /* Channel_UserModeDel */
589 c2f60abe 2002-05-27 alex GLOBAL CHAR *
590 c2f60abe 2002-05-27 alex Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
592 d72c55a0 2002-02-11 alex /* Channel-Modes eines Users liefern */
594 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
596 d72c55a0 2002-02-11 alex assert( Chan != NULL );
597 d72c55a0 2002-02-11 alex assert( Client != NULL );
599 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
600 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
602 d72c55a0 2002-02-11 alex return cl2chan->modes;
603 d72c55a0 2002-02-11 alex } /* Channel_UserModes */
606 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
607 c2f60abe 2002-05-27 alex Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
609 153aa0aa 2002-02-27 alex /* Pruefen, ob Client Mitglied in Channel ist */
611 153aa0aa 2002-02-27 alex assert( Chan != NULL );
612 153aa0aa 2002-02-27 alex assert( Client != NULL );
614 153aa0aa 2002-02-27 alex if( Get_Cl2Chan( Chan, Client )) return TRUE;
615 153aa0aa 2002-02-27 alex else return FALSE;
616 153aa0aa 2002-02-27 alex } /* Channel_IsMemberOf */
619 c2f60abe 2002-05-27 alex GLOBAL CHAR *
620 c2f60abe 2002-05-27 alex Channel_Topic( CHANNEL *Chan )
622 52548bd3 2002-02-27 alex assert( Chan != NULL );
623 52548bd3 2002-02-27 alex return Chan->topic;
624 52548bd3 2002-02-27 alex } /* Channel_Topic */
627 c2f60abe 2002-05-27 alex GLOBAL VOID
628 c2f60abe 2002-05-27 alex Channel_SetTopic( CHANNEL *Chan, CHAR *Topic )
630 52548bd3 2002-02-27 alex assert( Chan != NULL );
631 52548bd3 2002-02-27 alex assert( Topic != NULL );
633 0ced4181 2002-12-26 alex strlcpy( Chan->topic, Topic, sizeof( Chan->topic ));
634 52548bd3 2002-02-27 alex } /* Channel_SetTopic */
637 378e511e 2002-09-03 alex GLOBAL VOID
638 378e511e 2002-09-03 alex Channel_SetModes( CHANNEL *Chan, CHAR *Modes )
640 378e511e 2002-09-03 alex assert( Chan != NULL );
641 378e511e 2002-09-03 alex assert( Modes != NULL );
643 0ced4181 2002-12-26 alex strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
644 378e511e 2002-09-03 alex } /* Channel_SetModes */
647 2e88dd72 2002-12-16 alex GLOBAL VOID
648 2e88dd72 2002-12-16 alex Channel_SetKey( CHANNEL *Chan, CHAR *Key )
650 2e88dd72 2002-12-16 alex assert( Chan != NULL );
651 2e88dd72 2002-12-16 alex assert( Key != NULL );
653 0ced4181 2002-12-26 alex strlcpy( Chan->key, Key, sizeof( Chan->key ));
654 2e88dd72 2002-12-16 alex Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
655 2e88dd72 2002-12-16 alex } /* Channel_SetKey */
658 2e88dd72 2002-12-16 alex GLOBAL VOID
659 2e88dd72 2002-12-16 alex Channel_SetMaxUsers( CHANNEL *Chan, LONG Count )
661 2e88dd72 2002-12-16 alex assert( Chan != NULL );
663 2e88dd72 2002-12-16 alex Chan->maxusers = Count;
664 2e88dd72 2002-12-16 alex Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
665 2e88dd72 2002-12-16 alex } /* Channel_SetMaxUsers */
668 c2f60abe 2002-05-27 alex GLOBAL BOOLEAN
669 c2f60abe 2002-05-27 alex Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, CHAR *Text )
671 0e38d10b 2002-03-25 alex BOOLEAN is_member, has_voice, is_op, ok;
673 be79fabc 2003-11-06 alex /* Okay, target is a channel */
674 0e38d10b 2002-03-25 alex is_member = has_voice = is_op = FALSE;
675 0e38d10b 2002-03-25 alex if( Channel_IsMemberOf( Chan, From ))
677 0e38d10b 2002-03-25 alex is_member = TRUE;
678 0e38d10b 2002-03-25 alex if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = TRUE;
679 0e38d10b 2002-03-25 alex if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = TRUE;
682 be79fabc 2003-11-06 alex /* Check weather client is allowed to write to channel */
684 0e38d10b 2002-03-25 alex if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = FALSE;
685 0e38d10b 2002-03-25 alex if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = FALSE;
687 be79fabc 2003-11-06 alex /* Is the client banned? */
688 be79fabc 2003-11-06 alex if( Lists_CheckBanned( From, Chan ))
690 be79fabc 2003-11-06 alex /* Client is banned, bus is he channel operator or has voice? */
691 be79fabc 2003-11-06 alex if(( ! has_voice ) && ( ! is_op )) ok = FALSE;
694 0e38d10b 2002-03-25 alex if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
696 be79fabc 2003-11-06 alex /* Send text */
697 0e38d10b 2002-03-25 alex if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
698 0e38d10b 2002-03-25 alex return IRC_WriteStrChannelPrefix( Client, Chan, From, TRUE, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
699 0e38d10b 2002-03-25 alex } /* Channel_Write */
702 378e511e 2002-09-03 alex GLOBAL CHANNEL *
703 378e511e 2002-09-03 alex Channel_Create( CHAR *Name )
705 bb19cfda 2002-01-21 alex /* Neue Channel-Struktur anlegen */
707 bb19cfda 2002-01-21 alex CHANNEL *c;
709 bb19cfda 2002-01-21 alex assert( Name != NULL );
711 cb76d96e 2004-03-11 alex c = (CHANNEL *)malloc( sizeof( CHANNEL ));
714 1fd7215d 2002-06-09 alex Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
715 bb19cfda 2002-01-21 alex return NULL;
717 bb19cfda 2002-01-21 alex c->next = NULL;
718 0ced4181 2002-12-26 alex strlcpy( c->name, Name, sizeof( c->name ));
719 bb19cfda 2002-01-21 alex c->name[CHANNEL_NAME_LEN - 1] = '\0';
720 bb19cfda 2002-01-21 alex strcpy( c->modes, "" );
721 52548bd3 2002-02-27 alex strcpy( c->topic, "" );
722 0e38d10b 2002-03-25 alex c->hash = Hash( c->name );
723 2e88dd72 2002-12-16 alex strcpy( c->key, "" );
724 2e88dd72 2002-12-16 alex c->maxusers = 0;
726 378e511e 2002-09-03 alex /* Verketten */
727 378e511e 2002-09-03 alex c->next = My_Channels;
728 378e511e 2002-09-03 alex My_Channels = c;
730 bb19cfda 2002-01-21 alex Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
733 378e511e 2002-09-03 alex } /* Channel_Create */
736 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
737 c2f60abe 2002-05-27 alex Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
739 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
741 bb19cfda 2002-01-21 alex assert( Chan != NULL );
742 bb19cfda 2002-01-21 alex assert( Client != NULL );
744 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
745 bb19cfda 2002-01-21 alex while( cl2chan )
747 bb19cfda 2002-01-21 alex if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
748 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
750 bb19cfda 2002-01-21 alex return NULL;
751 bb19cfda 2002-01-21 alex } /* Get_Cl2Chan */
754 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
755 c2f60abe 2002-05-27 alex Add_Client( CHANNEL *Chan, CLIENT *Client )
757 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
759 bb19cfda 2002-01-21 alex assert( Chan != NULL );
760 bb19cfda 2002-01-21 alex assert( Client != NULL );
762 d72c55a0 2002-02-11 alex /* neue CL2CHAN-Struktur anlegen */
763 cb76d96e 2004-03-11 alex cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
764 bb19cfda 2002-01-21 alex if( ! cl2chan )
766 1fd7215d 2002-06-09 alex Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
767 bb19cfda 2002-01-21 alex return NULL;
769 bb19cfda 2002-01-21 alex cl2chan->channel = Chan;
770 bb19cfda 2002-01-21 alex cl2chan->client = Client;
771 d72c55a0 2002-02-11 alex strcpy( cl2chan->modes, "" );
773 bb19cfda 2002-01-21 alex /* Verketten */
774 bb19cfda 2002-01-21 alex cl2chan->next = My_Cl2Chan;
775 bb19cfda 2002-01-21 alex My_Cl2Chan = cl2chan;
777 d569c924 2002-01-27 alex Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
779 bb19cfda 2002-01-21 alex return cl2chan;
780 bb19cfda 2002-01-21 alex } /* Add_Client */
783 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
784 cc1e8514 2002-06-01 alex Remove_Client( INT Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, CHAR *Reason, BOOLEAN InformServer )
786 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan, *last_cl2chan;
787 bb19cfda 2002-01-21 alex CHANNEL *c;
789 bb19cfda 2002-01-21 alex assert( Chan != NULL );
790 bb19cfda 2002-01-21 alex assert( Client != NULL );
791 bb19cfda 2002-01-21 alex assert( Origin != NULL );
792 bb19cfda 2002-01-21 alex assert( Reason != NULL );
794 bb19cfda 2002-01-21 alex last_cl2chan = NULL;
795 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
796 bb19cfda 2002-01-21 alex while( cl2chan )
798 bb19cfda 2002-01-21 alex if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
799 bb19cfda 2002-01-21 alex last_cl2chan = cl2chan;
800 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
802 bb19cfda 2002-01-21 alex if( ! cl2chan ) return FALSE;
804 bb19cfda 2002-01-21 alex c = cl2chan->channel;
805 bb19cfda 2002-01-21 alex assert( c != NULL );
807 bb19cfda 2002-01-21 alex /* Aus Verkettung loesen und freigeben */
808 bb19cfda 2002-01-21 alex if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
809 bb19cfda 2002-01-21 alex else My_Cl2Chan = cl2chan->next;
810 bb19cfda 2002-01-21 alex free( cl2chan );
812 cc1e8514 2002-06-01 alex switch( Type )
814 cc1e8514 2002-06-01 alex case REMOVE_QUIT:
815 22365b2c 2002-06-01 alex /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
816 22365b2c 2002-06-01 alex * hier also "nur" noch alle User in betroffenen Channeln infomieren */
817 cc1e8514 2002-06-01 alex assert( InformServer == FALSE );
818 cc1e8514 2002-06-01 alex IRC_WriteStrChannelPrefix( Origin, c, Origin, FALSE, "QUIT :%s", Reason );
819 cc1e8514 2002-06-01 alex Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
821 cc1e8514 2002-06-01 alex case REMOVE_KICK:
822 22365b2c 2002-06-01 alex /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
823 22365b2c 2002-06-01 alex * im entsprechenden Channel informieren */
824 cc1e8514 2002-06-01 alex if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
825 cc1e8514 2002-06-01 alex IRC_WriteStrChannelPrefix( Client, c, Origin, FALSE, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
826 cc1e8514 2002-06-01 alex if(( Client_Conn( Client ) > NONE ) && ( Client_Type( Client ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Client, Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
827 cc1e8514 2002-06-01 alex Log( LOG_DEBUG, "User \"%s\" has been kicked of \"%s\" by \"%s\": %s.", Client_Mask( Client ), c->name, Client_ID( Origin ), Reason );
831 cc1e8514 2002-06-01 alex if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
832 cc1e8514 2002-06-01 alex IRC_WriteStrChannelPrefix( Origin, c, Client, FALSE, "PART %s :%s", c->name, Reason );
833 cc1e8514 2002-06-01 alex if(( Client_Conn( Origin ) > NONE ) && ( Client_Type( Origin ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
834 cc1e8514 2002-06-01 alex Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
837 040f5422 2002-05-21 alex /* Wenn Channel nun leer und nicht pre-defined: loeschen */
838 040f5422 2002-05-21 alex if( ! strchr( Channel_Modes( Chan ), 'P' ))
840 040f5422 2002-05-21 alex if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
843 bb19cfda 2002-01-21 alex return TRUE;
844 bb19cfda 2002-01-21 alex } /* Remove_Client */
847 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
848 c2f60abe 2002-05-27 alex Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
850 bb19cfda 2002-01-21 alex return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
851 bb19cfda 2002-01-21 alex } /* Get_First_Cl2Chan */
854 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
855 c2f60abe 2002-05-27 alex Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
857 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
859 bb19cfda 2002-01-21 alex assert( Client != NULL || Channel != NULL );
861 bb19cfda 2002-01-21 alex cl2chan = Start;
862 bb19cfda 2002-01-21 alex while( cl2chan )
864 bb19cfda 2002-01-21 alex if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
865 bb19cfda 2002-01-21 alex if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
866 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
868 bb19cfda 2002-01-21 alex return NULL;
869 bb19cfda 2002-01-21 alex } /* Get_Next_Cl2Chan */
872 c2f60abe 2002-05-27 alex LOCAL BOOLEAN
873 c2f60abe 2002-05-27 alex Delete_Channel( CHANNEL *Chan )
875 bb19cfda 2002-01-21 alex /* Channel-Struktur loeschen */
877 bb19cfda 2002-01-21 alex CHANNEL *chan, *last_chan;
879 bb19cfda 2002-01-21 alex last_chan = NULL;
880 bb19cfda 2002-01-21 alex chan = My_Channels;
881 bb19cfda 2002-01-21 alex while( chan )
883 bb19cfda 2002-01-21 alex if( chan == Chan ) break;
884 bb19cfda 2002-01-21 alex last_chan = chan;
885 bb19cfda 2002-01-21 alex chan = chan->next;
887 bb19cfda 2002-01-21 alex if( ! chan ) return FALSE;
889 bb19cfda 2002-01-21 alex Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
891 1fd7215d 2002-06-09 alex /* Invite- und Ban-Lists aufraeumen */
892 1fd7215d 2002-06-09 alex Lists_DeleteChannel( chan );
894 bb19cfda 2002-01-21 alex /* Neu verketten und freigeben */
895 bb19cfda 2002-01-21 alex if( last_chan ) last_chan->next = chan->next;
896 bb19cfda 2002-01-21 alex else My_Channels = chan->next;
897 bb19cfda 2002-01-21 alex free( chan );
899 bb19cfda 2002-01-21 alex return TRUE;
900 bb19cfda 2002-01-21 alex } /* Delete_Channel */
903 38b9cb88 2001-12-14 alex /* -eof- */