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