TEST(FilterTest, Iterators) { { const filter_t empty_filter; EXPECT_TRUE(empty_filter.begin() == empty_filter.end()); EXPECT_FALSE(empty_filter.begin() != empty_filter.end()); } { const filter_t c = {50, 70, 90}; ASSERT_TRUE(c.begin() != c.end()); ASSERT_FALSE(c.begin() == c.end()); auto it1 = c.begin(); ASSERT_TRUE(it1 == c.begin()); ASSERT_FALSE(it1 != c.begin()); EXPECT_EQ(50, *it1); auto it2 = ++it1; ASSERT_TRUE(it2 == it1); ASSERT_FALSE(it2 != it1); ASSERT_TRUE(it2 != c.begin() && it2 != c.end()); ASSERT_FALSE(it2 == c.begin() || it2 == c.end()); EXPECT_EQ(70, *it2); EXPECT_EQ(70, *it1); auto it3 = it2++; ASSERT_TRUE(it3 == it1); EXPECT_EQ(70, *it3); EXPECT_EQ(90, *it2++); ASSERT_TRUE(it2 == c.end()); ASSERT_FALSE(it2 != c.end()); } }
TEST(FilterTest, Find) { const filter_t c = {10, 20, 30, 40, 50}; { SCOPED_TRACE("Find existing key"); const auto it = c.find(30); ASSERT_TRUE(it != c.end()); EXPECT_EQ(30, *it); EXPECT_TRUE(it == std::next(c.begin(), 2)); } { SCOPED_TRACE("Find non existing key"); const auto it = c.find(35); EXPECT_TRUE(it == c.end()); } }
virtual void OnRehash(const std::string ¶meter) { // reload our config file on rehash - we must destroy and re-allocate the classes // to call the constructor again and re-read our data. ConfigReader* Conf = new ConfigReader(ServerInstance); std::string filterfile = Conf->ReadValue("filter","file",0); // this automatically re-reads the configuration file into the class ConfigReader* MyConf = new ConfigReader(ServerInstance, filterfile); if ((filterfile == "") || (!MyConf->Verify())) { // bail if the user forgot to create a config file FilterException e; throw(e); } for (filter_t::iterator n = filters.begin(); n != filters.end(); n++) { DELETE(n->second); } filters.clear(); for (int index = 0; index < MyConf->Enumerate("keyword"); index++) { std::string pattern = MyConf->ReadValue("keyword","pattern",index); std::string reason = MyConf->ReadValue("keyword","reason",index); std::string do_action = MyConf->ReadValue("keyword","action",index); if (do_action == "") do_action = "none"; Filter* x = new Filter; x->reason = reason; x->action = do_action; filters[pattern] = x; } ServerInstance->Log(DEFAULT,"m_filter: read configuration from "+filterfile); DELETE(Conf); DELETE(MyConf); }
// Checks whether a filter is empty and checks invariants. static void expect_empty(const filter_t &c) { EXPECT_TRUE(c.empty()); EXPECT_EQ(0, c.size()); EXPECT_EQ(0, distance(c.begin(), c.end())); EXPECT_FLOAT_EQ(0.0f, c.load_factor()); EXPECT_LE(c.load_factor(), c.max_load_factor()); }
virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) { std::string text2 = text+" "; for (filter_t::iterator index = filters.begin(); index != filters.end(); index++) { if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first))) { Filter* f = (Filter*)index->second; std::string target = ""; if (target_type == TYPE_USER) { userrec* t = (userrec*)dest; target = std::string(t->nick); } else if (target_type == TYPE_CHANNEL) { chanrec* t = (chanrec*)dest; target = std::string(t->name); } if (f->action == "block") { ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason); user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason); } ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action); if (f->action == "kill") { userrec::QuitUser(ServerInstance,user,f->reason); } return 1; } } return 0; }