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"
28 #include "conn-encoding.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 * Check if a list limit is reached and inform client accordingly.
52 * @param From The client.
53 * @param Count Reply item count.
54 * @param Limit Reply limit.
55 * @param Name Name of the list.
56 * @return true if list limit has been reached; false otherwise.
59 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
70 (void)IRC_WriteStrClient(From,
71 "NOTICE %s :%s list limit (%d) reached!",
72 Client_ID(From), Name, Limit);
73 IRC_SetPenalty(From, 2);
79 IRC_ERROR( CLIENT *Client, REQUEST *Req )
81 assert( Client != NULL );
82 assert( Req != NULL );
85 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
88 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
89 Client_Mask(Client), Req->argv[0]);
96 * Handler for the IRC "KILL" command.
98 * This function implements the IRC command "KILL" wich is used to selectively
99 * disconnect clients. It can be used by IRC operators and servers, for example
100 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
102 * Please note that this function is also called internally, without a real
103 * KILL command being received over the network! Client is Client_ThisServer()
104 * in this case, and the prefix in Req is NULL.
106 * @param Client The client from which this command has been received
107 * or Client_ThisServer() when generated interanlly.
108 * @param Req Request structure with prefix and all parameters.
109 * @returns CONNECTED or DISCONNECTED.
112 IRC_KILL( CLIENT *Client, REQUEST *Req )
115 char reason[COMMAND_LEN], *msg;
116 CONN_ID my_conn, conn;
118 assert (Client != NULL);
119 assert (Req != NULL);
121 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
122 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
126 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
127 Client_ID(Client), Req->command);
129 /* Get prefix (origin); use the client if no prefix is given. */
131 prefix = Client_Search(Req->prefix);
135 /* Log a warning message and use this server as origin when the
136 * prefix (origin) is invalid. */
138 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
140 prefix = Client_ThisServer();
143 if (Client != Client_ThisServer())
144 Log(LOG_NOTICE|LOG_snotice,
145 "Got KILL command from \"%s\" for \"%s\": %s",
146 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
148 /* Build reason string: Prefix the "reason" if the originator is a
149 * regular user, so users can't spoof KILLs of servers. */
150 if (Client_Type(Client) == CLIENT_USER)
151 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
152 Client_ID(Client), Req->argv[1]);
154 strlcpy(reason, Req->argv[1], sizeof(reason));
156 /* Inform other servers */
157 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
158 Req->argv[0], reason);
160 /* Save ID of this connection */
161 my_conn = Client_Conn( Client );
163 /* Do we host such a client? */
164 c = Client_Search( Req->argv[0] );
167 if(( Client_Type( c ) != CLIENT_USER ) &&
168 ( Client_Type( c ) != CLIENT_GOTNICK ))
170 /* Target of this KILL is not a regular user, this is
171 * invalid! So we ignore this case if we received a
172 * regular KILL from the network and try to kill the
173 * client/connection anyway (but log an error!) if the
174 * origin is the local server. */
176 if( Client != Client_ThisServer( ))
178 /* Invalid KILL received from remote */
179 if( Client_Type( c ) == CLIENT_SERVER )
180 msg = ERR_CANTKILLSERVER_MSG;
182 msg = ERR_NOPRIVILEGES_MSG;
183 return IRC_WriteStrClient( Client, msg,
184 Client_ID( Client ));
187 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
188 Client_Type( c ), Req->argv[0] );
191 /* Kill the client NOW:
192 * - Close the local connection (if there is one),
193 * - Destroy the CLIENT structure for remote clients.
194 * Note: Conn_Close() removes the CLIENT structure as well. */
195 conn = Client_Conn( c );
197 Conn_Close(conn, NULL, reason, true);
199 Client_Destroy(c, NULL, reason, false);
202 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
204 /* Are we still connected or were we killed, too? */
205 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
213 * Handler for the IRC command NOTICE.
216 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
218 return Send_Message(Client, Req, CLIENT_USER, false);
223 * Handler for the IRC command PRIVMSG.
226 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
228 return Send_Message(Client, Req, CLIENT_USER, true);
233 * Handler for the IRC command SQUERY.
236 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
238 return Send_Message(Client, Req, CLIENT_SERVICE, true);
243 IRC_TRACE( CLIENT *Client, REQUEST *Req )
245 CLIENT *from, *target, *c;
247 char user[CLIENT_USER_LEN];
249 assert( Client != NULL );
250 assert( Req != NULL );
252 /* Bad number of arguments? */
253 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
256 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
258 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
261 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
262 else target = Client_ThisServer( );
264 /* Forward command to other server? */
265 if( target != Client_ThisServer( ))
267 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
269 /* Send RPL_TRACELINK back to initiator */
270 idx = Client_Conn( Client ); assert( idx > NONE );
271 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
272 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;
274 /* Forward command */
275 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
279 /* Infos about all connected servers */
283 if( Client_Conn( c ) > NONE )
286 if( Client_Type( c ) == CLIENT_SERVER )
289 strlcpy( user, Client_User( c ), sizeof( user ));
290 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
291 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;
293 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
296 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
299 c = Client_Next( c );
302 IRC_SetPenalty( Client, 3 );
303 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
308 * Handler for the IRC "HELP" command.
310 * @param Client The client from which this command has been received.
311 * @param Req Request structure with prefix and all parameters.
312 * @return CONNECTED or DISCONNECTED.
315 IRC_HELP(CLIENT *Client, REQUEST *Req)
319 assert(Client != NULL);
322 /* Bad number of arguments? */
324 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
325 Client_ID(Client), Req->command);
327 cmd = Parse_GetCommandStruct();
329 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
330 Client_ID(Client), cmd->name))
335 IRC_SetPenalty(Client, 2);
342 Option_String(CONN_ID Idx)
344 Option_String(UNUSED CONN_ID Idx)
347 static char option_txt[8];
351 options = Conn_Options(Idx);
354 strcpy(option_txt, "F"); /* No idea what this means, but the
355 * original ircd sends it ... */
357 if(options & CONN_ZIP) /* zlib compression supported. */
358 strcat(option_txt, "z");
362 } /* Option_String */
366 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
371 char *currentTarget = Req->argv[0];
372 char *lastCurrentTarget = NULL;
373 char *message = NULL;
375 assert(Client != NULL);
378 if (Req->argc == 0) {
381 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
382 Client_ID(Client), Req->command);
384 if (Req->argc == 1) {
387 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
393 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
394 Client_ID(Client), Req->command);
397 if (Client_Type(Client) == CLIENT_SERVER)
398 from = Client_Search(Req->prefix);
402 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
403 Client_ID(Client), Req->prefix);
406 if (Client_Conn(Client) > NONE)
407 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
410 message = Req->argv[1];
412 /* handle msgtarget = msgto *("," msgto) */
413 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
414 ngt_UpperStr(Req->command);
416 while (currentTarget) {
417 /* Check for and handle valid <msgto> of form:
419 * msgto = channel / ( user [ "%" host ] "@" servername )
420 * msgto =/ ( user "%" host ) / targetmask
421 * msgto =/ nickname / ( nickname "!" user "@" host )
423 if (strchr(currentTarget, '!') == NULL)
425 cl = Client_Search(currentTarget);
430 /* If currentTarget isn't a nickname check for:
431 * user ["%" host] "@" servername
433 * nickname "!" user "@" host
435 char target[COMMAND_LEN];
439 char * server = NULL;
441 strlcpy(target, currentTarget, COMMAND_LEN);
442 server = strchr(target, '@');
447 host = strchr(target, '%');
452 user = strchr(target, '!');
454 /* msgto form: nick!user@host */
458 host = server; /* not "@server" but "@host" */
463 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
464 if (Client_Type(cl) != CLIENT_USER &&
465 Client_Type(cl) != CLIENT_SERVICE)
467 if (nick != NULL && host != NULL) {
468 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
469 strcasecmp(user, Client_User(cl)) == 0 &&
470 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
475 if (strcasecmp(user, Client_User(cl)) != 0)
477 if (host != NULL && strcasecmp(host,
478 Client_HostnameDisplayed(cl)) != 0)
480 if (server != NULL && strcasecmp(server,
481 Client_ID(Client_Introducer(cl))) != 0)
488 /* Target is a user, enforce type */
490 if (Client_Type(cl) != ForceType &&
491 !(ForceType == CLIENT_USER &&
492 (Client_Type(cl) == CLIENT_USER ||
493 Client_Type(cl) == CLIENT_SERVICE))) {
495 if (Client_Type(cl) != ForceType) {
497 if (SendErrors && !IRC_WriteStrClient(
498 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
501 goto send_next_target;
505 if (ForceType == CLIENT_SERVICE &&
506 (Conn_Options(Client_Conn(Client_NextHop(cl)))
508 /* SQUERY command but RFC 1459 link: convert
509 * request to PRIVMSG command */
510 Req->command = "PRIVMSG";
513 if (Client_HasMode(cl, 'b') &&
514 !Client_HasMode(from, 'R') &&
515 !Client_HasMode(from, 'o') &&
516 !(Client_Type(from) == CLIENT_SERVER) &&
517 !(Client_Type(from) == CLIENT_SERVICE)) {
518 if (SendErrors && !IRC_WriteStrClient(from,
520 Client_ID(from), Client_ID(cl)))
522 goto send_next_target;
525 if (Client_HasMode(cl, 'C')) {
526 cl2chan = Channel_FirstChannelOf(cl);
528 chan = Channel_GetChannel(cl2chan);
529 if (Channel_IsMemberOf(chan, from))
531 cl2chan = Channel_NextChannelOf(cl, cl2chan);
534 if (SendErrors && !IRC_WriteStrClient(
535 from, ERR_NOTONSAMECHANNEL_MSG,
536 Client_ID(from), Client_ID(cl)))
538 goto send_next_target;
542 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
543 && strchr(Client_Modes(cl), 'a')) {
545 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
551 if (Client_Conn(from) > NONE) {
552 Conn_UpdateIdle(Client_Conn(from));
554 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
555 Req->command, Client_ID(cl),
558 } else if (ForceType != CLIENT_SERVICE
559 && (chan = Channel_Search(currentTarget))) {
560 if (!Channel_Write(chan, from, Client, Req->command,
561 SendErrors, message))
563 } else if (ForceType != CLIENT_SERVICE
564 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
565 && strchr("$#", currentTarget[0])
566 && strchr(currentTarget, '.')) {
568 if (!Send_Message_Mask(from, Req->command, currentTarget,
569 message, SendErrors))
574 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
575 Client_ID(from), currentTarget))
580 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
582 Conn_SetPenalty(Client_Conn(Client), 1);
590 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
591 char * message, bool SendErrors)
595 char *mask = targetMask + 1;
596 const char *check_wildcards;
600 if (strchr(Client_Modes(from), 'o') == NULL) {
603 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
608 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
609 * dot (".") and no wildcards ("*", "?") following the last one.
611 check_wildcards = strrchr(targetMask, '.');
612 assert(check_wildcards != NULL);
613 if (check_wildcards &&
614 check_wildcards[strcspn(check_wildcards, "*?")])
618 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
621 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
622 if (targetMask[0] == '#') {
623 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
624 if (Client_Type(cl) != CLIENT_USER)
626 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
628 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
629 command, Client_ID(cl), message))
633 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
634 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
635 if (Client_Type(cl) != CLIENT_USER)
637 client_match = MatchCaseInsensitive(mask,
638 Client_ID(Client_Introducer(cl)));
640 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
641 command, Client_ID(cl), message))
646 } /* Send_Message_Mask */