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