]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
This one too, grr
[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_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  public:
98         FilterBase(InspIRCd* Me, const std::string &source);
99         virtual ~FilterBase();
100         virtual void Implements(char* List);
101         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
102         virtual FilterResult* FilterMatch(User* user, const std::string &text, int flags) = 0;
103         virtual bool DeleteFilter(const std::string &freeform) = 0;
104         virtual void SyncFilters(Module* proto, void* opaque) = 0;
105         virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
106         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;
107         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
108         virtual void OnRehash(User* user, const std::string &parameter);
109         virtual Version GetVersion();
110         std::string EncodeFilter(FilterResult* filter);
111         FilterResult DecodeFilter(const std::string &data);
112         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable = false);
113         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
114         virtual int OnStats(char symbol, User* user, string_list &results) = 0;
115         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line);
116         bool AppliesToMe(User* user, FilterResult* filter, int flags);
117 };
118
119 class CommandFilter : public Command
120 {
121         FilterBase* Base;
122  public:
123         CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &source) : Command(Me, "FILTER", 'o', 1), Base(f)
124         {
125                 this->source = source;
126                 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
127         }
128
129         CmdResult Handle(const char** parameters, int pcnt, User *user)
130         {
131                 if (pcnt == 1)
132                 {
133                         /* Deleting a filter */
134                         if (Base->DeleteFilter(parameters[0]))
135                         {
136                                 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
137                                 return CMD_SUCCESS;
138                         }
139                         else
140                         {
141                                 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
142                                 return CMD_FAILURE;
143                         }
144                 }
145                 else
146                 {
147                         /* Adding a filter */
148                         if (pcnt >= 4)
149                         {
150                                 std::string freeform = parameters[0];
151                                 std::string type = parameters[1];
152                                 std::string flags = parameters[2];
153                                 std::string reason;
154                                 long duration = 0;
155
156
157                                 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
158                                 {
159                                         user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
160                                         return CMD_FAILURE;
161                                 }
162
163                                 if (type == "gline")
164                                 {
165                                         if (pcnt >= 5)
166                                         {
167                                                 duration = ServerInstance->Duration(parameters[3]);
168                                                 reason = parameters[4];
169                                         }
170                                         else
171                                         {
172                                                 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
173                                                 return CMD_FAILURE;
174                                         }
175                                 }
176                                 else
177                                 {
178                                         reason = parameters[3];
179                                 }
180                                 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
181                                 if (result.first)
182                                 {
183                                         user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick, freeform.c_str(),
184                                                         type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[3] : ""),
185                                                         flags.c_str(), reason.c_str());
186                                         return CMD_SUCCESS;
187                                 }
188                                 else
189                                 {
190                                         user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
191                                         return CMD_FAILURE;
192                                 }
193                         }
194                         else
195                         {
196                                 this->TooFewParams(user, ".");
197                                 return CMD_FAILURE;
198                         }
199
200                 }
201         }
202
203         void TooFewParams(User* user, const std::string &extra_text)
204         {
205                 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
206         }
207 };
208
209 bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int flags)
210 {
211         if ((filter->flag_no_opers) && IS_OPER(user))
212                 return false;
213         if ((flags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
214                 return false;
215         if ((flags & FLAG_NOTICE) && (!filter->flag_notice))
216                 return false;
217         if ((flags & FLAG_QUIT)   && (!filter->flag_quit_message))
218                 return false;
219         if ((flags & FLAG_PART)   && (!filter->flag_part_message))
220                 return false;
221         return true;
222 }
223
224 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
225 {
226         filtcommand = new CommandFilter(this, Me, source);
227         ServerInstance->AddCommand(filtcommand);
228 }
229
230 FilterBase::~FilterBase()
231 {
232 }
233
234 void FilterBase::Implements(char* List)
235 {
236         List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
237 }
238
239 int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
240 {
241         flags = FLAG_PRIVMSG;
242         return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
243 }
244
245 int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
246 {
247         if (!flags)
248                 flags = FLAG_NOTICE;
249
250         /* Leave ulines alone */
251         if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
252                 return 0;
253
254         FilterResult* f = this->FilterMatch(user, text, flags);
255         if (f)
256         {
257                 std::string target = "";
258                 if (target_type == TYPE_USER)
259                 {
260                         User* t = (User*)dest;
261                         target = std::string(t->nick);
262                 }
263                 else if (target_type == TYPE_CHANNEL)
264                 {
265                         Channel* t = (Channel*)dest;
266                         target = std::string(t->name);
267                 }
268                 if (f->action == "block")
269                 {       
270                         ServerInstance->WriteOpers(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))
285                         {
286                                 ServerInstance->XLines->ApplyLines();
287                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
288                         }
289                         else
290                                 delete gl;
291                 }
292
293                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
294                 return 1;
295         }
296         return 0;
297 }
298
299 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
300 {
301         flags = 0;
302         if ((validated == 1) && (IS_LOCAL(user)))
303         {
304                 std::string checkline;
305                 int replacepoint = 0;
306                 bool parting = false;
307         
308                 if (command == "QUIT")
309                 {
310                         /* QUIT with no reason: nothing to do */
311                         if (pcnt < 1)
312                                 return 0;
313
314                         checkline = parameters[0];
315                         replacepoint = 0;
316                         parting = false;
317                         flags = FLAG_QUIT;
318                 }
319                 else if (command == "PART")
320                 {
321                         /* PART with no reason: nothing to do */
322                         if (pcnt < 2)
323                                 return 0;
324
325                         checkline = parameters[1];
326                         replacepoint = 1;
327                         parting = true;
328                         flags = FLAG_PART;
329                 }
330                 else
331                         /* We're only messing with PART and QUIT */
332                         return 0;
333
334                 FilterResult* f = NULL;
335                 
336                 if (flags)
337                         f = this->FilterMatch(user, checkline, flags);
338
339                 if (!f)
340                         /* PART or QUIT reason doesnt match a filter */
341                         return 0;
342
343                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
344                 Command* c = ServerInstance->Parser->GetHandler(command);
345                 if (c)
346                 {
347                         const char* params[MAXPARAMETERS];
348                         for (int item = 0; item < pcnt; item++)
349                                 params[item] = parameters[item];
350                         params[replacepoint] = "Reason filtered";
351
352                         /* We're blocking, OR theyre quitting and its a KILL action
353                          * (we cant kill someone whos already quitting, so filter them anyway)
354                          */
355                         if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
356                         {
357                                 c->Handle(params, pcnt, user);
358                                 return 1;
359                         }
360                         else
361                         {
362                                 /* Are they parting, if so, kill is applicable */
363                                 if ((parting) && (f->action == "kill"))
364                                 {
365                                         user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
366                                         User::QuitUser(ServerInstance, user, "Filtered: " + f->reason);
367                                 }
368                                 if (f->action == "gline")
369                                 {
370                                         /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
371                                         GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
372                                         if (ServerInstance->XLines->AddLine(gl))
373                                         {
374                                                 ServerInstance->XLines->ApplyLines();
375                                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
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 }
391         
392 Version FilterBase::GetVersion()
393 {
394         return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
395 }
396
397
398 std::string FilterBase::EncodeFilter(FilterResult* filter)
399 {
400         std::ostringstream stream;
401         std::string x = filter->freeform;
402
403         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
404         for (std::string::iterator n = x.begin(); n != x.end(); n++)
405                 if (*n == ' ')
406                         *n = '\7';
407
408         stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
409         return stream.str();
410 }
411
412 FilterResult FilterBase::DecodeFilter(const std::string &data)
413 {
414         FilterResult res;
415         irc::tokenstream tokens(data);
416         tokens.GetToken(res.freeform);
417         tokens.GetToken(res.action);
418         tokens.GetToken(res.flags);
419         if (res.flags == "-")
420                 res.flags = "";
421         res.FillFlags(res.flags);
422         tokens.GetToken(res.gline_time);
423         tokens.GetToken(res.reason);
424
425         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
426         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
427                 if (*n == '\7')
428                         *n = ' ';
429
430         return res;
431 }
432
433 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
434 {
435         this->SyncFilters(proto, opaque);
436 }
437
438 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
439 {
440         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
441 }
442
443 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
444 {
445         if ((target_type == TYPE_OTHER) && (extname == "filter"))
446         {
447                 FilterResult data = DecodeFilter(extdata);
448                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
449         }
450 }
451