]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
160092a63317772e7262943e07b0d9930c8cc930
[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, 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                 if (u->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         void DisplayExpiry()
95         {
96                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
97                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
98         }
99
100         const char* Displayable()
101         {
102                 return matchtext.c_str();
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, long duration, std::string source, std::string reason, std::string xline_specific_mask)
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         ~RLineFactory()
135         {
136         }
137 };
138
139 /** Handle /RLINE
140  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
141  */
142 class CommandRLine : public Command
143 {
144         std::string rxengine;
145         RLineFactory& factory;
146
147  public:
148         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
149         {
150                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
151         }
152
153         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
154         {
155
156                 if (parameters.size() >= 3)
157                 {
158                         // Adding - XXX todo make this respect <insane> tag perhaps..
159
160                         long duration = ServerInstance->Duration(parameters[1]);
161                         XLine *r = NULL;
162
163                         try
164                         {
165                                 r = factory.Generate(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
166                         }
167                         catch (ModuleException &e)
168                         {
169                                 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: %s", e.GetReason());
170                         }
171
172                         if (r)
173                         {
174                                 if (ServerInstance->XLines->AddLine(r, user))
175                                 {
176                                         if (!duration)
177                                         {
178                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
179                                         }
180                                         else
181                                         {
182                                                 time_t c_requires_crap = duration + ServerInstance->Time();
183                                                 std::string timestr = ServerInstance->TimeString(c_requires_crap);
184                                                 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());
185                                         }
186
187                                         ServerInstance->XLines->ApplyLines();
188                                 }
189                                 else
190                                 {
191                                         delete r;
192                                         user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
193                                 }
194                         }
195                 }
196                 else
197                 {
198                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
199                         {
200                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
201                         }
202                         else
203                         {
204                                 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
205                         }
206                 }
207
208                 return CMD_SUCCESS;
209         }
210
211         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
212         {
213                 if (IS_LOCAL(user))
214                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
215
216                 return ROUTE_BROADCAST;
217         }
218 };
219
220 class ModuleRLine : public Module
221 {
222  private:
223         dynamic_reference<RegexFactory> rxfactory;
224         RLineFactory f;
225         CommandRLine r;
226         bool MatchOnNickChange;
227         bool initing;
228         RegexFactory* factory;
229
230  public:
231         ModuleRLine()
232                 : rxfactory(this, "regex"), f(rxfactory), r(this, f)
233                 , initing(true)
234         {
235         }
236
237         void init()
238         {
239                 OnRehash(NULL);
240
241                 ServerInstance->Modules->AddService(r);
242                 ServerInstance->XLines->RegisterFactory(&f);
243
244                 Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer, I_OnUnloadModule };
245                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
246         }
247
248         virtual ~ModuleRLine()
249         {
250                 ServerInstance->XLines->DelAll("R");
251                 ServerInstance->XLines->UnregisterFactory(&f);
252         }
253
254         virtual Version GetVersion()
255         {
256                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
257         }
258
259         ModResult OnUserRegister(LocalUser* user)
260         {
261                 // Apply lines on user connect
262                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
263
264                 if (rl)
265                 {
266                         // Bang. :P
267                         rl->Apply(user);
268                         return MOD_RES_DENY;
269                 }
270                 return MOD_RES_PASSTHRU;
271         }
272
273         virtual void OnRehash(User *user)
274         {
275                 ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
276
277                 MatchOnNickChange = tag->getBool("matchonnickchange");
278                 ZlineOnMatch = tag->getBool("zlineonmatch");
279                 std::string newrxengine = tag->getString("engine");
280
281                 factory = rxfactory ? (rxfactory.operator->()) : NULL;
282
283                 if (newrxengine.empty())
284                         rxfactory.SetProvider("regex");
285                 else
286                         rxfactory.SetProvider("regex/" + newrxengine);
287
288                 if (!rxfactory)
289                 {
290                         if (newrxengine.empty())
291                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-Line functionality disabled until this is corrected.");
292                         else
293                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
294
295                         ServerInstance->XLines->DelAll(f.GetType());
296                 }
297                 else if ((!initing) && (rxfactory.operator->() != factory))
298                 {
299                         ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-Lines");
300                         ServerInstance->XLines->DelAll(f.GetType());
301                 }
302
303                 initing = false;
304         }
305
306         virtual ModResult OnStats(char symbol, User* user, string_list &results)
307         {
308                 if (symbol != 'R')
309                         return MOD_RES_PASSTHRU;
310
311                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
312                 return MOD_RES_DENY;
313         }
314
315         virtual void OnUserPostNick(User *user, const std::string &oldnick)
316         {
317                 if (!IS_LOCAL(user))
318                         return;
319
320                 if (!MatchOnNickChange)
321                         return;
322
323                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
324
325                 if (rl)
326                 {
327                         // Bang! :D
328                         rl->Apply(user);
329                 }
330         }
331
332         virtual void OnBackgroundTimer(time_t curtime)
333         {
334                 if (added_zline)
335                 {
336                         added_zline = false;
337                         ServerInstance->XLines->ApplyLines();
338                 }
339         }
340
341         void OnUnloadModule(Module* mod)
342         {
343                 // If the regex engine became unavailable or has changed, remove all rlines
344                 if (!rxfactory)
345                 {
346                         ServerInstance->XLines->DelAll(f.GetType());
347                 }
348                 else if (rxfactory.operator->() != factory)
349                 {
350                         factory = rxfactory.operator->();
351                         ServerInstance->XLines->DelAll(f.GetType());
352                 }
353         }
354
355         void Prioritize()
356         {
357                 Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
358                 ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
359         }
360 };
361
362 MODULE_INIT(ModuleRLine)