]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_lockserv.cpp
Remove $Core and $Mod* comments apart from $ModDep.
[user/henk/code/inspircd.git] / src / modules / m_lockserv.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2006-2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /** Adds numerics
24  * 988 <nick> <servername> :Closed for new connections
25  * 989 <nick> <servername> :Open for new connections
26  */
27
28 class CommandLockserv : public Command
29 {
30         bool& locked;
31
32  public:
33         CommandLockserv(Module* Creator, bool& lock) : Command(Creator, "LOCKSERV", 0), locked(lock)
34         {
35                 flags_needed = 'o';
36         }
37
38         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
39         {
40                 if (locked)
41                 {
42                         user->WriteNotice("The server is already locked.");
43                         return CMD_FAILURE;
44                 }
45
46                 locked = true;
47                 user->WriteNumeric(988, "%s %s :Closed for new connections", user->nick.c_str(), user->server.c_str());
48                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used LOCKSERV to temporarily disallow new connections", user->nick.c_str());
49                 return CMD_SUCCESS;
50         }
51 };
52
53 class CommandUnlockserv : public Command
54 {
55         bool& locked;
56
57  public:
58         CommandUnlockserv(Module* Creator, bool &lock) : Command(Creator, "UNLOCKSERV", 0), locked(lock)
59         {
60                 flags_needed = 'o';
61         }
62
63         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
64         {
65                 if (!locked)
66                 {
67                         user->WriteNotice("The server isn't locked.");
68                         return CMD_FAILURE;
69                 }
70
71                 locked = false;
72                 user->WriteNumeric(989, "%s %s :Open for new connections", user->nick.c_str(), user->server.c_str());
73                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used UNLOCKSERV to allow new connections", user->nick.c_str());
74                 return CMD_SUCCESS;
75         }
76 };
77
78 class ModuleLockserv : public Module
79 {
80         bool locked;
81         CommandLockserv lockcommand;
82         CommandUnlockserv unlockcommand;
83
84  public:
85         ModuleLockserv() : lockcommand(this, locked), unlockcommand(this, locked)
86         {
87         }
88
89         void init() CXX11_OVERRIDE
90         {
91                 locked = false;
92                 ServerInstance->Modules->AddService(lockcommand);
93                 ServerInstance->Modules->AddService(unlockcommand);
94                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnCheckReady };
95                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
96         }
97
98         void OnRehash(User* user) CXX11_OVERRIDE
99         {
100                 // Emergency way to unlock
101                 if (!user) locked = false;
102         }
103
104         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
105         {
106                 if (locked)
107                 {
108                         ServerInstance->Users->QuitUser(user, "Server is temporarily closed. Please try again later.");
109                         return MOD_RES_DENY;
110                 }
111                 return MOD_RES_PASSTHRU;
112         }
113
114         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
115         {
116                 return locked ? MOD_RES_DENY : MOD_RES_PASSTHRU;
117         }
118
119         Version GetVersion() CXX11_OVERRIDE
120         {
121                 return Version("Allows locking of the server to stop all incoming connections until unlocked again", VF_VENDOR);
122         }
123 };
124
125 MODULE_INIT(ModuleLockserv)