2 c23199d9 2002-02-27 alex * ngIRCd -- The Next Generation IRC Daemon
3 c23199d9 2002-02-27 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 c23199d9 2002-02-27 alex * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 c23199d9 2002-02-27 alex * der GNU General Public License (GPL), wie von der Free Software Foundation
7 c23199d9 2002-02-27 alex * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 c23199d9 2002-02-27 alex * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 c23199d9 2002-02-27 alex * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 c23199d9 2002-02-27 alex * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 d4fca86a 2002-03-25 alex * $Id: irc-mode.c,v 1.4 2002/03/25 17:11:45 alex Exp $
14 c23199d9 2002-02-27 alex * irc-mode.c: IRC-Befehle zur Mode-Aenderung (MODE, AWAY, ...)
18 ca33cbda 2002-03-12 alex #include "portab.h"
20 ca33cbda 2002-03-12 alex #include "imp.h"
21 c23199d9 2002-02-27 alex #include <assert.h>
22 c23199d9 2002-02-27 alex #include <string.h>
24 c23199d9 2002-02-27 alex #include "channel.h"
25 ca33cbda 2002-03-12 alex #include "defines.h"
26 c23199d9 2002-02-27 alex #include "irc-write.h"
27 c23199d9 2002-02-27 alex #include "log.h"
28 c23199d9 2002-02-27 alex #include "messages.h"
30 ca33cbda 2002-03-12 alex #include "exp.h"
31 c23199d9 2002-02-27 alex #include "irc-mode.h"
34 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_MODE( CLIENT *Client, REQUEST *Req )
36 c23199d9 2002-02-27 alex CHAR *mode_ptr, the_modes[CLIENT_MODE_LEN], x[2];
37 c23199d9 2002-02-27 alex CLIENT *cl, *chan_cl, *prefix;
38 c23199d9 2002-02-27 alex BOOLEAN set, ok;
39 c23199d9 2002-02-27 alex CHANNEL *chan;
41 c23199d9 2002-02-27 alex assert( Client != NULL );
42 c23199d9 2002-02-27 alex assert( Req != NULL );
44 c23199d9 2002-02-27 alex cl = chan_cl = prefix = NULL;
45 c23199d9 2002-02-27 alex chan = NULL;
47 c23199d9 2002-02-27 alex /* Valider Client? */
48 c23199d9 2002-02-27 alex if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
50 c23199d9 2002-02-27 alex /* Keine Parameter? */
51 c23199d9 2002-02-27 alex if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
53 c23199d9 2002-02-27 alex /* Ziel suchen: Client bzw. Channel */
54 c23199d9 2002-02-27 alex if( Client_IsValidNick( Req->argv[0] )) cl = Client_Search( Req->argv[0] );
55 c23199d9 2002-02-27 alex if( Channel_IsValidName( Req->argv[0] )) chan = Channel_Search( Req->argv[0] );
57 c23199d9 2002-02-27 alex /* Kein Ziel gefunden? */
58 c23199d9 2002-02-27 alex if(( ! cl ) && ( ! chan )) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
60 c23199d9 2002-02-27 alex assert(( cl && chan ) != TRUE );
62 c23199d9 2002-02-27 alex /* Falsche Anzahl Parameter? */
63 c23199d9 2002-02-27 alex if(( cl ) && ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
64 c23199d9 2002-02-27 alex if(( chan ) && ( Req->argc > 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
66 c23199d9 2002-02-27 alex /* Client ermitteln, wenn bei Channel-Modes mit 3 Parametern */
67 c23199d9 2002-02-27 alex if(( chan ) && (Req->argc == 3 ))
69 c23199d9 2002-02-27 alex chan_cl = Client_Search( Req->argv[2] );
70 c23199d9 2002-02-27 alex if( ! chan_cl ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
73 c23199d9 2002-02-27 alex /* Wenn Anfragender ein User ist: Zugriff erlaubt? */
74 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_USER )
78 c23199d9 2002-02-27 alex /* MODE ist nur fuer sich selber zulaessig! */
79 c23199d9 2002-02-27 alex if( cl != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
83 c23199d9 2002-02-27 alex /* Darf der User die Channel-Modes ermitteln? */
87 c23199d9 2002-02-27 alex /* Werden die Modes "nur" erfragt? */
88 c23199d9 2002-02-27 alex if(( cl ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_UMODEIS_MSG, Client_ID( Client ), Client_Modes( cl ));
89 c23199d9 2002-02-27 alex if(( chan ) && ( Req->argc == 1 )) return IRC_WriteStrClient( Client, RPL_CHANNELMODEIS_MSG, Client_ID( Client ), Channel_Name( chan ), Channel_Modes( chan ));
91 c23199d9 2002-02-27 alex mode_ptr = Req->argv[1];
93 c23199d9 2002-02-27 alex /* Sollen Modes gesetzt oder geloescht werden? */
96 c23199d9 2002-02-27 alex if( *mode_ptr == '+' ) set = TRUE;
97 c23199d9 2002-02-27 alex else if( *mode_ptr == '-' ) set = FALSE;
98 c23199d9 2002-02-27 alex else return IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
103 c23199d9 2002-02-27 alex if( *mode_ptr == '-' ) set = FALSE;
104 c23199d9 2002-02-27 alex else set = TRUE;
105 c23199d9 2002-02-27 alex if(( *mode_ptr == '-' ) || ( *mode_ptr == '+' )) mode_ptr++;
108 c23199d9 2002-02-27 alex /* Prefix fuer Antworten etc. ermitteln */
109 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_SERVER )
111 d4fca86a 2002-03-25 alex prefix = Client_Search( Req->prefix );
112 c23199d9 2002-02-27 alex if( ! prefix ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
114 c23199d9 2002-02-27 alex else prefix = Client;
116 c23199d9 2002-02-27 alex /* Reply-String mit Aenderungen vorbereiten */
117 c23199d9 2002-02-27 alex if( set ) strcpy( the_modes, "+" );
118 c23199d9 2002-02-27 alex else strcpy( the_modes, "-" );
121 c23199d9 2002-02-27 alex x[1] = '\0';
122 c23199d9 2002-02-27 alex while( *mode_ptr )
124 c23199d9 2002-02-27 alex x[0] = '\0';
125 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_SERVER )
127 c23199d9 2002-02-27 alex /* Befehl kommt von einem Server, daher
128 c23199d9 2002-02-27 alex * trauen wir ihm "unbesehen" ... */
129 c23199d9 2002-02-27 alex x[0] = *mode_ptr;
133 c23199d9 2002-02-27 alex /* Modes validieren */
136 c23199d9 2002-02-27 alex /* User-Modes */
137 c23199d9 2002-02-27 alex switch( *mode_ptr )
140 c23199d9 2002-02-27 alex /* invisible */
141 c23199d9 2002-02-27 alex x[0] = 'i';
144 c23199d9 2002-02-27 alex /* restricted (kann nur gesetzt werden) */
145 c23199d9 2002-02-27 alex if( set ) x[0] = 'r';
146 c23199d9 2002-02-27 alex else ok = IRC_WriteStrClient( Client, ERR_RESTRICTED_MSG, Client_ID( Client ));
149 c23199d9 2002-02-27 alex /* operator (kann nur geloescht werden) */
150 c23199d9 2002-02-27 alex if( ! set )
152 c23199d9 2002-02-27 alex Client_SetOperByMe( Client, FALSE );
153 c23199d9 2002-02-27 alex x[0] = 'o';
155 c23199d9 2002-02-27 alex else ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Client ));
158 d4fca86a 2002-03-25 alex /* server messages */
159 d4fca86a 2002-03-25 alex x[0] = 's';
162 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ));
163 c23199d9 2002-02-27 alex ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
164 c23199d9 2002-02-27 alex x[0] = '\0';
169 c23199d9 2002-02-27 alex /* Ist der User ein Channel Operator? */
170 c23199d9 2002-02-27 alex if( ! strchr( Channel_UserModes( chan, Client ), 'o' ))
172 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "Can't change modes: \"%s\" is not operator on %s!", Client_ID( Client ), Channel_Name( chan ));
173 c23199d9 2002-02-27 alex ok = IRC_WriteStrClient( Client, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Client ), Channel_Name( chan ));
177 c23199d9 2002-02-27 alex /* Channel-Modes oder Channel-User-Modes */
178 c23199d9 2002-02-27 alex if( chan_cl )
180 c23199d9 2002-02-27 alex /* Channel-User-Modes */
181 c23199d9 2002-02-27 alex switch( *mode_ptr )
184 c23199d9 2002-02-27 alex /* Channel Operator */
185 c23199d9 2002-02-27 alex x[0] = 'o';
188 c23199d9 2002-02-27 alex /* Voice */
189 c23199d9 2002-02-27 alex x[0] = 'v';
192 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "Unknown channel-user-mode \"%c%c\" from \"%s\" on \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Client_ID( chan_cl ), Channel_Name( chan ));
193 c23199d9 2002-02-27 alex ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
194 c23199d9 2002-02-27 alex x[0] = '\0';
199 c23199d9 2002-02-27 alex /* Channel-Modes */
200 c23199d9 2002-02-27 alex switch( *mode_ptr )
203 c23199d9 2002-02-27 alex /* Anonymous */
204 c23199d9 2002-02-27 alex x[0] = 'a';
207 c23199d9 2002-02-27 alex /* Moderated */
208 c23199d9 2002-02-27 alex x[0] = 'm';
211 c23199d9 2002-02-27 alex /* kein Schreiben in den Channel von aussen */
212 c23199d9 2002-02-27 alex x[0] = 'n';
215 c23199d9 2002-02-27 alex /* Private */
216 c23199d9 2002-02-27 alex x[0] = 'p';
219 c23199d9 2002-02-27 alex /* Quiet */
220 c23199d9 2002-02-27 alex x[0] = 'q';
223 c23199d9 2002-02-27 alex /* Secret */
224 c23199d9 2002-02-27 alex x[0] = 's';
227 c23199d9 2002-02-27 alex /* Topic Lock */
228 c23199d9 2002-02-27 alex x[0] = 't';
231 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "Unknown channel-mode \"%c%c\" from \"%s\" at %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Client ), Channel_Name( chan ));
232 c23199d9 2002-02-27 alex ok = IRC_WriteStrClient( Client, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Client ), set ? '+' : '-', *mode_ptr );
233 c23199d9 2002-02-27 alex x[0] = '\0';
238 c23199d9 2002-02-27 alex if( ! ok ) break;
240 c23199d9 2002-02-27 alex mode_ptr++;
241 c23199d9 2002-02-27 alex if( ! x[0] ) continue;
243 c23199d9 2002-02-27 alex /* Okay, gueltigen Mode gefunden */
246 c23199d9 2002-02-27 alex /* Es geht um User-Modes */
249 c23199d9 2002-02-27 alex /* Mode setzen. Wenn der Client ihn noch nicht hatte: merken */
250 c23199d9 2002-02-27 alex if( Client_ModeAdd( cl, x[0] )) strcat( the_modes, x );
255 c23199d9 2002-02-27 alex /* Modes geloescht. Wenn der Client ihn hatte: merken */
256 c23199d9 2002-02-27 alex if( Client_ModeDel( cl, x[0] )) strcat( the_modes, x );
259 802a17b1 2002-03-04 alex /* "nachbearbeiten" */
260 802a17b1 2002-03-04 alex if( x[0] == 'a' )
263 802a17b1 2002-03-04 alex if( set ) Client_SetAway( cl, DEFAULT_AWAY_MSG );
264 802a17b1 2002-03-04 alex else Client_SetAway( cl, NULL );
269 c23199d9 2002-02-27 alex /* Es geht um Channel-Modes oder Channel-User-Modes */
270 c23199d9 2002-02-27 alex if( chan_cl )
272 c23199d9 2002-02-27 alex /* Channel-User-Modes */
275 c23199d9 2002-02-27 alex /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
276 c23199d9 2002-02-27 alex if( Channel_UserModeAdd( chan, chan_cl, x[0] )) strcat( the_modes, x );
280 c23199d9 2002-02-27 alex /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
281 c23199d9 2002-02-27 alex if( Channel_UserModeDel( chan, chan_cl, x[0] )) strcat( the_modes, x );
286 c23199d9 2002-02-27 alex /* Channel-Mode */
289 c23199d9 2002-02-27 alex /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
290 c23199d9 2002-02-27 alex if( Channel_ModeAdd( chan, x[0] )) strcat( the_modes, x );
294 c23199d9 2002-02-27 alex /* Mode setzen. Wenn der Channel ihn noch nicht hatte: merken */
295 c23199d9 2002-02-27 alex if( Channel_ModeDel( chan, x[0] )) strcat( the_modes, x );
301 c23199d9 2002-02-27 alex /* Wurden Modes geaendert? */
302 c23199d9 2002-02-27 alex if( the_modes[1] )
306 c23199d9 2002-02-27 alex /* Client-Mode */
307 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_SERVER )
309 c23199d9 2002-02-27 alex /* Modes an andere Server forwarden */
310 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
314 c23199d9 2002-02-27 alex /* Bestaetigung an Client schicken & andere Server informieren */
315 c23199d9 2002-02-27 alex ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Client_ID( cl ), the_modes );
316 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Client_ID( cl ), the_modes );
318 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( cl ), Client_Modes( cl ));
322 c23199d9 2002-02-27 alex /* Channel-Modes oder Channel-User-Mode */
323 c23199d9 2002-02-27 alex if( chan_cl )
325 c23199d9 2002-02-27 alex /* Channel-User-Mode */
326 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_SERVER )
328 c23199d9 2002-02-27 alex /* Modes an andere Server und Channel-User forwarden */
329 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
330 c23199d9 2002-02-27 alex IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
334 c23199d9 2002-02-27 alex /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
335 c23199d9 2002-02-27 alex ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
336 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s %s :%s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
337 c23199d9 2002-02-27 alex IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s %s", Channel_Name( chan ), the_modes, Client_ID( chan_cl));
339 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "User \"%s\" on %s: Mode change, now \"%s\".", Client_Mask( chan_cl), Channel_Name( chan ), Channel_UserModes( chan, chan_cl ));
343 c23199d9 2002-02-27 alex /* Channel-Mode */
344 c23199d9 2002-02-27 alex if( Client_Type( Client ) == CLIENT_SERVER )
346 c23199d9 2002-02-27 alex /* Modes an andere Server und Channel-User forwarden */
347 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
348 c23199d9 2002-02-27 alex IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
352 c23199d9 2002-02-27 alex /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
353 c23199d9 2002-02-27 alex ok = IRC_WriteStrClientPrefix( Client, prefix, "MODE %s %s", Channel_Name( chan ), the_modes );
354 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, prefix, "MODE %s :%s", Channel_Name( chan ), the_modes );
355 c23199d9 2002-02-27 alex IRC_WriteStrChannelPrefix( Client, chan, prefix, FALSE, "MODE %s %s", Channel_Name( chan ), the_modes );
357 c23199d9 2002-02-27 alex Log( LOG_DEBUG, "Channel \"%s\": Mode change, now \"%s\".", Channel_Name( chan ), Channel_Modes( chan ));
363 c23199d9 2002-02-27 alex } /* IRC_MODE */
366 c23199d9 2002-02-27 alex GLOBAL BOOLEAN IRC_AWAY( CLIENT *Client, REQUEST *Req )
368 c23199d9 2002-02-27 alex assert( Client != NULL );
369 c23199d9 2002-02-27 alex assert( Req != NULL );
371 c23199d9 2002-02-27 alex if( Client_Type( Client ) != CLIENT_USER ) return IRC_WriteStrClient( Client, ERR_NOTREGISTERED_MSG, Client_ID( Client ));
373 c23199d9 2002-02-27 alex /* Falsche Anzahl Parameter? */
374 c23199d9 2002-02-27 alex if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
376 c23199d9 2002-02-27 alex if(( Req->argc == 1 ) && (Req->argv[0][0] ))
378 c23199d9 2002-02-27 alex /* AWAY setzen */
379 c23199d9 2002-02-27 alex Client_SetAway( Client, Req->argv[0] );
380 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
381 c23199d9 2002-02-27 alex return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
385 c23199d9 2002-02-27 alex /* AWAY loeschen */
386 c23199d9 2002-02-27 alex Client_SetAway( Client, NULL );
387 c23199d9 2002-02-27 alex IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
388 c23199d9 2002-02-27 alex return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
390 c23199d9 2002-02-27 alex } /* IRC_AWAY */
393 c23199d9 2002-02-27 alex /* -eof- */