]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_jumpserver.cpp
Replace hardcoded mode letters, part 3
[user/henk/code/inspircd.git] / src / modules / m_jumpserver.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /** Handle /JUMPSERVER
25  */
26 class CommandJumpserver : public Command
27 {
28  public:
29         bool redirect_new_users;
30         std::string redirect_to;
31         std::string reason;
32         int port;
33
34         CommandJumpserver(Module* Creator) : Command(Creator, "JUMPSERVER", 0, 4)
35         {
36                 flags_needed = 'o'; syntax = "[<server> <port> <+/-an> <reason>]";
37                 port = 0;
38                 redirect_new_users = false;
39         }
40
41         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
42         {
43                 int n_done = 0;
44                 reason = (parameters.size() < 4) ? "Please use this server/port instead" : parameters[3];
45                 bool redirect_all_immediately = false;
46                 redirect_new_users = true;
47                 bool direction = true;
48                 std::string n_done_s;
49
50                 /* No parameters: jumpserver disabled */
51                 if (!parameters.size())
52                 {
53                         if (port)
54                                 user->WriteNotice("*** Disabled jumpserver (previously set to '" + redirect_to + ":" + ConvToStr(port) + "')");
55                         else
56                                 user->WriteNotice("*** Jumpserver was not enabled.");
57
58                         port = 0;
59                         redirect_to.clear();
60                         return CMD_SUCCESS;
61                 }
62
63                 port = 0;
64                 redirect_to.clear();
65
66                 if (parameters.size() >= 3)
67                 {
68                         for (std::string::const_iterator n = parameters[2].begin(); n != parameters[2].end(); ++n)
69                         {
70                                 switch (*n)
71                                 {
72                                         case '+':
73                                                 direction = true;
74                                         break;
75                                         case '-':
76                                                 direction = false;
77                                         break;
78                                         case 'a':
79                                                 redirect_all_immediately = direction;
80                                         break;
81                                         case 'n':
82                                                 redirect_new_users = direction;
83                                         break;
84                                         default:
85                                                 user->WriteNotice("*** Invalid JUMPSERVER flag: " + ConvToStr(*n));
86                                                 return CMD_FAILURE;
87                                         break;
88                                 }
89                         }
90
91                         if (!atoi(parameters[1].c_str()))
92                         {
93                                 user->WriteNotice("*** Invalid port number");
94                                 return CMD_FAILURE;
95                         }
96
97                         if (redirect_all_immediately)
98                         {
99                                 /* Redirect everyone but the oper sending the command */
100                                 for (LocalUserList::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); ++i)
101                                 {
102                                         User* t = *i;
103                                         if (!t->IsOper())
104                                         {
105                                                 t->WriteNumeric(10, "%s %s %s :Please use this Server/Port instead", t->nick.c_str(), parameters[0].c_str(), parameters[1].c_str());
106                                                 ServerInstance->Users->QuitUser(t, reason);
107                                                 n_done++;
108                                         }
109                                 }
110                                 if (n_done)
111                                 {
112                                         n_done_s = ConvToStr(n_done);
113                                 }
114                         }
115
116                         if (redirect_new_users)
117                         {
118                                 redirect_to = parameters[0];
119                                 port = atoi(parameters[1].c_str());
120                         }
121
122                         user->WriteNotice("*** Set jumpserver to server '" + parameters[0] + "' port '" + parameters[1] + "', flags '+" +
123                                 (redirect_all_immediately ? "a" : "") + (redirect_new_users ? "n'" : "'") +
124                                 (n_done ? " (" + n_done_s + "user(s) redirected): " : ": ") + reason);
125                 }
126
127                 return CMD_SUCCESS;
128         }
129 };
130
131 class ModuleJumpServer : public Module
132 {
133         CommandJumpserver js;
134  public:
135         ModuleJumpServer() : js(this)
136         {
137         }
138
139         void init() CXX11_OVERRIDE
140         {
141                 ServerInstance->Modules->AddService(js);
142                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash };
143                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
144         }
145
146         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
147         {
148                 if (js.port && js.redirect_new_users)
149                 {
150                         user->WriteNumeric(10, "%s %s %d :Please use this Server/Port instead",
151                                 user->nick.c_str(), js.redirect_to.c_str(), js.port);
152                         ServerInstance->Users->QuitUser(user, js.reason);
153                         return MOD_RES_PASSTHRU;
154                 }
155                 return MOD_RES_PASSTHRU;
156         }
157
158         void OnRehash(User* user) CXX11_OVERRIDE
159         {
160                 // Emergency way to unlock
161                 if (!user) js.redirect_new_users = false;
162         }
163
164         Version GetVersion() CXX11_OVERRIDE
165         {
166                 return Version("Provides support for the RPL_REDIR numeric and the /JUMPSERVER command.", VF_VENDOR);
167         }
168 };
169
170 MODULE_INIT(ModuleJumpServer)