]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Merge pull request #574 from SaberUK/master+build-comment-cleanup
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6  *   Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
9  *   Copyright (C) 2008 Geoff Bricker <geoff.bricker@gmail.com>
10  *   Copyright (C) 2004-2006 Craig Edwards <craigedwards@brainbox.cc>
11  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28
29 class ModuleOverride : public Module
30 {
31         bool RequireKey;
32         bool NoisyOverride;
33
34  public:
35
36         void init() CXX11_OVERRIDE
37         {
38                 // read our config options (main config file)
39                 OnRehash(NULL);
40                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
41                 Implementation eventlist[] = { I_OnRehash, I_OnPreMode, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPreTopicChange };
42                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
43         }
44
45         void OnRehash(User* user) CXX11_OVERRIDE
46         {
47                 // re-read our config options on a rehash
48                 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
49                 NoisyOverride = tag->getBool("noisy");
50                 RequireKey = tag->getBool("requirekey");
51         }
52
53         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
54         {
55                 tokens["OVERRIDE"];
56         }
57
58         bool CanOverride(User* source, const char* token)
59         {
60                 std::string tokenlist = source->oper->getConfig("override");
61
62                 // its defined or * is set, return its value as a boolean for if the token is set
63                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
64         }
65
66
67         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic) CXX11_OVERRIDE
68         {
69                 if (IS_LOCAL(source) && source->IsOper() && CanOverride(source, "TOPIC"))
70                 {
71                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetPrefixValue(source) < HALFOP_VALUE))
72                         {
73                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
74                         }
75
76                         // Explicit allow
77                         return MOD_RES_ALLOW;
78                 }
79
80                 return MOD_RES_PASSTHRU;
81         }
82
83         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
84         {
85                 if (source->IsOper() && CanOverride(source,"KICK"))
86                 {
87                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
88                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE))
89                         {
90                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
91                                 return MOD_RES_ALLOW;
92                         }
93                 }
94                 return MOD_RES_PASSTHRU;
95         }
96
97         ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
98         {
99                 if (!source || !channel)
100                         return MOD_RES_PASSTHRU;
101                 if (!source->IsOper() || !IS_LOCAL(source))
102                         return MOD_RES_PASSTHRU;
103
104                 unsigned int mode = channel->GetPrefixValue(source);
105
106                 if (mode < HALFOP_VALUE && CanOverride(source, "MODE"))
107                 {
108                         std::string msg = source->nick+" overriding modes:";
109                         for(unsigned int i=0; i < parameters.size(); i++)
110                                 msg += " " + parameters[i];
111                         ServerInstance->SNO->WriteGlobalSno('v',msg);
112                         return MOD_RES_ALLOW;
113                 }
114                 return MOD_RES_PASSTHRU;
115         }
116
117         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
118         {
119                 if (user->IsOper())
120                 {
121                         if (chan)
122                         {
123                                 if (chan->IsModeSet('i') && (CanOverride(user,"INVITE")))
124                                 {
125                                         if (!IS_LOCAL(user)->IsInvited(chan))
126                                         {
127                                                 if (RequireKey && keygiven != "override")
128                                                 {
129                                                         // Can't join normally -- must use a special key to bypass restrictions
130                                                         user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
131                                                         return MOD_RES_PASSTHRU;
132                                                 }
133
134                                                 if (NoisyOverride)
135                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname.c_str(), user->nick.c_str());
136                                                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +i on " + cname);
137                                         }
138                                         return MOD_RES_ALLOW;
139                                 }
140
141                                 if (chan->IsModeSet('k') && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
142                                 {
143                                         if (RequireKey && keygiven != "override")
144                                         {
145                                                 // Can't join normally -- must use a special key to bypass restrictions
146                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
147                                                 return MOD_RES_PASSTHRU;
148                                         }
149
150                                         if (NoisyOverride)
151                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname.c_str(), user->nick.c_str());
152                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +k on " + cname);
153                                         return MOD_RES_ALLOW;
154                                 }
155
156                                 if (chan->IsModeSet('l') && (chan->GetUserCounter() >= ConvToInt(chan->GetModeParameter('l'))) && (CanOverride(user,"LIMIT")))
157                                 {
158                                         if (RequireKey && keygiven != "override")
159                                         {
160                                                 // Can't join normally -- must use a special key to bypass restrictions
161                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
162                                                 return MOD_RES_PASSTHRU;
163                                         }
164
165                                         if (NoisyOverride)
166                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname.c_str(), user->nick.c_str());
167                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +l on " + cname);
168                                         return MOD_RES_ALLOW;
169                                 }
170
171                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
172                                 {
173                                         if (RequireKey && keygiven != "override")
174                                         {
175                                                 // Can't join normally -- must use a special key to bypass restrictions
176                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
177                                                 return MOD_RES_PASSTHRU;
178                                         }
179
180                                         if (NoisyOverride)
181                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname.c_str(), user->nick.c_str());
182                                         ServerInstance->SNO->WriteGlobalSno('v',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname.c_str());
183                                         return MOD_RES_ALLOW;
184                                 }
185                         }
186                 }
187                 return MOD_RES_PASSTHRU;
188         }
189
190         Version GetVersion() CXX11_OVERRIDE
191         {
192                 return Version("Provides support for allowing opers to override certain things",VF_VENDOR);
193         }
194 };
195
196 MODULE_INIT(ModuleOverride)