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
24 FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt) : freeform(free), reason(rea), action(act), gline_time(gt)
32 virtual ~FilterResult()
39 class FilterBase : public Module
41 cmd_filter* filtcommand;
43 FilterBase(InspIRCd* Me, const std::string &source);
44 virtual ~FilterBase();
45 virtual void Implements(char* List);
46 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
47 virtual FilterResult* FilterMatch(const std::string &text) = 0;
48 virtual bool DeleteFilter(const std::string &freeform) = 0;
49 virtual void SyncFilters(Module* proto, void* opaque) = 0;
50 virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
51 virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration) = 0;
52 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
53 virtual void OnRehash(userrec* user, const std::string ¶meter);
54 virtual Version GetVersion();
55 std::string EncodeFilter(FilterResult* filter);
56 FilterResult DecodeFilter(const std::string &data);
57 virtual void OnSyncOtherMetaData(Module* proto, void* opaque);
58 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
59 virtual int OnStats(char symbol, userrec* user, string_list &results) = 0;
60 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line);
63 class cmd_filter : public command_t
67 cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(Me, "FILTER", 'o', 1), Base(f)
69 this->source = source;
70 this->syntax = "<filter-definition> <type> [<gline-duration>] :<reason>";
73 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
77 /* Deleting a filter */
78 if (Base->DeleteFilter(parameters[0]))
80 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
85 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
94 std::string freeform = parameters[0];
95 std::string type = parameters[1];
99 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill"))
101 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', and 'kill'.", user->nick, freeform.c_str());
109 duration = ServerInstance->Duration(parameters[2]);
110 reason = parameters[3];
114 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
120 reason = parameters[2];
122 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration);
125 user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, reason: '%s'", user->nick, freeform.c_str(),
126 type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[2] : ""),
132 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
138 this->TooFewParams(user, ".");
145 void TooFewParams(userrec* user, const std::string &extra_text)
147 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
151 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module::Module(Me)
153 filtcommand = new cmd_filter(this, Me, source);
154 ServerInstance->AddCommand(filtcommand);
157 FilterBase::~FilterBase()
161 void FilterBase::Implements(char* List)
163 List[I_OnPreCommand] = List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
166 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
168 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
171 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
173 /* Leave ulines alone */
174 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
177 FilterResult* f = this->FilterMatch(text);
180 std::string target = "";
181 if (target_type == TYPE_USER)
183 userrec* t = (userrec*)dest;
184 target = std::string(t->nick);
186 else if (target_type == TYPE_CHANNEL)
188 chanrec* t = (chanrec*)dest;
189 target = std::string(t->name);
191 if (f->action == "block")
193 ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason);
194 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason);
196 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
197 if (f->action == "kill")
199 userrec::QuitUser(ServerInstance,user,"Filtered: "+f->reason);
201 if (f->action == "gline")
203 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
205 ServerInstance->XLines->apply_lines(APPLY_GLINES);
206 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
214 int FilterBase::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
216 if ((validated == 1) && (IS_LOCAL(user)))
218 std::string checkline;
219 int replacepoint = 0;
220 bool parting = false;
222 if (command == "QUIT")
224 /* QUIT with no reason: nothing to do */
228 checkline = parameters[0];
232 else if (command == "PART")
234 /* PART with no reason: nothing to do */
238 checkline = parameters[1];
243 /* We're only messing with PART and QUIT */
246 FilterResult* f = this->FilterMatch(checkline);
249 /* PART or QUIT reason doesnt match a filter */
252 ServerInstance->Log(DEBUG,"Match block text");
254 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
255 command_t* c = ServerInstance->Parser->GetHandler(command);
258 ServerInstance->Log(DEBUG,"Found handler");
260 const char* params[127];
261 for (int item = 0; item < pcnt; item++)
262 params[item] = parameters[item];
263 params[replacepoint] = "Reason filtered";
265 /* We're blocking, OR theyre quitting and its a KILL action
266 * (we cant kill someone whos already quitting, so filter them anyway)
268 if ((f->action == "block") || (((!parting) && (f->action == "kill"))))
270 c->Handle(params, pcnt, user);
275 /* Are they parting, if so, kill is applicable */
276 if ((parting) && (f->action == "kill"))
278 user->SetWriteError("Filtered: "+f->reason);
279 /* This WriteServ causes the write error to be applied.
280 * Its not safe to kill here with QuitUser in a PreCommand handler,
281 * so we do it this way, which is safe just about anywhere.
283 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick, f->reason.c_str());
285 if (f->action == "gline")
287 if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
289 ServerInstance->XLines->apply_lines(APPLY_GLINES);
290 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
301 void FilterBase::OnRehash(userrec* user, const std::string ¶meter)
305 Version FilterBase::GetVersion()
307 return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
311 std::string FilterBase::EncodeFilter(FilterResult* filter)
313 std::ostringstream stream;
314 std::string x = filter->freeform;
316 for (std::string::iterator n = x.begin(); n != x.end(); n++)
320 stream << x << " " << filter->action << " " << filter->gline_time << " " << filter->reason;
324 FilterResult FilterBase::DecodeFilter(const std::string &data)
327 std::istringstream stream(data);
329 stream >> res.freeform;
330 stream >> res.action;
331 stream >> res.gline_time;
332 res.reason = stream.str();
334 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
341 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque)
343 this->SyncFilters(proto, opaque);
346 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
348 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
351 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
353 if ((target_type == TYPE_OTHER) && (extname == "filter"))
355 FilterResult data = DecodeFilter(extdata);
356 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time);