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 );
84 if (Client_Type(Client) != CLIENT_GOTPASS
85 && Client_Type(Client) != CLIENT_GOTPASS_2813
86 && Client_Type(Client) != CLIENT_UNKNOWNSERVER
87 && Client_Type(Client) != CLIENT_SERVER
88 && Client_Type(Client) != CLIENT_SERVICE) {
89 LogDebug("Ignored ERROR command from \"%s\" ...",
91 IRC_SetPenalty(Client, 2);
96 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
99 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
100 Client_Mask(Client), Req->argv[0]);
107 * Handler for the IRC "KILL" command.
109 * This function implements the IRC command "KILL" wich is used to selectively
110 * disconnect clients. It can be used by IRC operators and servers, for example
111 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
113 * Please note that this function is also called internally, without a real
114 * KILL command being received over the network! Client is Client_ThisServer()
115 * in this case, and the prefix in Req is NULL.
117 * @param Client The client from which this command has been received
118 * or Client_ThisServer() when generated interanlly.
119 * @param Req Request structure with prefix and all parameters.
120 * @returns CONNECTED or DISCONNECTED.
123 IRC_KILL( CLIENT *Client, REQUEST *Req )
126 char reason[COMMAND_LEN], *msg;
127 CONN_ID my_conn, conn;
129 assert (Client != NULL);
130 assert (Req != NULL);
132 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
133 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
137 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
138 Client_ID(Client), Req->command);
140 /* Get prefix (origin); use the client if no prefix is given. */
142 prefix = Client_Search(Req->prefix);
146 /* Log a warning message and use this server as origin when the
147 * prefix (origin) is invalid. */
149 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
151 prefix = Client_ThisServer();
154 if (Client != Client_ThisServer())
155 Log(LOG_NOTICE|LOG_snotice,
156 "Got KILL command from \"%s\" for \"%s\": %s",
157 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
159 /* Build reason string: Prefix the "reason" if the originator is a
160 * regular user, so users can't spoof KILLs of servers. */
161 if (Client_Type(Client) == CLIENT_USER)
162 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
163 Client_ID(Client), Req->argv[1]);
165 strlcpy(reason, Req->argv[1], sizeof(reason));
167 /* Inform other servers */
168 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
169 Req->argv[0], reason);
171 /* Save ID of this connection */
172 my_conn = Client_Conn( Client );
174 /* Do we host such a client? */
175 c = Client_Search( Req->argv[0] );
178 if(( Client_Type( c ) != CLIENT_USER ) &&
179 ( Client_Type( c ) != CLIENT_GOTNICK ))
181 /* Target of this KILL is not a regular user, this is
182 * invalid! So we ignore this case if we received a
183 * regular KILL from the network and try to kill the
184 * client/connection anyway (but log an error!) if the
185 * origin is the local server. */
187 if( Client != Client_ThisServer( ))
189 /* Invalid KILL received from remote */
190 if( Client_Type( c ) == CLIENT_SERVER )
191 msg = ERR_CANTKILLSERVER_MSG;
193 msg = ERR_NOPRIVILEGES_MSG;
194 return IRC_WriteStrClient( Client, msg,
195 Client_ID( Client ));
198 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
199 Client_Type( c ), Req->argv[0] );
202 /* Kill the client NOW:
203 * - Close the local connection (if there is one),
204 * - Destroy the CLIENT structure for remote clients.
205 * Note: Conn_Close() removes the CLIENT structure as well. */
206 conn = Client_Conn( c );
208 Conn_Close(conn, NULL, reason, true);
210 Client_Destroy(c, NULL, reason, false);
213 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
215 /* Are we still connected or were we killed, too? */
216 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
224 * Handler for the IRC command NOTICE.
227 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
229 return Send_Message(Client, Req, CLIENT_USER, false);
234 * Handler for the IRC command PRIVMSG.
237 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
239 return Send_Message(Client, Req, CLIENT_USER, true);
244 * Handler for the IRC command SQUERY.
247 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
249 return Send_Message(Client, Req, CLIENT_SERVICE, true);
254 IRC_TRACE( CLIENT *Client, REQUEST *Req )
256 CLIENT *from, *target, *c;
258 char user[CLIENT_USER_LEN];
260 assert( Client != NULL );
261 assert( Req != NULL );
263 /* Bad number of arguments? */
264 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
267 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
269 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
272 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
273 else target = Client_ThisServer( );
275 /* Forward command to other server? */
276 if( target != Client_ThisServer( ))
278 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
280 /* Send RPL_TRACELINK back to initiator */
281 idx = Client_Conn( Client ); assert( idx > NONE );
282 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
283 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;
285 /* Forward command */
286 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
290 /* Infos about all connected servers */
294 if( Client_Conn( c ) > NONE )
297 if( Client_Type( c ) == CLIENT_SERVER )
300 strlcpy( user, Client_User( c ), sizeof( user ));
301 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
302 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;
304 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
307 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
310 c = Client_Next( c );
313 IRC_SetPenalty( Client, 3 );
314 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
319 IRC_HELP( CLIENT *Client, REQUEST *Req )
323 assert( Client != NULL );
324 assert( Req != NULL );
326 /* Bad number of arguments? */
327 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
329 cmd = Parse_GetCommandStruct( );
332 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
336 IRC_SetPenalty( Client, 2 );
343 Option_String(CONN_ID Idx)
345 Option_String(UNUSED CONN_ID Idx)
348 static char option_txt[8];
352 options = Conn_Options(Idx);
355 strcpy(option_txt, "F"); /* No idea what this means, but the
356 * original ircd sends it ... */
358 if(options & CONN_ZIP) /* zlib compression supported. */
359 strcat(option_txt, "z");
363 } /* Option_String */
367 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
372 char *currentTarget = Req->argv[0];
373 char *lastCurrentTarget = NULL;
374 char *message = NULL;
376 assert(Client != NULL);
379 if (Req->argc == 0) {
382 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
383 Client_ID(Client), Req->command);
385 if (Req->argc == 1) {
388 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
394 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
395 Client_ID(Client), Req->command);
398 if (Client_Type(Client) == CLIENT_SERVER)
399 from = Client_Search(Req->prefix);
403 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
404 Client_ID(Client), Req->prefix);
407 if (Client_Conn(Client) > NONE)
408 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
411 message = Req->argv[1];
413 /* handle msgtarget = msgto *("," msgto) */
414 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
415 ngt_UpperStr(Req->command);
417 while (currentTarget) {
418 /* Check for and handle valid <msgto> of form:
420 * msgto = channel / ( user [ "%" host ] "@" servername )
421 * msgto =/ ( user "%" host ) / targetmask
422 * msgto =/ nickname / ( nickname "!" user "@" host )
424 if (strchr(currentTarget, '!') == NULL)
426 cl = Client_Search(currentTarget);
431 /* If currentTarget isn't a nickname check for:
432 * user ["%" host] "@" servername
434 * nickname "!" user "@" host
436 char target[COMMAND_LEN];
440 char * server = NULL;
442 strlcpy(target, currentTarget, COMMAND_LEN);
443 server = strchr(target, '@');
448 host = strchr(target, '%');
453 user = strchr(target, '!');
455 /* msgto form: nick!user@host */
459 host = server; /* not "@server" but "@host" */
464 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
465 if (Client_Type(cl) != CLIENT_USER &&
466 Client_Type(cl) != CLIENT_SERVICE)
468 if (nick != NULL && host != NULL) {
469 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
470 strcasecmp(user, Client_User(cl)) == 0 &&
471 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
476 if (strcasecmp(user, Client_User(cl)) != 0)
478 if (host != NULL && strcasecmp(host,
479 Client_HostnameDisplayed(cl)) != 0)
481 if (server != NULL && strcasecmp(server,
482 Client_ID(Client_Introducer(cl))) != 0)
489 /* Target is a user, enforce type */
491 if (Client_Type(cl) != ForceType &&
492 !(ForceType == CLIENT_USER &&
493 (Client_Type(cl) == CLIENT_USER ||
494 Client_Type(cl) == CLIENT_SERVICE))) {
496 if (Client_Type(cl) != ForceType) {
498 if (SendErrors && !IRC_WriteStrClient(
499 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
502 goto send_next_target;
506 if (ForceType == CLIENT_SERVICE &&
507 (Conn_Options(Client_Conn(Client_NextHop(cl)))
509 /* SQUERY command but RFC 1459 link: convert
510 * request to PRIVMSG command */
511 Req->command = "PRIVMSG";
514 if (Client_HasMode(cl, 'b') &&
515 !Client_HasMode(from, 'R') &&
516 !Client_HasMode(from, 'o') &&
517 !(Client_Type(from) == CLIENT_SERVER) &&
518 !(Client_Type(from) == CLIENT_SERVICE)) {
519 if (SendErrors && !IRC_WriteStrClient(from,
521 Client_ID(from), Client_ID(cl)))
523 goto send_next_target;
526 if (Client_HasMode(cl, 'C')) {
527 cl2chan = Channel_FirstChannelOf(cl);
529 chan = Channel_GetChannel(cl2chan);
530 if (Channel_IsMemberOf(chan, from))
532 cl2chan = Channel_NextChannelOf(cl, cl2chan);
535 if (SendErrors && !IRC_WriteStrClient(
536 from, ERR_NOTONSAMECHANNEL_MSG,
537 Client_ID(from), Client_ID(cl)))
539 goto send_next_target;
543 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
544 && strchr(Client_Modes(cl), 'a')) {
546 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
552 if (Client_Conn(from) > NONE) {
553 Conn_UpdateIdle(Client_Conn(from));
555 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
556 Req->command, Client_ID(cl),
559 } else if (ForceType != CLIENT_SERVICE
560 && (chan = Channel_Search(currentTarget))) {
561 if (!Channel_Write(chan, from, Client, Req->command,
562 SendErrors, message))
564 } else if (ForceType != CLIENT_SERVICE
565 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
566 && strchr("$#", currentTarget[0])
567 && strchr(currentTarget, '.')) {
569 if (!Send_Message_Mask(from, Req->command, currentTarget,
570 message, SendErrors))
575 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
576 Client_ID(from), currentTarget))
581 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
583 Conn_SetPenalty(Client_Conn(Client), 1);
591 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
592 char * message, bool SendErrors)
596 char *mask = targetMask + 1;
597 const char *check_wildcards;
601 if (strchr(Client_Modes(from), 'o') == NULL) {
604 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
609 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
610 * dot (".") and no wildcards ("*", "?") following the last one.
612 check_wildcards = strrchr(targetMask, '.');
613 assert(check_wildcards != NULL);
614 if (check_wildcards &&
615 check_wildcards[strcspn(check_wildcards, "*?")])
619 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
622 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
623 if (targetMask[0] == '#') {
624 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
625 if (Client_Type(cl) != CLIENT_USER)
627 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
629 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
630 command, Client_ID(cl), message))
634 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
635 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
636 if (Client_Type(cl) != CLIENT_USER)
638 client_match = MatchCaseInsensitive(mask,
639 Client_ID(Client_Introducer(cl)));
641 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
642 command, Client_ID(cl), message))
647 } /* Send_Message_Mask */