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 $";
25 #include "conn-func.h"
29 #include "irc-write.h"
40 static char *Option_String PARAMS((CONN_ID Idx));
41 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
44 char *targetMask, char *message,
49 IRC_ERROR( CLIENT *Client, REQUEST *Req )
51 assert( Client != NULL );
52 assert( Req != NULL );
55 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
58 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
59 Client_Mask(Client), Req->argv[0]);
66 * Kill client on request.
67 * This function implements the IRC command "KILL" wich is used to selectively
68 * disconnect clients. It can be used by IRC operators and servers, for example
69 * to "solve" nick collisions after netsplits.
70 * Please note that this function is also called internally, without a real
71 * KILL command being received over the network! Client is Client_ThisServer()
74 IRC_KILL( CLIENT *Client, REQUEST *Req )
77 char reason[COMMAND_LEN], *msg;
78 CONN_ID my_conn, conn;
80 assert( Client != NULL );
81 assert( Req != NULL );
83 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
84 ( ! Client_OperByMe( Client )))
86 /* The originator of the KILL is neither an IRC operator of
87 * this server nor a server. */
88 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
94 /* This command requires exactly 2 parameters! */
95 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
96 Client_ID( Client ), Req->command );
99 if( Req->prefix ) prefix = Client_Search( Req->prefix );
100 else prefix = Client;
103 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
105 prefix = Client_ThisServer( );
108 if( Client != Client_ThisServer( ))
110 /* This is a "real" KILL received from the network. */
111 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
112 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
115 /* Build reason string */
116 if( Client_Type( Client ) == CLIENT_USER )
118 /* Prefix the "reason" if the originator is a regular user,
119 * so users can't spoof KILLs of servers. */
120 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
121 Client_ID( Client ), Req->argv[1] );
124 strlcpy( reason, Req->argv[1], sizeof( reason ));
126 /* Inform other servers */
127 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
128 Req->argv[0], reason );
130 /* Save ID of this connection */
131 my_conn = Client_Conn( Client );
133 /* Do we host such a client? */
134 c = Client_Search( Req->argv[0] );
137 if(( Client_Type( c ) != CLIENT_USER ) &&
138 ( Client_Type( c ) != CLIENT_GOTNICK ))
140 /* Target of this KILL is not a regular user, this is
141 * invalid! So we ignore this case if we received a
142 * regular KILL from the network and try to kill the
143 * client/connection anyway (but log an error!) if the
144 * origin is the local server. */
146 if( Client != Client_ThisServer( ))
148 /* Invalid KILL received from remote */
149 if( Client_Type( c ) == CLIENT_SERVER )
150 msg = ERR_CANTKILLSERVER_MSG;
152 msg = ERR_NOPRIVILEGES_MSG;
153 return IRC_WriteStrClient( Client, msg,
154 Client_ID( Client ));
157 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
158 Client_Type( c ), Req->argv[0] );
161 /* Kill the client NOW:
162 * - Close the local connection (if there is one),
163 * - Destroy the CLIENT structure for remote clients.
164 * Note: Conn_Close() removes the CLIENT structure as well. */
165 conn = Client_Conn( c );
167 Conn_Close(conn, NULL, reason, true);
169 Client_Destroy(c, NULL, reason, false);
172 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
174 /* Are we still connected or were we killed, too? */
175 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
183 * Handler for the IRC command NOTICE.
186 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
188 return Send_Message(Client, Req, CLIENT_USER, false);
193 * Handler for the IRC command PRIVMSG.
196 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
198 return Send_Message(Client, Req, CLIENT_USER, true);
203 * Handler for the IRC command SQUERY.
206 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
208 return Send_Message(Client, Req, CLIENT_SERVICE, true);
213 IRC_TRACE( CLIENT *Client, REQUEST *Req )
215 CLIENT *from, *target, *c;
217 char user[CLIENT_USER_LEN];
219 assert( Client != NULL );
220 assert( Req != NULL );
222 /* Bad number of arguments? */
223 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
226 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
228 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
231 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
232 else target = Client_ThisServer( );
234 /* Forward command to other server? */
235 if( target != Client_ThisServer( ))
237 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
239 /* Send RPL_TRACELINK back to initiator */
240 idx = Client_Conn( Client ); assert( idx > NONE );
241 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
242 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;
244 /* Forward command */
245 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
249 /* Infos about all connected servers */
253 if( Client_Conn( c ) > NONE )
256 if( Client_Type( c ) == CLIENT_SERVER )
259 strlcpy( user, Client_User( c ), sizeof( user ));
260 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
261 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;
263 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
266 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
269 c = Client_Next( c );
272 IRC_SetPenalty( Client, 3 );
273 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
278 IRC_HELP( CLIENT *Client, REQUEST *Req )
282 assert( Client != NULL );
283 assert( Req != NULL );
285 /* Bad number of arguments? */
286 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
288 cmd = Parse_GetCommandStruct( );
291 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
295 IRC_SetPenalty( Client, 2 );
301 Option_String( CONN_ID Idx )
303 static char option_txt[8];
306 options = Conn_Options(Idx);
308 strcpy(option_txt, "F"); /* No idea what this means, but the
309 * original ircd sends it ... */
311 if(options & CONN_ZIP) /* zlib compression supported. */
312 strcat(option_txt, "z");
316 } /* Option_String */
320 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
324 char *currentTarget = Req->argv[0];
325 char *lastCurrentTarget = NULL;
327 assert(Client != NULL);
330 if (Req->argc == 0) {
333 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
334 Client_ID(Client), Req->command);
336 if (Req->argc == 1) {
339 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
345 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
346 Client_ID(Client), Req->command);
349 if (Client_Type(Client) == CLIENT_SERVER)
350 from = Client_Search(Req->prefix);
354 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
355 Client_ID(Client), Req->prefix);
357 /* handle msgtarget = msgto *("," msgto) */
358 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
359 ngt_UpperStr(Req->command);
361 while (currentTarget) {
362 /* Check for and handle valid <msgto> of form:
364 * msgto = channel / ( user [ "%" host ] "@" servername )
365 * msgto =/ ( user "%" host ) / targetmask
366 * msgto =/ nickname / ( nickname "!" user "@" host )
368 if (strchr(currentTarget, '!') == NULL)
370 cl = Client_Search(currentTarget);
375 /* If currentTarget isn't a nickname check for:
376 * user ["%" host] "@" servername
378 * nickname "!" user "@" host
380 char target[COMMAND_LEN];
384 char * server = NULL;
386 strlcpy(target, currentTarget, COMMAND_LEN);
387 server = strchr(target, '@');
392 host = strchr(target, '%');
397 user = strchr(target, '!');
399 /* msgto form: nick!user@host */
403 host = server; /* not "@server" but "@host" */
408 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
409 if (Client_Type(cl) != CLIENT_USER &&
410 Client_Type(cl) != CLIENT_SERVICE)
412 if (nick != NULL && host != NULL) {
413 if (strcmp(nick, Client_ID(cl)) == 0 &&
414 strcmp(user, Client_User(cl)) == 0 &&
415 strcasecmp(host, Client_Hostname(cl)) == 0)
420 if (strcasecmp(user, Client_User(cl)) != 0)
422 if (host != NULL && strcasecmp(host,
423 Client_Hostname(cl)) != 0)
425 if (server != NULL && strcasecmp(server,
426 Client_ID(Client_Introducer(cl))) != 0)
433 /* Target is a user, enforce type */
435 if (Client_Type(cl) != ForceType &&
436 !(ForceType == CLIENT_USER &&
437 (Client_Type(cl) == CLIENT_USER ||
438 Client_Type(cl) == CLIENT_SERVICE))) {
440 if (Client_Type(cl) != ForceType) {
444 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
450 if (ForceType == CLIENT_SERVICE &&
451 (Conn_Options(Client_Conn(Client_NextHop(cl)))
453 /* SQUERY command but RFC 1459 link: convert
454 * request to PRIVMSG command */
455 Req->command = "PRIVMSG";
459 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
460 && strchr(Client_Modes(cl), 'a')) {
462 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
468 if (Client_Conn(from) > NONE) {
469 Conn_UpdateIdle(Client_Conn(from));
471 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
472 Req->command, Client_ID(cl),
475 } else if (ForceType != CLIENT_SERVICE
476 && (chan = Channel_Search(currentTarget))) {
477 if (!Channel_Write(chan, from, Client, Req->command,
478 SendErrors, Req->argv[1]))
480 } else if (ForceType != CLIENT_SERVICE
481 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
482 && strchr("$#", currentTarget[0])
483 && strchr(currentTarget, '.')) {
485 if (!Send_Message_Mask(from, Req->command, currentTarget,
486 Req->argv[1], SendErrors))
491 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
492 Client_ID(from), currentTarget))
496 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
504 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
505 char * message, bool SendErrors)
509 char *mask = targetMask + 1;
510 const char *check_wildcards;
514 if (strchr(Client_Modes(from), 'o') == NULL) {
517 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
522 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
523 * dot (".") and no wildcards ("*", "?") following the last one.
525 check_wildcards = strrchr(targetMask, '.');
526 assert(check_wildcards != NULL);
527 if (check_wildcards &&
528 check_wildcards[strcspn(check_wildcards, "*?")])
532 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
535 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
536 if (targetMask[0] == '#') {
537 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
538 if (Client_Type(cl) != CLIENT_USER)
540 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
542 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
543 command, Client_ID(cl), message))
547 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
548 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
549 if (Client_Type(cl) != CLIENT_USER)
551 client_match = MatchCaseInsensitive(mask,
552 Client_ID(Client_Introducer(cl)));
554 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
555 command, Client_ID(cl), message))
560 } /* Send_Message_Mask */