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"
31 /** Handle /JUMPSERVER
33 class CommandJumpserver : public Command
36 bool redirect_new_users;
37 std::string redirect_to;
42 CommandJumpserver(Module* Creator) : Command(Creator, "JUMPSERVER", 0, 4)
45 syntax = "[<server> <port>[:<sslport>] <+/-an> <reason>]";
48 redirect_new_users = false;
51 CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
54 reason = (parameters.size() < 4) ? "Please use this server/port instead" : parameters[3];
55 bool redirect_all_immediately = false;
56 redirect_new_users = true;
57 bool direction = true;
60 /* No parameters: jumpserver disabled */
61 if (parameters.empty())
64 user->WriteNotice("*** Disabled jumpserver (previously set to '" + redirect_to + ":" + ConvToStr(port) + "')");
66 user->WriteNotice("*** Jumpserver was not enabled.");
77 if (parameters.size() >= 3)
79 for (std::string::const_iterator n = parameters[2].begin(); n != parameters[2].end(); ++n)
90 redirect_all_immediately = direction;
93 redirect_new_users = direction;
96 user->WriteNotice("*** Invalid JUMPSERVER flag: " + ConvToStr(*n));
102 size_t delimpos = parameters[1].find(':');
103 port = ConvToInt(parameters[1].substr(0, delimpos ? delimpos : std::string::npos));
104 sslport = (delimpos == std::string::npos ? 0 : ConvToInt(parameters[1].substr(delimpos + 1)));
106 if (parameters[1].find_first_not_of("0123456789:") != std::string::npos
107 || parameters[1].rfind(':') != delimpos
108 || port > 65535 || sslport > 65535)
110 user->WriteNotice("*** Invalid port number");
114 if (redirect_all_immediately)
116 /* Redirect everyone but the oper sending the command */
117 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
118 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); )
120 // Quitting the user removes it from the list
125 t->WriteNumeric(RPL_REDIR, parameters[0], GetPort(t), "Please use this Server/Port instead");
126 ServerInstance->Users->QuitUser(t, reason);
132 n_done_s = ConvToStr(n_done);
136 if (redirect_new_users)
137 redirect_to = parameters[0];
139 user->WriteNotice("*** Set jumpserver to server '" + parameters[0] + "' port '" + (port ? ConvToStr(port) : "Auto") + ", SSL " + (sslport ? ConvToStr(sslport) : "Auto") + "', flags '+" +
140 (redirect_all_immediately ? "a" : "") + (redirect_new_users ? "n'" : "'") +
141 (n_done ? " (" + n_done_s + "user(s) redirected): " : ": ") + reason);
147 int GetPort(LocalUser* user)
149 int p = (SSLIOHook::IsSSL(&user->eh) ? sslport : port);
151 p = user->GetServerPort();
156 class ModuleJumpServer : public Module
158 CommandJumpserver js;
160 ModuleJumpServer() : js(this)
164 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
166 if (js.redirect_new_users)
168 int port = js.GetPort(user);
169 user->WriteNumeric(RPL_REDIR, js.redirect_to, port, "Please use this Server/Port instead");
170 ServerInstance->Users->QuitUser(user, js.reason);
173 return MOD_RES_PASSTHRU;
176 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
178 // Emergency way to unlock
180 js.redirect_new_users = false;
183 Version GetVersion() CXX11_OVERRIDE
185 return Version("Provides support for the RPL_REDIR numeric and the /JUMPSERVER command.", VF_VENDOR);
189 MODULE_INIT(ModuleJumpServer)