]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Introduce Stats::Context, pass it to the OnStats hook and switch all code to it
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/regex.h"
25 #include "xline.h"
26
27 static bool ZlineOnMatch = false;
28 static bool added_zline = false;
29
30 class RLine : public XLine
31 {
32  public:
33
34         /** Create a R-Line.
35          * @param s_time The set time
36          * @param d The duration of the xline
37          * @param src The sender of the xline
38          * @param re The reason of the xline
39          * @param regex Pattern to match with
40          * @
41          */
42         RLine(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference<RegexFactory>& rxfactory)
43                 : XLine(s_time, d, src, re, "R")
44                 , matchtext(regexs)
45         {
46                 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
47                  * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
48                  */
49                 regex = rxfactory->Create(regexs);
50         }
51
52         /** Destructor
53          */
54         ~RLine()
55         {
56                 delete regex;
57         }
58
59         bool Matches(User *u)
60         {
61                 LocalUser* lu = IS_LOCAL(u);
62                 if (lu && lu->exempt)
63                         return false;
64
65                 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
66                 return regex->Matches(compare);
67         }
68
69         bool Matches(const std::string &compare)
70         {
71                 return regex->Matches(compare);
72         }
73
74         void Apply(User* u)
75         {
76                 if (ZlineOnMatch)
77                 {
78                         ZLine* zl = new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString());
79                         if (ServerInstance->XLines->AddLine(zl, NULL))
80                         {
81                                 std::string timestr = InspIRCd::TimeString(zl->expiry);
82                                 ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on *@%s%s%s: %s",
83                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? timestr.c_str() : "", zl->reason.c_str());
84                                 added_zline = true;
85                         }
86                         else
87                                 delete zl;
88                 }
89                 DefaultApply(u, "R", false);
90         }
91
92         const std::string& Displayable()
93         {
94                 return matchtext;
95         }
96
97         std::string matchtext;
98
99         Regex *regex;
100 };
101
102
103 /** An XLineFactory specialized to generate RLine* pointers
104  */
105 class RLineFactory : public XLineFactory
106 {
107  public:
108         dynamic_reference<RegexFactory>& rxfactory;
109         RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
110         {
111         }
112
113         /** Generate a RLine
114          */
115         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
116         {
117                 if (!rxfactory)
118                 {
119                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
120                         throw ModuleException("Regex engine not set or loaded!");
121                 }
122
123                 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
124         }
125 };
126
127 /** Handle /RLINE
128  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
129  */
130 class CommandRLine : public Command
131 {
132         std::string rxengine;
133         RLineFactory& factory;
134
135  public:
136         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
137         {
138                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
139         }
140
141         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
142         {
143
144                 if (parameters.size() >= 3)
145                 {
146                         // Adding - XXX todo make this respect <insane> tag perhaps..
147
148                         unsigned long duration = InspIRCd::Duration(parameters[1]);
149                         XLine *r = NULL;
150
151                         try
152                         {
153                                 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
154                         }
155                         catch (ModuleException &e)
156                         {
157                                 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: " + e.GetReason());
158                         }
159
160                         if (r)
161                         {
162                                 if (ServerInstance->XLines->AddLine(r, user))
163                                 {
164                                         if (!duration)
165                                         {
166                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
167                                         }
168                                         else
169                                         {
170                                                 time_t c_requires_crap = duration + ServerInstance->Time();
171                                                 std::string timestr = InspIRCd::TimeString(c_requires_crap);
172                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), timestr.c_str(), parameters[2].c_str());
173                                         }
174
175                                         ServerInstance->XLines->ApplyLines();
176                                 }
177                                 else
178                                 {
179                                         delete r;
180                                         user->WriteNotice("*** R-Line for " + parameters[0] + " already exists");
181                                 }
182                         }
183                 }
184                 else
185                 {
186                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
187                         {
188                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
189                         }
190                         else
191                         {
192                                 user->WriteNotice("*** R-Line " + parameters[0] + " not found in list, try /stats R.");
193                         }
194                 }
195
196                 return CMD_SUCCESS;
197         }
198
199         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
200         {
201                 if (IS_LOCAL(user))
202                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
203
204                 return ROUTE_BROADCAST;
205         }
206 };
207
208 class ModuleRLine : public Module
209 {
210         dynamic_reference<RegexFactory> rxfactory;
211         RLineFactory f;
212         CommandRLine r;
213         bool MatchOnNickChange;
214         bool initing;
215         RegexFactory* factory;
216
217  public:
218         ModuleRLine()
219                 : rxfactory(this, "regex"), f(rxfactory), r(this, f)
220                 , initing(true)
221         {
222         }
223
224         void init() CXX11_OVERRIDE
225         {
226                 ServerInstance->XLines->RegisterFactory(&f);
227         }
228
229         ~ModuleRLine()
230         {
231                 ServerInstance->XLines->DelAll("R");
232                 ServerInstance->XLines->UnregisterFactory(&f);
233         }
234
235         Version GetVersion() CXX11_OVERRIDE
236         {
237                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
238         }
239
240         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
241         {
242                 // Apply lines on user connect
243                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
244
245                 if (rl)
246                 {
247                         // Bang. :P
248                         rl->Apply(user);
249                         return MOD_RES_DENY;
250                 }
251                 return MOD_RES_PASSTHRU;
252         }
253
254         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
255         {
256                 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
257
258                 MatchOnNickChange = tag->getBool("matchonnickchange");
259                 ZlineOnMatch = tag->getBool("zlineonmatch");
260                 std::string newrxengine = tag->getString("engine");
261
262                 factory = rxfactory ? (rxfactory.operator->()) : NULL;
263
264                 if (newrxengine.empty())
265                         rxfactory.SetProvider("regex");
266                 else
267                         rxfactory.SetProvider("regex/" + newrxengine);
268
269                 if (!rxfactory)
270                 {
271                         if (newrxengine.empty())
272                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-Line functionality disabled until this is corrected.");
273                         else
274                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
275
276                         ServerInstance->XLines->DelAll(f.GetType());
277                 }
278                 else if ((!initing) && (rxfactory.operator->() != factory))
279                 {
280                         ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-Lines");
281                         ServerInstance->XLines->DelAll(f.GetType());
282                 }
283
284                 initing = false;
285         }
286
287         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
288         {
289                 if (stats.GetSymbol() != 'R')
290                         return MOD_RES_PASSTHRU;
291
292                 ServerInstance->XLines->InvokeStats("R", 223, stats);
293                 return MOD_RES_DENY;
294         }
295
296         void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
297         {
298                 if (!IS_LOCAL(user))
299                         return;
300
301                 if (!MatchOnNickChange)
302                         return;
303
304                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
305
306                 if (rl)
307                 {
308                         // Bang! :D
309                         rl->Apply(user);
310                 }
311         }
312
313         void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
314         {
315                 if (added_zline)
316                 {
317                         added_zline = false;
318                         ServerInstance->XLines->ApplyLines();
319                 }
320         }
321
322         void OnUnloadModule(Module* mod) CXX11_OVERRIDE
323         {
324                 // If the regex engine became unavailable or has changed, remove all rlines
325                 if (!rxfactory)
326                 {
327                         ServerInstance->XLines->DelAll(f.GetType());
328                 }
329                 else if (rxfactory.operator->() != factory)
330                 {
331                         factory = rxfactory.operator->();
332                         ServerInstance->XLines->DelAll(f.GetType());
333                 }
334         }
335
336         void Prioritize()
337         {
338                 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
339                 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
340         }
341 };
342
343 MODULE_INIT(ModuleRLine)