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