2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
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.
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
26 #include "conn-func.h"
31 #include "irc-write.h"
41 static char *Option_String PARAMS((CONN_ID Idx));
42 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType, bool SendErrors));
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *targetMask, char *message, bool SendErrors));
47 IRC_ERROR( CLIENT *Client, REQUEST *Req )
49 assert( Client != NULL );
50 assert( Req != NULL );
52 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
53 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
60 * Kill client on request.
61 * This function implements the IRC command "KILL" wich is used to selectively
62 * disconnect clients. It can be used by IRC operators and servers, for example
63 * to "solve" nick collisions after netsplits.
64 * Please note that this function is also called internally, without a real
65 * KILL command beeing received over the network! Client is Client_ThisServer()
68 IRC_KILL( CLIENT *Client, REQUEST *Req )
71 char reason[COMMAND_LEN], *msg;
72 CONN_ID my_conn, conn;
74 assert( Client != NULL );
75 assert( Req != NULL );
77 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
78 ( ! Client_OperByMe( Client )))
80 /* The originator of the KILL is neither an IRC operator of
81 * this server nor a server. */
82 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
88 /* This command requires exactly 2 parameters! */
89 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
90 Client_ID( Client ), Req->command );
93 if( Req->prefix ) prefix = Client_Search( Req->prefix );
97 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
99 prefix = Client_ThisServer( );
102 if( Client != Client_ThisServer( ))
104 /* This is a "real" KILL received from the network. */
105 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
106 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
109 /* Build reason string */
110 if( Client_Type( Client ) == CLIENT_USER )
112 /* Prefix the "reason" if the originator is a regular user,
113 * so users can't spoof KILLs of servers. */
114 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
115 Client_ID( Client ), Req->argv[1] );
118 strlcpy( reason, Req->argv[1], sizeof( reason ));
120 /* Inform other servers */
121 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
122 Req->argv[0], reason );
124 /* Save ID of this connection */
125 my_conn = Client_Conn( Client );
127 /* Do we host such a client? */
128 c = Client_Search( Req->argv[0] );
131 if(( Client_Type( c ) != CLIENT_USER ) &&
132 ( Client_Type( c ) != CLIENT_GOTNICK ))
134 /* Target of this KILL is not a regular user, this is
135 * invalid! So we ignore this case if we received a
136 * regular KILL from the network and try to kill the
137 * client/connection anyway (but log an error!) if the
138 * origin is the local server. */
140 if( Client != Client_ThisServer( ))
142 /* Invalid KILL received from remote */
143 if( Client_Type( c ) == CLIENT_SERVER )
144 msg = ERR_CANTKILLSERVER_MSG;
146 msg = ERR_NOPRIVILEGES_MSG;
147 return IRC_WriteStrClient( Client, msg,
148 Client_ID( Client ));
151 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
152 Client_Type( c ), Req->argv[0] );
155 /* Kill client NOW! */
156 conn = Client_Conn( c );
157 Client_Destroy( c, NULL, reason, false );
159 Conn_Close( conn, NULL, reason, true );
162 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
164 /* Are we still connected or were we killed, too? */
165 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
173 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
175 return Send_Message(Client, Req, CLIENT_USER, false);
180 * Handler for the IRC command PRIVMSG.
183 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
185 return Send_Message(Client, Req, CLIENT_USER, true);
190 * Handler for the IRC command SQUERY.
193 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
195 return Send_Message(Client, Req, CLIENT_SERVICE, true);
200 IRC_TRACE( CLIENT *Client, REQUEST *Req )
202 CLIENT *from, *target, *c;
204 char user[CLIENT_USER_LEN];
206 assert( Client != NULL );
207 assert( Req != NULL );
209 /* Bad number of arguments? */
210 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
213 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
215 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
218 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
219 else target = Client_ThisServer( );
221 /* Forward command to other server? */
222 if( target != Client_ThisServer( ))
224 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
226 /* Send RPL_TRACELINK back to initiator */
227 idx = Client_Conn( Client ); assert( idx > NONE );
228 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
229 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;
231 /* Forward command */
232 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
236 /* Infos about all connected servers */
240 if( Client_Conn( c ) > NONE )
243 if( Client_Type( c ) == CLIENT_SERVER )
246 strlcpy( user, Client_User( c ), sizeof( user ));
247 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
248 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;
250 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
253 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
256 c = Client_Next( c );
259 IRC_SetPenalty( Client, 3 );
260 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
265 IRC_HELP( CLIENT *Client, REQUEST *Req )
269 assert( Client != NULL );
270 assert( Req != NULL );
272 /* Bad number of arguments? */
273 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
275 cmd = Parse_GetCommandStruct( );
278 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
282 IRC_SetPenalty( Client, 2 );
288 Option_String( CONN_ID Idx )
290 static char option_txt[8];
293 options = Conn_Options(Idx);
295 strcpy(option_txt, "F"); /* No idea what this means, but the
296 * original ircd sends it ... */
298 if(options & CONN_ZIP) /* zlib compression supported. */
299 strcat(option_txt, "z");
303 } /* Option_String */
307 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
311 char *currentTarget = Req->argv[0];
312 char *lastCurrentTarget;
314 assert(Client != NULL);
317 if (Req->argc == 0) {
320 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
321 Client_ID(Client), Req->command);
323 if (Req->argc == 1) {
326 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
332 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
333 Client_ID(Client), Req->command);
336 if (Client_Type(Client) == CLIENT_SERVER)
337 from = Client_Search(Req->prefix);
341 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
342 Client_ID(Client), Req->prefix);
345 /* handle msgtarget = msgto *("," msgto) */
346 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
348 while (currentTarget) {
349 /* Check for and handle valid <msgto> of form:
351 * msgto = channel / ( user [ "%" host ] "@" servername )
352 * msgto =/ ( user "%" host ) / targetmask
353 * msgto =/ nickname / ( nickname "!" user "@" host )
355 if (strchr(currentTarget, '!') == NULL)
357 cl = Client_Search(currentTarget);
362 /* If currentTarget isn't a nickname check for:
363 * user ["%" host] "@" servername
365 * nickname "!" user "@" host
367 char target[COMMAND_LEN];
371 char * server = NULL;
373 strlcpy(target, currentTarget, COMMAND_LEN);
374 server = strchr(target, '@');
379 host = strchr(target, '%');
384 user = strchr(target, '!');
386 /* msgto form: nick!user@host */
390 host = server; /* not "@server" but "@host" */
395 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
396 if (Client_Type(cl) != CLIENT_USER)
399 if (strcmp(nick, Client_ID(cl)) == 0 &&
400 strcmp(user, Client_User(cl)) == 0 &&
401 strcasecmp(host, Client_Hostname(cl)) == 0)
406 if (strcasecmp(user, Client_User(cl)) != 0)
408 if (host != NULL && strcasecmp(host, Client_Hostname(cl)) != 0)
410 if (server != NULL && strcasecmp(server,
411 Client_ID(Client_Introducer(cl))) != 0)
418 /* Target is a user, enforce type */
419 if (Client_Type(cl) != ForceType) {
422 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
426 } else if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
427 && strchr(Client_Modes(cl), 'a')) {
431 if (!IRC_WriteStrClient
432 (from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl),
436 if (Client_Conn(from) > NONE) {
437 Conn_UpdateIdle(Client_Conn(from));
439 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
440 Client_ID(cl), Req->argv[1]))
442 } else if (strchr("$#", currentTarget[0]) && strchr(currentTarget, '.')) {
444 if (!Send_Message_Mask(from, currentTarget, Req->argv[1],
447 } else if ((chan = Channel_Search(currentTarget))) {
449 if (!Channel_Write(chan, from, Client, Req->argv[1]))
454 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
455 Client_ID(from), currentTarget))
459 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
468 Send_Message_Mask(CLIENT * from, char * targetMask, char * message, bool SendErrors)
472 char *mask = targetMask + 1;
476 if (strchr(Client_Modes(from), 'o') == NULL) {
479 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG, Client_ID(from));
482 if (targetMask[0] == '#') {
483 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
484 if (Client_Type(cl) != CLIENT_USER)
486 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
488 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
489 Client_ID(cl), message))
493 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
494 if (Client_Type(cl) != CLIENT_USER)
496 client_match = MatchCaseInsensitive(mask,
497 Client_ID(Client_Introducer(cl)));
499 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
500 Client_ID(cl), message))
505 } /* Send_Message_Mask */