]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
Clear up header insanity
[user/henk/code/inspircd.git] / src / modules / m_filter.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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_PART = 2,
19         FLAG_QUIT = 4,
20         FLAG_PRIVMSG = 8,
21         FLAG_NOTICE = 16
22 };
23
24 class FilterResult : public classbase
25 {
26  public:
27         std::string freeform;
28         std::string reason;
29         std::string action;
30         long gline_time;
31         std::string flags;
32
33         bool flag_no_opers;
34         bool flag_part_message;
35         bool flag_quit_message;
36         bool flag_privmsg;
37         bool flag_notice;
38
39         FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, const std::string &fla) : freeform(free), reason(rea),
40                                                                         action(act), gline_time(gt), flags(fla)
41         {
42                 this->FillFlags(flags);
43         }
44
45         int FillFlags(const std::string &fl)
46         {
47                 flags = fl;
48                 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
49                 size_t x = 0;
50
51                 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
52                 {
53                         switch (*n)
54                         {
55                                 case 'o':
56                                         flag_no_opers = true;
57                                 break;
58                                 case 'P':
59                                         flag_part_message = true;
60                                 break;
61                                 case 'q':
62                                         flag_quit_message = true;
63                                 break;
64                                 case 'p':
65                                         flag_privmsg = true;
66                                 break;
67                                 case 'n':
68                                         flag_notice = true;
69                                 break;
70                                 case '*':
71                                         flag_no_opers = flag_part_message = flag_quit_message =
72                                                 flag_privmsg = flag_notice = true;
73                                 break;
74                                 default:
75                                         return x;
76                                 break;
77                         }
78                 }
79                 return 0;
80         }
81
82         FilterResult()
83         {
84         }
85
86         virtual ~FilterResult()
87         {
88         }
89 };
90
91 class CommandFilter;
92
93 class FilterBase : public Module
94 {
95         CommandFilter* filtcommand;
96         int flags;
97 protected:
98         std::vector<std::string> exemptfromfilter; // List of channel names excluded from filtering.
99  public:
100         FilterBase(InspIRCd* Me, const std::string &source);
101         virtual ~FilterBase();
102         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
103         virtual FilterResult* FilterMatch(User* 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(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
109         virtual void OnRehash(User* 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, User* user, string_list &results) = 0;
116         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line);
117         bool AppliesToMe(User* user, FilterResult* filter, int flags);
118 };
119
120 class CommandFilter : public Command
121 {
122         FilterBase* Base;
123  public:
124         CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &source) : Command(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, User *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(User* 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(User* user, FilterResult* filter, int flags)
211 {
212         if ((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 CommandFilter(this, Me, source);
228         ServerInstance->AddCommand(filtcommand);
229         Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash };
230         ServerInstance->Modules->Attach(eventlist, this, 7);
231 }
232
233 FilterBase::~FilterBase()
234 {
235 }
236
237 int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
238 {
239         flags = FLAG_PRIVMSG;
240         return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
241 }
242
243 int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
244 {
245         if (!flags)
246                 flags = FLAG_NOTICE;
247
248         /* Leave ulines alone */
249         if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
250                 return 0;
251
252         FilterResult* f = this->FilterMatch(user, text, flags);
253         if (f)
254         {
255                 std::string target = "";
256                 if (target_type == TYPE_USER)
257                 {
258                         User* t = (User*)dest;
259                         target = std::string(t->nick);
260                 }
261                 else if (target_type == TYPE_CHANNEL)
262                 {
263                         Channel* t = (Channel*)dest;
264                         target = std::string(t->name);
265                         std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), target);
266                         if (i != exemptfromfilter.end()) return 0;
267                 }
268                 if (f->action == "block")
269                 {       
270                         ServerInstance->SNO->WriteToSnoMask('A', std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
271                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason);
272                 }
273                 if (f->action == "silent")
274                 {
275                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
276                 }
277                 if (f->action == "kill")
278                 {
279                         User::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
280                 }
281                 if (f->action == "gline")
282                 {
283                         GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
284                         if (ServerInstance->XLines->AddLine(gl,NULL))
285                         {
286                                 ServerInstance->XLines->ApplyLines();
287                         }
288                         else
289                                 delete gl;
290                 }
291
292                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
293                 return 1;
294         }
295         return 0;
296 }
297
298 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
299 {
300         flags = 0;
301         if ((validated == 1) && (IS_LOCAL(user)))
302         {
303                 std::string checkline;
304                 int replacepoint = 0;
305                 bool parting = false;
306         
307                 if (command == "QUIT")
308                 {
309                         /* QUIT with no reason: nothing to do */
310                         if (pcnt < 1)
311                                 return 0;
312
313                         checkline = parameters[0];
314                         replacepoint = 0;
315                         parting = false;
316                         flags = FLAG_QUIT;
317                 }
318                 else if (command == "PART")
319                 {
320                         /* PART with no reason: nothing to do */
321                         if (pcnt < 2)
322                                 return 0;
323
324                         std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), parameters[0]);
325                         if (i != exemptfromfilter.end()) return 0;
326                         checkline = parameters[1];
327                         replacepoint = 1;
328                         parting = true;
329                         flags = FLAG_PART;
330                 }
331                 else
332                         /* We're only messing with PART and QUIT */
333                         return 0;
334
335                 FilterResult* f = NULL;
336                 
337                 if (flags)
338                         f = this->FilterMatch(user, checkline, flags);
339
340                 if (!f)
341                         /* PART or QUIT reason doesnt match a filter */
342                         return 0;
343
344                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
345                 Command* c = ServerInstance->Parser->GetHandler(command);
346                 if (c)
347                 {
348                         const char* params[MAXPARAMETERS];
349                         for (int item = 0; item < pcnt; item++)
350                                 params[item] = parameters[item];
351                         params[replacepoint] = "Reason filtered";
352
353                         /* We're blocking, OR theyre quitting and its a KILL action
354                          * (we cant kill someone whos already quitting, so filter them anyway)
355                          */
356                         if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
357                         {
358                                 c->Handle(params, pcnt, user);
359                                 return 1;
360                         }
361                         else
362                         {
363                                 /* Are they parting, if so, kill is applicable */
364                                 if ((parting) && (f->action == "kill"))
365                                 {
366                                         user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
367                                         User::QuitUser(ServerInstance, user, "Filtered: " + f->reason);
368                                 }
369                                 if (f->action == "gline")
370                                 {
371                                         /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
372                                         GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
373                                         if (ServerInstance->XLines->AddLine(gl,NULL))
374                                         {
375                                                 ServerInstance->XLines->ApplyLines();
376                                         }
377                                         else
378                                                 delete gl;
379                                 }
380                                 return 1;
381                         }
382                 }
383                 return 0;
384         }
385         return 0;
386 }
387
388 void FilterBase::OnRehash(User* user, const std::string &parameter)
389 {
390         ConfigReader* MyConf = new ConfigReader(ServerInstance);
391         std::vector<std::string>().swap(exemptfromfilter);
392         for (int index = 0; index < MyConf->Enumerate("exemptfromfilter"); ++index)
393         {
394                 std::string chan = MyConf->ReadValue("exemptfromfilter", "channel", index);
395                 if (!chan.empty()) {
396                         exemptfromfilter.push_back(chan);
397                 }
398         }
399         delete MyConf;
400 }
401
402 Version FilterBase::GetVersion()
403 {
404         return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
405 }
406
407
408 std::string FilterBase::EncodeFilter(FilterResult* filter)
409 {
410         std::ostringstream stream;
411         std::string x = filter->freeform;
412
413         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
414         for (std::string::iterator n = x.begin(); n != x.end(); n++)
415                 if (*n == ' ')
416                         *n = '\7';
417
418         stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
419         return stream.str();
420 }
421
422 FilterResult FilterBase::DecodeFilter(const std::string &data)
423 {
424         FilterResult res;
425         irc::tokenstream tokens(data);
426         tokens.GetToken(res.freeform);
427         tokens.GetToken(res.action);
428         tokens.GetToken(res.flags);
429         if (res.flags == "-")
430                 res.flags = "";
431         res.FillFlags(res.flags);
432         tokens.GetToken(res.gline_time);
433         tokens.GetToken(res.reason);
434
435         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
436         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
437                 if (*n == '\7')
438                         *n = ' ';
439
440         return res;
441 }
442
443 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
444 {
445         this->SyncFilters(proto, opaque);
446 }
447
448 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
449 {
450         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
451 }
452
453 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
454 {
455         if ((target_type == TYPE_OTHER) && (extname == "filter"))
456         {
457                 FilterResult data = DecodeFilter(extdata);
458                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
459         }
460 }
461