]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
...because every now and again, i have to do a massive commit.
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 bool ZlineOnMatch = false;
19 static std::vector<ZLine *> background_zlines;
20
21 /* $ModDesc: RLINE: Regexp user banning. */
22
23 class RLine : public XLine
24 {
25  public:
26
27         /** Create a R-Line.
28          * @param s_time The set time
29          * @param d The duration of the xline
30          * @param src The sender of the xline
31          * @param re The reason of the xline
32          * @param regex Pattern to match with
33          * @
34          */
35         RLine(time_t s_time, long d, std::string src, std::string re, std::string regexs, dynamic_reference<RegexFactory>& rxfactory)
36                 : XLine(s_time, d, src, re, "R")
37         {
38                 matchtext = regexs;
39
40                 /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
41                  * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
42                  */
43                 regex = rxfactory->Create(regexs);
44         }
45
46         /** Destructor
47          */
48         ~RLine()
49         {
50                 delete regex;
51         }
52
53         bool Matches(User *u)
54         {
55                 if (u->exempt)
56                         return false;
57
58                 std::string compare = u->nick + "!" + u->ident + "@" + u->host + " " + u->fullname;
59                 return regex->Matches(compare);
60         }
61
62         bool Matches(const std::string &compare)
63         {
64                 return regex->Matches(compare);
65         }
66
67         void Apply(User* u)
68         {
69                 if (ZlineOnMatch) {
70                         background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString()));
71                 }
72                 DefaultApply(u, "R", false);
73         }
74
75         void DisplayExpiry()
76         {
77                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired R-line %s (set by %s %ld seconds ago)",
78                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
79         }
80
81         const char* Displayable()
82         {
83                 return matchtext.c_str();
84         }
85
86         std::string matchtext;
87
88         Regex *regex;
89 };
90
91
92 /** An XLineFactory specialized to generate RLine* pointers
93  */
94 class RLineFactory : public XLineFactory
95 {
96  public:
97         dynamic_reference<RegexFactory>& rxfactory;
98         RLineFactory(dynamic_reference<RegexFactory>& rx) : XLineFactory("R"), rxfactory(rx)
99         {
100         }
101         
102         /** Generate a RLine
103          */
104         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
105         {
106                 if (!rxfactory)
107                 {
108                         ServerInstance->SNO->WriteToSnoMask('a', "Cannot create regexes until engine is set to a loaded provider!");
109                         throw ModuleException("Regex engine not set or loaded!");
110                 }
111
112                 return new RLine(set_time, duration, source, reason, xline_specific_mask, rxfactory);
113         }
114
115         ~RLineFactory()
116         {
117         }
118 };
119
120 /** Handle /RLINE
121  * Syntax is same as other lines: RLINE regex_goes_here 1d :reason
122  */
123 class CommandRLine : public Command
124 {
125         std::string rxengine;
126         RLineFactory& factory;
127
128  public:
129         CommandRLine(Module* Creator, RLineFactory& rlf) : Command(Creator,"RLINE", 1, 3), factory(rlf)
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                         XLine *r = NULL;
143
144                         try
145                         {
146                                 r = factory.Generate(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         dynamic_reference<RegexFactory> rxfactory;
201         RLineFactory f;
202         CommandRLine r;
203         bool MatchOnNickChange;
204         std::string RegexEngine;
205
206  public:
207         ModuleRLine() : rxfactory(this, "regex"), f(rxfactory), r(this, f)
208         {
209                 OnRehash(NULL);
210
211                 ServerInstance->AddCommand(&r);
212                 ServerInstance->XLines->RegisterFactory(&f);
213
214                 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer };
215                 ServerInstance->Modules->Attach(eventlist, this, 5);
216         }
217
218         virtual ~ModuleRLine()
219         {
220                 ServerInstance->XLines->DelAll("R");
221                 ServerInstance->XLines->UnregisterFactory(&f);
222         }
223
224         virtual Version GetVersion()
225         {
226                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR);
227         }
228
229         virtual void OnUserConnect(LocalUser* user)
230         {
231                 // Apply lines on user connect
232                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
233
234                 if (rl)
235                 {
236                         // Bang. :P
237                         rl->Apply(user);
238                 }
239         }
240
241         virtual void OnRehash(User *user)
242         {
243                 ConfigReader Conf;
244
245                 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
246                         background_zlines.clear();
247
248                 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
249                 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
250                 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
251
252                 rxfactory.SetProvider("regex/" + newrxengine);
253                 if (!rxfactory)
254                 {
255                         ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
256                 }
257         }
258
259         virtual ModResult OnStats(char symbol, User* user, string_list &results)
260         {
261                 if (symbol != 'R')
262                         return MOD_RES_PASSTHRU;
263
264                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
265                 return MOD_RES_DENY;
266         }
267
268         virtual void OnUserPostNick(User *user, const std::string &oldnick)
269         {
270                 if (!IS_LOCAL(user))
271                         return;
272
273                 if (!MatchOnNickChange)
274                         return;
275
276                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
277
278                 if (rl)
279                 {
280                         // Bang! :D
281                         rl->Apply(user);
282                 }
283         }
284
285         virtual void OnBackgroundTimer(time_t curtime)
286         {
287                 if (!ZlineOnMatch) return;
288                 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
289                 {
290                         ZLine *zl = *i;
291                         if (ServerInstance->XLines->AddLine(zl,NULL))
292                         {
293                                 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s", 
294                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
295                                 ServerInstance->XLines->ApplyLines();
296                         }
297                 }
298                 background_zlines.clear();
299         }
300
301 };
302
303 MODULE_INIT(ModuleRLine)
304