]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Merge pull request #96 from Justasic/insp20
[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 /* $ModDesc: Provides support for unreal-style oper-override */
30
31 class ModuleOverride : public Module
32 {
33         bool RequireKey;
34         bool NoisyOverride;
35
36  public:
37
38         ModuleOverride()
39                         {
40                 // read our config options (main config file)
41                 OnRehash(NULL);
42                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
43                 Implementation eventlist[] = { I_OnRehash, I_OnPreMode, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPreTopicChange };
44                 ServerInstance->Modules->Attach(eventlist, this, 6);
45         }
46
47         void OnRehash(User* user)
48         {
49                 // on a rehash we delete our classes for good measure and create them again.
50                 ConfigReader Conf;
51
52                 // re-read our config options on a rehash
53                 NoisyOverride = Conf.ReadFlag("override", "noisy", 0);
54                 RequireKey = Conf.ReadFlag("override", "requirekey", 0);
55         }
56
57         void On005Numeric(std::string &output)
58         {
59                 output.append(" OVERRIDE");
60         }
61
62         bool CanOverride(User* source, const char* token)
63         {
64                 std::string tokenlist = source->oper->getConfig("override");
65
66                 // its defined or * is set, return its value as a boolean for if the token is set
67                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
68         }
69
70
71         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic)
72         {
73                 if (IS_LOCAL(source) && IS_OPER(source) && CanOverride(source, "TOPIC"))
74                 {
75                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetPrefixValue(source) < HALFOP_VALUE))
76                         {
77                                 ServerInstance->SNO->WriteGlobalSno('v',std::string(source->nick)+" used oper override to change a topic on "+std::string(channel->name));
78                         }
79
80                         // Explicit allow
81                         return MOD_RES_ALLOW;
82                 }
83
84                 return MOD_RES_PASSTHRU;
85         }
86
87         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
88         {
89                 if (IS_OPER(source) && CanOverride(source,"KICK"))
90                 {
91                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
92                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE))
93                         {
94                                 ServerInstance->SNO->WriteGlobalSno('v',std::string(source->nick)+" used oper override to kick "+std::string(memb->user->nick)+" on "+std::string(memb->chan->name)+" ("+reason+")");
95                                 return MOD_RES_ALLOW;
96                         }
97                 }
98                 return MOD_RES_PASSTHRU;
99         }
100
101         ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters)
102         {
103                 if (!source || !channel)
104                         return MOD_RES_PASSTHRU;
105                 if (!IS_OPER(source) || !IS_LOCAL(source))
106                         return MOD_RES_PASSTHRU;
107
108                 unsigned int mode = channel->GetPrefixValue(source);
109
110                 if (mode < HALFOP_VALUE && CanOverride(source, "MODE"))
111                 {
112                         std::string msg = std::string(source->nick)+" overriding modes:";
113                         for(unsigned int i=0; i < parameters.size(); i++)
114                                 msg += " " + parameters[i];
115                         ServerInstance->SNO->WriteGlobalSno('v',msg);
116                         return MOD_RES_ALLOW;
117                 }
118                 return MOD_RES_PASSTHRU;
119         }
120
121         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
122         {
123                 if (IS_LOCAL(user) && IS_OPER(user))
124                 {
125                         if (chan)
126                         {
127                                 if (chan->IsModeSet('i') && (CanOverride(user,"INVITE")))
128                                 {
129                                         irc::string x(chan->name.c_str());
130                                         if (!IS_LOCAL(user)->IsInvited(x))
131                                         {
132                                                 if (RequireKey && keygiven != "override")
133                                                 {
134                                                         // Can't join normally -- must use a special key to bypass restrictions
135                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
136                                                         return MOD_RES_PASSTHRU;
137                                                 }
138
139                                                 if (NoisyOverride)
140                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
141                                                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +i on "+std::string(cname));
142                                         }
143                                         return MOD_RES_ALLOW;
144                                 }
145
146                                 if (chan->IsModeSet('k') && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
147                                 {
148                                         if (RequireKey && keygiven != "override")
149                                         {
150                                                 // Can't join normally -- must use a special key to bypass restrictions
151                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
152                                                 return MOD_RES_PASSTHRU;
153                                         }
154
155                                         if (NoisyOverride)
156                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
157                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +k on "+std::string(cname));
158                                         return MOD_RES_ALLOW;
159                                 }
160
161                                 if (chan->IsModeSet('l') && (chan->GetUserCounter() >= atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
162                                 {
163                                         if (RequireKey && keygiven != "override")
164                                         {
165                                                 // Can't join normally -- must use a special key to bypass restrictions
166                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
167                                                 return MOD_RES_PASSTHRU;
168                                         }
169
170                                         if (NoisyOverride)
171                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
172                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +l on "+std::string(cname));
173                                         return MOD_RES_ALLOW;
174                                 }
175
176                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
177                                 {
178                                         if (RequireKey && keygiven != "override")
179                                         {
180                                                 // Can't join normally -- must use a special key to bypass restrictions
181                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
182                                                 return MOD_RES_PASSTHRU;
183                                         }
184
185                                         if (NoisyOverride)
186                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName.c_str(), "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
187                                         ServerInstance->SNO->WriteGlobalSno('v',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
188                                         return MOD_RES_ALLOW;
189                                 }
190                         }
191                 }
192                 return MOD_RES_PASSTHRU;
193         }
194
195         Version GetVersion()
196         {
197                 return Version("Provides support for unreal-style oper-override",VF_VENDOR);
198         }
199 };
200
201 MODULE_INIT(ModuleOverride)