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, 2020 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2012-2013, 2016 Attila Molnar <attilamolnar@hush.com>
8 * Copyright (C) 2012, 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
40 RLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference<RegexFactory>& rxfactory)
41 : XLine(s_time, d, src, re, "R")
44 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
45 * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
47 regex = rxfactory->Create(regexs);
55 bool Matches(User* u) CXX11_OVERRIDE
57 LocalUser* lu = IS_LOCAL(u);
61 const std::string host = u->nick + "!" + u->ident + "@" + u->GetRealHost() + " " + u->GetRealName();
62 const std::string ip = u->nick + "!" + u->ident + "@" + u->GetIPString() + " " + u->GetRealName();
63 return (regex->Matches(host) || regex->Matches(ip));
66 bool Matches(const std::string& compare) CXX11_OVERRIDE
68 return regex->Matches(compare);
71 void Apply(User* u) CXX11_OVERRIDE
75 ZLine* zl = new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString());
76 if (ServerInstance->XLines->AddLine(zl, NULL))
78 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()) : "";
79 ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on %s%s: %s",
80 zl->ipaddr.c_str(), expirystr.c_str(), zl->reason.c_str());
86 DefaultApply(u, "R", false);
89 const std::string& Displayable() CXX11_OVERRIDE
94 std::string matchtext;
100 /** An XLineFactory specialized to generate RLine* pointers
102 class RLineFactory : public XLineFactory
105 dynamic_reference<RegexFactory>& rxfactory;
106 RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
112 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
116 ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
117 throw ModuleException("Regex engine not set or loaded!");
120 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
125 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
127 class CommandRLine : public Command
129 std::string rxengine;
130 RLineFactory& factory;
133 CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
135 flags_needed = 'o'; this->syntax = "<regex> [<duration> :<reason>]";
138 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
141 if (parameters.size() >= 3)
143 // Adding - XXX todo make this respect <insane> tag perhaps..
145 unsigned long duration;
146 if (!InspIRCd::Duration(parameters[1], duration))
148 user->WriteNotice("*** Invalid duration for R-line.");
155 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
157 catch (ModuleException &e)
159 ServerInstance->SNO->WriteToSnoMask('a', "Could not add R-line: " + e.GetReason());
164 if (ServerInstance->XLines->AddLine(r, user))
168 ServerInstance->SNO->WriteToSnoMask('x', "%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
172 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s, expires in %s (on %s): %s",
173 user->nick.c_str(), parameters[0].c_str(), InspIRCd::DurationString(duration).c_str(),
174 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), parameters[2].c_str());
177 ServerInstance->XLines->ApplyLines();
182 user->WriteNotice("*** R-line for " + parameters[0] + " already exists.");
190 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", reason, user))
192 ServerInstance->SNO->WriteToSnoMask('x', "%s removed R-line on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
196 user->WriteNotice("*** R-line " + parameters[0] + " not found on the list.");
203 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
206 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
208 return ROUTE_BROADCAST;
212 class ModuleRLine : public Module, public Stats::EventListener
214 dynamic_reference<RegexFactory> rxfactory;
217 bool MatchOnNickChange;
219 RegexFactory* factory;
223 : Stats::EventListener(this)
224 , rxfactory(this, "regex")
231 void init() CXX11_OVERRIDE
233 ServerInstance->XLines->RegisterFactory(&f);
238 ServerInstance->XLines->DelAll("R");
239 ServerInstance->XLines->UnregisterFactory(&f);
242 Version GetVersion() CXX11_OVERRIDE
244 return Version("Adds the /RLINE command which allows server operators to prevent users matching a nickname!username@hostname+realname regular expression from connecting to the server.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
247 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
249 // Apply lines on user connect
250 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
258 return MOD_RES_PASSTHRU;
261 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
263 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
265 MatchOnNickChange = tag->getBool("matchonnickchange");
266 ZlineOnMatch = tag->getBool("zlineonmatch");
267 std::string newrxengine = tag->getString("engine");
269 factory = rxfactory ? (rxfactory.operator->()) : NULL;
271 if (newrxengine.empty())
272 rxfactory.SetProvider("regex");
274 rxfactory.SetProvider("regex/" + newrxengine);
278 if (newrxengine.empty())
279 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-line functionality disabled until this is corrected.");
281 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-line functionality disabled until this is corrected.", newrxengine.c_str());
283 ServerInstance->XLines->DelAll(f.GetType());
285 else if ((!initing) && (rxfactory.operator->() != factory))
287 ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-lines.");
288 ServerInstance->XLines->DelAll(f.GetType());
294 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
296 if (stats.GetSymbol() != 'R')
297 return MOD_RES_PASSTHRU;
299 ServerInstance->XLines->InvokeStats("R", stats);
303 void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
308 if (!MatchOnNickChange)
311 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
320 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
325 ServerInstance->XLines->ApplyLines();
329 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
331 // If the regex engine became unavailable or has changed, remove all R-lines.
334 ServerInstance->XLines->DelAll(f.GetType());
336 else if (rxfactory.operator->() != factory)
338 factory = rxfactory.operator->();
339 ServerInstance->XLines->DelAll(f.GetType());
343 void Prioritize() CXX11_OVERRIDE
345 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
346 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
350 MODULE_INIT(ModuleRLine)