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 * Handler for the IRC command NOTICE.
176 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
178 return Send_Message(Client, Req, CLIENT_USER, false);
183 * Handler for the IRC command PRIVMSG.
186 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
188 return Send_Message(Client, Req, CLIENT_USER, true);
193 * Handler for the IRC command SQUERY.
196 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
198 return Send_Message(Client, Req, CLIENT_SERVICE, true);
203 IRC_TRACE( CLIENT *Client, REQUEST *Req )
205 CLIENT *from, *target, *c;
207 char user[CLIENT_USER_LEN];
209 assert( Client != NULL );
210 assert( Req != NULL );
212 /* Bad number of arguments? */
213 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
216 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
218 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
221 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
222 else target = Client_ThisServer( );
224 /* Forward command to other server? */
225 if( target != Client_ThisServer( ))
227 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
229 /* Send RPL_TRACELINK back to initiator */
230 idx = Client_Conn( Client ); assert( idx > NONE );
231 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
232 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;
234 /* Forward command */
235 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
239 /* Infos about all connected servers */
243 if( Client_Conn( c ) > NONE )
246 if( Client_Type( c ) == CLIENT_SERVER )
249 strlcpy( user, Client_User( c ), sizeof( user ));
250 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
251 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;
253 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
256 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
259 c = Client_Next( c );
262 IRC_SetPenalty( Client, 3 );
263 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
268 IRC_HELP( CLIENT *Client, REQUEST *Req )
272 assert( Client != NULL );
273 assert( Req != NULL );
275 /* Bad number of arguments? */
276 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
278 cmd = Parse_GetCommandStruct( );
281 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
285 IRC_SetPenalty( Client, 2 );
291 Option_String( CONN_ID Idx )
293 static char option_txt[8];
296 options = Conn_Options(Idx);
298 strcpy(option_txt, "F"); /* No idea what this means, but the
299 * original ircd sends it ... */
301 if(options & CONN_ZIP) /* zlib compression supported. */
302 strcat(option_txt, "z");
306 } /* Option_String */
310 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
314 char *currentTarget = Req->argv[0];
315 char *lastCurrentTarget = NULL;
317 assert(Client != NULL);
320 if (Req->argc == 0) {
323 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
324 Client_ID(Client), Req->command);
326 if (Req->argc == 1) {
329 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
335 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
336 Client_ID(Client), Req->command);
339 if (Client_Type(Client) == CLIENT_SERVER)
340 from = Client_Search(Req->prefix);
344 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
345 Client_ID(Client), Req->prefix);
347 /* handle msgtarget = msgto *("," msgto) */
348 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
350 while (currentTarget) {
351 /* Check for and handle valid <msgto> of form:
353 * msgto = channel / ( user [ "%" host ] "@" servername )
354 * msgto =/ ( user "%" host ) / targetmask
355 * msgto =/ nickname / ( nickname "!" user "@" host )
357 if (strchr(currentTarget, '!') == NULL)
359 cl = Client_Search(currentTarget);
364 /* If currentTarget isn't a nickname check for:
365 * user ["%" host] "@" servername
367 * nickname "!" user "@" host
369 char target[COMMAND_LEN];
373 char * server = NULL;
375 strlcpy(target, currentTarget, COMMAND_LEN);
376 server = strchr(target, '@');
381 host = strchr(target, '%');
386 user = strchr(target, '!');
388 /* msgto form: nick!user@host */
392 host = server; /* not "@server" but "@host" */
397 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
398 if (Client_Type(cl) != CLIENT_USER)
400 if (nick != NULL && host != NULL) {
401 if (strcmp(nick, Client_ID(cl)) == 0 &&
402 strcmp(user, Client_User(cl)) == 0 &&
403 strcasecmp(host, Client_Hostname(cl)) == 0)
408 if (strcasecmp(user, Client_User(cl)) != 0)
410 if (host != NULL && strcasecmp(host,
411 Client_Hostname(cl)) != 0)
413 if (server != NULL && strcasecmp(server,
414 Client_ID(Client_Introducer(cl))) != 0)
421 /* Target is a user, enforce type */
423 if (Client_Type(cl) != ForceType &&
424 !(ForceType == CLIENT_USER &&
425 (Client_Type(cl) == CLIENT_USER ||
426 Client_Type(cl) == CLIENT_SERVICE))) {
428 if (Client_Type(cl) != ForceType) {
432 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
436 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
437 && strchr(Client_Modes(cl), 'a')) {
439 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
445 if (Client_Conn(from) > NONE) {
446 Conn_UpdateIdle(Client_Conn(from));
448 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
449 Client_ID(cl), Req->argv[1]))
451 } else if (strchr("$#", currentTarget[0])
452 && strchr(currentTarget, '.')) {
454 if (!Send_Message_Mask(from, currentTarget,
455 Req->argv[1], SendErrors))
457 } else if ((chan = Channel_Search(currentTarget))) {
459 if (!Channel_Write(chan, from, Client, Req->argv[1]))
464 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
465 Client_ID(from), currentTarget))
469 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
477 Send_Message_Mask(CLIENT * from, char * targetMask, char * message, bool SendErrors)
481 char *mask = targetMask + 1;
485 if (strchr(Client_Modes(from), 'o') == NULL) {
488 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
492 if (targetMask[0] == '#') {
493 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
494 if (Client_Type(cl) != CLIENT_USER)
496 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
498 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
499 Client_ID(cl), message))
503 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
504 if (Client_Type(cl) != CLIENT_USER)
506 client_match = MatchCaseInsensitive(mask,
507 Client_ID(Client_Introducer(cl)));
509 if (!IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
510 Client_ID(cl), message))
515 } /* Send_Message_Mask */