Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2004 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 * IRC commands
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: irc.c,v 1.126 2005/04/18 15:44:39 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "ngircd.h"
25 #include "conn.h"
26 #include "resolve.h"
27 #include "conf.h"
28 #include "conn-func.h"
29 #include "client.h"
30 #include "channel.h"
31 #include "defines.h"
32 #include "irc-write.h"
33 #include "log.h"
34 #include "messages.h"
35 #include "parse.h"
37 #include "exp.h"
38 #include "irc.h"
41 LOCAL char *Option_String PARAMS(( CONN_ID Idx ));
44 GLOBAL bool
45 IRC_ERROR( CLIENT *Client, REQUEST *Req )
46 {
47 assert( Client != NULL );
48 assert( Req != NULL );
50 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
51 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
53 return CONNECTED;
54 } /* IRC_ERROR */
57 /**
58 * Kill client on request.
59 * This function implements the IRC command "KILL" wich is used to selectively
60 * disconnect clients. It can be used by IRC operators and servers, for example
61 * to "solve" nick collisions after netsplits.
62 * Please note that this function is also called internally, without a real
63 * KILL command beeing received over the network! Client is Client_ThisServer()
64 * in this case. */
65 GLOBAL bool
66 IRC_KILL( CLIENT *Client, REQUEST *Req )
67 {
68 CLIENT *prefix, *c;
69 char reason[COMMAND_LEN], *msg;
70 CONN_ID my_conn, conn;
72 assert( Client != NULL );
73 assert( Req != NULL );
75 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
76 ( ! Client_OperByMe( Client )))
77 {
78 /* The originator of the KILL is neither an IRC operator of
79 * this server nor a server. */
80 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
81 Client_ID( Client ));
82 }
84 if( Req->argc != 2 )
85 {
86 /* This command requires exactly 2 parameters! */
87 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
88 Client_ID( Client ), Req->command );
89 }
91 if( Req->prefix ) prefix = Client_Search( Req->prefix );
92 else prefix = Client;
93 if( ! prefix )
94 {
95 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
96 Req->prefix );
97 prefix = Client_ThisServer( );
98 }
100 if( Client != Client_ThisServer( ))
102 /* This is a "real" KILL received from the network. */
103 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
104 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
107 /* Build reason string */
108 if( Client_Type( Client ) == CLIENT_USER )
110 /* Prefix the "reason" if the originator is a regular user,
111 * so users can't spoof KILLs of servers. */
112 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
113 Client_ID( Client ), Req->argv[1] );
115 else
116 strlcpy( reason, Req->argv[1], sizeof( reason ));
118 /* Inform other servers */
119 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
120 Req->argv[0], reason );
122 /* Save ID of this connection */
123 my_conn = Client_Conn( Client );
125 /* Do we host such a client? */
126 c = Client_Search( Req->argv[0] );
127 if( c )
129 if(( Client_Type( c ) != CLIENT_USER ) &&
130 ( Client_Type( c ) != CLIENT_GOTNICK ))
132 /* Target of this KILL is not a regular user, this is
133 * invalid! So we ignore this case if we received a
134 * regular KILL from the network and try to kill the
135 * client/connection anyway (but log an error!) if the
136 * origin is the local server. */
138 if( Client != Client_ThisServer( ))
140 /* Invalid KILL received from remote */
141 if( Client_Type( c ) == CLIENT_SERVER )
142 msg = ERR_CANTKILLSERVER_MSG;
143 else
144 msg = ERR_NOPRIVILEGES_MSG;
145 return IRC_WriteStrClient( Client, msg,
146 Client_ID( Client ));
149 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
150 Client_Type( c ), Req->argv[0] );
153 /* Kill client NOW! */
154 conn = Client_Conn( Client_NextHop( c ));
155 Client_Destroy( c, NULL, reason, false );
156 if( conn > NONE )
157 Conn_Close( conn, NULL, reason, true );
159 else
160 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
162 /* Are we still connected or were we killed, too? */
163 if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn )))
164 return CONNECTED;
165 else
166 return DISCONNECTED;
167 } /* IRC_KILL */
170 GLOBAL bool
171 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
173 CLIENT *to, *from;
175 assert( Client != NULL );
176 assert( Req != NULL );
178 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
180 /* Falsche Anzahl Parameter? */
181 if( Req->argc != 2 ) return CONNECTED;
183 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
184 else from = Client;
185 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
187 to = Client_Search( Req->argv[0] );
188 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
190 /* Okay, Ziel ist ein User */
191 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
193 else return CONNECTED;
194 } /* IRC_NOTICE */
197 GLOBAL bool
198 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
200 CLIENT *cl, *from;
201 CHANNEL *chan;
203 assert( Client != NULL );
204 assert( Req != NULL );
206 /* Falsche Anzahl Parameter? */
207 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
208 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
209 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
211 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
212 else from = Client;
213 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
215 cl = Client_Search( Req->argv[0] );
216 if( cl )
218 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
219 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
221 /* Okay, Ziel ist ein User */
222 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
224 /* Ziel-User ist AWAY: Meldung verschicken */
225 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
228 /* Text senden */
229 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
230 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
233 chan = Channel_Search( Req->argv[0] );
234 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
236 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
237 } /* IRC_PRIVMSG */
240 GLOBAL bool
241 IRC_TRACE( CLIENT *Client, REQUEST *Req )
243 CLIENT *from, *target, *c;
244 CONN_ID idx, idx2;
245 char user[CLIENT_USER_LEN];
247 assert( Client != NULL );
248 assert( Req != NULL );
250 /* Bad number of arguments? */
251 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
253 /* Search sender */
254 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
255 else from = Client;
256 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
258 /* Search target */
259 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
260 else target = Client_ThisServer( );
262 /* Forward command to other server? */
263 if( target != Client_ThisServer( ))
265 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
267 /* Send RPL_TRACELINK back to initiator */
268 idx = Client_Conn( Client ); assert( idx > NONE );
269 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
270 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
272 /* Forward command */
273 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
274 return CONNECTED;
277 /* Infos about all connected servers */
278 c = Client_First( );
279 while( c )
281 if( Client_Conn( c ) > NONE )
283 /* Local client */
284 if( Client_Type( c ) == CLIENT_SERVER )
286 /* Server link */
287 strlcpy( user, Client_User( c ), sizeof( user ));
288 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
289 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
291 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
293 /* IRC Operator */
294 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
297 c = Client_Next( c );
300 IRC_SetPenalty( Client, 3 );
301 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
302 } /* IRC_TRACE */
305 GLOBAL bool
306 IRC_HELP( CLIENT *Client, REQUEST *Req )
308 COMMAND *cmd;
310 assert( Client != NULL );
311 assert( Req != NULL );
313 /* Bad number of arguments? */
314 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
316 cmd = Parse_GetCommandStruct( );
317 while( cmd->name )
319 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
320 cmd++;
323 IRC_SetPenalty( Client, 2 );
324 return CONNECTED;
325 } /* IRC_HELP */
328 LOCAL char *
329 Option_String( CONN_ID Idx )
331 static char option_txt[8];
332 int options;
334 options = Conn_Options( Idx );
336 strcpy( option_txt, "F" ); /* No idea what this means but the original ircd sends it ... */
337 #ifdef ZLIB
338 if( options & CONN_ZIP ) strcat( option_txt, "z" );
339 #endif
341 return option_txt;
342 } /* Option_String */
345 /* -eof- */