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.
26 #include "conn-func.h"
30 #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 );
56 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
59 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
60 Client_Mask(Client), Req->argv[0]);
67 * Kill client on request.
68 * This function implements the IRC command "KILL" wich is used to selectively
69 * disconnect clients. It can be used by IRC operators and servers, for example
70 * to "solve" nick collisions after netsplits.
71 * Please note that this function is also called internally, without a real
72 * KILL command being received over the network! Client is Client_ThisServer()
75 IRC_KILL( CLIENT *Client, REQUEST *Req )
78 char reason[COMMAND_LEN], *msg;
79 CONN_ID my_conn, conn;
81 assert( Client != NULL );
82 assert( Req != NULL );
84 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
85 ( ! Client_OperByMe( Client )))
87 /* The originator of the KILL is neither an IRC operator of
88 * this server nor a server. */
89 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
95 /* This command requires exactly 2 parameters! */
96 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
97 Client_ID( Client ), Req->command );
100 if( Req->prefix ) prefix = Client_Search( Req->prefix );
101 else prefix = Client;
104 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
106 prefix = Client_ThisServer( );
109 if( Client != Client_ThisServer( ))
111 /* This is a "real" KILL received from the network. */
112 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
113 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
116 /* Build reason string */
117 if( Client_Type( Client ) == CLIENT_USER )
119 /* Prefix the "reason" if the originator is a regular user,
120 * so users can't spoof KILLs of servers. */
121 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
122 Client_ID( Client ), Req->argv[1] );
125 strlcpy( reason, Req->argv[1], sizeof( reason ));
127 /* Inform other servers */
128 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
129 Req->argv[0], reason );
131 /* Save ID of this connection */
132 my_conn = Client_Conn( Client );
134 /* Do we host such a client? */
135 c = Client_Search( Req->argv[0] );
138 if(( Client_Type( c ) != CLIENT_USER ) &&
139 ( Client_Type( c ) != CLIENT_GOTNICK ))
141 /* Target of this KILL is not a regular user, this is
142 * invalid! So we ignore this case if we received a
143 * regular KILL from the network and try to kill the
144 * client/connection anyway (but log an error!) if the
145 * origin is the local server. */
147 if( Client != Client_ThisServer( ))
149 /* Invalid KILL received from remote */
150 if( Client_Type( c ) == CLIENT_SERVER )
151 msg = ERR_CANTKILLSERVER_MSG;
153 msg = ERR_NOPRIVILEGES_MSG;
154 return IRC_WriteStrClient( Client, msg,
155 Client_ID( Client ));
158 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
159 Client_Type( c ), Req->argv[0] );
162 /* Kill the client NOW:
163 * - Close the local connection (if there is one),
164 * - Destroy the CLIENT structure for remote clients.
165 * Note: Conn_Close() removes the CLIENT structure as well. */
166 conn = Client_Conn( c );
168 Conn_Close(conn, NULL, reason, true);
170 Client_Destroy(c, NULL, reason, false);
173 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
175 /* Are we still connected or were we killed, too? */
176 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
184 * Handler for the IRC command NOTICE.
187 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
189 return Send_Message(Client, Req, CLIENT_USER, false);
194 * Handler for the IRC command PRIVMSG.
197 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
199 return Send_Message(Client, Req, CLIENT_USER, true);
204 * Handler for the IRC command SQUERY.
207 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
209 return Send_Message(Client, Req, CLIENT_SERVICE, true);
214 IRC_TRACE( CLIENT *Client, REQUEST *Req )
216 CLIENT *from, *target, *c;
218 char user[CLIENT_USER_LEN];
220 assert( Client != NULL );
221 assert( Req != NULL );
223 /* Bad number of arguments? */
224 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
227 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
229 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
232 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
233 else target = Client_ThisServer( );
235 /* Forward command to other server? */
236 if( target != Client_ThisServer( ))
238 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
240 /* Send RPL_TRACELINK back to initiator */
241 idx = Client_Conn( Client ); assert( idx > NONE );
242 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
243 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;
245 /* Forward command */
246 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
250 /* Infos about all connected servers */
254 if( Client_Conn( c ) > NONE )
257 if( Client_Type( c ) == CLIENT_SERVER )
260 strlcpy( user, Client_User( c ), sizeof( user ));
261 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
262 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;
264 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
267 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
270 c = Client_Next( c );
273 IRC_SetPenalty( Client, 3 );
274 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
279 IRC_HELP( CLIENT *Client, REQUEST *Req )
283 assert( Client != NULL );
284 assert( Req != NULL );
286 /* Bad number of arguments? */
287 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
289 cmd = Parse_GetCommandStruct( );
292 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
296 IRC_SetPenalty( Client, 2 );
302 Option_String( CONN_ID Idx )
304 static char option_txt[8];
307 options = Conn_Options(Idx);
309 strcpy(option_txt, "F"); /* No idea what this means, but the
310 * original ircd sends it ... */
312 if(options & CONN_ZIP) /* zlib compression supported. */
313 strcat(option_txt, "z");
317 } /* Option_String */
321 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
325 char *currentTarget = Req->argv[0];
326 char *lastCurrentTarget = NULL;
328 assert(Client != NULL);
331 if (Req->argc == 0) {
334 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
335 Client_ID(Client), Req->command);
337 if (Req->argc == 1) {
340 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
346 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
347 Client_ID(Client), Req->command);
350 if (Client_Type(Client) == CLIENT_SERVER)
351 from = Client_Search(Req->prefix);
355 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
356 Client_ID(Client), Req->prefix);
358 /* handle msgtarget = msgto *("," msgto) */
359 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
360 ngt_UpperStr(Req->command);
362 while (currentTarget) {
363 /* Check for and handle valid <msgto> of form:
365 * msgto = channel / ( user [ "%" host ] "@" servername )
366 * msgto =/ ( user "%" host ) / targetmask
367 * msgto =/ nickname / ( nickname "!" user "@" host )
369 if (strchr(currentTarget, '!') == NULL)
371 cl = Client_Search(currentTarget);
376 /* If currentTarget isn't a nickname check for:
377 * user ["%" host] "@" servername
379 * nickname "!" user "@" host
381 char target[COMMAND_LEN];
385 char * server = NULL;
387 strlcpy(target, currentTarget, COMMAND_LEN);
388 server = strchr(target, '@');
393 host = strchr(target, '%');
398 user = strchr(target, '!');
400 /* msgto form: nick!user@host */
404 host = server; /* not "@server" but "@host" */
409 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
410 if (Client_Type(cl) != CLIENT_USER &&
411 Client_Type(cl) != CLIENT_SERVICE)
413 if (nick != NULL && host != NULL) {
414 if (strcmp(nick, Client_ID(cl)) == 0 &&
415 strcmp(user, Client_User(cl)) == 0 &&
416 strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
421 if (strcasecmp(user, Client_User(cl)) != 0)
423 if (host != NULL && strcasecmp(host,
424 Client_HostnameCloaked(cl)) != 0)
426 if (server != NULL && strcasecmp(server,
427 Client_ID(Client_Introducer(cl))) != 0)
434 /* Target is a user, enforce type */
436 if (Client_Type(cl) != ForceType &&
437 !(ForceType == CLIENT_USER &&
438 (Client_Type(cl) == CLIENT_USER ||
439 Client_Type(cl) == CLIENT_SERVICE))) {
441 if (Client_Type(cl) != ForceType) {
445 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
451 if (ForceType == CLIENT_SERVICE &&
452 (Conn_Options(Client_Conn(Client_NextHop(cl)))
454 /* SQUERY command but RFC 1459 link: convert
455 * request to PRIVMSG command */
456 Req->command = "PRIVMSG";
460 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
461 && strchr(Client_Modes(cl), 'a')) {
463 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
469 if (Client_Conn(from) > NONE) {
470 Conn_UpdateIdle(Client_Conn(from));
472 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
473 Req->command, Client_ID(cl),
476 } else if (ForceType != CLIENT_SERVICE
477 && (chan = Channel_Search(currentTarget))) {
478 if (!Channel_Write(chan, from, Client, Req->command,
479 SendErrors, Req->argv[1]))
481 } else if (ForceType != CLIENT_SERVICE
482 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
483 && strchr("$#", currentTarget[0])
484 && strchr(currentTarget, '.')) {
486 if (!Send_Message_Mask(from, Req->command, currentTarget,
487 Req->argv[1], SendErrors))
492 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
493 Client_ID(from), currentTarget))
497 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
505 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
506 char * message, bool SendErrors)
510 char *mask = targetMask + 1;
511 const char *check_wildcards;
515 if (strchr(Client_Modes(from), 'o') == NULL) {
518 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
523 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
524 * dot (".") and no wildcards ("*", "?") following the last one.
526 check_wildcards = strrchr(targetMask, '.');
527 assert(check_wildcards != NULL);
528 if (check_wildcards &&
529 check_wildcards[strcspn(check_wildcards, "*?")])
533 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
536 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
537 if (targetMask[0] == '#') {
538 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
539 if (Client_Type(cl) != CLIENT_USER)
541 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
543 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
544 command, Client_ID(cl), message))
548 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
549 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
550 if (Client_Type(cl) != CLIENT_USER)
552 client_match = MatchCaseInsensitive(mask,
553 Client_ID(Client_Introducer(cl)));
555 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
556 command, Client_ID(cl), message))
561 } /* Send_Message_Mask */