1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 static bool ZlineOnMatch = false;
19 static std::vector<ZLine *> background_zlines;
21 /* $ModDesc: RLINE: Regexp user banning. */
23 class RLine : public XLine
28 * @param s_time The set time
29 * @param d The duration of the xline
30 * @param src The sender of the xline
31 * @param re The reason of the xline
32 * @param regex Pattern to match with
35 RLine(time_t s_time, long d, std::string src, std::string re, std::string regexs, dynamic_reference<RegexFactory>& rxfactory)
36 : XLine(s_time, d, src, re, "R")
40 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
41 * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
43 regex = rxfactory->Create(regexs);
58 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
59 return regex->Matches(compare);
62 bool Matches(const std::string &compare)
64 return regex->Matches(compare);
70 background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString()));
72 DefaultApply(u, "R", false);
77 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
78 this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
81 const char* Displayable()
83 return matchtext.c_str();
86 std::string matchtext;
92 /** An XLineFactory specialized to generate RLine* pointers
94 class RLineFactory : public XLineFactory
97 dynamic_reference<RegexFactory>& rxfactory;
98 RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
104 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
108 ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
109 throw ModuleException("Regex engine not set or loaded!");
112 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
121 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
123 class CommandRLine : public Command
125 std::string rxengine;
126 RLineFactory& factory;
129 CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
131 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
134 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
137 if (parameters.size() >= 3)
139 // Adding - XXX todo make this respect <insane> tag perhaps..
141 long duration = ServerInstance->Duration(parameters[1]);
146 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
148 catch (ModuleException &e)
150 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: %s", e.GetReason());
155 if (ServerInstance->XLines->AddLine(r, user))
159 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
163 time_t c_requires_crap = duration + ServerInstance->Time();
164 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
167 ServerInstance->XLines->ApplyLines();
172 user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
178 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
180 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
184 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
191 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
193 return ROUTE_BROADCAST;
197 class ModuleRLine : public Module
200 dynamic_reference<RegexFactory> rxfactory;
203 bool MatchOnNickChange;
206 ModuleRLine() : rxfactory(this, "regex"), f(rxfactory), r(this, f)
214 ServerInstance->AddCommand(&r);
215 ServerInstance->XLines->RegisterFactory(&f);
217 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer };
218 ServerInstance->Modules->Attach(eventlist, this, 5);
221 virtual ~ModuleRLine()
223 ServerInstance->XLines->DelAll("R");
224 ServerInstance->XLines->UnregisterFactory(&f);
227 virtual Version GetVersion()
229 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
232 virtual void OnUserConnect(LocalUser* user)
234 // Apply lines on user connect
235 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
244 virtual void OnRehash(User *user)
248 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
249 background_zlines.clear();
251 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
252 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
253 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
255 if (newrxengine.empty())
256 rxfactory.SetProvider("regex");
258 rxfactory.SetProvider("regex/" + newrxengine);
261 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
265 virtual ModResult OnStats(char symbol, User* user, string_list &results)
268 return MOD_RES_PASSTHRU;
270 ServerInstance->XLines->InvokeStats("R", 223, user, results);
274 virtual void OnUserPostNick(User *user, const std::string &oldnick)
279 if (!MatchOnNickChange)
282 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
291 virtual void OnBackgroundTimer(time_t curtime)
293 if (!ZlineOnMatch) return;
294 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
297 if (ServerInstance->XLines->AddLine(zl,NULL))
299 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s",
300 zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
301 ServerInstance->XLines->ApplyLines();
304 background_zlines.clear();
309 MODULE_INIT(ModuleRLine)