2 38b9cb88 2001-12-14 alex * ngIRCd -- The Next Generation IRC Daemon
3 dfe662ad 2005-04-27 alex * Copyright (c)2001-2005 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 51732180 2005-06-04 fw static char UNUSED id[] = "$Id: channel.c,v 1.48 2005/06/04 11:53:25 fw 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 dfe662ad 2005-04-27 alex #include "defines.h"
29 b8d7dcec 2002-12-30 alex #include "conn-func.h"
30 bb19cfda 2002-01-21 alex #include "client.h"
32 c2f60abe 2002-05-27 alex #include "exp.h"
33 c2f60abe 2002-05-27 alex #include "channel.h"
35 c2f60abe 2002-05-27 alex #include "imp.h"
36 c2f60abe 2002-05-27 alex #include "irc-write.h"
37 c2f60abe 2002-05-27 alex #include "resolve.h"
38 040f5422 2002-05-21 alex #include "conf.h"
39 0e38d10b 2002-03-25 alex #include "hash.h"
40 1fd7215d 2002-06-09 alex #include "lists.h"
41 bb19cfda 2002-01-21 alex #include "log.h"
42 bb19cfda 2002-01-21 alex #include "messages.h"
44 ca33cbda 2002-03-12 alex #include "exp.h"
47 cc1e8514 2002-06-01 alex #define REMOVE_PART 0
48 cc1e8514 2002-06-01 alex #define REMOVE_QUIT 1
49 cc1e8514 2002-06-01 alex #define REMOVE_KICK 2
52 38b9cb88 2001-12-14 alex LOCAL CHANNEL *My_Channels;
53 bb19cfda 2002-01-21 alex LOCAL CL2CHAN *My_Cl2Chan;
56 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 8adff592 2005-03-19 fw LOCAL bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
59 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
60 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
61 8adff592 2005-03-19 fw LOCAL bool Delete_Channel PARAMS(( CHANNEL *Chan ));
65 8adff592 2005-03-19 fw Channel_Init( void )
67 38b9cb88 2001-12-14 alex My_Channels = NULL;
68 bb19cfda 2002-01-21 alex My_Cl2Chan = NULL;
69 04162c39 2002-06-02 alex } /* Channel_Init */
73 8adff592 2005-03-19 fw Channel_InitPredefined( void )
75 040f5422 2002-05-21 alex /* Vordefinierte persistente Channels erzeugen */
77 04162c39 2002-06-02 alex CHANNEL *chan;
81 040f5422 2002-05-21 alex for( i = 0; i < Conf_Channel_Count; i++ )
83 040f5422 2002-05-21 alex /* Ist ein Name konfiguriert? */
84 adcd9289 2002-09-02 alex if( ! Conf_Channel[i].name[0] ) continue;
86 040f5422 2002-05-21 alex /* Gueltiger Channel-Name? */
87 040f5422 2002-05-21 alex if( ! Channel_IsValidName( Conf_Channel[i].name ))
89 040f5422 2002-05-21 alex Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
93 7233b721 2002-11-22 alex /* Gibt es den Channel bereits? */
94 7233b721 2002-11-22 alex chan = Channel_Search( Conf_Channel[i].name );
97 7233b721 2002-11-22 alex Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
101 040f5422 2002-05-21 alex /* Channel anlegen */
102 378e511e 2002-09-03 alex chan = Channel_Create( Conf_Channel[i].name );
105 040f5422 2002-05-21 alex Channel_ModeAdd( chan, 'P' );
106 040f5422 2002-05-21 alex Channel_SetTopic( chan, Conf_Channel[i].topic );
107 040f5422 2002-05-21 alex c = Conf_Channel[i].modes;
108 040f5422 2002-05-21 alex while( *c ) Channel_ModeAdd( chan, *c++ );
109 040f5422 2002-05-21 alex Log( LOG_INFO, "Created pre-defined channel \"%s\".", Conf_Channel[i].name );
111 040f5422 2002-05-21 alex else Log( LOG_ERR, "Can't create pre-defined channel \"%s\"!", Conf_Channel[i].name );
113 04162c39 2002-06-02 alex } /* Channel_InitPredefined */
117 8adff592 2005-03-19 fw Channel_Exit( void )
119 bb19cfda 2002-01-21 alex CHANNEL *c, *c_next;
120 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan, *cl2chan_next;
122 bb19cfda 2002-01-21 alex /* Channel-Strukturen freigeben */
123 bb19cfda 2002-01-21 alex c = My_Channels;
126 bb19cfda 2002-01-21 alex c_next = c->next;
128 bb19cfda 2002-01-21 alex c = c_next;
131 bb19cfda 2002-01-21 alex /* Channel-Zuordnungstabelle freigeben */
132 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
135 bb19cfda 2002-01-21 alex cl2chan_next = cl2chan->next;
136 bb19cfda 2002-01-21 alex free( cl2chan );
137 bb19cfda 2002-01-21 alex cl2chan = cl2chan_next;
139 38b9cb88 2001-12-14 alex } /* Channel_Exit */
143 8adff592 2005-03-19 fw Channel_Join( CLIENT *Client, char *Name )
145 bb19cfda 2002-01-21 alex CHANNEL *chan;
147 bb19cfda 2002-01-21 alex assert( Client != NULL );
148 bb19cfda 2002-01-21 alex assert( Name != NULL );
150 bb19cfda 2002-01-21 alex /* Valider Channel-Name? */
151 f53914b1 2002-02-06 alex if( ! Channel_IsValidName( Name ))
153 bb19cfda 2002-01-21 alex IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
154 8adff592 2005-03-19 fw return false;
157 bb19cfda 2002-01-21 alex /* Channel suchen */
158 8a324b6d 2002-01-26 alex chan = Channel_Search( Name );
161 bb19cfda 2002-01-21 alex /* Ist der Client bereits Mitglied? */
162 8adff592 2005-03-19 fw if( Get_Cl2Chan( chan, Client )) return false;
166 bb19cfda 2002-01-21 alex /* Gibt es noch nicht? Dann neu anlegen: */
167 378e511e 2002-09-03 alex chan = Channel_Create( Name );
168 8adff592 2005-03-19 fw if( ! chan ) return false;
171 bb19cfda 2002-01-21 alex /* User dem Channel hinzufuegen */
172 8adff592 2005-03-19 fw if( ! Add_Client( chan, Client )) return false;
173 8adff592 2005-03-19 fw else return true;
174 bb19cfda 2002-01-21 alex } /* Channel_Join */
178 8adff592 2005-03-19 fw Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
180 bb19cfda 2002-01-21 alex CHANNEL *chan;
182 bb19cfda 2002-01-21 alex assert( Client != NULL );
183 bb19cfda 2002-01-21 alex assert( Name != NULL );
184 cc1e8514 2002-06-01 alex assert( Reason != NULL );
186 bb19cfda 2002-01-21 alex /* Channel suchen */
187 8a324b6d 2002-01-26 alex chan = Channel_Search( Name );
188 bb19cfda 2002-01-21 alex if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
190 bb19cfda 2002-01-21 alex IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
191 8adff592 2005-03-19 fw return false;
194 bb19cfda 2002-01-21 alex /* User aus Channel entfernen */
195 8adff592 2005-03-19 fw if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
196 8adff592 2005-03-19 fw else return true;
197 bb19cfda 2002-01-21 alex } /* Channel_Part */
201 8adff592 2005-03-19 fw Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
203 cc1e8514 2002-06-01 alex CHANNEL *chan;
205 cc1e8514 2002-06-01 alex assert( Client != NULL );
206 cc1e8514 2002-06-01 alex assert( Origin != NULL );
207 cc1e8514 2002-06-01 alex assert( Name != NULL );
208 cc1e8514 2002-06-01 alex assert( Reason != NULL );
210 cc1e8514 2002-06-01 alex /* Channel suchen */
211 cc1e8514 2002-06-01 alex chan = Channel_Search( Name );
212 cc1e8514 2002-06-01 alex if( ! chan )
214 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
218 cc1e8514 2002-06-01 alex /* Ist der User Mitglied in dem Channel? */
219 cc1e8514 2002-06-01 alex if( ! Channel_IsMemberOf( chan, Origin ))
221 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
225 cc1e8514 2002-06-01 alex /* Ist der User Channel-Operator? */
226 cc1e8514 2002-06-01 alex if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
228 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
232 cc1e8514 2002-06-01 alex /* Ist der Ziel-User Mitglied im Channel? */
233 cc1e8514 2002-06-01 alex if( ! Channel_IsMemberOf( chan, Client ))
235 cc1e8514 2002-06-01 alex IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
239 8adff592 2005-03-19 fw Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
240 cc1e8514 2002-06-01 alex } /* Channel_Kick */
244 8adff592 2005-03-19 fw Channel_Quit( CLIENT *Client, char *Reason )
246 bb19cfda 2002-01-21 alex CHANNEL *c, *next_c;
248 bb19cfda 2002-01-21 alex assert( Client != NULL );
249 cc1e8514 2002-06-01 alex assert( Reason != NULL );
251 51732180 2005-06-04 fw IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
253 bb19cfda 2002-01-21 alex c = My_Channels;
256 bb19cfda 2002-01-21 alex next_c = c->next;
257 8adff592 2005-03-19 fw Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
258 bb19cfda 2002-01-21 alex c = next_c;
260 cc1e8514 2002-06-01 alex } /* Channel_Quit */
264 8adff592 2005-03-19 fw Channel_Count( void )
266 bb19cfda 2002-01-21 alex CHANNEL *c;
267 8adff592 2005-03-19 fw long count = 0;
269 bb19cfda 2002-01-21 alex c = My_Channels;
273 bb19cfda 2002-01-21 alex c = c->next;
275 bb19cfda 2002-01-21 alex return count;
276 56823924 2002-01-16 alex } /* Channel_Count */
280 c2f60abe 2002-05-27 alex Channel_MemberCount( CHANNEL *Chan )
282 f22f1cf4 2002-04-23 alex CL2CHAN *cl2chan;
283 8adff592 2005-03-19 fw long count = 0;
285 f22f1cf4 2002-04-23 alex assert( Chan != NULL );
287 f22f1cf4 2002-04-23 alex cl2chan = My_Cl2Chan;
288 f22f1cf4 2002-04-23 alex while( cl2chan )
290 f22f1cf4 2002-04-23 alex if( cl2chan->channel == Chan ) count++;
291 f22f1cf4 2002-04-23 alex cl2chan = cl2chan->next;
293 f22f1cf4 2002-04-23 alex return count;
294 f22f1cf4 2002-04-23 alex } /* Channel_MemberCount */
298 e9078163 2002-12-14 alex Channel_CountForUser( CLIENT *Client )
300 b7eb5f66 2002-12-13 alex /* Count number of channels a user is member of. */
302 b7eb5f66 2002-12-13 alex CL2CHAN *cl2chan;
303 8adff592 2005-03-19 fw int count = 0;
305 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 8adff592 2005-03-19 fw 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;
324 8adff592 2005-03-19 fw int count = 0;
326 e9078163 2002-12-14 alex chan = My_Channels;
327 e9078163 2002-12-14 alex while( chan )
329 e9078163 2002-12-14 alex if( strchr( chan->modes, 'P' )) count++;
330 e9078163 2002-12-14 alex chan = chan->next;
333 e9078163 2002-12-14 alex return count;
334 e9078163 2002-12-14 alex } /* Channel_PCount */
337 8adff592 2005-03-19 fw GLOBAL char *
338 c2f60abe 2002-05-27 alex Channel_Name( CHANNEL *Chan )
340 71c7a6fc 2002-01-28 alex assert( Chan != NULL );
341 71c7a6fc 2002-01-28 alex return Chan->name;
342 71c7a6fc 2002-01-28 alex } /* Channel_Name */
345 8adff592 2005-03-19 fw GLOBAL char *
346 c2f60abe 2002-05-27 alex Channel_Modes( CHANNEL *Chan )
348 f53914b1 2002-02-06 alex assert( Chan != NULL );
349 f53914b1 2002-02-06 alex return Chan->modes;
350 f53914b1 2002-02-06 alex } /* Channel_Modes */
353 8adff592 2005-03-19 fw GLOBAL char *
354 2e88dd72 2002-12-16 alex Channel_Key( CHANNEL *Chan )
356 2e88dd72 2002-12-16 alex assert( Chan != NULL );
357 2e88dd72 2002-12-16 alex return Chan->key;
358 2e88dd72 2002-12-16 alex } /* Channel_Key */
362 2e88dd72 2002-12-16 alex Channel_MaxUsers( CHANNEL *Chan )
364 2e88dd72 2002-12-16 alex assert( Chan != NULL );
365 2e88dd72 2002-12-16 alex return Chan->maxusers;
366 2e88dd72 2002-12-16 alex } /* Channel_MaxUsers */
369 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
370 8adff592 2005-03-19 fw Channel_First( void )
372 71c7a6fc 2002-01-28 alex return My_Channels;
373 71c7a6fc 2002-01-28 alex } /* Channel_First */
376 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
377 c2f60abe 2002-05-27 alex Channel_Next( CHANNEL *Chan )
379 71c7a6fc 2002-01-28 alex assert( Chan != NULL );
380 71c7a6fc 2002-01-28 alex return Chan->next;
381 71c7a6fc 2002-01-28 alex } /* Channel_Next */
384 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
385 8adff592 2005-03-19 fw Channel_Search( char *Name )
387 bb19cfda 2002-01-21 alex /* Channel-Struktur suchen */
389 bb19cfda 2002-01-21 alex CHANNEL *c;
390 0e38d10b 2002-03-25 alex UINT32 search_hash;
392 bb19cfda 2002-01-21 alex assert( Name != NULL );
394 0e38d10b 2002-03-25 alex search_hash = Hash( Name );
395 bb19cfda 2002-01-21 alex c = My_Channels;
398 0e38d10b 2002-03-25 alex if( search_hash == c->hash )
400 0e38d10b 2002-03-25 alex /* lt. Hash-Wert: Treffer! */
401 0e38d10b 2002-03-25 alex if( strcasecmp( Name, c->name ) == 0 ) return c;
403 bb19cfda 2002-01-21 alex c = c->next;
405 bb19cfda 2002-01-21 alex return NULL;
406 8a324b6d 2002-01-26 alex } /* Channel_Search */
409 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
410 c2f60abe 2002-05-27 alex Channel_FirstMember( CHANNEL *Chan )
412 8a324b6d 2002-01-26 alex assert( Chan != NULL );
413 8a324b6d 2002-01-26 alex return Get_First_Cl2Chan( NULL, Chan );
414 0bb9db1a 2002-01-29 alex } /* Channel_FirstMember */
417 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
418 c2f60abe 2002-05-27 alex Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
420 8a324b6d 2002-01-26 alex assert( Chan != NULL );
421 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
422 8a324b6d 2002-01-26 alex return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
423 8a324b6d 2002-01-26 alex } /* Channel_NextMember */
426 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
427 c2f60abe 2002-05-27 alex Channel_FirstChannelOf( CLIENT *Client )
429 0bb9db1a 2002-01-29 alex assert( Client != NULL );
430 0bb9db1a 2002-01-29 alex return Get_First_Cl2Chan( Client, NULL );
431 0bb9db1a 2002-01-29 alex } /* Channel_FirstChannelOf */
434 c2f60abe 2002-05-27 alex GLOBAL CL2CHAN *
435 c2f60abe 2002-05-27 alex Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
437 0bb9db1a 2002-01-29 alex assert( Client != NULL );
438 0bb9db1a 2002-01-29 alex assert( Cl2Chan != NULL );
439 0bb9db1a 2002-01-29 alex return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
440 0bb9db1a 2002-01-29 alex } /* Channel_NextChannelOf */
443 c2f60abe 2002-05-27 alex GLOBAL CLIENT *
444 c2f60abe 2002-05-27 alex Channel_GetClient( CL2CHAN *Cl2Chan )
446 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
447 8a324b6d 2002-01-26 alex return Cl2Chan->client;
448 8a324b6d 2002-01-26 alex } /* Channel_GetClient */
451 c2f60abe 2002-05-27 alex GLOBAL CHANNEL *
452 c2f60abe 2002-05-27 alex Channel_GetChannel( CL2CHAN *Cl2Chan )
454 8a324b6d 2002-01-26 alex assert( Cl2Chan != NULL );
455 8a324b6d 2002-01-26 alex return Cl2Chan->channel;
456 8a324b6d 2002-01-26 alex } /* Channel_GetChannel */
460 8adff592 2005-03-19 fw Channel_IsValidName( char *Name )
462 f7327524 2002-05-30 alex /* Pruefen, ob Name als Channelname gueltig */
464 8adff592 2005-03-19 fw char *ptr, badchars[10];
466 f53914b1 2002-02-06 alex assert( Name != NULL );
468 8adff592 2005-03-19 fw if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
470 7157d936 2002-03-02 alex ptr = Name;
471 d9a13b31 2002-12-25 alex strcpy( badchars, " ,:\007" );
472 7157d936 2002-03-02 alex while( *ptr )
474 8adff592 2005-03-19 fw if( strchr( badchars, *ptr )) return false;
479 f53914b1 2002-02-06 alex } /* Channel_IsValidName */
483 8adff592 2005-03-19 fw Channel_ModeAdd( CHANNEL *Chan, char Mode )
486 8adff592 2005-03-19 fw * If the channel already had this mode, return false.
487 8adff592 2005-03-19 fw * If the channel mode was newly set return true.
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 8adff592 2005-03-19 fw /* Channel does not have this mode yet, set it */
498 6626395c 2002-12-26 alex strlcat( Chan->modes, x, sizeof( Chan->modes ));
501 8adff592 2005-03-19 fw else return false;
502 d72c55a0 2002-02-11 alex } /* Channel_ModeAdd */
506 8adff592 2005-03-19 fw Channel_ModeDel( CHANNEL *Chan, char Mode )
508 8adff592 2005-03-19 fw /* Delete mode.
509 8adff592 2005-03-19 fw * if the mode was removed return true.
510 8adff592 2005-03-19 fw * if the channel did not have the mode, return false.
512 8adff592 2005-03-19 fw 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 8adff592 2005-03-19 fw if( ! p ) return false;
521 8adff592 2005-03-19 fw /* Channel has mode -> delete */
522 d72c55a0 2002-02-11 alex while( *p )
524 d72c55a0 2002-02-11 alex *p = *(p + 1);
528 d72c55a0 2002-02-11 alex } /* Channel_ModeDel */
532 8adff592 2005-03-19 fw Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
534 8adff592 2005-03-19 fw /* Set Channel-User-Mode.
535 8adff592 2005-03-19 fw * if mode was newly set, return true.
536 8adff592 2005-03-19 fw * if the User already had this channel-mode, return false.
539 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
542 d72c55a0 2002-02-11 alex assert( Chan != NULL );
543 d72c55a0 2002-02-11 alex assert( Client != NULL );
545 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
546 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
548 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
549 d72c55a0 2002-02-11 alex if( ! strchr( cl2chan->modes, x[0] ))
551 8adff592 2005-03-19 fw /* mode not set, -> set it */
552 6626395c 2002-12-26 alex strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
555 8adff592 2005-03-19 fw else return false;
556 d72c55a0 2002-02-11 alex } /* Channel_UserModeAdd */
560 8adff592 2005-03-19 fw Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
562 8adff592 2005-03-19 fw /* Delete Channel-User-Mode.
563 8adff592 2005-03-19 fw * If Mode was removed, return true.
564 8adff592 2005-03-19 fw * If User did not have the Channel-Mode, return false.
567 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
568 8adff592 2005-03-19 fw char x[2], *p;
570 d72c55a0 2002-02-11 alex assert( Chan != NULL );
571 d72c55a0 2002-02-11 alex assert( Client != NULL );
573 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
574 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
576 d72c55a0 2002-02-11 alex x[0] = Mode; x[1] = '\0';
578 d72c55a0 2002-02-11 alex p = strchr( cl2chan->modes, x[0] );
579 8adff592 2005-03-19 fw if( ! p ) return false;
581 8adff592 2005-03-19 fw /* Client has Mode -> delete */
582 d72c55a0 2002-02-11 alex while( *p )
584 d72c55a0 2002-02-11 alex *p = *(p + 1);
588 d72c55a0 2002-02-11 alex } /* Channel_UserModeDel */
591 8adff592 2005-03-19 fw GLOBAL char *
592 c2f60abe 2002-05-27 alex Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
594 8adff592 2005-03-19 fw /* return Users' Channel-Modes */
596 d72c55a0 2002-02-11 alex CL2CHAN *cl2chan;
598 d72c55a0 2002-02-11 alex assert( Chan != NULL );
599 d72c55a0 2002-02-11 alex assert( Client != NULL );
601 d72c55a0 2002-02-11 alex cl2chan = Get_Cl2Chan( Chan, Client );
602 d72c55a0 2002-02-11 alex assert( cl2chan != NULL );
604 d72c55a0 2002-02-11 alex return cl2chan->modes;
605 d72c55a0 2002-02-11 alex } /* Channel_UserModes */
609 c2f60abe 2002-05-27 alex Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
611 8adff592 2005-03-19 fw /* Test if Client is on Channel Chan */
613 153aa0aa 2002-02-27 alex assert( Chan != NULL );
614 153aa0aa 2002-02-27 alex assert( Client != NULL );
616 8adff592 2005-03-19 fw if( Get_Cl2Chan( Chan, Client )) return true;
617 8adff592 2005-03-19 fw else return false;
618 153aa0aa 2002-02-27 alex } /* Channel_IsMemberOf */
621 8adff592 2005-03-19 fw GLOBAL char *
622 c2f60abe 2002-05-27 alex Channel_Topic( CHANNEL *Chan )
624 52548bd3 2002-02-27 alex assert( Chan != NULL );
625 52548bd3 2002-02-27 alex return Chan->topic;
626 52548bd3 2002-02-27 alex } /* Channel_Topic */
630 8adff592 2005-03-19 fw Channel_SetTopic( CHANNEL *Chan, char *Topic )
632 52548bd3 2002-02-27 alex assert( Chan != NULL );
633 52548bd3 2002-02-27 alex assert( Topic != NULL );
635 0ced4181 2002-12-26 alex strlcpy( Chan->topic, Topic, sizeof( Chan->topic ));
636 52548bd3 2002-02-27 alex } /* Channel_SetTopic */
640 8adff592 2005-03-19 fw Channel_SetModes( CHANNEL *Chan, char *Modes )
642 378e511e 2002-09-03 alex assert( Chan != NULL );
643 378e511e 2002-09-03 alex assert( Modes != NULL );
645 0ced4181 2002-12-26 alex strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
646 378e511e 2002-09-03 alex } /* Channel_SetModes */
650 8adff592 2005-03-19 fw Channel_SetKey( CHANNEL *Chan, char *Key )
652 2e88dd72 2002-12-16 alex assert( Chan != NULL );
653 2e88dd72 2002-12-16 alex assert( Key != NULL );
655 0ced4181 2002-12-26 alex strlcpy( Chan->key, Key, sizeof( Chan->key ));
656 2e88dd72 2002-12-16 alex Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
657 2e88dd72 2002-12-16 alex } /* Channel_SetKey */
661 8adff592 2005-03-19 fw Channel_SetMaxUsers( CHANNEL *Chan, long Count )
663 2e88dd72 2002-12-16 alex assert( Chan != NULL );
665 2e88dd72 2002-12-16 alex Chan->maxusers = Count;
666 2e88dd72 2002-12-16 alex Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
667 2e88dd72 2002-12-16 alex } /* Channel_SetMaxUsers */
671 8adff592 2005-03-19 fw Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
673 8adff592 2005-03-19 fw bool is_member, has_voice, is_op, ok;
675 be79fabc 2003-11-06 alex /* Okay, target is a channel */
676 8adff592 2005-03-19 fw is_member = has_voice = is_op = false;
677 0e38d10b 2002-03-25 alex if( Channel_IsMemberOf( Chan, From ))
679 8adff592 2005-03-19 fw is_member = true;
680 8adff592 2005-03-19 fw if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
681 8adff592 2005-03-19 fw if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
684 8adff592 2005-03-19 fw /* Is the client allowed to write to channel? */
686 8adff592 2005-03-19 fw if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
687 8adff592 2005-03-19 fw if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
689 be79fabc 2003-11-06 alex /* Is the client banned? */
690 be79fabc 2003-11-06 alex if( Lists_CheckBanned( From, Chan ))
692 be79fabc 2003-11-06 alex /* Client is banned, bus is he channel operator or has voice? */
693 8adff592 2005-03-19 fw if(( ! has_voice ) && ( ! is_op )) ok = false;
696 0e38d10b 2002-03-25 alex if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
698 be79fabc 2003-11-06 alex /* Send text */
699 0e38d10b 2002-03-25 alex if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
700 8adff592 2005-03-19 fw return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
701 0e38d10b 2002-03-25 alex } /* Channel_Write */
704 378e511e 2002-09-03 alex GLOBAL CHANNEL *
705 8adff592 2005-03-19 fw Channel_Create( char *Name )
707 8adff592 2005-03-19 fw /* Create new CHANNEL structure and add it to linked list */
708 bb19cfda 2002-01-21 alex CHANNEL *c;
710 bb19cfda 2002-01-21 alex assert( Name != NULL );
712 cb76d96e 2004-03-11 alex c = (CHANNEL *)malloc( sizeof( CHANNEL ));
715 1fd7215d 2002-06-09 alex Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
716 bb19cfda 2002-01-21 alex return NULL;
718 8adff592 2005-03-19 fw memset( c, 0, sizeof( CHANNEL ));
719 0ced4181 2002-12-26 alex strlcpy( c->name, Name, sizeof( c->name ));
720 0e38d10b 2002-03-25 alex c->hash = Hash( c->name );
721 378e511e 2002-09-03 alex c->next = My_Channels;
722 378e511e 2002-09-03 alex My_Channels = c;
724 bb19cfda 2002-01-21 alex Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
727 378e511e 2002-09-03 alex } /* Channel_Create */
730 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
731 c2f60abe 2002-05-27 alex Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
733 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
735 bb19cfda 2002-01-21 alex assert( Chan != NULL );
736 bb19cfda 2002-01-21 alex assert( Client != NULL );
738 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
739 bb19cfda 2002-01-21 alex while( cl2chan )
741 bb19cfda 2002-01-21 alex if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
742 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
744 bb19cfda 2002-01-21 alex return NULL;
745 bb19cfda 2002-01-21 alex } /* Get_Cl2Chan */
748 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
749 c2f60abe 2002-05-27 alex Add_Client( CHANNEL *Chan, CLIENT *Client )
751 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
753 bb19cfda 2002-01-21 alex assert( Chan != NULL );
754 bb19cfda 2002-01-21 alex assert( Client != NULL );
756 d72c55a0 2002-02-11 alex /* neue CL2CHAN-Struktur anlegen */
757 cb76d96e 2004-03-11 alex cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
758 bb19cfda 2002-01-21 alex if( ! cl2chan )
760 1fd7215d 2002-06-09 alex Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
761 bb19cfda 2002-01-21 alex return NULL;
763 bb19cfda 2002-01-21 alex cl2chan->channel = Chan;
764 bb19cfda 2002-01-21 alex cl2chan->client = Client;
765 d72c55a0 2002-02-11 alex strcpy( cl2chan->modes, "" );
767 bb19cfda 2002-01-21 alex /* Verketten */
768 bb19cfda 2002-01-21 alex cl2chan->next = My_Cl2Chan;
769 bb19cfda 2002-01-21 alex My_Cl2Chan = cl2chan;
771 d569c924 2002-01-27 alex Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
773 bb19cfda 2002-01-21 alex return cl2chan;
774 bb19cfda 2002-01-21 alex } /* Add_Client */
778 8adff592 2005-03-19 fw Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
780 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan, *last_cl2chan;
781 bb19cfda 2002-01-21 alex CHANNEL *c;
783 bb19cfda 2002-01-21 alex assert( Chan != NULL );
784 bb19cfda 2002-01-21 alex assert( Client != NULL );
785 bb19cfda 2002-01-21 alex assert( Origin != NULL );
786 bb19cfda 2002-01-21 alex assert( Reason != NULL );
788 bb19cfda 2002-01-21 alex last_cl2chan = NULL;
789 bb19cfda 2002-01-21 alex cl2chan = My_Cl2Chan;
790 bb19cfda 2002-01-21 alex while( cl2chan )
792 bb19cfda 2002-01-21 alex if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
793 bb19cfda 2002-01-21 alex last_cl2chan = cl2chan;
794 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
796 8adff592 2005-03-19 fw if( ! cl2chan ) return false;
798 bb19cfda 2002-01-21 alex c = cl2chan->channel;
799 bb19cfda 2002-01-21 alex assert( c != NULL );
801 bb19cfda 2002-01-21 alex /* Aus Verkettung loesen und freigeben */
802 bb19cfda 2002-01-21 alex if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
803 bb19cfda 2002-01-21 alex else My_Cl2Chan = cl2chan->next;
804 bb19cfda 2002-01-21 alex free( cl2chan );
806 cc1e8514 2002-06-01 alex switch( Type )
808 cc1e8514 2002-06-01 alex case REMOVE_QUIT:
809 22365b2c 2002-06-01 alex /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
810 22365b2c 2002-06-01 alex * hier also "nur" noch alle User in betroffenen Channeln infomieren */
811 8adff592 2005-03-19 fw assert( InformServer == false );
812 cc1e8514 2002-06-01 alex Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
814 cc1e8514 2002-06-01 alex case REMOVE_KICK:
815 22365b2c 2002-06-01 alex /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
816 22365b2c 2002-06-01 alex * im entsprechenden Channel informieren */
817 cc1e8514 2002-06-01 alex if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
818 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, c, Origin, false, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
819 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 );
820 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 );
824 cc1e8514 2002-06-01 alex if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
825 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Origin, c, Client, false, "PART %s :%s", c->name, Reason );
826 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 );
827 cc1e8514 2002-06-01 alex Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
830 040f5422 2002-05-21 alex /* Wenn Channel nun leer und nicht pre-defined: loeschen */
831 040f5422 2002-05-21 alex if( ! strchr( Channel_Modes( Chan ), 'P' ))
833 040f5422 2002-05-21 alex if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
837 bb19cfda 2002-01-21 alex } /* Remove_Client */
840 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
841 c2f60abe 2002-05-27 alex Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
843 bb19cfda 2002-01-21 alex return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
844 bb19cfda 2002-01-21 alex } /* Get_First_Cl2Chan */
847 c2f60abe 2002-05-27 alex LOCAL CL2CHAN *
848 c2f60abe 2002-05-27 alex Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
850 bb19cfda 2002-01-21 alex CL2CHAN *cl2chan;
852 bb19cfda 2002-01-21 alex assert( Client != NULL || Channel != NULL );
854 bb19cfda 2002-01-21 alex cl2chan = Start;
855 bb19cfda 2002-01-21 alex while( cl2chan )
857 bb19cfda 2002-01-21 alex if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
858 bb19cfda 2002-01-21 alex if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
859 bb19cfda 2002-01-21 alex cl2chan = cl2chan->next;
861 bb19cfda 2002-01-21 alex return NULL;
862 bb19cfda 2002-01-21 alex } /* Get_Next_Cl2Chan */
866 c2f60abe 2002-05-27 alex Delete_Channel( CHANNEL *Chan )
868 bb19cfda 2002-01-21 alex /* Channel-Struktur loeschen */
870 bb19cfda 2002-01-21 alex CHANNEL *chan, *last_chan;
872 bb19cfda 2002-01-21 alex last_chan = NULL;
873 bb19cfda 2002-01-21 alex chan = My_Channels;
874 bb19cfda 2002-01-21 alex while( chan )
876 bb19cfda 2002-01-21 alex if( chan == Chan ) break;
877 bb19cfda 2002-01-21 alex last_chan = chan;
878 bb19cfda 2002-01-21 alex chan = chan->next;
880 8adff592 2005-03-19 fw if( ! chan ) return false;
882 bb19cfda 2002-01-21 alex Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
884 1fd7215d 2002-06-09 alex /* Invite- und Ban-Lists aufraeumen */
885 1fd7215d 2002-06-09 alex Lists_DeleteChannel( chan );
887 bb19cfda 2002-01-21 alex /* Neu verketten und freigeben */
888 bb19cfda 2002-01-21 alex if( last_chan ) last_chan->next = chan->next;
889 bb19cfda 2002-01-21 alex else My_Channels = chan->next;
890 bb19cfda 2002-01-21 alex free( chan );
893 bb19cfda 2002-01-21 alex } /* Delete_Channel */
896 38b9cb88 2001-12-14 alex /* -eof- */