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 * ---------------------------------------------------
25 class FilterResult : public classbase
35 bool flag_part_message;
36 bool flag_quit_message;
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)
43 this->FillFlags(flags);
46 int FillFlags(const std::string &fl)
49 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
52 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
60 flag_part_message = true;
63 flag_quit_message = true;
72 flag_no_opers = flag_part_message = flag_quit_message =
73 flag_privmsg = flag_notice = true;
87 virtual ~FilterResult()
94 class FilterBase : public Module
96 cmd_filter* filtcommand;
99 FilterBase(InspIRCd* Me, const std::string &source);
100 virtual ~FilterBase();
101 virtual void Implements(char* List);
102 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
103 virtual FilterResult* FilterMatch(userrec* 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(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
109 virtual void OnRehash(userrec* 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, userrec* user, string_list &results) = 0;
116 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line);
117 bool AppliesToMe(userrec* user, FilterResult* filter, int flags);
120 class cmd_filter : public command_t
124 cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(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, userrec *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(userrec* 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(userrec* user, FilterResult* filter, int flags)
212 if ((flags & FLAG_NOOPERS) && (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 cmd_filter(this, Me, source);
228 ServerInstance->AddCommand(filtcommand);
231 FilterBase::~FilterBase()
235 void FilterBase::Implements(char* List)
237 List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
240 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
242 flags = FLAG_PRIVMSG;
243 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
246 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
251 /* Leave ulines alone */
252 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
255 FilterResult* f = this->FilterMatch(user, text, flags);
258 std::string target = "";
259 if (target_type == TYPE_USER)
261 userrec* t = (userrec*)dest;
262 target = std::string(t->nick);
264 else if (target_type == TYPE_CHANNEL)
266 chanrec* t = (chanrec*)dest;
267 target = std::string(t->name);
269 if (f->action == "block")
271 ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
272 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason);
274 if (f->action == "silent")
276 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
278 if (f->action == "kill")
280 userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
282 if (f->action == "gline")
284 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
286 ServerInstance->XLines->apply_lines(APPLY_GLINES);
287 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
291 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
297 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
300 if ((validated == 1) && (IS_LOCAL(user)))
302 std::string checkline;
303 int replacepoint = 0;
304 bool parting = false;
306 if (command == "QUIT")
308 /* QUIT with no reason: nothing to do */
312 checkline = parameters[0];
317 else if (command == "PART")
319 /* PART with no reason: nothing to do */
323 checkline = parameters[1];
329 /* We're only messing with PART and QUIT */
332 FilterResult* f = NULL;
335 f = this->FilterMatch(user, checkline, flags);
338 /* PART or QUIT reason doesnt match a filter */
341 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
342 command_t* c = ServerInstance->Parser->GetHandler(command);
345 const char* params[127];
346 for (int item = 0; item < pcnt; item++)
347 params[item] = parameters[item];
348 params[replacepoint] = "Reason filtered";
350 /* We're blocking, OR theyre quitting and its a KILL action
351 * (we cant kill someone whos already quitting, so filter them anyway)
353 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
355 c->Handle(params, pcnt, user);
360 /* Are they parting, if so, kill is applicable */
361 if ((parting) && (f->action == "kill"))
363 user->SetWriteError("Filtered: "+f->reason);
364 /* This WriteServ causes the write error to be applied.
365 * Its not safe to kill here with QuitUser in a PreCommand handler,
366 * so we do it this way, which is safe just about anywhere.
368 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
370 if (f->action == "gline")
372 /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
373 std::string wild = "*@";
374 wild.append(user->GetIPString());
376 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
378 ServerInstance->XLines->apply_lines(APPLY_GLINES);
379 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
390 void FilterBase::OnRehash(userrec* user, const std::string ¶meter)
394 Version FilterBase::GetVersion()
396 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
400 std::string FilterBase::EncodeFilter(FilterResult* filter)
402 std::ostringstream stream;
403 std::string x = filter->freeform;
405 for (std::string::iterator n = x.begin(); n != x.end(); n++)
409 stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " " << filter->reason;
413 FilterResult FilterBase::DecodeFilter(const std::string &data)
416 std::istringstream stream(data);
418 stream >> res.freeform;
419 stream >> res.action;
421 if (res.flags == "-")
423 res.FillFlags(res.flags);
424 stream >> res.gline_time;
425 res.reason = stream.str();
427 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
434 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
436 this->SyncFilters(proto, opaque);
439 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
441 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
444 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
446 if ((target_type == TYPE_OTHER) && (extname == "filter"))
448 FilterResult data = DecodeFilter(extdata);
449 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);