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 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
116 t->WriteNumeric(10, "%s %s %d :Please use this Server/Port instead", t->nick.c_str(), parameters[0].c_str(), GetPort(t));
117 ServerInstance->Users->QuitUser(t, reason);
123 n_done_s = ConvToStr(n_done);
127 if (redirect_new_users)
128 redirect_to = parameters[0];
130 user->WriteNotice("*** Set jumpserver to server '" + parameters[0] + "' port '" + (port ? ConvToStr(port) : "Auto") + ", SSL " + (sslport ? ConvToStr(sslport) : "Auto") + "', flags '+" +
131 (redirect_all_immediately ? "a" : "") + (redirect_new_users ? "n'" : "'") +
132 (n_done ? " (" + n_done_s + "user(s) redirected): " : ": ") + reason);
138 int GetPort(LocalUser* user)
140 int p = (SSLClientCert::GetCertificate(&user->eh) ? sslport : port);
142 p = user->GetServerPort();
147 class ModuleJumpServer : public Module
149 CommandJumpserver js;
151 ModuleJumpServer() : js(this)
155 void init() CXX11_OVERRIDE
157 ServerInstance->Modules->AddService(js);
160 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
162 if (js.redirect_new_users)
164 int port = js.GetPort(user);
165 user->WriteNumeric(10, "%s %s %d :Please use this Server/Port instead",
166 user->nick.c_str(), js.redirect_to.c_str(), port);
167 ServerInstance->Users->QuitUser(user, js.reason);
168 return MOD_RES_PASSTHRU;
170 return MOD_RES_PASSTHRU;
173 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
175 // Emergency way to unlock
177 js.redirect_new_users = false;
180 Version GetVersion() CXX11_OVERRIDE
182 return Version("Provides support for the RPL_REDIR numeric and the /JUMPSERVER command.", VF_VENDOR);
186 MODULE_INIT(ModuleJumpServer)