1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 // Message and notice filtering using glob patterns
18 // a module based on the original work done by Craig Edwards in 2003
19 // for the chatspike network.
27 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
31 class ModuleFilter : public Module
34 ConfigReader *Conf, *MyConf;
39 // read the configuration file on startup.
40 // it is perfectly valid to set <filter file> to the value of the
41 // main config file, then append your <keyword> tags to the bottom
42 // of the main config... but rather messy. That's why the capability
43 // of using a seperate config file is provided.
45 Conf = new ConfigReader;
46 std::string filterfile = Conf->ReadValue("filter","file",0);
47 MyConf = new ConfigReader(filterfile);
48 if ((filterfile == "") || (!MyConf->Verify()))
50 printf("Error, could not find <filter file=\"\"> definition in your config file!");
51 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
54 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
57 virtual ~ModuleFilter()
64 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
66 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
68 std::string text2 = text + " ";
69 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
71 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
72 if (Srv->MatchText(text2,pattern))
74 std::string target = "";
75 std::string reason = MyConf->ReadValue("keyword","reason",index);
76 std::string do_action = MyConf->ReadValue("keyword","action",index);
81 if (target_type == TYPE_USER)
83 userrec* t = (userrec*)dest;
84 target = std::string(t->nick);
86 else if (target_type == TYPE_CHANNEL)
88 chanrec* t = (chanrec*)dest;
89 target = std::string(t->name);
91 if (do_action == "block")
93 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
94 std::string(" had their message filtered, target was ")+
96 // this form of SendTo (with the source as NuLL) sends a server notice
97 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
98 " :Your message has been filtered and opers notified: "+reason);
101 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
102 std::string(" had their message filtered, target was ")+
103 target+": "+reason+" Action: "+do_action);
105 if (do_action == "kill")
107 Srv->QuitUser(user,reason);
115 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
117 std::string text2 = text + " ";
118 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
120 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
121 if (Srv->MatchText(text2,pattern))
123 std::string target = "";
124 std::string reason = MyConf->ReadValue("keyword","reason",index);
125 std::string do_action = MyConf->ReadValue("keyword","action",index);
130 if (target_type == TYPE_USER)
132 userrec* t = (userrec*)dest;
133 target = std::string(t->nick);
135 else if (target_type == TYPE_CHANNEL)
137 chanrec* t = (chanrec*)dest;
138 target = std::string(t->name);
140 if (do_action == "block")
142 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
143 std::string(" had their notice filtered, target was ")+
145 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
146 " :Your notice has been filtered and opers notified: "+reason);
148 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
149 std::string(" had their notice filtered, target was ")+
150 target+": "+reason+" Action: "+do_action);
152 if (do_action == "kill")
154 Srv->QuitUser(user,reason);
162 virtual void OnRehash()
164 // reload our config file on rehash - we must destroy and re-allocate the classes
165 // to call the constructor again and re-read our data.
168 Conf = new ConfigReader;
169 std::string filterfile = Conf->ReadValue("filter","file",0);
170 // this automatically re-reads the configuration file into the class
171 MyConf = new ConfigReader(filterfile);
172 if ((filterfile == "") || (!MyConf->Verify()))
174 // bail if the user forgot to create a config file
175 printf("Error, could not find <filter file=\"\"> definition in your config file!");
176 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
179 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
182 virtual Version GetVersion()
184 // This is version 2 because version 1.x is the unreleased unrealircd module
185 return Version(2,0,0,1,VF_VENDOR);
190 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
192 class ModuleFilterFactory : public ModuleFactory
195 ModuleFilterFactory()
199 ~ModuleFilterFactory()
203 virtual Module * CreateModule()
205 return new ModuleFilter;
211 extern "C" void * init_module( void )
213 return new ModuleFilterFactory;