1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
24 class FilterResult : public classbase
34 bool flag_part_message;
35 bool flag_quit_message;
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)
42 this->FillFlags(flags);
45 int FillFlags(const std::string &fl)
48 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
51 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
59 flag_part_message = true;
62 flag_quit_message = true;
71 flag_no_opers = flag_part_message = flag_quit_message =
72 flag_privmsg = flag_notice = true;
86 virtual ~FilterResult()
93 class FilterBase : public Module
95 CommandFilter* filtcommand;
98 std::vector<std::string> exemptfromfilter; // List of channel names excluded from filtering.
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 ¶meter);
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);
120 class CommandFilter : public Command
124 CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &source) : Command(Me, "FILTER", 'o', 1), Base(f)
126 this->source = source;
127 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
130 CmdResult Handle(const char** parameters, int pcnt, User *user)
134 /* Deleting a filter */
135 if (Base->DeleteFilter(parameters[0]))
137 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
142 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
148 /* Adding a filter */
151 std::string freeform = parameters[0];
152 std::string type = parameters[1];
153 std::string flags = parameters[2];
158 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
160 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
168 duration = ServerInstance->Duration(parameters[3]);
169 reason = parameters[4];
173 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
179 reason = parameters[3];
181 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
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());
191 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
197 this->TooFewParams(user, ".");
204 void TooFewParams(User* user, const std::string &extra_text)
206 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
210 bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int flags)
212 if ((filter->flag_no_opers) && IS_OPER(user))
214 if ((flags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
216 if ((flags & FLAG_NOTICE) && (!filter->flag_notice))
218 if ((flags & FLAG_QUIT) && (!filter->flag_quit_message))
220 if ((flags & FLAG_PART) && (!filter->flag_part_message))
225 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
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);
233 FilterBase::~FilterBase()
237 int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
239 flags = FLAG_PRIVMSG;
240 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
243 int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
248 /* Leave ulines alone */
249 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
252 FilterResult* f = this->FilterMatch(user, text, flags);
255 std::string target = "";
256 if (target_type == TYPE_USER)
258 User* t = (User*)dest;
259 target = std::string(t->nick);
261 else if (target_type == TYPE_CHANNEL)
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;
268 if (f->action == "block")
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);
273 if (f->action == "silent")
275 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
277 if (f->action == "kill")
279 User::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
281 if (f->action == "gline")
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))
286 ServerInstance->XLines->ApplyLines();
292 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
298 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
301 if ((validated == 1) && (IS_LOCAL(user)))
303 std::string checkline;
304 int replacepoint = 0;
305 bool parting = false;
307 if (command == "QUIT")
309 /* QUIT with no reason: nothing to do */
313 checkline = parameters[0];
318 else if (command == "PART")
320 /* PART with no reason: nothing to do */
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];
332 /* We're only messing with PART and QUIT */
335 FilterResult* f = NULL;
338 f = this->FilterMatch(user, checkline, flags);
341 /* PART or QUIT reason doesnt match a filter */
344 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
345 Command* c = ServerInstance->Parser->GetHandler(command);
348 const char* params[MAXPARAMETERS];
349 for (int item = 0; item < pcnt; item++)
350 params[item] = parameters[item];
351 params[replacepoint] = "Reason filtered";
353 /* We're blocking, OR theyre quitting and its a KILL action
354 * (we cant kill someone whos already quitting, so filter them anyway)
356 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
358 c->Handle(params, pcnt, user);
363 /* Are they parting, if so, kill is applicable */
364 if ((parting) && (f->action == "kill"))
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);
369 if (f->action == "gline")
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))
375 ServerInstance->XLines->ApplyLines();
388 void FilterBase::OnRehash(User* user, const std::string ¶meter)
390 ConfigReader* MyConf = new ConfigReader(ServerInstance);
391 vector<std::string>().swap(exemptfromfilter);
392 for (int index = 0; index < MyConf->Enumerate("exemptfromfilter"); ++index)
394 std::string chan = MyConf->ReadValue("exemptfromfilter", "channel", index);
396 exemptfromfilter.push_back(chan);
402 Version FilterBase::GetVersion()
404 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
408 std::string FilterBase::EncodeFilter(FilterResult* filter)
410 std::ostringstream stream;
411 std::string x = filter->freeform;
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++)
418 stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
422 FilterResult FilterBase::DecodeFilter(const std::string &data)
425 irc::tokenstream tokens(data);
426 tokens.GetToken(res.freeform);
427 tokens.GetToken(res.action);
428 tokens.GetToken(res.flags);
429 if (res.flags == "-")
431 res.FillFlags(res.flags);
432 tokens.GetToken(res.gline_time);
433 tokens.GetToken(res.reason);
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++)
443 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
445 this->SyncFilters(proto, opaque);
448 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
450 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
453 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
455 if ((target_type == TYPE_OTHER) && (extname == "filter"))
457 FilterResult data = DecodeFilter(extdata);
458 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);