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"
27 static bool ZlineOnMatch = false;
28 static bool added_zline = false;
30 class RLine : public XLine
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
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")
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
49 regex = rxfactory->Create(regexs);
61 LocalUser* lu = IS_LOCAL(u);
65 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
66 return regex->Matches(compare);
69 bool Matches(const std::string &compare)
71 return regex->Matches(compare);
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))
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());
89 DefaultApply(u, "R", false);
92 const std::string& Displayable()
97 std::string matchtext;
103 /** An XLineFactory specialized to generate RLine* pointers
105 class RLineFactory : public XLineFactory
108 dynamic_reference<RegexFactory>& rxfactory;
109 RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
115 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
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!");
123 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
128 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
130 class CommandRLine : public Command
132 std::string rxengine;
133 RLineFactory& factory;
136 CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
138 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
141 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
144 if (parameters.size() >= 3)
146 // Adding - XXX todo make this respect <insane> tag perhaps..
148 unsigned long duration = InspIRCd::Duration(parameters[1]);
153 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
155 catch (ModuleException &e)
157 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: " + e.GetReason());
162 if (ServerInstance->XLines->AddLine(r, user))
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());
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());
175 ServerInstance->XLines->ApplyLines();
180 user->WriteNotice("*** R-Line for " + parameters[0] + " already exists");
186 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
188 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
192 user->WriteNotice("*** R-Line " + parameters[0] + " not found in list, try /stats R.");
199 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
202 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
204 return ROUTE_BROADCAST;
208 class ModuleRLine : public Module
210 dynamic_reference<RegexFactory> rxfactory;
213 bool MatchOnNickChange;
215 RegexFactory* factory;
219 : rxfactory(this, "regex"), f(rxfactory), r(this, f)
224 void init() CXX11_OVERRIDE
226 ServerInstance->XLines->RegisterFactory(&f);
231 ServerInstance->XLines->DelAll("R");
232 ServerInstance->XLines->UnregisterFactory(&f);
235 Version GetVersion() CXX11_OVERRIDE
237 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
240 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
242 // Apply lines on user connect
243 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
251 return MOD_RES_PASSTHRU;
254 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
256 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
258 MatchOnNickChange = tag->getBool("matchonnickchange");
259 ZlineOnMatch = tag->getBool("zlineonmatch");
260 std::string newrxengine = tag->getString("engine");
262 factory = rxfactory ? (rxfactory.operator->()) : NULL;
264 if (newrxengine.empty())
265 rxfactory.SetProvider("regex");
267 rxfactory.SetProvider("regex/" + newrxengine);
271 if (newrxengine.empty())
272 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-Line functionality disabled until this is corrected.");
274 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
276 ServerInstance->XLines->DelAll(f.GetType());
278 else if ((!initing) && (rxfactory.operator->() != factory))
280 ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-Lines");
281 ServerInstance->XLines->DelAll(f.GetType());
287 ModResult OnStats(char symbol, User* user, string_list &results) CXX11_OVERRIDE
290 return MOD_RES_PASSTHRU;
292 ServerInstance->XLines->InvokeStats("R", 223, user, results);
296 void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
301 if (!MatchOnNickChange)
304 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
313 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
318 ServerInstance->XLines->ApplyLines();
322 void OnUnloadModule(Module* mod) CXX11_OVERRIDE
324 // If the regex engine became unavailable or has changed, remove all rlines
327 ServerInstance->XLines->DelAll(f.GetType());
329 else if (rxfactory.operator->() != factory)
331 factory = rxfactory.operator->();
332 ServerInstance->XLines->DelAll(f.GetType());
338 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
339 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
343 MODULE_INIT(ModuleRLine)