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 );
330 Option_String( CONN_ID Idx )
332 static char option_txt[8];
335 options = Conn_Options(Idx);
337 strcpy(option_txt, "F"); /* No idea what this means, but the
338 * original ircd sends it ... */
340 if(options & CONN_ZIP) /* zlib compression supported. */
341 strcat(option_txt, "z");
345 } /* Option_String */
349 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
354 char *currentTarget = Req->argv[0];
355 char *lastCurrentTarget = NULL;
357 assert(Client != NULL);
360 if (Req->argc == 0) {
363 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
364 Client_ID(Client), Req->command);
366 if (Req->argc == 1) {
369 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
375 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
376 Client_ID(Client), Req->command);
379 if (Client_Type(Client) == CLIENT_SERVER)
380 from = Client_Search(Req->prefix);
384 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
385 Client_ID(Client), Req->prefix);
387 /* handle msgtarget = msgto *("," msgto) */
388 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
389 ngt_UpperStr(Req->command);
391 while (currentTarget) {
392 /* Check for and handle valid <msgto> of form:
394 * msgto = channel / ( user [ "%" host ] "@" servername )
395 * msgto =/ ( user "%" host ) / targetmask
396 * msgto =/ nickname / ( nickname "!" user "@" host )
398 if (strchr(currentTarget, '!') == NULL)
400 cl = Client_Search(currentTarget);
405 /* If currentTarget isn't a nickname check for:
406 * user ["%" host] "@" servername
408 * nickname "!" user "@" host
410 char target[COMMAND_LEN];
414 char * server = NULL;
416 strlcpy(target, currentTarget, COMMAND_LEN);
417 server = strchr(target, '@');
422 host = strchr(target, '%');
427 user = strchr(target, '!');
429 /* msgto form: nick!user@host */
433 host = server; /* not "@server" but "@host" */
438 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
439 if (Client_Type(cl) != CLIENT_USER &&
440 Client_Type(cl) != CLIENT_SERVICE)
442 if (nick != NULL && host != NULL) {
443 if (strcmp(nick, Client_ID(cl)) == 0 &&
444 strcmp(user, Client_User(cl)) == 0 &&
445 strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
450 if (strcasecmp(user, Client_User(cl)) != 0)
452 if (host != NULL && strcasecmp(host,
453 Client_HostnameCloaked(cl)) != 0)
455 if (server != NULL && strcasecmp(server,
456 Client_ID(Client_Introducer(cl))) != 0)
463 /* Target is a user, enforce type */
465 if (Client_Type(cl) != ForceType &&
466 !(ForceType == CLIENT_USER &&
467 (Client_Type(cl) == CLIENT_USER ||
468 Client_Type(cl) == CLIENT_SERVICE))) {
470 if (Client_Type(cl) != ForceType) {
474 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
480 if (ForceType == CLIENT_SERVICE &&
481 (Conn_Options(Client_Conn(Client_NextHop(cl)))
483 /* SQUERY command but RFC 1459 link: convert
484 * request to PRIVMSG command */
485 Req->command = "PRIVMSG";
489 if (Client_HasMode(cl, 'C')) {
490 cl2chan = Channel_FirstChannelOf(cl);
492 chan = Channel_GetChannel(cl2chan);
493 if (Channel_IsMemberOf(chan, from))
495 cl2chan = Channel_NextChannelOf(cl, cl2chan);
498 if (SendErrors && !IRC_WriteStrClient(
499 from, ERR_NOTONSAMECHANNEL_MSG,
500 Client_ID(from), Client_ID(cl)))
502 goto send_next_target;
506 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
507 && strchr(Client_Modes(cl), 'a')) {
509 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
515 if (Client_Conn(from) > NONE) {
516 Conn_UpdateIdle(Client_Conn(from));
518 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
519 Req->command, Client_ID(cl),
522 } else if (ForceType != CLIENT_SERVICE
523 && (chan = Channel_Search(currentTarget))) {
524 if (!Channel_Write(chan, from, Client, Req->command,
525 SendErrors, Req->argv[1]))
527 } else if (ForceType != CLIENT_SERVICE
528 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
529 && strchr("$#", currentTarget[0])
530 && strchr(currentTarget, '.')) {
532 if (!Send_Message_Mask(from, Req->command, currentTarget,
533 Req->argv[1], SendErrors))
538 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
539 Client_ID(from), currentTarget))
544 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
546 Conn_SetPenalty(Client_Conn(Client), 1);
554 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
555 char * message, bool SendErrors)
559 char *mask = targetMask + 1;
560 const char *check_wildcards;
564 if (strchr(Client_Modes(from), 'o') == NULL) {
567 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
572 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
573 * dot (".") and no wildcards ("*", "?") following the last one.
575 check_wildcards = strrchr(targetMask, '.');
576 assert(check_wildcards != NULL);
577 if (check_wildcards &&
578 check_wildcards[strcspn(check_wildcards, "*?")])
582 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
585 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
586 if (targetMask[0] == '#') {
587 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
588 if (Client_Type(cl) != CLIENT_USER)
590 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
592 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
593 command, Client_ID(cl), message))
597 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
598 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
599 if (Client_Type(cl) != CLIENT_USER)
601 client_match = MatchCaseInsensitive(mask,
602 Client_ID(Client_Introducer(cl)));
604 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
605 command, Client_ID(cl), message))
610 } /* Send_Message_Mask */