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.124 2004/02/28 02:18:16 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 BOOLEAN
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 GLOBAL BOOLEAN
58 IRC_KILL( CLIENT *Client, REQUEST *Req )
59 {
60 CLIENT *prefix, *c;
61 CHAR reason[COMMAND_LEN];
62 CONN_ID my_conn, conn;
64 assert( Client != NULL );
65 assert( Req != NULL );
67 /* Is the user an IRC operator? */
68 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
70 /* Bad number of parameters? */
71 if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
73 if( Req->prefix ) prefix = Client_Search( Req->prefix );
74 else prefix = Client;
75 if( ! prefix )
76 {
77 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
78 prefix = Client_ThisServer( );
79 }
81 if( Client != Client_ThisServer( )) Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s", Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
83 /* Build reason string */
84 if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
85 else strlcpy( reason, Req->argv[1], sizeof( reason ));
87 /* Inform other servers */
88 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
90 /* Save ID of this connection */
91 my_conn = Client_Conn( Client );
93 /* Do we host such a client? */
94 c = Client_Search( Req->argv[0] );
95 if( c )
96 {
97 /* Yes, there is such a client -- but is it a valid user? */
98 if( Client_Type( c ) == CLIENT_SERVER )
99 {
100 if( Client != Client_ThisServer( )) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
101 else
103 /* Oops, I should kill another server!? */
104 Log( LOG_ERR, "Can't KILL server \"%s\"!", Req->argv[0] );
105 conn = Client_Conn( Client_NextHop( c ));
106 assert( conn > NONE );
107 Conn_Close( conn, NULL, "Nick collision for server!?", TRUE );
110 else if( Client_Type( c ) != CLIENT_USER )
112 if( Client != Client_ThisServer( )) IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
113 else
115 /* Oops, what sould I close?? */
116 Log( LOG_ERR, "Can't KILL \"%s\": invalid client type!", Req->argv[0] );
117 conn = Client_Conn( Client_NextHop( c ));
118 assert( conn > NONE );
119 Conn_Close( conn, NULL, "Collision for invalid client type!?", TRUE );
122 else
124 /* Kill user NOW! */
125 conn = Client_Conn( c );
126 Client_Destroy( c, NULL, reason, FALSE );
127 if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
130 else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
132 /* Are we still connected or were we killed, too? */
133 if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
134 else return DISCONNECTED;
135 } /* IRC_KILL */
138 GLOBAL BOOLEAN
139 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
141 CLIENT *to, *from;
143 assert( Client != NULL );
144 assert( Req != NULL );
146 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
148 /* Falsche Anzahl Parameter? */
149 if( Req->argc != 2 ) return CONNECTED;
151 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
152 else from = Client;
153 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
155 to = Client_Search( Req->argv[0] );
156 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
158 /* Okay, Ziel ist ein User */
159 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
161 else return CONNECTED;
162 } /* IRC_NOTICE */
165 GLOBAL BOOLEAN
166 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
168 CLIENT *cl, *from;
169 CHANNEL *chan;
171 assert( Client != NULL );
172 assert( Req != NULL );
174 /* Falsche Anzahl Parameter? */
175 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
176 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
177 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
179 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
180 else from = Client;
181 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
183 cl = Client_Search( Req->argv[0] );
184 if( cl )
186 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
187 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
189 /* Okay, Ziel ist ein User */
190 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
192 /* Ziel-User ist AWAY: Meldung verschicken */
193 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
196 /* Text senden */
197 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
198 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
201 chan = Channel_Search( Req->argv[0] );
202 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
204 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
205 } /* IRC_PRIVMSG */
208 GLOBAL BOOLEAN
209 IRC_TRACE( CLIENT *Client, REQUEST *Req )
211 CLIENT *from, *target, *c;
212 CONN_ID idx, idx2;
213 CHAR user[CLIENT_USER_LEN];
215 assert( Client != NULL );
216 assert( Req != NULL );
218 /* Bad number of arguments? */
219 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
221 /* Search sender */
222 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
223 else from = Client;
224 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
226 /* Search target */
227 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
228 else target = Client_ThisServer( );
230 /* Forward command to other server? */
231 if( target != Client_ThisServer( ))
233 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
235 /* Send RPL_TRACELINK back to initiator */
236 idx = Client_Conn( Client ); assert( idx > NONE );
237 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
238 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;
240 /* Forward command */
241 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
242 return CONNECTED;
245 /* Infos about all connected servers */
246 c = Client_First( );
247 while( c )
249 if( Client_Conn( c ) > NONE )
251 /* Local client */
252 if( Client_Type( c ) == CLIENT_SERVER )
254 /* Server link */
255 strlcpy( user, Client_User( c ), sizeof( user ));
256 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
257 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;
259 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
261 /* IRC Operator */
262 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
265 c = Client_Next( c );
268 IRC_SetPenalty( Client, 3 );
269 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
270 } /* IRC_TRACE */
273 GLOBAL BOOLEAN
274 IRC_HELP( CLIENT *Client, REQUEST *Req )
276 COMMAND *cmd;
278 assert( Client != NULL );
279 assert( Req != NULL );
281 /* Bad number of arguments? */
282 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
284 cmd = Parse_GetCommandStruct( );
285 while( cmd->name )
287 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
288 cmd++;
291 IRC_SetPenalty( Client, 2 );
292 return CONNECTED;
293 } /* IRC_HELP */
296 LOCAL CHAR *
297 Option_String( CONN_ID Idx )
299 STATIC CHAR option_txt[8];
300 INT options;
302 options = Conn_Options( Idx );
304 strcpy( option_txt, "F" ); /* No idea what this means but the original ircd sends it ... */
305 #ifdef ZLIB
306 if( options & CONN_ZIP ) strcat( option_txt, "z" );
307 #endif
309 return option_txt;
310 } /* Option_String */
313 /* -eof- */