]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_lockserv.cpp
Add Module* creator to Command and ModeHandler
[user/henk/code/inspircd.git] / src / modules / m_lockserv.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: Allows locking of the server to stop all incoming connections till unlocked again */
17
18 /** Adds numerics
19  * 988 <nick> <servername> :Closed for new connections
20  * 989 <nick> <servername> :Open for new connections
21 */
22
23
24 class CommandLockserv : public Command
25 {
26         bool& locked;
27 public:
28         CommandLockserv (InspIRCd* Instance, Module* Creator, bool& lock)
29                 : Command(Instance, Creator, "LOCKSERV", "o", 0), locked(lock)
30         {
31                 syntax.clear();
32         }
33
34         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
35         {
36                 locked = true;
37                 user->WriteNumeric(988, "%s %s :Closed for new connections", user->nick.c_str(), user->server);
38                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used LOCKSERV to temporarily close for new connections", user->nick.c_str());
39                 /* Dont send to the network */
40                 return CMD_LOCALONLY;
41         }
42 };
43
44 class CommandUnlockserv : public Command
45 {
46 private:
47         bool& locked;
48
49 public:
50         CommandUnlockserv (InspIRCd* Instance, Module* Creator, bool &lock)
51                 : Command(Instance, Creator, "UNLOCKSERV", "o", 0), locked(lock)
52         {
53                 syntax.clear();
54         }
55
56         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
57         {
58                 locked = false;
59                 user->WriteNumeric(989, "%s %s :Open for new connections", user->nick.c_str(), user->server);
60                 ServerInstance->SNO->WriteGlobalSno('a', "Oper %s used UNLOCKSERV to allow for new connections", user->nick.c_str());
61                 /* Dont send to the network */
62                 return CMD_LOCALONLY;
63         }
64 };
65
66 class ModuleLockserv : public Module
67 {
68 private:
69         bool locked;
70         CommandLockserv lockcommand;
71         CommandUnlockserv unlockcommand;
72
73         virtual void ResetLocked()
74         {
75                 locked = false;
76         }
77
78 public:
79         ModuleLockserv(InspIRCd* Me) : Module(Me), lockcommand(Me, this, locked), unlockcommand(Me, this, locked)
80         {
81                 ResetLocked();
82                 ServerInstance->AddCommand(&lockcommand);
83                 ServerInstance->AddCommand(&unlockcommand);
84                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnCheckReady };
85                 ServerInstance->Modules->Attach(eventlist, this, 3);
86         }
87
88         virtual ~ModuleLockserv()
89         {
90         }
91
92
93         virtual void OnRehash(User* user)
94         {
95                 ResetLocked();
96         }
97
98         virtual int OnUserRegister(User* user)
99         {
100                 if (locked)
101                 {
102                         ServerInstance->Users->QuitUser(user, "Server is temporarily closed. Please try again later.");
103                         return 1;
104                 }
105                 return 0;
106         }
107
108         virtual bool OnCheckReady(User* user)
109         {
110                 return !locked;
111         }
112
113         virtual Version GetVersion()
114         {
115                 return Version("$Id$", VF_VENDOR, API_VERSION);
116         }
117 };
118
119 MODULE_INIT(ModuleLockserv)