]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_lockserv.cpp
Minor spelling errors in m_spanningtree.so
[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 /* $ModDesc: Allows locking of the server to stop all incoming connections till unlocked again */
24
25 /** Adds numerics
26  * 988 <nick> <servername> :Closed for new connections
27  * 989 <nick> <servername> :Open for new connections
28 */
29
30
31 class CommandLockserv : public Command
32 {
33         bool& locked;
34 public:
35         CommandLockserv(Module* Creator, bool& lock) : Command(Creator, "LOCKSERV", 0), locked(lock)
36         {
37                 flags_needed = 'o';
38         }
39
40         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
41         {
42                 if (locked)
43                 {
44                         user->WriteServ("NOTICE %s :The server is already locked.", user->nick.c_str());
45                         return CMD_FAILURE;
46                 }
47
48                 locked = true;
49                 user->WriteNumeric(988, "%s %s :Closed for new connections", user->nick.c_str(), user->server.c_str());
50                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used LOCKSERV to temporarily disallow new connections", user->nick.c_str());
51                 return CMD_SUCCESS;
52         }
53 };
54
55 class CommandUnlockserv : public Command
56 {
57 private:
58         bool& locked;
59
60 public:
61         CommandUnlockserv(Module* Creator, bool &lock) : Command(Creator, "UNLOCKSERV", 0), locked(lock)
62         {
63                 flags_needed = 'o';
64         }
65
66         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
67         {
68                 if (!locked)
69                 {
70                         user->WriteServ("NOTICE %s :The server isn't locked.", user->nick.c_str());
71                         return CMD_FAILURE;
72                 }
73
74                 locked = false;
75                 user->WriteNumeric(989, "%s %s :Open for new connections", user->nick.c_str(), user->server.c_str());
76                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used UNLOCKSERV to allow new connections", user->nick.c_str());
77                 return CMD_SUCCESS;
78         }
79 };
80
81 class ModuleLockserv : public Module
82 {
83 private:
84         bool locked;
85         CommandLockserv lockcommand;
86         CommandUnlockserv unlockcommand;
87
88 public:
89         ModuleLockserv() : lockcommand(this, locked), unlockcommand(this, locked)
90         {
91         }
92
93         void init()
94         {
95                 locked = false;
96                 ServerInstance->Modules->AddService(lockcommand);
97                 ServerInstance->Modules->AddService(unlockcommand);
98                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnCheckReady };
99                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
100         }
101
102         virtual ~ModuleLockserv()
103         {
104         }
105
106
107         virtual void OnRehash(User* user)
108         {
109                 // Emergency way to unlock
110                 if (!user) locked = false;
111         }
112
113         virtual ModResult OnUserRegister(LocalUser* user)
114         {
115                 if (locked)
116                 {
117                         ServerInstance->Users->QuitUser(user, "Server is temporarily closed. Please try again later.");
118                         return MOD_RES_DENY;
119                 }
120                 return MOD_RES_PASSTHRU;
121         }
122
123         virtual ModResult OnCheckReady(LocalUser* user)
124         {
125                 return locked ? MOD_RES_DENY : MOD_RES_PASSTHRU;
126         }
127
128         virtual Version GetVersion()
129         {
130                 return Version("Allows locking of the server to stop all incoming connections until unlocked again", VF_VENDOR);
131         }
132 };
133
134 MODULE_INIT(ModuleLockserv)