]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Prevent using invalid UIDs and enforce UID/SID matching
[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
205  public:
206         ModuleRLine() : rxfactory(this, "regex"), f(rxfactory), r(this, f)
207         {
208         }
209
210         void init()
211         {
212                 OnRehash(NULL);
213
214                 ServerInstance->AddCommand(&r);
215                 ServerInstance->XLines->RegisterFactory(&f);
216
217                 Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer };
218                 ServerInstance->Modules->Attach(eventlist, this, 5);
219         }
220
221         virtual ~ModuleRLine()
222         {
223                 ServerInstance->XLines->DelAll("R");
224                 ServerInstance->XLines->UnregisterFactory(&f);
225         }
226
227         virtual Version GetVersion()
228         {
229                 return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
230         }
231
232         virtual void OnUserConnect(LocalUser* user)
233         {
234                 // Apply lines on user connect
235                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
236
237                 if (rl)
238                 {
239                         // Bang. :P
240                         rl->Apply(user);
241                 }
242         }
243
244         virtual void OnRehash(User *user)
245         {
246                 ConfigReader Conf;
247
248                 if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
249                         background_zlines.clear();
250
251                 MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
252                 ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
253                 std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
254
255                 if (newrxengine.empty())
256                         rxfactory.SetProvider("regex");
257                 else
258                         rxfactory.SetProvider("regex/" + newrxengine);
259                 if (!rxfactory)
260                 {
261                         ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
262                 }
263         }
264
265         virtual ModResult OnStats(char symbol, User* user, string_list &results)
266         {
267                 if (symbol != 'R')
268                         return MOD_RES_PASSTHRU;
269
270                 ServerInstance->XLines->InvokeStats("R", 223, user, results);
271                 return MOD_RES_DENY;
272         }
273
274         virtual void OnUserPostNick(User *user, const std::string &oldnick)
275         {
276                 if (!IS_LOCAL(user))
277                         return;
278
279                 if (!MatchOnNickChange)
280                         return;
281
282                 XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
283
284                 if (rl)
285                 {
286                         // Bang! :D
287                         rl->Apply(user);
288                 }
289         }
290
291         virtual void OnBackgroundTimer(time_t curtime)
292         {
293                 if (!ZlineOnMatch) return;
294                 for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
295                 {
296                         ZLine *zl = *i;
297                         if (ServerInstance->XLines->AddLine(zl,NULL))
298                         {
299                                 ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s", 
300                                         zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
301                                 ServerInstance->XLines->ApplyLines();
302                         }
303                 }
304                 background_zlines.clear();
305         }
306
307 };
308
309 MODULE_INIT(ModuleRLine)
310