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