1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 Module* rxengine = NULL;
19 static Module* mymodule = NULL; /* Needed to let RLine send request! */
20 static bool ZlineOnMatch = false;
21 static std::vector<ZLine *> background_zlines;
23 /* $ModDesc: RLINE: Regexp user banning. */
25 class RLine : public XLine
30 * @param s_time The set time
31 * @param d The duration of the xline
32 * @param src The sender of the xline
33 * @param re The reason of the xline
34 * @param regex Pattern to match with
37 RLine(time_t s_time, long d, std::string src, std::string re, std::string regexs)
38 : XLine(s_time, d, src, re, "R")
44 ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
45 throw ModuleException("Regex engine not set or loaded!");
48 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
49 * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
51 regex = RegexFactoryRequest(mymodule, rxengine, regexs).Create();
66 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
67 return regex->Matches(compare);
70 bool Matches(const std::string &compare)
72 return regex->Matches(compare);
78 background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString()));
80 DefaultApply(u, "R", false);
85 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
86 this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
89 const char* Displayable()
91 return matchtext.c_str();
94 std::string matchtext;
100 /** An XLineFactory specialized to generate RLine* pointers
102 class RLineFactory : public XLineFactory
105 RLineFactory() : XLineFactory("R")
111 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
113 return new RLine(set_time, duration, source, reason, xline_specific_mask);
122 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
124 class CommandRLine : public Command
126 std::string rxengine;
129 CommandRLine(Module* Creator) : Command(Creator,"RLINE", 1, 3)
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 = new RLine(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
202 bool MatchOnNickChange;
203 std::string RegexEngine;
206 ModuleRLine() : r(this)
211 ServerInstance->Modules->UseInterface("RegularExpression");
213 ServerInstance->AddCommand(&r);
214 ServerInstance->XLines->RegisterFactory(&f);
216 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnLoadModule, I_OnStats, I_OnBackgroundTimer };
217 ServerInstance->Modules->Attach(eventlist, this, 6);
221 virtual ~ModuleRLine()
223 ServerInstance->Modules->DoneWithInterface("RegularExpression");
224 ServerInstance->XLines->DelAll("R");
225 ServerInstance->XLines->UnregisterFactory(&f);
228 virtual Version GetVersion()
230 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, API_VERSION);
233 virtual void OnUserConnect(User* user)
235 // Apply lines on user connect
236 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
245 virtual void OnRehash(User *user)
249 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
250 background_zlines.clear();
252 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
253 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
254 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
256 if (!RegexEngine.empty())
258 if (RegexEngine == newrxengine)
261 ServerInstance->SNO->WriteToSnoMask('x', "Dumping all R-Lines due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
262 ServerInstance->XLines->DelAll("R");
265 RegexEngine = newrxengine;
266 modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
269 for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
271 if (RegexNameRequest(this, *i).result == newrxengine)
273 ServerInstance->SNO->WriteToSnoMask('a', "R-Line now using engine '%s'", RegexEngine.c_str());
280 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
284 virtual ModResult OnStats(char symbol, User* user, string_list &results)
287 return MOD_RES_PASSTHRU;
289 ServerInstance->XLines->InvokeStats("R", 223, user, results);
293 virtual void OnLoadModule(Module* mod, const std::string& name)
295 if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
297 std::string rxname = RegexNameRequest(this, mod).result;
298 if (rxname == RegexEngine)
300 ServerInstance->SNO->WriteToSnoMask('a', "R-Line now using engine '%s'", RegexEngine.c_str());
306 virtual void OnUserPostNick(User *user, const std::string &oldnick)
311 if (!MatchOnNickChange)
314 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
323 virtual void OnBackgroundTimer(time_t curtime)
325 if (!ZlineOnMatch) return;
326 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
329 if (ServerInstance->XLines->AddLine(zl,NULL))
331 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s",
332 zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
333 ServerInstance->XLines->ApplyLines();
336 background_zlines.clear();
341 MODULE_INIT(ModuleRLine)