]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
92f0f8efcbb3a83a4ec70b19de7621e36681be58
[user/henk/code/inspircd.git] / src / modules / m_override.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 #include "m_override.h"
16
17 /* $ModDesc: Provides support for unreal-style oper-override */
18
19 typedef std::map<std::string,std::string> override_t;
20
21 class ModuleOverride : public Module
22 {
23         override_t overrides;
24         bool RequireKey;
25         bool NoisyOverride;
26
27  public:
28
29         ModuleOverride(InspIRCd* Me)
30                 : Module(Me)
31         {
32                 // read our config options (main config file)
33                 OnRehash(NULL);
34                 ServerInstance->SNO->EnableSnomask('G', "GODMODE");
35                 if (!ServerInstance->Modules->PublishFeature("Override", this))
36                 {
37                         throw ModuleException("m_override: Unable to publish feature 'Override'");
38                 }
39                 Implementation eventlist[] = { I_OnRehash, I_OnPreMode, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPreTopicChange, I_OnRequest };
40                 ServerInstance->Modules->Attach(eventlist, this, 7);
41         }
42
43         void OnRehash(User* user)
44         {
45                 // on a rehash we delete our classes for good measure and create them again.
46                 ConfigReader Conf(ServerInstance);
47
48                 // re-read our config options on a rehash
49                 NoisyOverride = Conf.ReadFlag("override", "noisy", 0);
50                 RequireKey = Conf.ReadFlag("override", "requirekey", 0);
51
52                 overrides.clear();
53
54                 for (int j =0; j < Conf.Enumerate("type"); j++)
55                 {
56                         std::string typen = Conf.ReadValue("type","name",j);
57                         std::string tokenlist = Conf.ReadValue("type","override",j);
58                         overrides[typen] = tokenlist;
59                 }
60         }
61
62         void On005Numeric(std::string &output)
63         {
64                 output.append(" OVERRIDE");
65         }
66
67         bool CanOverride(User* source, const char* token)
68         {
69                 // checks to see if the oper's type has <type:override>
70                 override_t::iterator j = overrides.find(source->oper);
71
72                 if (j != overrides.end())
73                 {
74                         // its defined or * is set, return its value as a boolean for if the token is set
75                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
76                 }
77
78                 // its not defined at all, count as false
79                 return false;
80         }
81
82
83         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic)
84         {
85                 if (IS_LOCAL(source) && IS_OPER(source) && CanOverride(source, "TOPIC"))
86                 {
87                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetPrefixValue(source) < HALFOP_VALUE))
88                         {
89                                 ServerInstance->SNO->WriteGlobalSno('G',std::string(source->nick)+" used oper override to change a topic on "+std::string(channel->name));
90                         }
91
92                         // Explicit allow
93                         return MOD_RES_ALLOW;
94                 }
95
96                 return MOD_RES_PASSTHRU;
97         }
98
99         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
100         {
101                 if (IS_OPER(source) && CanOverride(source,"KICK"))
102                 {
103                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
104                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE))
105                         {
106                                 ServerInstance->SNO->WriteGlobalSno('G',std::string(source->nick)+" used oper override to kick "+std::string(memb->user->nick)+" on "+std::string(memb->chan->name)+" ("+reason+")");
107                                 return MOD_RES_ALLOW;
108                         }
109                 }
110                 return MOD_RES_PASSTHRU;
111         }
112
113         ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters)
114         {
115                 if (!IS_OPER(source))
116                         return MOD_RES_PASSTHRU;
117                 if (!source || !channel)
118                         return MOD_RES_PASSTHRU;
119
120                 unsigned int mode = 0;
121                 if (channel->HasUser(source))
122                         mode = channel->GetPrefixValue(source);
123
124                 if (mode < HALFOP_VALUE && CanOverride(source, "MODE"))
125                 {
126                         std::string msg = std::string(source->nick)+" overriding modes:";
127                         for(unsigned int i=0; i < parameters.size(); i++)
128                                 msg += " " + parameters[i];
129                         ServerInstance->SNO->WriteGlobalSno('G',msg);
130                         return MOD_RES_ALLOW;
131                 }
132                 return MOD_RES_PASSTHRU;
133         }
134
135         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
136         {
137                 if (IS_LOCAL(user) && IS_OPER(user))
138                 {
139                         if (chan)
140                         {
141                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
142                                 {
143                                         irc::string x(chan->name.c_str());
144                                         if (!user->IsInvited(x))
145                                         {
146                                                 if (RequireKey && keygiven != "override")
147                                                 {
148                                                         // Can't join normally -- must use a special key to bypass restrictions
149                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
150                                                         return MOD_RES_PASSTHRU;
151                                                 }
152
153                                                 if (NoisyOverride)
154                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
155                                                 ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +i on "+std::string(cname));
156                                         }
157                                         return MOD_RES_ALLOW;
158                                 }
159
160                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
161                                 {
162                                         if (RequireKey && keygiven != "override")
163                                         {
164                                                 // Can't join normally -- must use a special key to bypass restrictions
165                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
166                                                 return MOD_RES_PASSTHRU;
167                                         }
168
169                                         if (NoisyOverride)
170                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
171                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +k on "+std::string(cname));
172                                         return MOD_RES_ALLOW;
173                                 }
174
175                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >= atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
176                                 {
177                                         if (RequireKey && keygiven != "override")
178                                         {
179                                                 // Can't join normally -- must use a special key to bypass restrictions
180                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
181                                                 return MOD_RES_PASSTHRU;
182                                         }
183
184                                         if (NoisyOverride)
185                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
186                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
187                                         return MOD_RES_ALLOW;
188                                 }
189
190                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
191                                 {
192                                         if (RequireKey && keygiven != "override")
193                                         {
194                                                 // Can't join normally -- must use a special key to bypass restrictions
195                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
196                                                 return MOD_RES_PASSTHRU;
197                                         }
198
199                                         if (NoisyOverride)
200                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
201                                         ServerInstance->SNO->WriteGlobalSno('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
202                                         return MOD_RES_ALLOW;
203                                 }
204                         }
205                 }
206                 return MOD_RES_PASSTHRU;
207         }
208
209         const char* OnRequest(Request* request)
210         {
211                 if(strcmp(OVRREQID, request->GetId()) == 0)
212                 {
213                         OVRrequest* req = static_cast<OVRrequest*>(request);
214                         return this->CanOverride(req->requser,req->reqtoken.c_str()) ? "yes":"";
215                 }
216                 return NULL;
217         }
218
219         ~ModuleOverride()
220         {
221                 ServerInstance->Modules->UnpublishFeature("Override");
222                 ServerInstance->SNO->DisableSnomask('G');
223         }
224
225         Version GetVersion()
226         {
227                 return Version("Provides support for unreal-style oper-override",VF_VENDOR,API_VERSION);
228         }
229 };
230
231 MODULE_INIT(ModuleOverride)