]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_jumpserver.cpp
Another big commit, just to please all my fans out there.. cmd_* -> Command*. Muahaha.
[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 "inspircd.h"
15
16 /* $ModDesc: Provides support for unreal-style SAPART command */
17
18 /** Handle /SAPART
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) : Command(Instance, "JUMPSERVER", 'o', 0)
31         {
32                 this->source = "m_jumpserver.so";
33                 syntax = "[<server> <port> <+/-a> :<reason>]";
34                 redirect_to.clear();
35                 reason.clear();
36                 port = 0;
37                 redirect_all_immediately = redirect_new_users = false;
38         }
39
40         CmdResult Handle (const char** parameters, int pcnt, User *user)
41         {
42                 int n_done = 0;
43                 reason = (pcnt < 4) ? "Please use this server/port instead" : parameters[3];
44                 redirect_all_immediately = false;
45                 redirect_new_users = true;
46                 direction = true;
47                 std::string n_done_s;
48
49                 /* No parameters: jumpserver disabled */
50                 if (!pcnt)
51                 {
52                         if (port)
53                                 user->WriteServ("NOTICE %s :*** Disabled jumpserver (previously set to '%s:%d')", user->nick, redirect_to.c_str(), port);
54                         else
55                                 user->WriteServ("NOTICE %s :*** jumpserver was not enabled.", user->nick);
56
57                         port = 0;
58                         redirect_to.clear();
59                         return CMD_LOCALONLY;
60                 }
61
62                 port = 0;
63                 redirect_to.clear();
64
65                 for (const char* n = parameters[2]; *n; n++)
66                 {
67                         switch (*n)
68                         {
69                                 case '+':
70                                         direction = true;
71                                 break;
72                                 case '-':
73                                         direction = false;
74                                 break;
75                                 case 'a':
76                                         redirect_all_immediately = direction;
77                                 break;
78                                 case 'n':
79                                         redirect_new_users = direction;
80                                 break;
81                         }
82                 }
83
84                 if (redirect_all_immediately)
85                 {
86                         /* Redirect everyone but the oper sending the command */
87                         for (std::vector<User*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
88                         {
89                                 User* t = *i;
90                                 if (!IS_OPER(t))
91                                 {
92                                         t->WriteServ("010 %s %s %s :Please use this Server/Port instead", user->nick, parameters[0], parameters[1]);
93                                         User::QuitUser(ServerInstance, t, reason);
94                                         n_done++;
95                                 }
96                         }
97                         if (n_done)
98                         {
99                                 n_done_s = ConvToStr(n_done);
100                         }
101                 }
102
103                 if (redirect_new_users)
104                 {
105                         redirect_to = parameters[0];
106                         port = atoi(parameters[1]);
107                 }
108
109                 user->WriteServ("NOTICE %s :*** Set jumpserver to server '%s' port '%s', flags '+%s%s'%s%s%s: %s", user->nick, parameters[0], parameters[1],
110                                 redirect_all_immediately ? "a" : "",
111                                 redirect_new_users ? "n" : "",
112                                 n_done ? " (" : "",
113                                 n_done ? n_done_s.c_str() : "",
114                                 n_done ? " user(s) redirected)" : "",
115                                 reason.c_str());
116
117                 return CMD_LOCALONLY;
118         }
119 };
120
121
122 class ModuleJumpServer : public Module
123 {
124         CommandJumpserver*      js;
125  public:
126         ModuleJumpServer(InspIRCd* Me)
127                 : Module(Me)
128         {
129                 
130                 js = new CommandJumpserver(ServerInstance);
131                 ServerInstance->AddCommand(js);
132         }
133         
134         virtual ~ModuleJumpServer()
135         {
136         }
137
138         virtual int OnUserRegister(User* user)
139         {
140                 if (js->port && js->redirect_new_users)
141                 {
142                         user->WriteServ("010 %s %s %d :Please use this Server/Port instead", user->nick, js->redirect_to.c_str(), js->port);
143                         User::QuitUser(ServerInstance, user, js->reason);
144                         return 0;
145                 }
146                 return 0;
147         }
148
149         virtual void Implements(char* List)
150         {
151                 List[I_OnUserRegister] = 1;
152         }
153
154         virtual Version GetVersion()
155         {
156                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
157         }
158         
159 };
160
161 MODULE_INIT(ModuleJumpServer)