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