2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
24 #include "modules/regex.h"
25 #include "modules/stats.h"
28 static bool ZlineOnMatch = false;
29 static bool added_zline = false;
31 class RLine : public XLine
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
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")
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
50 regex = rxfactory->Create(regexs);
60 bool Matches(User* u) CXX11_OVERRIDE
62 LocalUser* lu = IS_LOCAL(u);
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));
71 bool Matches(const std::string& compare) CXX11_OVERRIDE
73 return regex->Matches(compare);
76 void Apply(User* u) CXX11_OVERRIDE
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))
83 std::string timestr = InspIRCd::TimeString(zl->expiry);
84 ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on %s%s%s: %s",
85 zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? timestr.c_str() : "", zl->reason.c_str());
91 DefaultApply(u, "R", false);
94 const std::string& Displayable() CXX11_OVERRIDE
99 std::string matchtext;
105 /** An XLineFactory specialized to generate RLine* pointers
107 class RLineFactory : public XLineFactory
110 dynamic_reference<RegexFactory>& rxfactory;
111 RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
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
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!");
125 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
130 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
132 class CommandRLine : public Command
134 std::string rxengine;
135 RLineFactory& factory;
138 CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
140 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
143 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
146 if (parameters.size() >= 3)
148 // Adding - XXX todo make this respect <insane> tag perhaps..
150 unsigned long duration;
151 if (!InspIRCd::Duration(parameters[1], duration))
153 user->WriteNotice("*** Invalid duration for R-line.");
160 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
162 catch (ModuleException &e)
164 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: " + e.GetReason());
169 if (ServerInstance->XLines->AddLine(r, user))
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());
177 time_t c_requires_crap = duration + ServerInstance->Time();
178 std::string timestr = InspIRCd::TimeString(c_requires_crap);
179 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());
182 ServerInstance->XLines->ApplyLines();
187 user->WriteNotice("*** R-line for " + parameters[0] + " already exists.");
193 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
195 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
199 user->WriteNotice("*** R-line " + parameters[0] + " not found in list, try /stats R.");
206 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
209 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
211 return ROUTE_BROADCAST;
215 class ModuleRLine : public Module, public Stats::EventListener
217 dynamic_reference<RegexFactory> rxfactory;
220 bool MatchOnNickChange;
222 RegexFactory* factory;
226 : Stats::EventListener(this)
227 , rxfactory(this, "regex")
234 void init() CXX11_OVERRIDE
236 ServerInstance->XLines->RegisterFactory(&f);
241 ServerInstance->XLines->DelAll("R");
242 ServerInstance->XLines->UnregisterFactory(&f);
245 Version GetVersion() CXX11_OVERRIDE
247 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
250 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
252 // Apply lines on user connect
253 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
261 return MOD_RES_PASSTHRU;
264 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
266 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
268 MatchOnNickChange = tag->getBool("matchonnickchange");
269 ZlineOnMatch = tag->getBool("zlineonmatch");
270 std::string newrxengine = tag->getString("engine");
272 factory = rxfactory ? (rxfactory.operator->()) : NULL;
274 if (newrxengine.empty())
275 rxfactory.SetProvider("regex");
277 rxfactory.SetProvider("regex/" + newrxengine);
281 if (newrxengine.empty())
282 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-line functionality disabled until this is corrected.");
284 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-line functionality disabled until this is corrected.", newrxengine.c_str());
286 ServerInstance->XLines->DelAll(f.GetType());
288 else if ((!initing) && (rxfactory.operator->() != factory))
290 ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-lines.");
291 ServerInstance->XLines->DelAll(f.GetType());
297 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
299 if (stats.GetSymbol() != 'R')
300 return MOD_RES_PASSTHRU;
302 ServerInstance->XLines->InvokeStats("R", 223, stats);
306 void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
311 if (!MatchOnNickChange)
314 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
323 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
328 ServerInstance->XLines->ApplyLines();
332 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
334 // If the regex engine became unavailable or has changed, remove all R-lines.
337 ServerInstance->XLines->DelAll(f.GetType());
339 else if (rxfactory.operator->() != factory)
341 factory = rxfactory.operator->();
342 ServerInstance->XLines->DelAll(f.GetType());
346 void Prioritize() CXX11_OVERRIDE
348 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
349 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
353 MODULE_INIT(ModuleRLine)