2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2017, 2019 Matt Schatz <genius3000@g3k.solutions>
6 * Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012-2013, 2016 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 2018-2019 Robby <robby@chatbelgie.be>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
12 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
13 * Copyright (C) 2008 Craig Edwards <brain@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "modules/regex.h"
31 #include "modules/stats.h"
34 static bool ZlineOnMatch = false;
35 static bool added_zline = false;
37 class RLine : public XLine
42 * @param s_time The set time
43 * @param d The duration of the xline
44 * @param src The sender of the xline
45 * @param re The reason of the xline
46 * @param regex Pattern to match with
49 RLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference<RegexFactory>& rxfactory)
50 : XLine(s_time, d, src, re, "R")
53 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
54 * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
56 regex = rxfactory->Create(regexs);
66 bool Matches(User* u) CXX11_OVERRIDE
68 LocalUser* lu = IS_LOCAL(u);
72 const std::string host = u->nick + "!" + u->ident + "@" + u->GetRealHost() + " " + u->GetRealName();
73 const std::string ip = u->nick + "!" + u->ident + "@" + u->GetIPString() + " " + u->GetRealName();
74 return (regex->Matches(host) || regex->Matches(ip));
77 bool Matches(const std::string& compare) CXX11_OVERRIDE
79 return regex->Matches(compare);
82 void Apply(User* u) CXX11_OVERRIDE
86 ZLine* zl = new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString());
87 if (ServerInstance->XLines->AddLine(zl, NULL))
89 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()) : "";
90 ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on %s%s: %s",
91 zl->ipaddr.c_str(), expirystr.c_str(), zl->reason.c_str());
97 DefaultApply(u, "R", false);
100 const std::string& Displayable() CXX11_OVERRIDE
105 std::string matchtext;
111 /** An XLineFactory specialized to generate RLine* pointers
113 class RLineFactory : public XLineFactory
116 dynamic_reference<RegexFactory>& rxfactory;
117 RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
123 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
127 ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
128 throw ModuleException("Regex engine not set or loaded!");
131 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
136 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
138 class CommandRLine : public Command
140 std::string rxengine;
141 RLineFactory& factory;
144 CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
146 flags_needed = 'o'; this->syntax = "<regex> [<duration> :<reason>]";
149 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
152 if (parameters.size() >= 3)
154 // Adding - XXX todo make this respect <insane> tag perhaps..
156 unsigned long duration;
157 if (!InspIRCd::Duration(parameters[1], duration))
159 user->WriteNotice("*** Invalid duration for R-line.");
166 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
168 catch (ModuleException &e)
170 ServerInstance->SNO->WriteToSnoMask('a', "Could not add R-line: " + e.GetReason());
175 if (ServerInstance->XLines->AddLine(r, user))
179 ServerInstance->SNO->WriteToSnoMask('x', "%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
183 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s, expires in %s (on %s): %s",
184 user->nick.c_str(), parameters[0].c_str(), InspIRCd::DurationString(duration).c_str(),
185 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), parameters[2].c_str());
188 ServerInstance->XLines->ApplyLines();
193 user->WriteNotice("*** R-line for " + parameters[0] + " already exists.");
201 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", reason, user))
203 ServerInstance->SNO->WriteToSnoMask('x', "%s removed R-line on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
207 user->WriteNotice("*** R-line " + parameters[0] + " not found on the list.");
214 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
217 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
219 return ROUTE_BROADCAST;
223 class ModuleRLine : public Module, public Stats::EventListener
225 dynamic_reference<RegexFactory> rxfactory;
228 bool MatchOnNickChange;
230 RegexFactory* factory;
234 : Stats::EventListener(this)
235 , rxfactory(this, "regex")
242 void init() CXX11_OVERRIDE
244 ServerInstance->XLines->RegisterFactory(&f);
249 ServerInstance->XLines->DelAll("R");
250 ServerInstance->XLines->UnregisterFactory(&f);
253 Version GetVersion() CXX11_OVERRIDE
255 return Version("Provides support for banning users through regular expression patterns", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
258 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
260 // Apply lines on user connect
261 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
269 return MOD_RES_PASSTHRU;
272 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
274 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
276 MatchOnNickChange = tag->getBool("matchonnickchange");
277 ZlineOnMatch = tag->getBool("zlineonmatch");
278 std::string newrxengine = tag->getString("engine");
280 factory = rxfactory ? (rxfactory.operator->()) : NULL;
282 if (newrxengine.empty())
283 rxfactory.SetProvider("regex");
285 rxfactory.SetProvider("regex/" + newrxengine);
289 if (newrxengine.empty())
290 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-line functionality disabled until this is corrected.");
292 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-line functionality disabled until this is corrected.", newrxengine.c_str());
294 ServerInstance->XLines->DelAll(f.GetType());
296 else if ((!initing) && (rxfactory.operator->() != factory))
298 ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-lines.");
299 ServerInstance->XLines->DelAll(f.GetType());
305 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
307 if (stats.GetSymbol() != 'R')
308 return MOD_RES_PASSTHRU;
310 ServerInstance->XLines->InvokeStats("R", 223, stats);
314 void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
319 if (!MatchOnNickChange)
322 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
331 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
336 ServerInstance->XLines->ApplyLines();
340 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
342 // If the regex engine became unavailable or has changed, remove all R-lines.
345 ServerInstance->XLines->DelAll(f.GetType());
347 else if (rxfactory.operator->() != factory)
349 factory = rxfactory.operator->();
350 ServerInstance->XLines->DelAll(f.GetType());
354 void Prioritize() CXX11_OVERRIDE
356 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
357 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
361 MODULE_INIT(ModuleRLine)