1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 cmd_filter* filtcommand;
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 ¶meter);
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);
119 class cmd_filter : public Command
123 cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : Command(Me, "FILTER", 'o', 1), Base(f)
125 this->source = source;
126 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
129 CmdResult Handle(const char** parameters, int pcnt, User *user)
133 /* Deleting a filter */
134 if (Base->DeleteFilter(parameters[0]))
136 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
141 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
147 /* Adding a filter */
150 std::string freeform = parameters[0];
151 std::string type = parameters[1];
152 std::string flags = parameters[2];
157 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
159 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
167 duration = ServerInstance->Duration(parameters[3]);
168 reason = parameters[4];
172 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
178 reason = parameters[3];
180 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
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());
190 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
196 this->TooFewParams(user, ".");
203 void TooFewParams(User* user, const std::string &extra_text)
205 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
209 bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int flags)
211 if ((filter->flag_no_opers) && IS_OPER(user))
213 if ((flags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
215 if ((flags & FLAG_NOTICE) && (!filter->flag_notice))
217 if ((flags & FLAG_QUIT) && (!filter->flag_quit_message))
219 if ((flags & FLAG_PART) && (!filter->flag_part_message))
224 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
226 filtcommand = new cmd_filter(this, Me, source);
227 ServerInstance->AddCommand(filtcommand);
230 FilterBase::~FilterBase()
234 void FilterBase::Implements(char* List)
236 List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
239 int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
241 flags = FLAG_PRIVMSG;
242 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
245 int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
250 /* Leave ulines alone */
251 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
254 FilterResult* f = this->FilterMatch(user, text, flags);
257 std::string target = "";
258 if (target_type == TYPE_USER)
260 User* t = (User*)dest;
261 target = std::string(t->nick);
263 else if (target_type == TYPE_CHANNEL)
265 Channel* t = (Channel*)dest;
266 target = std::string(t->name);
268 if (f->action == "block")
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);
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 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
285 ServerInstance->XLines->apply_lines(APPLY_GLINES);
286 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
290 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
296 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, User *user, bool validated, const std::string &original_line)
299 if ((validated == 1) && (IS_LOCAL(user)))
301 std::string checkline;
302 int replacepoint = 0;
303 bool parting = false;
305 if (command == "QUIT")
307 /* QUIT with no reason: nothing to do */
311 checkline = parameters[0];
316 else if (command == "PART")
318 /* PART with no reason: nothing to do */
322 checkline = parameters[1];
328 /* We're only messing with PART and QUIT */
331 FilterResult* f = NULL;
334 f = this->FilterMatch(user, checkline, flags);
337 /* PART or QUIT reason doesnt match a filter */
340 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
341 Command* c = ServerInstance->Parser->GetHandler(command);
344 const char* params[MAXPARAMETERS];
345 for (int item = 0; item < pcnt; item++)
346 params[item] = parameters[item];
347 params[replacepoint] = "Reason filtered";
349 /* We're blocking, OR theyre quitting and its a KILL action
350 * (we cant kill someone whos already quitting, so filter them anyway)
352 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
354 c->Handle(params, pcnt, user);
359 /* Are they parting, if so, kill is applicable */
360 if ((parting) && (f->action == "kill"))
362 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
363 User::QuitUser(ServerInstance, user, "Filtered: " + f->reason);
365 if (f->action == "gline")
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());
371 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
373 ServerInstance->XLines->apply_lines(APPLY_GLINES);
374 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
385 void FilterBase::OnRehash(User* user, const std::string ¶meter)
389 Version FilterBase::GetVersion()
391 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
395 std::string FilterBase::EncodeFilter(FilterResult* filter)
397 std::ostringstream stream;
398 std::string x = filter->freeform;
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++)
405 stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
409 FilterResult FilterBase::DecodeFilter(const std::string &data)
412 irc::tokenstream tokens(data);
413 tokens.GetToken(res.freeform);
414 tokens.GetToken(res.action);
415 tokens.GetToken(res.flags);
416 if (res.flags == "-")
418 res.FillFlags(res.flags);
419 tokens.GetToken(res.gline_time);
420 tokens.GetToken(res.reason);
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++)
430 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
432 this->SyncFilters(proto, opaque);
435 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
437 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
440 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
442 if ((target_type == TYPE_OTHER) && (extname == "filter"))
444 FilterResult data = DecodeFilter(extdata);
445 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);