1 // Message and notice filtering using glob patterns
2 // a module based on the original work done by Craig Edwards in 2003
3 // for the chatspike network.
11 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
15 class ModuleFilter : public Module
18 ConfigReader *Conf, *MyConf;
23 // read the configuration file on startup.
24 // it is perfectly valid to set <filter file> to the value of the
25 // main config file, then append your <keyword> tags to the bottom
26 // of the main config... but rather messy. That's why the capability
27 // of using a seperate config file is provided.
29 Conf = new ConfigReader;
30 std::string filterfile = Conf->ReadValue("filter","file",0);
31 MyConf = new ConfigReader(filterfile);
32 if ((filterfile == "") || (!MyConf->Verify()))
34 printf("Error, could not find <filter file=\"\"> definition in your config file!");
37 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
40 virtual ~ModuleFilter()
47 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
49 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string text)
52 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
54 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
55 if (Srv->MatchText(text,pattern))
57 std::string target = "";
58 std::string reason = MyConf->ReadValue("keyword","reason",index);
59 std::string do_action = MyConf->ReadValue("keyword","action",index);
64 if (target_type == TYPE_USER)
66 userrec* t = (userrec*)dest;
67 target = std::string(t->nick);
69 else if (target_type == TYPE_CHANNEL)
71 chanrec* t = (chanrec*)dest;
72 target = std::string(t->name);
74 if (do_action == "block")
76 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
77 std::string(" had their message filtered, target was ")+
79 // this form of SendTo (with the source as NuLL) sends a server notice
80 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
81 " :Your message has been filtered and opers notified: "+reason);
84 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
85 std::string(" had their message filtered, target was ")+
86 target+": "+reason+" Action: "+do_action);
88 if (do_action == "kill")
90 Srv->QuitUser(user,reason);
98 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text)
101 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
103 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
104 if (Srv->MatchText(text,pattern))
106 std::string target = "";
107 std::string reason = MyConf->ReadValue("keyword","reason",index);
108 std::string do_action = MyConf->ReadValue("keyword","action",index);
113 if (target_type == TYPE_USER)
115 userrec* t = (userrec*)dest;
116 target = std::string(t->nick);
118 else if (target_type == TYPE_CHANNEL)
120 chanrec* t = (chanrec*)dest;
121 target = std::string(t->name);
123 if (do_action == "block")
125 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
126 std::string(" had their notice filtered, target was ")+
128 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
129 " :Your notice has been filtered and opers notified: "+reason);
131 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
132 std::string(" had their notice filtered, target was ")+
133 target+": "+reason+" Action: "+do_action);
135 if (do_action == "kill")
137 Srv->QuitUser(user,reason);
145 virtual void OnRehash()
147 // reload our config file on rehash - we must destroy and re-allocate the classes
148 // to call the constructor again and re-read our data.
151 Conf = new ConfigReader;
152 std::string filterfile = Conf->ReadValue("filter","file",0);
153 // this automatically re-reads the configuration file into the class
154 MyConf = new ConfigReader(filterfile);
155 if ((filterfile == "") || (!MyConf->Verify()))
157 // bail if the user forgot to create a config file
158 printf("Error, could not find <filter file=\"\"> definition in your config file!");
161 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
164 virtual Version GetVersion()
166 // This is version 2 because version 1.x is the unreleased unrealircd module
167 return Version(2,0,0,0);
172 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
174 class ModuleFilterFactory : public ModuleFactory
177 ModuleFilterFactory()
181 ~ModuleFilterFactory()
185 virtual Module * CreateModule()
187 return new ModuleFilter;
193 extern "C" void * init_module( void )
195 return new ModuleFilterFactory;