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