00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015 #include "xline.h"
00016 #include "m_regex.h"
00017
00018
00019
00020 static std::string RegexEngine = "";
00021 static Module* rxengine = NULL;
00022
00023 enum FilterFlags
00024 {
00025 FLAG_PART = 2,
00026 FLAG_QUIT = 4,
00027 FLAG_PRIVMSG = 8,
00028 FLAG_NOTICE = 16
00029 };
00030
00031 class FilterResult : public classbase
00032 {
00033 public:
00034 std::string freeform;
00035 std::string reason;
00036 std::string action;
00037 long gline_time;
00038 std::string flags;
00039
00040 bool flag_no_opers;
00041 bool flag_part_message;
00042 bool flag_quit_message;
00043 bool flag_privmsg;
00044 bool flag_notice;
00045
00046 FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, const std::string &fla) :
00047 freeform(free), reason(rea), action(act), gline_time(gt), flags(fla)
00048 {
00049 this->FillFlags(fla);
00050 }
00051
00052 int FillFlags(const std::string &fl)
00053 {
00054 flags = fl;
00055 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
00056 size_t x = 0;
00057
00058 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
00059 {
00060 switch (*n)
00061 {
00062 case 'o':
00063 flag_no_opers = true;
00064 break;
00065 case 'P':
00066 flag_part_message = true;
00067 break;
00068 case 'q':
00069 flag_quit_message = true;
00070 break;
00071 case 'p':
00072 flag_privmsg = true;
00073 break;
00074 case 'n':
00075 flag_notice = true;
00076 break;
00077 case '*':
00078 flag_no_opers = flag_part_message = flag_quit_message =
00079 flag_privmsg = flag_notice = true;
00080 break;
00081 default:
00082 return x;
00083 break;
00084 }
00085 }
00086 return 0;
00087 }
00088
00089 FilterResult()
00090 {
00091 }
00092
00093 virtual ~FilterResult()
00094 {
00095 }
00096 };
00097
00098 class CommandFilter;
00099
00100 class FilterBase : public Module
00101 {
00102 CommandFilter* filtcommand;
00103 int flags;
00104 protected:
00105 std::vector<std::string> exemptfromfilter;
00106 public:
00107 FilterBase(InspIRCd* Me, const std::string &source);
00108 virtual ~FilterBase();
00109 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
00110 virtual FilterResult* FilterMatch(User* user, const std::string &text, int flags) = 0;
00111 virtual bool DeleteFilter(const std::string &freeform) = 0;
00112 virtual void SyncFilters(Module* proto, void* opaque) = 0;
00113 virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
00114 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;
00115 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
00116 virtual void OnRehash(User* user, const std::string ¶meter);
00117 virtual Version GetVersion();
00118 std::string EncodeFilter(FilterResult* filter);
00119 FilterResult DecodeFilter(const std::string &data);
00120 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable = false);
00121 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
00122 virtual int OnStats(char symbol, User* user, string_list &results) = 0;
00123 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line);
00124 bool AppliesToMe(User* user, FilterResult* filter, int flags);
00125 void OnLoadModule(Module* mod, const std::string& name);
00126 virtual void ReadFilters(ConfigReader &MyConf) = 0;
00127 };
00128
00129 class CommandFilter : public Command
00130 {
00131 FilterBase* Base;
00132 public:
00133 CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &ssource) : Command(Me, "FILTER", "o", 1, 5), Base(f)
00134 {
00135 this->source = ssource;
00136 this->syntax = "<filter-definition> <type> <flags> [<gline-duration>] :<reason>";
00137 }
00138
00139 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
00140 {
00141 if (parameters.size() == 1)
00142 {
00143
00144 if (Base->DeleteFilter(parameters[0]))
00145 {
00146 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick.c_str(), parameters[0].c_str());
00147 return CMD_SUCCESS;
00148 }
00149 else
00150 {
00151 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick.c_str(), parameters[0].c_str());
00152 return CMD_FAILURE;
00153 }
00154 }
00155 else
00156 {
00157
00158 if (parameters.size() >= 4)
00159 {
00160 std::string freeform = parameters[0];
00161 std::string type = parameters[1];
00162 std::string flags = parameters[2];
00163 std::string reason;
00164 long duration = 0;
00165
00166
00167 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
00168 {
00169 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick.c_str(), freeform.c_str());
00170 return CMD_FAILURE;
00171 }
00172
00173 if (type == "gline")
00174 {
00175 if (parameters.size() >= 5)
00176 {
00177 duration = ServerInstance->Duration(parameters[3]);
00178 reason = parameters[4];
00179 }
00180 else
00181 {
00182 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
00183 return CMD_FAILURE;
00184 }
00185 }
00186 else
00187 {
00188 reason = parameters[3];
00189 }
00190 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
00191 if (result.first)
00192 {
00193 user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick.c_str(), freeform.c_str(),
00194 type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[3].c_str() : ""),
00195 flags.c_str(), reason.c_str());
00196 return CMD_SUCCESS;
00197 }
00198 else
00199 {
00200 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick.c_str(), freeform.c_str(), result.second.c_str());
00201 return CMD_FAILURE;
00202 }
00203 }
00204 else
00205 {
00206 this->TooFewParams(user, ".");
00207 return CMD_FAILURE;
00208 }
00209
00210 }
00211 }
00212
00213 void TooFewParams(User* user, const std::string &extra_text)
00214 {
00215 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick.c_str(), extra_text.c_str());
00216 }
00217 };
00218
00219 bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int iflags)
00220 {
00221 if ((filter->flag_no_opers) && IS_OPER(user))
00222 return false;
00223 if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
00224 return false;
00225 if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
00226 return false;
00227 if ((iflags & FLAG_QUIT) && (!filter->flag_quit_message))
00228 return false;
00229 if ((iflags & FLAG_PART) && (!filter->flag_part_message))
00230 return false;
00231 return true;
00232 }
00233
00234 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me)
00235 {
00236 Me->Modules->UseInterface("RegularExpression");
00237 filtcommand = new CommandFilter(this, Me, source);
00238 ServerInstance->AddCommand(filtcommand);
00239 Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnLoadModule };
00240 ServerInstance->Modules->Attach(eventlist, this, 8);
00241 }
00242
00243 FilterBase::~FilterBase()
00244 {
00245 ServerInstance->Modules->DoneWithInterface("RegularExpression");
00246 }
00247
00248 int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
00249 {
00250 flags = FLAG_PRIVMSG;
00251 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
00252 }
00253
00254 int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
00255 {
00256 if (!flags)
00257 flags = FLAG_NOTICE;
00258
00259
00260 if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
00261 return 0;
00262
00263 FilterResult* f = this->FilterMatch(user, text, flags);
00264 if (f)
00265 {
00266 std::string target = "";
00267 if (target_type == TYPE_USER)
00268 {
00269 User* t = (User*)dest;
00270 target = std::string(t->nick);
00271 }
00272 else if (target_type == TYPE_CHANNEL)
00273 {
00274 Channel* t = (Channel*)dest;
00275 target = std::string(t->name);
00276 std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), target);
00277 if (i != exemptfromfilter.end()) return 0;
00278 }
00279 if (f->action == "block")
00280 {
00281 ServerInstance->SNO->WriteToSnoMask('A', std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
00282 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason);
00283 }
00284 if (f->action == "silent")
00285 {
00286 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason);
00287 }
00288 if (f->action == "kill")
00289 {
00290 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
00291 }
00292 if (f->action == "gline")
00293 {
00294 GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
00295 if (ServerInstance->XLines->AddLine(gl,NULL))
00296 {
00297 ServerInstance->XLines->ApplyLines();
00298 }
00299 else
00300 delete gl;
00301 }
00302
00303 ServerInstance->Logs->Log("FILTER",DEFAULT,"FILTER: "+ user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + f->action);
00304 return 1;
00305 }
00306 return 0;
00307 }
00308
00309 int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
00310 {
00311 flags = 0;
00312 if (validated && IS_LOCAL(user))
00313 {
00314 std::string checkline;
00315 int replacepoint = 0;
00316 bool parting = false;
00317
00318 if (command == "QUIT")
00319 {
00320
00321 if (parameters.size() < 1)
00322 return 0;
00323
00324 checkline = parameters[0];
00325 replacepoint = 0;
00326 parting = false;
00327 flags = FLAG_QUIT;
00328 }
00329 else if (command == "PART")
00330 {
00331
00332 if (parameters.size() < 2)
00333 return 0;
00334
00335 std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), parameters[0]);
00336 if (i != exemptfromfilter.end()) return 0;
00337 checkline = parameters[1];
00338 replacepoint = 1;
00339 parting = true;
00340 flags = FLAG_PART;
00341 }
00342 else
00343
00344 return 0;
00345
00346 FilterResult* f = NULL;
00347
00348 if (flags)
00349 f = this->FilterMatch(user, checkline, flags);
00350
00351 if (!f)
00352
00353 return 0;
00354
00355
00356 Command* c = ServerInstance->Parser->GetHandler(command);
00357 if (c)
00358 {
00359 std::vector<std::string> params;
00360 for (int item = 0; item < (int)parameters.size(); item++)
00361 params.push_back(parameters[item]);
00362 params[replacepoint] = "Reason filtered";
00363
00364
00365
00366
00367 if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
00368 {
00369 c->Handle(params, user);
00370 return 1;
00371 }
00372 else
00373 {
00374
00375 if ((parting) && (f->action == "kill"))
00376 {
00377 user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick.c_str(), f->reason.c_str());
00378 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
00379 }
00380 if (f->action == "gline")
00381 {
00382
00383 GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
00384 if (ServerInstance->XLines->AddLine(gl,NULL))
00385 {
00386 ServerInstance->XLines->ApplyLines();
00387 }
00388 else
00389 delete gl;
00390 }
00391 return 1;
00392 }
00393 }
00394 return 0;
00395 }
00396 return 0;
00397 }
00398
00399 void FilterBase::OnRehash(User* user, const std::string ¶meter)
00400 {
00401 ConfigReader* MyConf = new ConfigReader(ServerInstance);
00402 std::vector<std::string>().swap(exemptfromfilter);
00403 for (int index = 0; index < MyConf->Enumerate("exemptfromfilter"); ++index)
00404 {
00405 std::string chan = MyConf->ReadValue("exemptfromfilter", "channel", index);
00406 if (!chan.empty()) {
00407 exemptfromfilter.push_back(chan);
00408 }
00409 }
00410 std::string newrxengine = MyConf->ReadValue("filteropts", "engine", 0);
00411 if (!RegexEngine.empty())
00412 {
00413 if (RegexEngine == newrxengine)
00414 return;
00415
00416 ServerInstance->SNO->WriteToSnoMask('A', "Dumping all filters due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
00417
00418 }
00419 rxengine = NULL;
00420
00421 RegexEngine = newrxengine;
00422 modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
00423 if (ml)
00424 {
00425 for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
00426 {
00427 if (RegexNameRequest(this, *i).Send() == newrxengine)
00428 {
00429 ServerInstance->SNO->WriteToSnoMask('A', "Filter now using engine '%s'", RegexEngine.c_str());
00430 rxengine = *i;
00431 }
00432 }
00433 }
00434 if (!rxengine)
00435 {
00436 ServerInstance->SNO->WriteToSnoMask('A', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", RegexEngine.c_str());
00437 }
00438
00439 delete MyConf;
00440 }
00441
00442 void FilterBase::OnLoadModule(Module* mod, const std::string& name)
00443 {
00444 if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
00445 {
00446 std::string rxname = RegexNameRequest(this, mod).Send();
00447 if (rxname == RegexEngine)
00448 {
00449 rxengine = mod;
00450
00451
00452
00453 ConfigReader Config(ServerInstance);
00454 ServerInstance->SNO->WriteToSnoMask('A', "Found and activated regex module '%s' for m_filter.so.", RegexEngine.c_str());
00455 ReadFilters(Config);
00456 }
00457 }
00458 }
00459
00460
00461 Version FilterBase::GetVersion()
00462 {
00463 return Version("$Id: m_filter.cpp 10718 2008-10-25 16:41:13Z w00t $", VF_VENDOR | VF_COMMON, API_VERSION);
00464 }
00465
00466
00467 std::string FilterBase::EncodeFilter(FilterResult* filter)
00468 {
00469 std::ostringstream stream;
00470 std::string x = filter->freeform;
00471
00472
00473 for (std::string::iterator n = x.begin(); n != x.end(); n++)
00474 if (*n == ' ')
00475 *n = '\7';
00476
00477 stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
00478 return stream.str();
00479 }
00480
00481 FilterResult FilterBase::DecodeFilter(const std::string &data)
00482 {
00483 FilterResult res;
00484 irc::tokenstream tokens(data);
00485 tokens.GetToken(res.freeform);
00486 tokens.GetToken(res.action);
00487 tokens.GetToken(res.flags);
00488 if (res.flags == "-")
00489 res.flags = "";
00490 res.FillFlags(res.flags);
00491 tokens.GetToken(res.gline_time);
00492 tokens.GetToken(res.reason);
00493
00494
00495 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
00496 if (*n == '\7')
00497 *n = ' ';
00498
00499 return res;
00500 }
00501
00502 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
00503 {
00504 this->SyncFilters(proto, opaque);
00505 }
00506
00507 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
00508 {
00509 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
00510 }
00511
00512 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
00513 {
00514 if ((target_type == TYPE_OTHER) && (extname == "filter"))
00515 {
00516 FilterResult data = DecodeFilter(extdata);
00517 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
00518 }
00519 }
00520
00521 class ImplFilter : public FilterResult
00522 {
00523 public:
00524 Regex* regex;
00525
00526 ImplFilter(Module* mymodule, const std::string &rea, const std::string &act, long glinetime, const std::string &pat, const std::string &flgs)
00527 : FilterResult(pat, rea, act, glinetime, flgs)
00528 {
00529 if (!rxengine)
00530 throw ModuleException("Regex module implementing '"+RegexEngine+"' is not loaded!");
00531
00532 regex = RegexFactoryRequest(mymodule, rxengine, pat).Create();
00533 }
00534
00535 ImplFilter()
00536 {
00537 }
00538 };
00539
00540 class ModuleFilter : public FilterBase
00541 {
00542 std::vector<ImplFilter> filters;
00543 const char *error;
00544 int erroffset;
00545 ImplFilter fr;
00546
00547 public:
00548 ModuleFilter(InspIRCd* Me)
00549 : FilterBase(Me, "m_filter.so")
00550 {
00551 OnRehash(NULL,"");
00552 }
00553
00554 virtual ~ModuleFilter()
00555 {
00556 }
00557
00558 virtual FilterResult* FilterMatch(User* user, const std::string &text, int flgs)
00559 {
00560 for (std::vector<ImplFilter>::iterator index = filters.begin(); index != filters.end(); index++)
00561 {
00562
00563 if (!FilterBase::AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flgs))
00564 continue;
00565
00566
00567 if (index->regex->Matches(text))
00568 {
00569
00570 fr = *index;
00571 if (index != filters.begin())
00572 {
00573
00574 filters.erase(index);
00575 filters.insert(filters.begin(), fr);
00576 }
00577 return &fr;
00578 }
00579
00580 }
00581 return NULL;
00582 }
00583
00584 virtual bool DeleteFilter(const std::string &freeform)
00585 {
00586 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
00587 {
00588 if (i->freeform == freeform)
00589 {
00590 delete i->regex;
00591 filters.erase(i);
00592 return true;
00593 }
00594 }
00595 return false;
00596 }
00597
00598 virtual void SyncFilters(Module* proto, void* opaque)
00599 {
00600 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
00601 {
00602 this->SendFilter(proto, opaque, &(*i));
00603 }
00604 }
00605
00606 virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flgs)
00607 {
00608 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
00609 {
00610 if (i->freeform == freeform)
00611 {
00612 return std::make_pair(false, "Filter already exists");
00613 }
00614 }
00615
00616 try
00617 {
00618 filters.push_back(ImplFilter(this, reason, type, duration, freeform, flgs));
00619 }
00620 catch (ModuleException &e)
00621 {
00622 ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason());
00623 return std::make_pair(false, e.GetReason());
00624 }
00625 return std::make_pair(true, "");
00626 }
00627
00628 virtual void OnRehash(User* user, const std::string ¶meter)
00629 {
00630 ConfigReader MyConf(ServerInstance);
00631 FilterBase::OnRehash(user, parameter);
00632 ReadFilters(MyConf);
00633 }
00634
00635 void ReadFilters(ConfigReader &MyConf)
00636 {
00637 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
00638 {
00639 this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
00640
00641 std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
00642 std::string reason = MyConf.ReadValue("keyword", "reason", index);
00643 std::string action = MyConf.ReadValue("keyword", "action", index);
00644 std::string flgs = MyConf.ReadValue("keyword", "flags", index);
00645 long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index));
00646 if (action.empty())
00647 action = "none";
00648 if (flgs.empty())
00649 flgs = "*";
00650
00651 try
00652 {
00653 filters.push_back(ImplFilter(this, reason, action, gline_time, pattern, flgs));
00654 ServerInstance->Logs->Log("m_filter", DEFAULT, "Regular expression %s loaded.", pattern.c_str());
00655 }
00656 catch (ModuleException &e)
00657 {
00658 ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason());
00659 }
00660 }
00661 }
00662
00663 virtual int OnStats(char symbol, User* user, string_list &results)
00664 {
00665 if (symbol == 's')
00666 {
00667 std::string sn = ServerInstance->Config->ServerName;
00668 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
00669 {
00670 results.push_back(sn+" 223 "+user->nick+" :"+RegexEngine+":"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
00671 }
00672 for (std::vector<std::string>::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i)
00673 {
00674 results.push_back(sn+" 223 "+user->nick+" :EXEMPT "+(*i));
00675 }
00676 }
00677 return 0;
00678 }
00679 };
00680
00681 MODULE_INIT(ModuleFilter)
00682