]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Merge pull request #92 from Robby-/insp20-headers
[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 #include "inspircd.h"
24 #include "m_regex.h"
25 #include "xline.h"
26
27 static bool ZlineOnMatch = false;
28 static std::vector<ZLine *> background_zlines;
29
30 /* $ModDesc: RLINE: Regexp user banning. */
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                         background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString()));
80                 }
81                 DefaultApply(u, "R", false);
82         }
83
84         void DisplayExpiry()
85         {
86                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
87                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
88         }
89
90         const char* Displayable()
91         {
92                 return matchtext.c_str();
93         }
94
95         std::string matchtext;
96
97         Regex *regex;
98 };
99
100
101 /** An XLineFactory specialized to generate RLine* pointers
102  */
103 class RLineFactory : public XLineFactory
104 {
105  public:
106         dynamic_reference<RegexFactory>& rxfactory;
107         RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
108         {
109         }
110         
111         /** Generate a RLine
112          */
113         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
114         {
115                 if (!rxfactory)
116                 {
117                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
118                         throw ModuleException("Regex engine not set or loaded!");
119                 }
120
121                 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
122         }
123
124         ~RLineFactory()
125         {
126         }
127 };
128
129 /** Handle /RLINE
130  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
131  */
132 class CommandRLine : public Command
133 {
134         std::string rxengine;
135         RLineFactory& factory;
136
137  public:
138         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
139         {
140                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
141         }
142
143         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
144         {
145
146                 if (parameters.size() >= 3)
147                 {
148                         // Adding - XXX todo make this respect <insane> tag perhaps..
149
150                         long duration = ServerInstance->Duration(parameters[1]);
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 RLINE: %s", 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                                                 time_t c_requires_crap = duration + ServerInstance->Time();
173                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
174                                         }
175
176                                         ServerInstance->XLines->ApplyLines();
177                                 }
178                                 else
179                                 {
180                                         delete r;
181                                         user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
182                                 }
183                         }
184                 }
185                 else
186                 {
187                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
188                         {
189                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
190                         }
191                         else
192                         {
193                                 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
194                         }
195                 }
196
197                 return CMD_SUCCESS;
198         }
199
200         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
201         {
202                 return ROUTE_BROADCAST;
203         }
204 };
205
206 class ModuleRLine : public Module
207 {
208  private:
209         dynamic_reference<RegexFactory> rxfactory;
210         RLineFactory f;
211         CommandRLine r;
212         bool MatchOnNickChange;
213
214  public:
215         ModuleRLine() : rxfactory(this, "regex"), f(rxfactory), r(this, f)
216         {
217         }
218
219         void init()
220         {
221                 OnRehash(NULL);
222
223                 ServerInstance->AddCommand(&r);
224                 ServerInstance->XLines->RegisterFactory(&f);
225
226                 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer };
227                 ServerInstance->Modules->Attach(eventlist, this, 5);
228         }
229
230         virtual ~ModuleRLine()
231         {
232                 ServerInstance->XLines->DelAll("R");
233                 ServerInstance->XLines->UnregisterFactory(&f);
234         }
235
236         virtual Version GetVersion()
237         {
238                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
239         }
240
241         virtual void OnUserConnect(LocalUser* user)
242         {
243                 // Apply lines on user connect
244                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
245
246                 if (rl)
247                 {
248                         // Bang. :P
249                         rl->Apply(user);
250                 }
251         }
252
253         virtual void OnRehash(User *user)
254         {
255                 ConfigReader Conf;
256
257                 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
258                         background_zlines.clear();
259
260                 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
261                 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
262                 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
263
264                 if (newrxengine.empty())
265                         rxfactory.SetProvider("regex");
266                 else
267                         rxfactory.SetProvider("regex/" + newrxengine);
268                 if (!rxfactory)
269                 {
270                         ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
271                 }
272         }
273
274         virtual ModResult OnStats(char symbol, User* user, string_list &results)
275         {
276                 if (symbol != 'R')
277                         return MOD_RES_PASSTHRU;
278
279                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
280                 return MOD_RES_DENY;
281         }
282
283         virtual void OnUserPostNick(User *user, const std::string &oldnick)
284         {
285                 if (!IS_LOCAL(user))
286                         return;
287
288                 if (!MatchOnNickChange)
289                         return;
290
291                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
292
293                 if (rl)
294                 {
295                         // Bang! :D
296                         rl->Apply(user);
297                 }
298         }
299
300         virtual void OnBackgroundTimer(time_t curtime)
301         {
302                 if (!ZlineOnMatch) return;
303                 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
304                 {
305                         ZLine *zl = *i;
306                         if (ServerInstance->XLines->AddLine(zl,NULL))
307                         {
308                                 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s", 
309                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
310                                 ServerInstance->XLines->ApplyLines();
311                         }
312                 }
313                 background_zlines.clear();
314         }
315
316 };
317
318 MODULE_INIT(ModuleRLine)
319