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