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 * ---------------------------------------------------
16 class FilterResult : public classbase
25 FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, bool operex = false) : freeform(free), reason(rea),
26 action(act), gline_time(gt), opers_exempt(operex)
34 virtual ~FilterResult()
41 class FilterBase : public Module
43 cmd_filter* filtcommand;
45 FilterBase(InspIRCd* Me, const std::string &source);
46 virtual ~FilterBase();
47 virtual void Implements(char* List);
48 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
49 virtual FilterResult* FilterMatch(const std::string &text) = 0;
50 virtual bool DeleteFilter(const std::string &freeform) = 0;
51 virtual void SyncFilters(Module* proto, void* opaque) = 0;
52 virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
53 virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, bool operexempt) = 0;
54 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
55 virtual void OnRehash(userrec* user, const std::string ¶meter);
56 virtual Version GetVersion();
57 std::string EncodeFilter(FilterResult* filter);
58 FilterResult DecodeFilter(const std::string &data);
59 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable = false);
60 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
61 virtual int OnStats(char symbol, userrec* user, string_list &results) = 0;
62 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line);
65 class cmd_filter : public command_t
69 cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(Me, "FILTER", 'o', 1), Base(f)
71 this->source = source;
72 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
75 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
79 /* Deleting a filter */
80 if (Base->DeleteFilter(parameters[0]))
82 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
87 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
96 std::string freeform = parameters[0];
97 std::string type = parameters[1];
98 std::string flags = parameters[2];
101 bool operexempt = false;
103 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n)
113 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
115 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick, freeform.c_str());
123 duration = ServerInstance->Duration(parameters[3]);
124 reason = parameters[4];
128 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
134 reason = parameters[3];
136 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, operexempt);
139 user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, reason: '%s'", user->nick, freeform.c_str(),
140 type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[3] : ""),
146 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
152 this->TooFewParams(user, ".");
159 void TooFewParams(userrec* user, const std::string &extra_text)
161 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
165 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
167 filtcommand = new cmd_filter(this, Me, source);
168 ServerInstance->AddCommand(filtcommand);
171 FilterBase::~FilterBase()
175 void FilterBase::Implements(char* List)
177 List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
180 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
182 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
185 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
187 /* Leave ulines alone */
188 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
191 FilterResult* f = this->FilterMatch(text);
194 std::string target = "";
195 if (target_type == TYPE_USER)
197 userrec* t = (userrec*)dest;
198 target = std::string(t->nick);
200 else if (target_type == TYPE_CHANNEL)
202 chanrec* t = (chanrec*)dest;
203 target = std::string(t->name);
205 if (f->action == "block")
207 ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
208 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason);
210 if (f->action == "silent")
212 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
214 if (f->action == "kill")
216 userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
218 if (f->action == "gline")
220 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
222 ServerInstance->XLines->apply_lines(APPLY_GLINES);
223 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
227 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their message filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
233 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
235 if ((validated == 1) && (IS_LOCAL(user)))
237 std::string checkline;
238 int replacepoint = 0;
239 bool parting = false;
241 if (command == "QUIT")
243 /* QUIT with no reason: nothing to do */
247 checkline = parameters[0];
251 else if (command == "PART")
253 /* PART with no reason: nothing to do */
257 checkline = parameters[1];
262 /* We're only messing with PART and QUIT */
265 FilterResult* f = this->FilterMatch(checkline);
268 /* PART or QUIT reason doesnt match a filter */
271 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
272 command_t* c = ServerInstance->Parser->GetHandler(command);
275 const char* params[127];
276 for (int item = 0; item < pcnt; item++)
277 params[item] = parameters[item];
278 params[replacepoint] = "Reason filtered";
280 /* We're blocking, OR theyre quitting and its a KILL action
281 * (we cant kill someone whos already quitting, so filter them anyway)
283 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
285 c->Handle(params, pcnt, user);
290 /* Are they parting, if so, kill is applicable */
291 if ((parting) && (f->action == "kill"))
293 user->SetWriteError("Filtered: "+f->reason);
294 /* This WriteServ causes the write error to be applied.
295 * Its not safe to kill here with QuitUser in a PreCommand handler,
296 * so we do it this way, which is safe just about anywhere.
298 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
300 if (f->action == "gline")
302 /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
303 std::string wild = "*@";
304 wild.append(user->GetIPString());
306 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), wild.c_str()))
308 ServerInstance->XLines->apply_lines(APPLY_GLINES);
309 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
320 void FilterBase::OnRehash(userrec* user, const std::string ¶meter)
324 Version FilterBase::GetVersion()
326 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
330 std::string FilterBase::EncodeFilter(FilterResult* filter)
332 std::ostringstream stream;
333 std::string x = filter->freeform;
335 for (std::string::iterator n = x.begin(); n != x.end(); n++)
339 stream << x << " " << filter->action << " " << filter->opers_exempt << " " << filter->gline_time << " " << filter->reason;
343 FilterResult FilterBase::DecodeFilter(const std::string &data)
346 std::istringstream stream(data);
348 stream >> res.freeform;
349 stream >> res.action;
350 stream >> res.opers_exempt;
351 stream >> res.gline_time;
352 res.reason = stream.str();
354 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
361 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
363 this->SyncFilters(proto, opaque);
366 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
368 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
371 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
373 if ((target_type == TYPE_OTHER) && (extname == "filter"))
375 FilterResult data = DecodeFilter(extdata);
376 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.opers_exempt);