]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
More (last of extra)
[user/henk/code/inspircd.git] / src / modules / m_filter.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "xline.h"
15
16 enum FilterFlags
17 {
18         FLAG_NOOPERS = 1,
19         FLAG_PART = 2,
20         FLAG_QUIT = 4,
21         FLAG_PRIVMSG = 8,
22         FLAG_NOTICE = 16
23 };
24
25 class FilterResult : public classbase
26 {
27  public:
28         std::string freeform;
29         std::string reason;
30         std::string action;
31         long gline_time;
32         std::string flags;
33
34         bool flag_no_opers;
35         bool flag_part_message;
36         bool flag_quit_message;
37         bool flag_privmsg;
38         bool flag_notice;
39
40         FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, const std::string &fla) : freeform(free), reason(rea),
41                                                                         action(act), gline_time(gt), flags(fla)
42         {
43                 this->FillFlags(flags);
44         }
45
46         int FillFlags(const std::string &fl)
47         {
48                 flags = fl;
49                 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
50                 size_t x = 0;
51
52                 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
53                 {
54                         switch (*n)
55                         {
56                                 case 'o':
57                                         flag_no_opers = true;
58                                 break;
59                                 case 'P':
60                                         flag_part_message = true;
61                                 break;
62                                 case 'q':
63                                         flag_quit_message = true;
64                                 break;
65                                 case 'p':
66                                         flag_privmsg = true;
67                                 break;
68                                 case 'n':
69                                         flag_notice = true;
70                                 break;
71                                 case '*':
72                                         flag_no_opers = flag_part_message = flag_quit_message =
73                                                 flag_privmsg = flag_notice = true;
74                                 break;
75                                 default:
76                                         return x;
77                                 break;
78                         }
79                 }
80                 return 0;
81         }
82
83         FilterResult()
84         {
85         }
86
87         virtual ~FilterResult()
88         {
89         }
90 };
91
92 class cmd_filter;
93
94 class FilterBase : public Module
95 {
96         cmd_filter* filtcommand;
97         int flags;
98  public:
99         FilterBase(InspIRCd* Me, const std::string &source);
100         virtual ~FilterBase();
101         virtual void Implements(char* List);
102         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
103         virtual FilterResult* FilterMatch(userrec* user, const std::string &text, int flags) = 0;
104         virtual bool DeleteFilter(const std::string &freeform) = 0;
105         virtual void SyncFilters(Module* proto, void* opaque) = 0;
106         virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
107         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flags) = 0;
108         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
109         virtual void OnRehash(userrec* user, const std::string &parameter);
110         virtual Version GetVersion();
111         std::string EncodeFilter(FilterResult* filter);
112         FilterResult DecodeFilter(const std::string &data);
113         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable = false);
114         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
115         virtual int OnStats(char symbol, userrec* user, string_list &results) = 0;
116         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line);
117         bool AppliesToMe(userrec* user, FilterResult* filter, int flags);
118 };
119
120 class cmd_filter : public command_t
121 {
122         FilterBase* Base;
123  public:
124         cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(Me, "FILTER", 'o', 1), Base(f)
125         {
126                 this->source = source;
127                 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
128         }
129
130         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
131         {
132                 if (pcnt == 1)
133                 {
134                         /* Deleting a filter */
135                         if (Base->DeleteFilter(parameters[0]))
136                         {
137                                 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
138                                 return CMD_SUCCESS;
139                         }
140                         else
141                         {
142                                 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
143                                 return CMD_FAILURE;
144                         }
145                 }
146                 else
147                 {
148                         /* Adding a filter */
149                         if (pcnt >= 4)
150                         {
151                                 std::string freeform = parameters[0];
152                                 std::string type = parameters[1];
153                                 std::string flags = parameters[2];
154                                 std::string reason;
155                                 long duration = 0;
156
157
158                                 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
159                                 {
160                                         user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
161                                         return CMD_FAILURE;
162                                 }
163
164                                 if (type == "gline")
165                                 {
166                                         if (pcnt >= 5)
167                                         {
168                                                 duration = ServerInstance->Duration(parameters[3]);
169                                                 reason = parameters[4];
170                                         }
171                                         else
172                                         {
173                                                 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
174                                                 return CMD_FAILURE;
175                                         }
176                                 }
177                                 else
178                                 {
179                                         reason = parameters[3];
180                                 }
181                                 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
182                                 if (result.first)
183                                 {
184                                         user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick, freeform.c_str(),
185                                                         type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[3] : ""),
186                                                         flags.c_str(), reason.c_str());
187                                         return CMD_SUCCESS;
188                                 }
189                                 else
190                                 {
191                                         user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
192                                         return CMD_FAILURE;
193                                 }
194                         }
195                         else
196                         {
197                                 this->TooFewParams(user, ".");
198                                 return CMD_FAILURE;
199                         }
200
201                 }
202         }
203
204         void TooFewParams(userrec* user, const std::string &extra_text)
205         {
206                 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
207         }
208 };
209
210 bool FilterBase::AppliesToMe(userrec* user, FilterResult* filter, int flags)
211 {
212         if ((flags & FLAG_NOOPERS) && (filter->flag_no_opers) && IS_OPER(user))
213                 return false;
214         if ((flags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
215                 return false;
216         if ((flags & FLAG_NOTICE) && (!filter->flag_notice))
217                 return false;
218         if ((flags & FLAG_QUIT)   && (!filter->flag_quit_message))
219                 return false;
220         if ((flags & FLAG_PART)   && (!filter->flag_part_message))
221                 return false;
222         return true;
223 }
224
225 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
226 {
227         filtcommand = new cmd_filter(this, Me, source);
228         ServerInstance->AddCommand(filtcommand);
229 }
230
231 FilterBase::~FilterBase()
232 {
233 }
234
235 void FilterBase::Implements(char* List)
236 {
237         List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
238 }
239
240 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
241 {
242         flags = FLAG_PRIVMSG;
243         return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
244 }
245
246 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
247 {
248         if (!flags)
249                 flags = FLAG_NOTICE;
250
251         /* Leave ulines alone */
252         if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
253                 return 0;
254
255         FilterResult* f = this->FilterMatch(user, text, flags);
256         if (f)
257         {
258                 std::string target = "";
259                 if (target_type == TYPE_USER)
260                 {
261                         userrec* t = (userrec*)dest;
262                         target = std::string(t->nick);
263                 }
264                 else if (target_type == TYPE_CHANNEL)
265                 {
266                         chanrec* t = (chanrec*)dest;
267                         target = std::string(t->name);
268                 }
269                 if (f->action == "block")
270                 {       
271                         ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
272                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason);
273                 }
274                 if (f->action == "silent")
275                 {
276                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
277                 }
278                 if (f->action == "kill")
279                 {
280                         userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
281                 }
282                 if (f->action == "gline")
283                 {
284                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
285                         {
286                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
287                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
288                         }
289                 }
290
291                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
292                 return 1;
293         }
294         return 0;
295 }
296
297 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
298 {
299         flags = 0;
300         if ((validated == 1) && (IS_LOCAL(user)))
301         {
302                 std::string checkline;
303                 int replacepoint = 0;
304                 bool parting = false;
305         
306                 if (command == "QUIT")
307                 {
308                         /* QUIT with no reason: nothing to do */
309                         if (pcnt < 1)
310                                 return 0;
311
312                         checkline = parameters[0];
313                         replacepoint = 0;
314                         parting = false;
315                         flags = FLAG_QUIT;
316                 }
317                 else if (command == "PART")
318                 {
319                         /* PART with no reason: nothing to do */
320                         if (pcnt < 2)
321                                 return 0;
322
323                         checkline = parameters[1];
324                         replacepoint = 1;
325                         parting = true;
326                         flags = FLAG_PART;
327                 }
328                 else
329                         /* We're only messing with PART and QUIT */
330                         return 0;
331
332                 FilterResult* f = NULL;
333                 
334                 if (flags)
335                         f = this->FilterMatch(user, checkline, flags);
336
337                 if (!f)
338                         /* PART or QUIT reason doesnt match a filter */
339                         return 0;
340
341                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
342                 command_t* c = ServerInstance->Parser->GetHandler(command);
343                 if (c)
344                 {
345                         const char* params[127];
346                         for (int item = 0; item < pcnt; item++)
347                                 params[item] = parameters[item];
348                         params[replacepoint] = "Reason filtered";
349
350                         /* We're blocking, OR theyre quitting and its a KILL action
351                          * (we cant kill someone whos already quitting, so filter them anyway)
352                          */
353                         if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
354                         {
355                                 c->Handle(params, pcnt, user);
356                                 return 1;
357                         }
358                         else
359                         {
360                                 /* Are they parting, if so, kill is applicable */
361                                 if ((parting) && (f->action == "kill"))
362                                 {
363                                         user->SetWriteError("Filtered: "+f->reason);
364                                         /* This WriteServ causes the write error to be applied.
365                                          * Its not safe to kill here with QuitUser in a PreCommand handler,
366                                          * so we do it this way, which is safe just about anywhere.
367                                          */
368                                         user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
369                                 }
370                                 if (f->action == "gline")
371                                 {
372                                         /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
373                                         std::string wild = "*@";
374                                         wild.append(user->GetIPString());
375
376                                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
377                                         {
378                                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
379                                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
380                                         }
381                                 }
382                                 return 1;
383                         }
384                 }
385                 return 0;
386         }
387         return 0;
388 }
389
390 void FilterBase::OnRehash(userrec* user, const std::string &parameter)
391 {
392 }
393         
394 Version FilterBase::GetVersion()
395 {
396         return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
397 }
398
399
400 std::string FilterBase::EncodeFilter(FilterResult* filter)
401 {
402         std::ostringstream stream;
403         std::string x = filter->freeform;
404
405         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
406         for (std::string::iterator n = x.begin(); n != x.end(); n++)
407                 if (*n == ' ')
408                         *n = '\7';
409
410         stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
411         return stream.str();
412 }
413
414 FilterResult FilterBase::DecodeFilter(const std::string &data)
415 {
416         FilterResult res;
417         irc::tokenstream tokens(data);
418         tokens.GetToken(res.freeform);
419         tokens.GetToken(res.action);
420         tokens.GetToken(res.flags);
421         if (res.flags == "-")
422                 res.flags = "";
423         res.FillFlags(res.flags);
424         tokens.GetToken(res.gline_time);
425         tokens.GetToken(res.reason);
426
427         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
428         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
429                 if (*n == '\7')
430                         *n = ' ';
431
432         return res;
433 }
434
435 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
436 {
437         this->SyncFilters(proto, opaque);
438 }
439
440 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
441 {
442         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
443 }
444
445 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
446 {
447         if ((target_type == TYPE_OTHER) && (extname == "filter"))
448         {
449                 FilterResult data = DecodeFilter(extdata);
450                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
451         }
452 }
453