]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_regex.h"
16 #include "xline.h"
17
18 static Module* rxengine = NULL;
19 static Module* mymodule = NULL; /* Needed to let RLine send request! */
20 static bool ZlineOnMatch = false;
21 static std::vector<ZLine *> background_zlines;
22
23 /* $ModDesc: RLINE: Regexp user banning. */
24
25 class RLine : public XLine
26 {
27  public:
28
29         /** Create a R-Line.
30          * @param s_time The set time
31          * @param d The duration of the xline
32          * @param src The sender of the xline
33          * @param re The reason of the xline
34          * @param regex Pattern to match with
35          * @
36          */
37         RLine(time_t s_time, long d, std::string src, std::string re, std::string regexs)
38                 : XLine(s_time, d, src, re, "R")
39         {
40                 matchtext = regexs;
41
42                 if (!rxengine)
43                 {
44                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
45                         throw ModuleException("Regex engine not set or loaded!");
46                 }
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 = RegexFactoryRequest(mymodule, rxengine, regexs).Create();
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                         background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName, reason.c_str(), u->GetIPString()));
79                 }
80                 DefaultApply(u, "R", false);
81         }
82
83         void DisplayExpiry()
84         {
85                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
86                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
87         }
88
89         const char* Displayable()
90         {
91                 return matchtext.c_str();
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         RLineFactory() : XLineFactory("R")
106         {
107         }
108
109         /** Generate a RLine
110          */
111         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
112         {
113                 return new RLine(set_time, duration, source, reason, xline_specific_mask);
114         }
115
116         ~RLineFactory()
117         {
118         }
119 };
120
121 /** Handle /RLINE
122  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
123  */
124 class CommandRLine : public Command
125 {
126         std::string rxengine;
127
128  public:
129         CommandRLine(Module* Creator) : Command(Creator,"RLINE", 1, 3)
130         {
131                 flags_needed = 'o'; this->syntax = "<regex> [<rline-duration>] :<reason>";
132         }
133
134         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
135         {
136
137                 if (parameters.size() >= 3)
138                 {
139                         // Adding - XXX todo make this respect <insane> tag perhaps..
140
141                         long duration = ServerInstance->Duration(parameters[1]);
142                         RLine *r = NULL;
143
144                         try
145                         {
146                                 r = new RLine(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
147                         }
148                         catch (ModuleException &e)
149                         {
150                                 ServerInstance->SNO->WriteToSnoMask('a',"Could not add RLINE: %s", e.GetReason());
151                         }
152
153                         if (r)
154                         {
155                                 if (ServerInstance->XLines->AddLine(r, user))
156                                 {
157                                         if (!duration)
158                                         {
159                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent R-line for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
160                                         }
161                                         else
162                                         {
163                                                 time_t c_requires_crap = duration + ServerInstance->Time();
164                                                 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());
165                                         }
166
167                                         ServerInstance->XLines->ApplyLines();
168                                 }
169                                 else
170                                 {
171                                         delete r;
172                                         user->WriteServ("NOTICE %s :*** R-Line for %s already exists", user->nick.c_str(), parameters[0].c_str());
173                                 }
174                         }
175                 }
176                 else
177                 {
178                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "R", user))
179                         {
180                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed R-line on %s",user->nick.c_str(),parameters[0].c_str());
181                         }
182                         else
183                         {
184                                 user->WriteServ("NOTICE %s :*** R-Line %s not found in list, try /stats R.",user->nick.c_str(),parameters[0].c_str());
185                         }
186                 }
187
188                 return CMD_SUCCESS;
189         }
190
191         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
192         {
193                 return ROUTE_BROADCAST;
194         }
195 };
196
197 class ModuleRLine : public Module
198 {
199  private:
200         CommandRLine r;
201         RLineFactory f;
202         bool MatchOnNickChange;
203         std::string RegexEngine;
204
205  public:
206         ModuleRLine() : r(this)
207         {
208                 mymodule = this;
209                 OnRehash(NULL);
210
211                 ServerInstance->Modules->UseInterface("RegularExpression");
212
213                 ServerInstance->AddCommand(&r);
214                 ServerInstance->XLines->RegisterFactory(&f);
215
216                 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnLoadModule, I_OnStats, I_OnBackgroundTimer };
217                 ServerInstance->Modules->Attach(eventlist, this, 6);
218
219         }
220
221         virtual ~ModuleRLine()
222         {
223                 ServerInstance->Modules->DoneWithInterface("RegularExpression");
224                 ServerInstance->XLines->DelAll("R");
225                 ServerInstance->XLines->UnregisterFactory(&f);
226         }
227
228         virtual Version GetVersion()
229         {
230                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, API_VERSION);
231         }
232
233         virtual void OnUserConnect(User* user)
234         {
235                 // Apply lines on user connect
236                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
237
238                 if (rl)
239                 {
240                         // Bang. :P
241                         rl->Apply(user);
242                 }
243         }
244
245         virtual void OnRehash(User *user)
246         {
247                 ConfigReader Conf;
248
249                 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
250                         background_zlines.clear();
251
252                 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
253                 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
254                 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
255
256                 if (!RegexEngine.empty())
257                 {
258                         if (RegexEngine == newrxengine)
259                                 return;
260
261                         ServerInstance->SNO->WriteToSnoMask('x', "Dumping all R-Lines due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
262                         ServerInstance->XLines->DelAll("R");
263                 }
264                 rxengine = 0;
265                 RegexEngine = newrxengine;
266                 modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
267                 if (ml)
268                 {
269                         for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
270                         {
271                                 if (RegexNameRequest(this, *i).Send() == newrxengine)
272                                 {
273                                         ServerInstance->SNO->WriteToSnoMask('a', "R-Line now using engine '%s'", RegexEngine.c_str());
274                                         rxengine = *i;
275                                 }
276                         }
277                 }
278                 if (!rxengine)
279                 {
280                         ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
281                 }
282         }
283
284         virtual ModResult OnStats(char symbol, User* user, string_list &results)
285         {
286                 if (symbol != 'R')
287                         return MOD_RES_PASSTHRU;
288
289                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
290                 return MOD_RES_DENY;
291         }
292
293         virtual void OnLoadModule(Module* mod, const std::string& name)
294         {
295                 if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
296                 {
297                         std::string rxname = RegexNameRequest(this, mod).Send();
298                         if (rxname == RegexEngine)
299                         {
300                                 ServerInstance->SNO->WriteToSnoMask('a', "R-Line now using engine '%s'", RegexEngine.c_str());
301                                 rxengine = mod;
302                         }
303                 }
304         }
305
306         virtual void OnUserPostNick(User *user, const std::string &oldnick)
307         {
308                 if (!IS_LOCAL(user))
309                         return;
310
311                 if (!MatchOnNickChange)
312                         return;
313
314                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
315
316                 if (rl)
317                 {
318                         // Bang! :D
319                         rl->Apply(user);
320                 }
321         }
322
323         virtual void OnBackgroundTimer(time_t curtime)
324         {
325                 if (!ZlineOnMatch) return;
326                 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
327                 {
328                         ZLine *zl = *i;
329                         if (ServerInstance->XLines->AddLine(zl,NULL))
330                         {
331                                 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s", 
332                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
333                                 ServerInstance->XLines->ApplyLines();
334                         }
335                 }
336                 background_zlines.clear();
337         }
338
339 };
340
341 MODULE_INIT(ModuleRLine)
342