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