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