1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 static Module* rxengine = 0;
19 static Module* mymodule = 0; /* Needed to let RLine send request! */
21 /* $ModDesc: RLINE: Regexp user banning. */
23 class CoreExport 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(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* regexs) : XLine(Instance, s_time, d, src, re, "R")
41 ServerInstance->SNO->WriteToSnoMask('x', "Cannot create regexes until engine is set to a loaded provider!");
42 throw ModuleException("Regex engine not set or loaded!");
47 regex = RegexFactoryRequest(mymodule, rxengine, regexs).Create();
49 catch (ModuleException& ex)
51 ServerInstance->SNO->WriteToSnoMask('x', "Bad regex: %s", ex.GetReason());
65 std::string compare = std::string(u->nick) + "!" + u->ident + "@" + u->host + " " + u->fullname;
67 ServerInstance->Logs->Log("m_rline",DEBUG, "Matching " + matchtext + " against string " + compare);
69 if (regex->Matches(compare))
78 bool Matches(const std::string &compare)
80 if (regex->Matches(compare))
91 DefaultApply(u, "R", true);
96 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed R-Line %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, this->duration);
99 const char* Displayable()
101 return matchtext.c_str();
104 std::string matchtext;
110 /** An XLineFactory specialized to generate RLine* pointers
112 class CoreExport RLineFactory : public XLineFactory
115 RLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "R") { }
119 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
121 return new RLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
126 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
128 class CommandRLine : public Command
130 std::string rxengine;
133 CommandRLine (InspIRCd* Instance) : Command(Instance,"RLINE", "o", 1)
135 this->source = "m_rline.so";
138 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
141 if (parameters.size() >= 3)
143 // Adding - XXX todo make this respect <insane> tag perhaps..
145 long duration = ServerInstance->Duration(parameters[1]);
150 r = new RLine(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
154 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
159 if (ServerInstance->XLines->AddLine(r, user))
163 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-Line for %s.", user->nick.c_str(), parameters[0].c_str());
167 time_t c_requires_crap = duration + ServerInstance->Time();
168 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-Line for %s, expires on %s", user->nick.c_str(), parameters[0].c_str(),
169 ServerInstance->TimeString(c_requires_crap).c_str());
172 ServerInstance->XLines->ApplyLines();
177 user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
183 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
185 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed R-Line on %s.",user->nick.c_str(),parameters[0].c_str());
189 // XXX todo implement stats
190 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats g.",user->nick.c_str(),parameters[0].c_str());
198 class ModuleRLine : public Module
203 bool MatchOnNickChange;
204 std::string RegexEngine;
207 ModuleRLine(InspIRCd* Me) : Module(Me)
212 Me->Modules->UseInterface("RegularExpression");
214 // Create a new command
215 r = new CommandRLine(ServerInstance);
216 ServerInstance->AddCommand(r);
218 f = new RLineFactory(ServerInstance);
219 ServerInstance->XLines->RegisterFactory(f);
221 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnLoadModule };
222 ServerInstance->Modules->Attach(eventlist, this, 3);
226 virtual ~ModuleRLine()
228 ServerInstance->Modules->DoneWithInterface("RegularExpression");
229 ServerInstance->XLines->DelAll("R");
230 ServerInstance->XLines->UnregisterFactory(f);
233 virtual Version GetVersion()
235 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
238 virtual void OnUserConnect(User* user)
240 // Apply lines on user connect
241 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
250 virtual void OnRehash(User *user, const std::string ¶meter)
252 ConfigReader Conf(ServerInstance);
254 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
256 std::string newrxengine;
258 newrxengine = Conf.ReadValue("rline", "engine", 0);
260 if (!RegexEngine.empty())
262 if (RegexEngine == newrxengine)
264 ServerInstance->SNO->WriteToSnoMask('x', "Dumping all R-Lines due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
265 ServerInstance->XLines->DelAll("R");
268 RegexEngine = newrxengine;
269 modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
272 for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
274 std::string rxname = RegexNameRequest(this, *i).Send();
275 if (rxname == newrxengine)
277 ServerInstance->SNO->WriteToSnoMask('x', "R-Line now using engine '%s'", RegexEngine.c_str());
284 ServerInstance->SNO->WriteToSnoMask('x', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
288 virtual void OnLoadModule(Module* mod, const std::string& name)
290 if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
292 std::string rxname = RegexNameRequest(this, mod).Send();
293 if (rxname == RegexEngine)
295 ServerInstance->SNO->WriteToSnoMask('x', "R-Line now using engine '%s'", RegexEngine.c_str());
301 virtual void OnUserPostNick(User *user, const std::string &oldnick)
306 if (!MatchOnNickChange)
309 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
320 MODULE_INIT(ModuleRLine)