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