2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
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.
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 * Check if a list limit is reached and inform client accordingly.
51 * @param From The client.
52 * @param Count Reply item count.
53 * @param Limit Reply limit.
54 * @param Name Name of the list.
55 * @return true if list limit has been reached; false otherwise.
58 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
69 (void)IRC_WriteStrClient(From,
70 "NOTICE %s :%s list limit (%d) reached!",
71 Client_ID(From), Name, Limit);
72 IRC_SetPenalty(From, 2);
78 IRC_ERROR( CLIENT *Client, REQUEST *Req )
80 assert( Client != NULL );
81 assert( Req != NULL );
84 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
87 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
88 Client_Mask(Client), Req->argv[0]);
95 * Handler for the IRC "KILL" command.
97 * This function implements the IRC command "KILL" wich is used to selectively
98 * disconnect clients. It can be used by IRC operators and servers, for example
99 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
101 * Please note that this function is also called internally, without a real
102 * KILL command being received over the network! Client is Client_ThisServer()
103 * in this case, and the prefix in Req is NULL.
105 * @param Client The client from which this command has been received
106 * or Client_ThisServer() when generated interanlly.
107 * @param Req Request structure with prefix and all parameters.
108 * @returns CONNECTED or DISCONNECTED.
111 IRC_KILL( CLIENT *Client, REQUEST *Req )
114 char reason[COMMAND_LEN], *msg;
115 CONN_ID my_conn, conn;
117 assert (Client != NULL);
118 assert (Req != NULL);
120 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
121 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
125 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
126 Client_ID(Client), Req->command);
128 /* Get prefix (origin); use the client if no prefix is given. */
130 prefix = Client_Search(Req->prefix);
134 /* Log a warning message and use this server as origin when the
135 * prefix (origin) is invalid. */
137 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
139 prefix = Client_ThisServer();
142 if (Client != Client_ThisServer())
143 Log(LOG_NOTICE|LOG_snotice,
144 "Got KILL command from \"%s\" for \"%s\": %s",
145 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
147 /* Build reason string: Prefix the "reason" if the originator is a
148 * regular user, so users can't spoof KILLs of servers. */
149 if (Client_Type(Client) == CLIENT_USER)
150 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
151 Client_ID(Client), Req->argv[1]);
153 strlcpy(reason, Req->argv[1], sizeof(reason));
155 /* Inform other servers */
156 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
157 Req->argv[0], reason);
159 /* Save ID of this connection */
160 my_conn = Client_Conn( Client );
162 /* Do we host such a client? */
163 c = Client_Search( Req->argv[0] );
166 if(( Client_Type( c ) != CLIENT_USER ) &&
167 ( Client_Type( c ) != CLIENT_GOTNICK ))
169 /* Target of this KILL is not a regular user, this is
170 * invalid! So we ignore this case if we received a
171 * regular KILL from the network and try to kill the
172 * client/connection anyway (but log an error!) if the
173 * origin is the local server. */
175 if( Client != Client_ThisServer( ))
177 /* Invalid KILL received from remote */
178 if( Client_Type( c ) == CLIENT_SERVER )
179 msg = ERR_CANTKILLSERVER_MSG;
181 msg = ERR_NOPRIVILEGES_MSG;
182 return IRC_WriteStrClient( Client, msg,
183 Client_ID( Client ));
186 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
187 Client_Type( c ), Req->argv[0] );
190 /* Kill the client NOW:
191 * - Close the local connection (if there is one),
192 * - Destroy the CLIENT structure for remote clients.
193 * Note: Conn_Close() removes the CLIENT structure as well. */
194 conn = Client_Conn( c );
196 Conn_Close(conn, NULL, reason, true);
198 Client_Destroy(c, NULL, reason, false);
201 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
203 /* Are we still connected or were we killed, too? */
204 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
212 * Handler for the IRC command NOTICE.
215 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
217 return Send_Message(Client, Req, CLIENT_USER, false);
222 * Handler for the IRC command PRIVMSG.
225 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
227 return Send_Message(Client, Req, CLIENT_USER, true);
232 * Handler for the IRC command SQUERY.
235 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
237 return Send_Message(Client, Req, CLIENT_SERVICE, true);
242 IRC_TRACE( CLIENT *Client, REQUEST *Req )
244 CLIENT *from, *target, *c;
246 char user[CLIENT_USER_LEN];
248 assert( Client != NULL );
249 assert( Req != NULL );
251 /* Bad number of arguments? */
252 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
255 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
257 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
260 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
261 else target = Client_ThisServer( );
263 /* Forward command to other server? */
264 if( target != Client_ThisServer( ))
266 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
268 /* Send RPL_TRACELINK back to initiator */
269 idx = Client_Conn( Client ); assert( idx > NONE );
270 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
271 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;
273 /* Forward command */
274 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
278 /* Infos about all connected servers */
282 if( Client_Conn( c ) > NONE )
285 if( Client_Type( c ) == CLIENT_SERVER )
288 strlcpy( user, Client_User( c ), sizeof( user ));
289 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
290 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;
292 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
295 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
298 c = Client_Next( c );
301 IRC_SetPenalty( Client, 3 );
302 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
307 IRC_HELP( CLIENT *Client, REQUEST *Req )
311 assert( Client != NULL );
312 assert( Req != NULL );
314 /* Bad number of arguments? */
315 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
317 cmd = Parse_GetCommandStruct( );
320 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
324 IRC_SetPenalty( Client, 2 );
331 Option_String(CONN_ID Idx)
333 Option_String(UNUSED CONN_ID Idx)
336 static char option_txt[8];
340 options = Conn_Options(Idx);
343 strcpy(option_txt, "F"); /* No idea what this means, but the
344 * original ircd sends it ... */
346 if(options & CONN_ZIP) /* zlib compression supported. */
347 strcat(option_txt, "z");
351 } /* Option_String */
355 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
360 char *currentTarget = Req->argv[0];
361 char *lastCurrentTarget = NULL;
363 assert(Client != NULL);
366 if (Req->argc == 0) {
369 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
370 Client_ID(Client), Req->command);
372 if (Req->argc == 1) {
375 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
381 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
382 Client_ID(Client), Req->command);
385 if (Client_Type(Client) == CLIENT_SERVER)
386 from = Client_Search(Req->prefix);
390 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
391 Client_ID(Client), Req->prefix);
393 /* handle msgtarget = msgto *("," msgto) */
394 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
395 ngt_UpperStr(Req->command);
397 while (currentTarget) {
398 /* Check for and handle valid <msgto> of form:
400 * msgto = channel / ( user [ "%" host ] "@" servername )
401 * msgto =/ ( user "%" host ) / targetmask
402 * msgto =/ nickname / ( nickname "!" user "@" host )
404 if (strchr(currentTarget, '!') == NULL)
406 cl = Client_Search(currentTarget);
411 /* If currentTarget isn't a nickname check for:
412 * user ["%" host] "@" servername
414 * nickname "!" user "@" host
416 char target[COMMAND_LEN];
420 char * server = NULL;
422 strlcpy(target, currentTarget, COMMAND_LEN);
423 server = strchr(target, '@');
428 host = strchr(target, '%');
433 user = strchr(target, '!');
435 /* msgto form: nick!user@host */
439 host = server; /* not "@server" but "@host" */
444 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
445 if (Client_Type(cl) != CLIENT_USER &&
446 Client_Type(cl) != CLIENT_SERVICE)
448 if (nick != NULL && host != NULL) {
449 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
450 strcasecmp(user, Client_User(cl)) == 0 &&
451 strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
456 if (strcasecmp(user, Client_User(cl)) != 0)
458 if (host != NULL && strcasecmp(host,
459 Client_HostnameCloaked(cl)) != 0)
461 if (server != NULL && strcasecmp(server,
462 Client_ID(Client_Introducer(cl))) != 0)
469 /* Target is a user, enforce type */
471 if (Client_Type(cl) != ForceType &&
472 !(ForceType == CLIENT_USER &&
473 (Client_Type(cl) == CLIENT_USER ||
474 Client_Type(cl) == CLIENT_SERVICE))) {
476 if (Client_Type(cl) != ForceType) {
478 if (SendErrors && !IRC_WriteStrClient(
479 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
482 goto send_next_target;
486 if (ForceType == CLIENT_SERVICE &&
487 (Conn_Options(Client_Conn(Client_NextHop(cl)))
489 /* SQUERY command but RFC 1459 link: convert
490 * request to PRIVMSG command */
491 Req->command = "PRIVMSG";
495 if (Client_HasMode(cl, 'C')) {
496 cl2chan = Channel_FirstChannelOf(cl);
498 chan = Channel_GetChannel(cl2chan);
499 if (Channel_IsMemberOf(chan, from))
501 cl2chan = Channel_NextChannelOf(cl, cl2chan);
504 if (SendErrors && !IRC_WriteStrClient(
505 from, ERR_NOTONSAMECHANNEL_MSG,
506 Client_ID(from), Client_ID(cl)))
508 goto send_next_target;
512 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
513 && strchr(Client_Modes(cl), 'a')) {
515 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
521 if (Client_Conn(from) > NONE) {
522 Conn_UpdateIdle(Client_Conn(from));
524 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
525 Req->command, Client_ID(cl),
528 } else if (ForceType != CLIENT_SERVICE
529 && (chan = Channel_Search(currentTarget))) {
530 if (!Channel_Write(chan, from, Client, Req->command,
531 SendErrors, Req->argv[1]))
533 } else if (ForceType != CLIENT_SERVICE
534 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
535 && strchr("$#", currentTarget[0])
536 && strchr(currentTarget, '.')) {
538 if (!Send_Message_Mask(from, Req->command, currentTarget,
539 Req->argv[1], SendErrors))
544 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
545 Client_ID(from), currentTarget))
550 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
552 Conn_SetPenalty(Client_Conn(Client), 1);
560 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
561 char * message, bool SendErrors)
565 char *mask = targetMask + 1;
566 const char *check_wildcards;
570 if (strchr(Client_Modes(from), 'o') == NULL) {
573 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
578 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
579 * dot (".") and no wildcards ("*", "?") following the last one.
581 check_wildcards = strrchr(targetMask, '.');
582 assert(check_wildcards != NULL);
583 if (check_wildcards &&
584 check_wildcards[strcspn(check_wildcards, "*?")])
588 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
591 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
592 if (targetMask[0] == '#') {
593 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
594 if (Client_Type(cl) != CLIENT_USER)
596 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
598 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
599 command, Client_ID(cl), message))
603 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
604 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
605 if (Client_Type(cl) != CLIENT_USER)
607 client_match = MatchCaseInsensitive(mask,
608 Client_ID(Client_Introducer(cl)));
610 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
611 command, Client_ID(cl), message))
616 } /* Send_Message_Mask */