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