2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "modules/ssl.h"
25 /** Handle /JUMPSERVER
27 class CommandJumpserver : public Command
30 bool redirect_new_users;
31 std::string redirect_to;
36 CommandJumpserver(Module* Creator) : Command(Creator, "JUMPSERVER", 0, 4)
39 syntax = "[<server> <port>[:<sslport>] <+/-an> <reason>]";
42 redirect_new_users = false;
45 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
48 reason = (parameters.size() < 4) ? "Please use this server/port instead" : parameters[3];
49 bool redirect_all_immediately = false;
50 redirect_new_users = true;
51 bool direction = true;
54 /* No parameters: jumpserver disabled */
55 if (!parameters.size())
58 user->WriteNotice("*** Disabled jumpserver (previously set to '" + redirect_to + ":" + ConvToStr(port) + "')");
60 user->WriteNotice("*** Jumpserver was not enabled.");
71 if (parameters.size() >= 3)
73 for (std::string::const_iterator n = parameters[2].begin(); n != parameters[2].end(); ++n)
84 redirect_all_immediately = direction;
87 redirect_new_users = direction;
90 user->WriteNotice("*** Invalid JUMPSERVER flag: " + ConvToStr(*n));
96 size_t delimpos = parameters[1].find(':');
97 port = ConvToInt(parameters[1].substr(0, delimpos ? delimpos : std::string::npos));
98 sslport = (delimpos == std::string::npos ? 0 : ConvToInt(parameters[1].substr(delimpos + 1)));
100 if (parameters[1].find_first_not_of("0123456789:") != std::string::npos
101 || parameters[1].rfind(':') != delimpos
102 || port > 65535 || sslport > 65535)
104 user->WriteNotice("*** Invalid port number");
108 if (redirect_all_immediately)
110 /* Redirect everyone but the oper sending the command */
111 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
112 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); )
114 // Quitting the user removes it from the list
119 t->WriteNumeric(RPL_REDIR, parameters[0], GetPort(t), "Please use this Server/Port instead");
120 ServerInstance->Users->QuitUser(t, reason);
126 n_done_s = ConvToStr(n_done);
130 if (redirect_new_users)
131 redirect_to = parameters[0];
133 user->WriteNotice("*** Set jumpserver to server '" + parameters[0] + "' port '" + (port ? ConvToStr(port) : "Auto") + ", SSL " + (sslport ? ConvToStr(sslport) : "Auto") + "', flags '+" +
134 (redirect_all_immediately ? "a" : "") + (redirect_new_users ? "n'" : "'") +
135 (n_done ? " (" + n_done_s + "user(s) redirected): " : ": ") + reason);
141 int GetPort(LocalUser* user)
143 int p = (SSLIOHook::IsSSL(&user->eh) ? sslport : port);
145 p = user->GetServerPort();
150 class ModuleJumpServer : public Module
152 CommandJumpserver js;
154 ModuleJumpServer() : js(this)
158 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
160 if (js.redirect_new_users)
162 int port = js.GetPort(user);
163 user->WriteNumeric(RPL_REDIR, js.redirect_to, port, "Please use this Server/Port instead");
164 ServerInstance->Users->QuitUser(user, js.reason);
165 return MOD_RES_PASSTHRU;
167 return MOD_RES_PASSTHRU;
170 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
172 // Emergency way to unlock
174 js.redirect_new_users = false;
177 Version GetVersion() CXX11_OVERRIDE
179 return Version("Provides support for the RPL_REDIR numeric and the /JUMPSERVER command.", VF_VENDOR);
183 MODULE_INIT(ModuleJumpServer)