]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
5e74177e5073ce59ccadd902c4d1074cd518a1c7
[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 cmd_filter;
92
93 class FilterBase : public Module
94 {
95         cmd_filter* 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(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
102         virtual FilterResult* FilterMatch(userrec* 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(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
108         virtual void OnRehash(userrec* 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, userrec* user, string_list &results) = 0;
115         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line);
116         bool AppliesToMe(userrec* user, FilterResult* filter, int flags);
117 };
118
119 class cmd_filter : public command_t
120 {
121         FilterBase* Base;
122  public:
123         cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(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, userrec *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(userrec* 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(userrec* 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 cmd_filter(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(userrec* 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(userrec* 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                         userrec* t = (userrec*)dest;
261                         target = std::string(t->nick);
262                 }
263                 else if (target_type == TYPE_CHANNEL)
264                 {
265                         chanrec* t = (chanrec*)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                         userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
280                 }
281                 if (f->action == "gline")
282                 {
283                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
284                         {
285                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
286                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
287                         }
288                 }
289
290                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
291                 return 1;
292         }
293         return 0;
294 }
295
296 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
297 {
298         flags = 0;
299         if ((validated == 1) && (IS_LOCAL(user)))
300         {
301                 std::string checkline;
302                 int replacepoint = 0;
303                 bool parting = false;
304         
305                 if (command == "QUIT")
306                 {
307                         /* QUIT with no reason: nothing to do */
308                         if (pcnt < 1)
309                                 return 0;
310
311                         checkline = parameters[0];
312                         replacepoint = 0;
313                         parting = false;
314                         flags = FLAG_QUIT;
315                 }
316                 else if (command == "PART")
317                 {
318                         /* PART with no reason: nothing to do */
319                         if (pcnt < 2)
320                                 return 0;
321
322                         checkline = parameters[1];
323                         replacepoint = 1;
324                         parting = true;
325                         flags = FLAG_PART;
326                 }
327                 else
328                         /* We're only messing with PART and QUIT */
329                         return 0;
330
331                 FilterResult* f = NULL;
332                 
333                 if (flags)
334                         f = this->FilterMatch(user, checkline, flags);
335
336                 if (!f)
337                         /* PART or QUIT reason doesnt match a filter */
338                         return 0;
339
340                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
341                 command_t* c = ServerInstance->Parser->GetHandler(command);
342                 if (c)
343                 {
344                         const char* params[MAXPARAMETERS];
345                         for (int item = 0; item < pcnt; item++)
346                                 params[item] = parameters[item];
347                         params[replacepoint] = "Reason filtered";
348
349                         /* We're blocking, OR theyre quitting and its a KILL action
350                          * (we cant kill someone whos already quitting, so filter them anyway)
351                          */
352                         if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
353                         {
354                                 c->Handle(params, pcnt, user);
355                                 return 1;
356                         }
357                         else
358                         {
359                                 /* Are they parting, if so, kill is applicable */
360                                 if ((parting) && (f->action == "kill"))
361                                 {
362                                         user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
363                                         userrec::QuitUser(ServerInstance, user, "Filtered: " + f->reason);
364                                 }
365                                 if (f->action == "gline")
366                                 {
367                                         /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
368                                         std::string wild = "*@";
369                                         wild.append(user->GetIPString());
370
371                                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
372                                         {
373                                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
374                                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
375                                         }
376                                 }
377                                 return 1;
378                         }
379                 }
380                 return 0;
381         }
382         return 0;
383 }
384
385 void FilterBase::OnRehash(userrec* user, const std::string &parameter)
386 {
387 }
388         
389 Version FilterBase::GetVersion()
390 {
391         return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
392 }
393
394
395 std::string FilterBase::EncodeFilter(FilterResult* filter)
396 {
397         std::ostringstream stream;
398         std::string x = filter->freeform;
399
400         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
401         for (std::string::iterator n = x.begin(); n != x.end(); n++)
402                 if (*n == ' ')
403                         *n = '\7';
404
405         stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
406         return stream.str();
407 }
408
409 FilterResult FilterBase::DecodeFilter(const std::string &data)
410 {
411         FilterResult res;
412         irc::tokenstream tokens(data);
413         tokens.GetToken(res.freeform);
414         tokens.GetToken(res.action);
415         tokens.GetToken(res.flags);
416         if (res.flags == "-")
417                 res.flags = "";
418         res.FillFlags(res.flags);
419         tokens.GetToken(res.gline_time);
420         tokens.GetToken(res.reason);
421
422         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
423         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
424                 if (*n == '\7')
425                         *n = ' ';
426
427         return res;
428 }
429
430 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
431 {
432         this->SyncFilters(proto, opaque);
433 }
434
435 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
436 {
437         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
438 }
439
440 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
441 {
442         if ((target_type == TYPE_OTHER) && (extname == "filter"))
443         {
444                 FilterResult data = DecodeFilter(extdata);
445                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
446         }
447 }
448