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 &flags)
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(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 ¶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, 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);
118 class cmd_filter : public command_t
122 cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(Me, "FILTER", 'o', 1), Base(f)
124 this->source = source;
125 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
128 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
132 /* Deleting a filter */
133 if (Base->DeleteFilter(parameters[0]))
135 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
140 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
146 /* Adding a filter */
149 std::string freeform = parameters[0];
150 std::string type = parameters[1];
151 std::string flags = parameters[2];
156 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
158 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
166 duration = ServerInstance->Duration(parameters[3]);
167 reason = parameters[4];
171 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
177 reason = parameters[3];
179 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
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());
189 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
195 this->TooFewParams(user, ".");
202 void TooFewParams(userrec* user, const std::string &extra_text)
204 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
208 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
210 filtcommand = new cmd_filter(this, Me, source);
211 ServerInstance->AddCommand(filtcommand);
214 FilterBase::~FilterBase()
218 void FilterBase::Implements(char* List)
220 List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
223 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
225 flags = FLAG_PRIVMSG;
226 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
229 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
234 /* Leave ulines alone */
235 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
238 FilterResult* f = this->FilterMatch(text, flags);
241 std::string target = "";
242 if (target_type == TYPE_USER)
244 userrec* t = (userrec*)dest;
245 target = std::string(t->nick);
247 else if (target_type == TYPE_CHANNEL)
249 chanrec* t = (chanrec*)dest;
250 target = std::string(t->name);
252 if (f->action == "block")
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);
257 if (f->action == "silent")
259 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
261 if (f->action == "kill")
263 userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
265 if (f->action == "gline")
267 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
269 ServerInstance->XLines->apply_lines(APPLY_GLINES);
270 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
274 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
280 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
283 if ((validated == 1) && (IS_LOCAL(user)))
285 std::string checkline;
286 int replacepoint = 0;
287 bool parting = false;
289 if (command == "QUIT")
291 /* QUIT with no reason: nothing to do */
295 checkline = parameters[0];
300 else if (command == "PART")
302 /* PART with no reason: nothing to do */
306 checkline = parameters[1];
312 /* We're only messing with PART and QUIT */
315 FilterResult* f = NULL;
318 f = this->FilterMatch(checkline, flags);
321 /* PART or QUIT reason doesnt match a filter */
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);
328 const char* params[127];
329 for (int item = 0; item < pcnt; item++)
330 params[item] = parameters[item];
331 params[replacepoint] = "Reason filtered";
333 /* We're blocking, OR theyre quitting and its a KILL action
334 * (we cant kill someone whos already quitting, so filter them anyway)
336 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
338 c->Handle(params, pcnt, user);
343 /* Are they parting, if so, kill is applicable */
344 if ((parting) && (f->action == "kill"))
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.
351 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
353 if (f->action == "gline")
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());
359 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
361 ServerInstance->XLines->apply_lines(APPLY_GLINES);
362 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
373 void FilterBase::OnRehash(userrec* user, const std::string ¶meter)
377 Version FilterBase::GetVersion()
379 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
383 std::string FilterBase::EncodeFilter(FilterResult* filter)
385 std::ostringstream stream;
386 std::string x = filter->freeform;
388 for (std::string::iterator n = x.begin(); n != x.end(); n++)
392 stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " " << filter->reason;
396 FilterResult FilterBase::DecodeFilter(const std::string &data)
399 std::istringstream stream(data);
401 stream >> res.freeform;
402 stream >> res.action;
404 if (res.flags == "-")
406 res.FillFlags(res.flags);
407 stream >> res.gline_time;
408 res.reason = stream.str();
410 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
417 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
419 this->SyncFilters(proto, opaque);
422 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
424 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
427 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
429 if ((target_type == TYPE_OTHER) && (extname == "filter"))
431 FilterResult data = DecodeFilter(extdata);
432 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);