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! */
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(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string regexs)
36 : XLine(Instance, s_time, d, src, re, "R")
42 ServerInstance->SNO->WriteToSnoMask('x', "Cannot create regexes until engine is set to a loaded provider!");
43 throw ModuleException("Regex engine not set or loaded!");
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 = RegexFactoryRequest(mymodule, rxengine, regexs).Create();
64 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
65 return regex->Matches(compare);
68 bool Matches(const std::string &compare)
70 return regex->Matches(compare);
75 DefaultApply(u, "R", true);
80 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-Line %s (set by %s %ld seconds ago)",
81 this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
84 const char* Displayable()
86 return matchtext.c_str();
89 std::string matchtext;
95 /** An XLineFactory specialized to generate RLine* pointers
97 class RLineFactory : public XLineFactory
100 RLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "R")
106 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
108 return new RLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
117 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
119 class CommandRLine : public Command
121 std::string rxengine;
124 CommandRLine(Module* Creator) : Command(Creator,"RLINE", 1, 3)
126 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
129 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
132 if (parameters.size() >= 3)
134 // Adding - XXX todo make this respect <insane> tag perhaps..
136 long duration = ServerInstance->Duration(parameters[1]);
141 r = new RLine(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
143 catch (ModuleException &e)
145 ServerInstance->SNO->WriteToSnoMask('x',"Could not add RLINE: %s", e.GetReason());
150 if (ServerInstance->XLines->AddLine(r, user))
154 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-Line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
158 time_t c_requires_crap = duration + ServerInstance->Time();
159 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-Line for %s, expires on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
162 ServerInstance->XLines->ApplyLines();
167 user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
173 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
175 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed R-Line on %s.",user->nick.c_str(),parameters[0].c_str());
179 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
186 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
188 return ROUTE_BROADCAST;
192 class ModuleRLine : public Module
197 bool MatchOnNickChange;
198 std::string RegexEngine;
201 ModuleRLine(InspIRCd* Me) : Module(Me), r(this), f(Me)
206 Me->Modules->UseInterface("RegularExpression");
208 ServerInstance->AddCommand(&r);
209 ServerInstance->XLines->RegisterFactory(&f);
211 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnLoadModule, I_OnStats };
212 ServerInstance->Modules->Attach(eventlist, this, 5);
216 virtual ~ModuleRLine()
218 ServerInstance->Modules->DoneWithInterface("RegularExpression");
219 ServerInstance->XLines->DelAll("R");
220 ServerInstance->XLines->UnregisterFactory(&f);
223 virtual Version GetVersion()
225 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
228 virtual void OnUserConnect(User* user)
230 // Apply lines on user connect
231 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
240 virtual void OnRehash(User *user)
242 ConfigReader Conf(ServerInstance);
244 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
245 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
247 if (!RegexEngine.empty())
249 if (RegexEngine == newrxengine)
252 ServerInstance->SNO->WriteToSnoMask('x', "Dumping all R-Lines due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
253 ServerInstance->XLines->DelAll("R");
256 RegexEngine = newrxengine;
257 modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
260 for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
262 if (RegexNameRequest(this, *i).Send() == newrxengine)
264 ServerInstance->SNO->WriteToSnoMask('x', "R-Line now using engine '%s'", RegexEngine.c_str());
271 ServerInstance->SNO->WriteToSnoMask('x', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
275 virtual ModResult OnStats(char symbol, User* user, string_list &results)
278 return MOD_RES_PASSTHRU;
280 ServerInstance->XLines->InvokeStats("R", 223, user, results);
284 virtual void OnLoadModule(Module* mod, const std::string& name)
286 if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
288 std::string rxname = RegexNameRequest(this, mod).Send();
289 if (rxname == RegexEngine)
291 ServerInstance->SNO->WriteToSnoMask('x', "R-Line now using engine '%s'", RegexEngine.c_str());
297 virtual void OnUserPostNick(User *user, const std::string &oldnick)
302 if (!MatchOnNickChange)
305 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
316 MODULE_INIT(ModuleRLine)