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)
353 char *currentTarget = Req->argv[0];
354 char *lastCurrentTarget = NULL;
356 assert(Client != NULL);
359 if (Req->argc == 0) {
362 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
363 Client_ID(Client), Req->command);
365 if (Req->argc == 1) {
368 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
374 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
375 Client_ID(Client), Req->command);
378 if (Client_Type(Client) == CLIENT_SERVER)
379 from = Client_Search(Req->prefix);
383 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
384 Client_ID(Client), Req->prefix);
386 /* handle msgtarget = msgto *("," msgto) */
387 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
388 ngt_UpperStr(Req->command);
390 while (currentTarget) {
391 /* Check for and handle valid <msgto> of form:
393 * msgto = channel / ( user [ "%" host ] "@" servername )
394 * msgto =/ ( user "%" host ) / targetmask
395 * msgto =/ nickname / ( nickname "!" user "@" host )
397 if (strchr(currentTarget, '!') == NULL)
399 cl = Client_Search(currentTarget);
404 /* If currentTarget isn't a nickname check for:
405 * user ["%" host] "@" servername
407 * nickname "!" user "@" host
409 char target[COMMAND_LEN];
413 char * server = NULL;
415 strlcpy(target, currentTarget, COMMAND_LEN);
416 server = strchr(target, '@');
421 host = strchr(target, '%');
426 user = strchr(target, '!');
428 /* msgto form: nick!user@host */
432 host = server; /* not "@server" but "@host" */
437 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
438 if (Client_Type(cl) != CLIENT_USER &&
439 Client_Type(cl) != CLIENT_SERVICE)
441 if (nick != NULL && host != NULL) {
442 if (strcmp(nick, Client_ID(cl)) == 0 &&
443 strcmp(user, Client_User(cl)) == 0 &&
444 strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
449 if (strcasecmp(user, Client_User(cl)) != 0)
451 if (host != NULL && strcasecmp(host,
452 Client_HostnameCloaked(cl)) != 0)
454 if (server != NULL && strcasecmp(server,
455 Client_ID(Client_Introducer(cl))) != 0)
462 /* Target is a user, enforce type */
464 if (Client_Type(cl) != ForceType &&
465 !(ForceType == CLIENT_USER &&
466 (Client_Type(cl) == CLIENT_USER ||
467 Client_Type(cl) == CLIENT_SERVICE))) {
469 if (Client_Type(cl) != ForceType) {
473 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
479 if (ForceType == CLIENT_SERVICE &&
480 (Conn_Options(Client_Conn(Client_NextHop(cl)))
482 /* SQUERY command but RFC 1459 link: convert
483 * request to PRIVMSG command */
484 Req->command = "PRIVMSG";
488 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
489 && strchr(Client_Modes(cl), 'a')) {
491 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
497 if (Client_Conn(from) > NONE) {
498 Conn_UpdateIdle(Client_Conn(from));
500 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
501 Req->command, Client_ID(cl),
504 } else if (ForceType != CLIENT_SERVICE
505 && (chan = Channel_Search(currentTarget))) {
506 if (!Channel_Write(chan, from, Client, Req->command,
507 SendErrors, Req->argv[1]))
509 } else if (ForceType != CLIENT_SERVICE
510 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
511 && strchr("$#", currentTarget[0])
512 && strchr(currentTarget, '.')) {
514 if (!Send_Message_Mask(from, Req->command, currentTarget,
515 Req->argv[1], SendErrors))
520 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
521 Client_ID(from), currentTarget))
525 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
527 Conn_SetPenalty(Client_Conn(Client), 1);
535 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
536 char * message, bool SendErrors)
540 char *mask = targetMask + 1;
541 const char *check_wildcards;
545 if (strchr(Client_Modes(from), 'o') == NULL) {
548 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
553 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
554 * dot (".") and no wildcards ("*", "?") following the last one.
556 check_wildcards = strrchr(targetMask, '.');
557 assert(check_wildcards != NULL);
558 if (check_wildcards &&
559 check_wildcards[strcspn(check_wildcards, "*?")])
563 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
566 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
567 if (targetMask[0] == '#') {
568 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
569 if (Client_Type(cl) != CLIENT_USER)
571 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
573 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
574 command, Client_ID(cl), message))
578 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
579 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
580 if (Client_Type(cl) != CLIENT_USER)
582 client_match = MatchCaseInsensitive(mask,
583 Client_ID(Client_Introducer(cl)));
585 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
586 command, Client_ID(cl), message))
591 } /* Send_Message_Mask */