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,
44 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
45 char *targetMask, char *message,
50 IRC_ERROR( CLIENT *Client, REQUEST *Req )
52 assert( Client != NULL );
53 assert( Req != NULL );
55 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
56 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
63 * Kill client on request.
64 * This function implements the IRC command "KILL" wich is used to selectively
65 * disconnect clients. It can be used by IRC operators and servers, for example
66 * to "solve" nick collisions after netsplits.
67 * Please note that this function is also called internally, without a real
68 * KILL command beeing received over the network! Client is Client_ThisServer()
71 IRC_KILL( CLIENT *Client, REQUEST *Req )
74 char reason[COMMAND_LEN], *msg;
75 CONN_ID my_conn, conn;
77 assert( Client != NULL );
78 assert( Req != NULL );
80 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
81 ( ! Client_OperByMe( Client )))
83 /* The originator of the KILL is neither an IRC operator of
84 * this server nor a server. */
85 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
91 /* This command requires exactly 2 parameters! */
92 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
93 Client_ID( Client ), Req->command );
96 if( Req->prefix ) prefix = Client_Search( Req->prefix );
100 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
102 prefix = Client_ThisServer( );
105 if( Client != Client_ThisServer( ))
107 /* This is a "real" KILL received from the network. */
108 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
109 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
112 /* Build reason string */
113 if( Client_Type( Client ) == CLIENT_USER )
115 /* Prefix the "reason" if the originator is a regular user,
116 * so users can't spoof KILLs of servers. */
117 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
118 Client_ID( Client ), Req->argv[1] );
121 strlcpy( reason, Req->argv[1], sizeof( reason ));
123 /* Inform other servers */
124 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
125 Req->argv[0], reason );
127 /* Save ID of this connection */
128 my_conn = Client_Conn( Client );
130 /* Do we host such a client? */
131 c = Client_Search( Req->argv[0] );
134 if(( Client_Type( c ) != CLIENT_USER ) &&
135 ( Client_Type( c ) != CLIENT_GOTNICK ))
137 /* Target of this KILL is not a regular user, this is
138 * invalid! So we ignore this case if we received a
139 * regular KILL from the network and try to kill the
140 * client/connection anyway (but log an error!) if the
141 * origin is the local server. */
143 if( Client != Client_ThisServer( ))
145 /* Invalid KILL received from remote */
146 if( Client_Type( c ) == CLIENT_SERVER )
147 msg = ERR_CANTKILLSERVER_MSG;
149 msg = ERR_NOPRIVILEGES_MSG;
150 return IRC_WriteStrClient( Client, msg,
151 Client_ID( Client ));
154 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
155 Client_Type( c ), Req->argv[0] );
158 /* Kill client NOW! */
159 conn = Client_Conn( c );
160 Client_Destroy( c, NULL, reason, false );
162 Conn_Close( conn, NULL, reason, true );
165 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
167 /* Are we still connected or were we killed, too? */
168 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
176 * Handler for the IRC command NOTICE.
179 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
181 return Send_Message(Client, Req, CLIENT_USER, false);
186 * Handler for the IRC command PRIVMSG.
189 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
191 return Send_Message(Client, Req, CLIENT_USER, true);
196 * Handler for the IRC command SQUERY.
199 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
201 return Send_Message(Client, Req, CLIENT_SERVICE, true);
206 IRC_TRACE( CLIENT *Client, REQUEST *Req )
208 CLIENT *from, *target, *c;
210 char user[CLIENT_USER_LEN];
212 assert( Client != NULL );
213 assert( Req != NULL );
215 /* Bad number of arguments? */
216 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
219 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
221 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
224 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
225 else target = Client_ThisServer( );
227 /* Forward command to other server? */
228 if( target != Client_ThisServer( ))
230 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
232 /* Send RPL_TRACELINK back to initiator */
233 idx = Client_Conn( Client ); assert( idx > NONE );
234 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
235 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;
237 /* Forward command */
238 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
242 /* Infos about all connected servers */
246 if( Client_Conn( c ) > NONE )
249 if( Client_Type( c ) == CLIENT_SERVER )
252 strlcpy( user, Client_User( c ), sizeof( user ));
253 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
254 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;
256 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
259 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
262 c = Client_Next( c );
265 IRC_SetPenalty( Client, 3 );
266 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
271 IRC_HELP( CLIENT *Client, REQUEST *Req )
275 assert( Client != NULL );
276 assert( Req != NULL );
278 /* Bad number of arguments? */
279 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
281 cmd = Parse_GetCommandStruct( );
284 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
288 IRC_SetPenalty( Client, 2 );
294 Option_String( CONN_ID Idx )
296 static char option_txt[8];
299 options = Conn_Options(Idx);
301 strcpy(option_txt, "F"); /* No idea what this means, but the
302 * original ircd sends it ... */
304 if(options & CONN_ZIP) /* zlib compression supported. */
305 strcat(option_txt, "z");
309 } /* Option_String */
313 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
317 char *currentTarget = Req->argv[0];
318 char *lastCurrentTarget = NULL;
320 assert(Client != NULL);
323 if (Req->argc == 0) {
326 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
327 Client_ID(Client), Req->command);
329 if (Req->argc == 1) {
332 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
338 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
339 Client_ID(Client), Req->command);
342 if (Client_Type(Client) == CLIENT_SERVER)
343 from = Client_Search(Req->prefix);
347 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
348 Client_ID(Client), Req->prefix);
350 /* handle msgtarget = msgto *("," msgto) */
351 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
353 while (currentTarget) {
354 /* Check for and handle valid <msgto> of form:
356 * msgto = channel / ( user [ "%" host ] "@" servername )
357 * msgto =/ ( user "%" host ) / targetmask
358 * msgto =/ nickname / ( nickname "!" user "@" host )
360 if (strchr(currentTarget, '!') == NULL)
362 cl = Client_Search(currentTarget);
367 /* If currentTarget isn't a nickname check for:
368 * user ["%" host] "@" servername
370 * nickname "!" user "@" host
372 char target[COMMAND_LEN];
376 char * server = NULL;
378 strlcpy(target, currentTarget, COMMAND_LEN);
379 server = strchr(target, '@');
384 host = strchr(target, '%');
389 user = strchr(target, '!');
391 /* msgto form: nick!user@host */
395 host = server; /* not "@server" but "@host" */
400 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
401 if (Client_Type(cl) != CLIENT_USER)
403 if (nick != NULL && host != NULL) {
404 if (strcmp(nick, Client_ID(cl)) == 0 &&
405 strcmp(user, Client_User(cl)) == 0 &&
406 strcasecmp(host, Client_Hostname(cl)) == 0)
411 if (strcasecmp(user, Client_User(cl)) != 0)
413 if (host != NULL && strcasecmp(host,
414 Client_Hostname(cl)) != 0)
416 if (server != NULL && strcasecmp(server,
417 Client_ID(Client_Introducer(cl))) != 0)
424 /* Target is a user, enforce type */
426 if (Client_Type(cl) != ForceType &&
427 !(ForceType == CLIENT_USER &&
428 (Client_Type(cl) == CLIENT_USER ||
429 Client_Type(cl) == CLIENT_SERVICE))) {
431 if (Client_Type(cl) != ForceType) {
435 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
439 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
440 && strchr(Client_Modes(cl), 'a')) {
442 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
448 if (Client_Conn(from) > NONE) {
449 Conn_UpdateIdle(Client_Conn(from));
451 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
452 Req->command, Client_ID(cl),
455 } else if (strchr("$#", currentTarget[0])
456 && strchr(currentTarget, '.')) {
458 if (!Send_Message_Mask(from, Req->command, currentTarget,
459 Req->argv[1], SendErrors))
461 } else if ((chan = Channel_Search(currentTarget))) {
463 if (!Channel_Write(chan, from, Client, Req->argv[1]))
468 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
469 Client_ID(from), currentTarget))
473 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
481 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
482 char * message, bool SendErrors)
486 char *mask = targetMask + 1;
490 if (strchr(Client_Modes(from), 'o') == NULL) {
493 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
497 if (targetMask[0] == '#') {
498 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
499 if (Client_Type(cl) != CLIENT_USER)
501 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
503 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
504 command, Client_ID(cl), message))
508 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
509 if (Client_Type(cl) != CLIENT_USER)
511 client_match = MatchCaseInsensitive(mask,
512 Client_ID(Client_Introducer(cl)));
514 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
515 command, Client_ID(cl), message))
520 } /* Send_Message_Mask */