]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Release v2.0.25
[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 "m_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                 if (u->exempt)
64                         return false;
65
66                 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
67                 return regex->Matches(compare);
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 = ServerInstance->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         void DisplayExpiry()
94         {
95                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
96                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
97         }
98
99         const char* Displayable()
100         {
101                 return matchtext.c_str();
102         }
103
104         std::string matchtext;
105
106         Regex *regex;
107 };
108
109
110 /** An XLineFactory specialized to generate RLine* pointers
111  */
112 class RLineFactory : public XLineFactory
113 {
114  public:
115         dynamic_reference<RegexFactory>& rxfactory;
116         RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
117         {
118         }
119         
120         /** Generate a RLine
121          */
122         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
123         {
124                 if (!rxfactory)
125                 {
126                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
127                         throw ModuleException("Regex engine not set or loaded!");
128                 }
129
130                 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
131         }
132
133         ~RLineFactory()
134         {
135         }
136 };
137
138 /** Handle /RLINE
139  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
140  */
141 class CommandRLine : public Command
142 {
143         std::string rxengine;
144         RLineFactory& factory;
145
146  public:
147         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
148         {
149                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
150         }
151
152         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
153         {
154
155                 if (parameters.size() >= 3)
156                 {
157                         // Adding - XXX todo make this respect <insane> tag perhaps..
158
159                         long duration = ServerInstance->Duration(parameters[1]);
160                         XLine *r = NULL;
161
162                         try
163                         {
164                                 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
165                         }
166                         catch (ModuleException &e)
167                         {
168                                 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: %s", e.GetReason());
169                         }
170
171                         if (r)
172                         {
173                                 if (ServerInstance->XLines->AddLine(r, user))
174                                 {
175                                         if (!duration)
176                                         {
177                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
178                                         }
179                                         else
180                                         {
181                                                 time_t c_requires_crap = duration + ServerInstance->Time();
182                                                 std::string timestr = ServerInstance->TimeString(c_requires_crap);
183                                                 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());
184                                         }
185
186                                         ServerInstance->XLines->ApplyLines();
187                                 }
188                                 else
189                                 {
190                                         delete r;
191                                         user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
192                                 }
193                         }
194                 }
195                 else
196                 {
197                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
198                         {
199                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
200                         }
201                         else
202                         {
203                                 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
204                         }
205                 }
206
207                 return CMD_SUCCESS;
208         }
209
210         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
211         {
212                 if (IS_LOCAL(user))
213                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
214
215                 return ROUTE_BROADCAST;
216         }
217 };
218
219 class ModuleRLine : public Module
220 {
221  private:
222         dynamic_reference<RegexFactory> rxfactory;
223         RLineFactory f;
224         CommandRLine r;
225         bool MatchOnNickChange;
226         bool initing;
227         RegexFactory* factory;
228
229  public:
230         ModuleRLine()
231                 : rxfactory(this, "regex"), f(rxfactory), r(this, f)
232                 , initing(true)
233         {
234         }
235
236         void init()
237         {
238                 OnRehash(NULL);
239
240                 ServerInstance->Modules->AddService(r);
241                 ServerInstance->XLines->RegisterFactory(&f);
242
243                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer, I_OnUnloadModule };
244                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
245         }
246
247         virtual ~ModuleRLine()
248         {
249                 ServerInstance->XLines->DelAll("R");
250                 ServerInstance->XLines->UnregisterFactory(&f);
251         }
252
253         virtual Version GetVersion()
254         {
255                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
256         }
257
258         ModResult OnUserRegister(LocalUser* user)
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         virtual void OnRehash(User *user)
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         virtual ModResult OnStats(char symbol, User* user, string_list &results)
306         {
307                 if (symbol != 'R')
308                         return MOD_RES_PASSTHRU;
309
310                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
311                 return MOD_RES_DENY;
312         }
313
314         virtual void OnUserPostNick(User *user, const std::string &oldnick)
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         virtual void OnBackgroundTimer(time_t curtime)
332         {
333                 if (added_zline)
334                 {
335                         added_zline = false;
336                         ServerInstance->XLines->ApplyLines();
337                 }
338         }
339
340         void OnUnloadModule(Module* mod)
341         {
342                 // If the regex engine became unavailable or has changed, remove all rlines
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()
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)