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 /* $ModDesc: RLINE: Regexp user banning. */
19 /* $CompileFlags: exec("pcre-config --cflags") */
20 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
23 #pragma comment(lib, "pcre.lib")
26 class CoreExport RLine : public XLine
31 * @param s_time The set time
32 * @param d The duration of the xline
33 * @param src The sender of the xline
34 * @param re The reason of the xline
35 * @param regex Pattern to match with
38 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")
45 regex = pcre_compile(regexs, 0, &error, &erroffset, NULL);
49 ServerInstance->SNO->WriteToSnoMask('x',"Error in regular expression: %s at offset %d: %s\n", regexs, erroffset, error);
50 throw ModuleException("Bad regex pattern.");
63 std::string compare = std::string(u->nick) + "!" + u->ident + "@" + u->host + " " + u->fullname;
65 ServerInstance->Logs->Log("m_rline",DEBUG, "Matching " + matchtext + " against string " + compare);
67 if (pcre_exec(regex, NULL, compare.c_str(), compare.length(), 0, 0, NULL, 0) > -1)
76 bool Matches(const std::string &compare)
78 if (pcre_exec(regex, NULL, compare.c_str(), compare.length(), 0, 0, NULL, 0) > -1)
89 DefaultApply(u, "R", true);
94 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed R-Line %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, this->duration);
97 const char* Displayable()
99 return matchtext.c_str();
102 std::string matchtext;
108 /** An XLineFactory specialized to generate RLine* pointers
110 class CoreExport RLineFactory : public XLineFactory
113 RLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "R") { }
117 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
119 return new RLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
124 * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
126 class CommandRLine : public Command
129 CommandRLine (InspIRCd* Instance) : Command(Instance,"RLINE", "o", 1)
131 this->source = "m_rline.so";
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, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
150 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
155 if (ServerInstance->XLines->AddLine(r, user))
159 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-Line for %s.", user->nick.c_str(), parameters[0].c_str());
163 time_t c_requires_crap = duration + ServerInstance->Time();
164 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-Line for %s, expires on %s", user->nick.c_str(), parameters[0].c_str(),
165 ServerInstance->TimeString(c_requires_crap).c_str());
168 ServerInstance->XLines->ApplyLines();
173 user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
179 if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
181 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed R-Line on %s.",user->nick.c_str(),parameters[0].c_str());
185 // XXX todo implement stats
186 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats g.",user->nick.c_str(),parameters[0].c_str());
194 class ModuleRLine : public Module
199 bool MatchOnNickChange;
202 ModuleRLine(InspIRCd* Me) : Module(Me)
206 // Create a new command
207 r = new CommandRLine(ServerInstance);
208 ServerInstance->AddCommand(r);
210 f = new RLineFactory(ServerInstance);
211 ServerInstance->XLines->RegisterFactory(f);
213 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick };
214 ServerInstance->Modules->Attach(eventlist, this, 3);
218 virtual ~ModuleRLine()
220 ServerInstance->XLines->DelAll("R");
221 ServerInstance->XLines->UnregisterFactory(f);
224 virtual Version GetVersion()
226 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
229 virtual void OnUserConnect(User* user)
231 // Apply lines on user connect
232 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
241 virtual void OnRehash(User *user, const std::string ¶meter)
243 ConfigReader Conf(ServerInstance);
245 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 1);
248 virtual void OnUserPostNick(User *user, const std::string &oldnick)
253 if (!MatchOnNickChange)
256 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
267 MODULE_INIT(ModuleRLine)