]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
6fd7b832c568b190633aa1d80551a688b5dc423f
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/regex.h"
25 #include "xline.h"
26
27 static bool ZlineOnMatch = false;
28 static bool added_zline = false;
29
30 class RLine : public XLine
31 {
32  public:
33
34         /** Create a R-Line.
35          * @param s_time The set time
36          * @param d The duration of the xline
37          * @param src The sender of the xline
38          * @param re The reason of the xline
39          * @param regex Pattern to match with
40          * @
41          */
42         RLine(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference<RegexFactory>& rxfactory)
43                 : XLine(s_time, d, src, re, "R")
44                 , matchtext(regexs)
45         {
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
48                  */
49                 regex = rxfactory->Create(regexs);
50         }
51
52         /** Destructor
53          */
54         ~RLine()
55         {
56                 delete regex;
57         }
58
59         bool Matches(User *u)
60         {
61                 LocalUser* lu = IS_LOCAL(u);
62                 if (lu && lu->exempt)
63                         return false;
64
65                 const std::string host = u->nick + "!" + u->ident + "@" + u->GetRealHost() + " " + u->fullname;
66                 const std::string ip = u->nick + "!" + u->ident + "@" + u->GetIPString() + " " + u->fullname;
67                 return (regex->Matches(host) || regex->Matches(ip));
68         }
69
70         bool Matches(const std::string &compare)
71         {
72                 return regex->Matches(compare);
73         }
74
75         void Apply(User* u)
76         {
77                 if (ZlineOnMatch)
78                 {
79                         ZLine* zl = new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString());
80                         if (ServerInstance->XLines->AddLine(zl, NULL))
81                         {
82                                 std::string timestr = InspIRCd::TimeString(zl->expiry);
83                                 ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on *@%s%s%s: %s",
84                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? timestr.c_str() : "", zl->reason.c_str());
85                                 added_zline = true;
86                         }
87                         else
88                                 delete zl;
89                 }
90                 DefaultApply(u, "R", false);
91         }
92
93         const std::string& Displayable()
94         {
95                 return matchtext;
96         }
97
98         std::string matchtext;
99
100         Regex *regex;
101 };
102
103
104 /** An XLineFactory specialized to generate RLine* pointers
105  */
106 class RLineFactory : public XLineFactory
107 {
108  public:
109         dynamic_reference<RegexFactory>& rxfactory;
110         RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
111         {
112         }
113
114         /** Generate a RLine
115          */
116         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
117         {
118                 if (!rxfactory)
119                 {
120                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
121                         throw ModuleException("Regex engine not set or loaded!");
122                 }
123
124                 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
125         }
126 };
127
128 /** Handle /RLINE
129  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
130  */
131 class CommandRLine : public Command
132 {
133         std::string rxengine;
134         RLineFactory& factory;
135
136  public:
137         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
138         {
139                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
140         }
141
142         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
143         {
144
145                 if (parameters.size() >= 3)
146                 {
147                         // Adding - XXX todo make this respect <insane> tag perhaps..
148
149                         unsigned long duration = InspIRCd::Duration(parameters[1]);
150                         XLine *r = NULL;
151
152                         try
153                         {
154                                 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
155                         }
156                         catch (ModuleException &e)
157                         {
158                                 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: " + e.GetReason());
159                         }
160
161                         if (r)
162                         {
163                                 if (ServerInstance->XLines->AddLine(r, user))
164                                 {
165                                         if (!duration)
166                                         {
167                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
168                                         }
169                                         else
170                                         {
171                                                 time_t c_requires_crap = duration + ServerInstance->Time();
172                                                 std::string timestr = InspIRCd::TimeString(c_requires_crap);
173                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), timestr.c_str(), parameters[2].c_str());
174                                         }
175
176                                         ServerInstance->XLines->ApplyLines();
177                                 }
178                                 else
179                                 {
180                                         delete r;
181                                         user->WriteNotice("*** R-Line for " + parameters[0] + " already exists");
182                                 }
183                         }
184                 }
185                 else
186                 {
187                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
188                         {
189                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
190                         }
191                         else
192                         {
193                                 user->WriteNotice("*** R-Line " + parameters[0] + " not found in list, try /stats R.");
194                         }
195                 }
196
197                 return CMD_SUCCESS;
198         }
199
200         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
201         {
202                 if (IS_LOCAL(user))
203                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
204
205                 return ROUTE_BROADCAST;
206         }
207 };
208
209 class ModuleRLine : public Module
210 {
211         dynamic_reference<RegexFactory> rxfactory;
212         RLineFactory f;
213         CommandRLine r;
214         bool MatchOnNickChange;
215         bool initing;
216         RegexFactory* factory;
217
218  public:
219         ModuleRLine()
220                 : rxfactory(this, "regex"), f(rxfactory), r(this, f)
221                 , initing(true)
222         {
223         }
224
225         void init() CXX11_OVERRIDE
226         {
227                 ServerInstance->XLines->RegisterFactory(&f);
228         }
229
230         ~ModuleRLine()
231         {
232                 ServerInstance->XLines->DelAll("R");
233                 ServerInstance->XLines->UnregisterFactory(&f);
234         }
235
236         Version GetVersion() CXX11_OVERRIDE
237         {
238                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
239         }
240
241         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
242         {
243                 // Apply lines on user connect
244                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
245
246                 if (rl)
247                 {
248                         // Bang. :P
249                         rl->Apply(user);
250                         return MOD_RES_DENY;
251                 }
252                 return MOD_RES_PASSTHRU;
253         }
254
255         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
256         {
257                 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
258
259                 MatchOnNickChange = tag->getBool("matchonnickchange");
260                 ZlineOnMatch = tag->getBool("zlineonmatch");
261                 std::string newrxengine = tag->getString("engine");
262
263                 factory = rxfactory ? (rxfactory.operator->()) : NULL;
264
265                 if (newrxengine.empty())
266                         rxfactory.SetProvider("regex");
267                 else
268                         rxfactory.SetProvider("regex/" + newrxengine);
269
270                 if (!rxfactory)
271                 {
272                         if (newrxengine.empty())
273                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-Line functionality disabled until this is corrected.");
274                         else
275                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
276
277                         ServerInstance->XLines->DelAll(f.GetType());
278                 }
279                 else if ((!initing) && (rxfactory.operator->() != factory))
280                 {
281                         ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-Lines");
282                         ServerInstance->XLines->DelAll(f.GetType());
283                 }
284
285                 initing = false;
286         }
287
288         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
289         {
290                 if (stats.GetSymbol() != 'R')
291                         return MOD_RES_PASSTHRU;
292
293                 ServerInstance->XLines->InvokeStats("R", 223, stats);
294                 return MOD_RES_DENY;
295         }
296
297         void OnUserPostNick(User *user, const std::string &oldnick) CXX11_OVERRIDE
298         {
299                 if (!IS_LOCAL(user))
300                         return;
301
302                 if (!MatchOnNickChange)
303                         return;
304
305                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
306
307                 if (rl)
308                 {
309                         // Bang! :D
310                         rl->Apply(user);
311                 }
312         }
313
314         void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
315         {
316                 if (added_zline)
317                 {
318                         added_zline = false;
319                         ServerInstance->XLines->ApplyLines();
320                 }
321         }
322
323         void OnUnloadModule(Module* mod) CXX11_OVERRIDE
324         {
325                 // If the regex engine became unavailable or has changed, remove all rlines
326                 if (!rxfactory)
327                 {
328                         ServerInstance->XLines->DelAll(f.GetType());
329                 }
330                 else if (rxfactory.operator->() != factory)
331                 {
332                         factory = rxfactory.operator->();
333                         ServerInstance->XLines->DelAll(f.GetType());
334                 }
335         }
336
337         void Prioritize() CXX11_OVERRIDE
338         {
339                 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
340                 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
341         }
342 };
343
344 MODULE_INIT(ModuleRLine)