]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
cb36b3f5111e24a96b17b163d89fa3e5523c10da
[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 #include "modules/invite.h"
29
30 class ModuleOverride : public Module
31 {
32         bool RequireKey;
33         bool NoisyOverride;
34         ChanModeReference topiclock;
35         ChanModeReference inviteonly;
36         ChanModeReference key;
37         ChanModeReference limit;
38         Invite::API invapi;
39
40         static bool IsOverride(unsigned int userlevel, const Modes::ChangeList::List& list)
41         {
42                 for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
43                 {
44                         ModeHandler* mh = i->mh;
45                         if (mh->GetLevelRequired(i->adding) > userlevel)
46                                 return true;
47                 }
48                 return false;
49         }
50
51         ModResult HandleJoinOverride(LocalUser* user, Channel* chan, const std::string& keygiven, const char* bypasswhat, const char* mode)
52         {
53                 if (RequireKey && keygiven != "override")
54                 {
55                         // Can't join normally -- must use a special key to bypass restrictions
56                         user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
57                         return MOD_RES_PASSTHRU;
58                 }
59
60                 if (NoisyOverride)
61                         chan->WriteNotice(InspIRCd::Format("%s used oper override to bypass %s", user->nick.c_str(), bypasswhat));
62                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass " + mode + " on " + chan->name);
63                 return MOD_RES_ALLOW;
64         }
65
66  public:
67         ModuleOverride()
68                 : topiclock(this, "topiclock")
69                 , inviteonly(this, "inviteonly")
70                 , key(this, "key")
71                 , limit(this, "limit")
72                 , invapi(this)
73         {
74         }
75
76         void init() CXX11_OVERRIDE
77         {
78                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
79         }
80
81         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
82         {
83                 // re-read our config options
84                 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
85                 NoisyOverride = tag->getBool("noisy");
86                 RequireKey = tag->getBool("requirekey");
87         }
88
89         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
90         {
91                 tokens["OVERRIDE"];
92         }
93
94         bool CanOverride(User* source, const char* token)
95         {
96                 std::string tokenlist = source->oper->getConfig("override");
97
98                 // its defined or * is set, return its value as a boolean for if the token is set
99                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
100         }
101
102
103         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic) CXX11_OVERRIDE
104         {
105                 if (IS_LOCAL(source) && source->IsOper() && CanOverride(source, "TOPIC"))
106                 {
107                         if (!channel->HasUser(source) || (channel->IsModeSet(topiclock) && channel->GetPrefixValue(source) < HALFOP_VALUE))
108                         {
109                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
110                         }
111
112                         // Explicit allow
113                         return MOD_RES_ALLOW;
114                 }
115
116                 return MOD_RES_PASSTHRU;
117         }
118
119         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
120         {
121                 if (source->IsOper() && CanOverride(source,"KICK"))
122                 {
123                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
124                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE) ||
125                             (memb->chan->GetPrefixValue(source) == HALFOP_VALUE && memb->getRank() == HALFOP_VALUE))
126                         {
127                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
128                                 return MOD_RES_ALLOW;
129                         }
130                 }
131                 return MOD_RES_PASSTHRU;
132         }
133
134         ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
135         {
136                 if (!channel)
137                         return MOD_RES_PASSTHRU;
138                 if (!source->IsOper() || !IS_LOCAL(source))
139                         return MOD_RES_PASSTHRU;
140
141                 const Modes::ChangeList::List& list = modes.getlist();
142                 unsigned int mode = channel->GetPrefixValue(source);
143
144                 if (!IsOverride(mode, list))
145                         return MOD_RES_PASSTHRU;
146
147                 if (CanOverride(source, "MODE"))
148                 {
149                         std::string msg = source->nick + " overriding modes: ";
150
151                         // Construct a MODE string in the old format for sending it as a snotice
152                         std::string params;
153                         char pm = 0;
154                         for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
155                         {
156                                 const Modes::Change& item = *i;
157                                 if (!item.param.empty())
158                                         params.append(1, ' ').append(item.param);
159
160                                 char wanted_pm = (item.adding ? '+' : '-');
161                                 if (wanted_pm != pm)
162                                 {
163                                         pm = wanted_pm;
164                                         msg += pm;
165                                 }
166
167                                 msg += item.mh->GetModeChar();
168                         }
169                         msg += params;
170                         ServerInstance->SNO->WriteGlobalSno('v',msg);
171                         return MOD_RES_ALLOW;
172                 }
173                 return MOD_RES_PASSTHRU;
174         }
175
176         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
177         {
178                 if (user->IsOper())
179                 {
180                         if (chan)
181                         {
182                                 if (chan->IsModeSet(inviteonly) && (CanOverride(user,"INVITE")))
183                                 {
184                                         if (!invapi->IsInvited(user, chan))
185                                                 return HandleJoinOverride(user, chan, keygiven, "invite-only", "+i");
186                                         return MOD_RES_ALLOW;
187                                 }
188
189                                 if (chan->IsModeSet(key) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter(key))
190                                         return HandleJoinOverride(user, chan, keygiven, "the channel key", "+k");
191
192                                 if (chan->IsModeSet(limit) && (chan->GetUserCounter() >= ConvToNum<size_t>(chan->GetModeParameter(limit))) && (CanOverride(user,"LIMIT")))
193                                         return HandleJoinOverride(user, chan, keygiven, "the channel limit", "+l");
194
195                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
196                                         return HandleJoinOverride(user, chan, keygiven, "channel ban", "channel ban");
197                         }
198                 }
199                 return MOD_RES_PASSTHRU;
200         }
201
202         Version GetVersion() CXX11_OVERRIDE
203         {
204                 return Version("Provides support for allowing opers to override certain things",VF_VENDOR);
205         }
206 };
207
208 MODULE_INIT(ModuleOverride)