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 * ---------------------------------------------------
19 // Message and notice filtering using glob patterns
20 // a module based on the original work done by Craig Edwards in 2003
21 // for the chatspike network.
28 #include "helperfuncs.h"
30 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
32 class ModuleFilter : public Module
35 ConfigReader *Conf, *MyConf;
40 // read the configuration file on startup.
41 // it is perfectly valid to set <filter file> to the value of the
42 // main config file, then append your <keyword> tags to the bottom
43 // of the main config... but rather messy. That's why the capability
44 // of using a seperate config file is provided.
46 Conf = new ConfigReader;
47 std::string filterfile = Conf->ReadValue("filter","file",0);
48 MyConf = new ConfigReader(filterfile);
49 if ((filterfile == "") || (!MyConf->Verify()))
51 printf("Error, could not find <filter file=\"\"> definition in your config file!");
52 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
55 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
58 virtual ~ModuleFilter()
65 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
67 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
69 std::string text2 = text+" ";
70 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
72 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
73 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
75 std::string target = "";
76 std::string reason = MyConf->ReadValue("keyword","reason",index);
77 std::string do_action = MyConf->ReadValue("keyword","action",index);
82 if (target_type == TYPE_USER)
84 userrec* t = (userrec*)dest;
85 target = std::string(t->nick);
87 else if (target_type == TYPE_CHANNEL)
89 chanrec* t = (chanrec*)dest;
90 target = std::string(t->name);
92 if (do_action == "block")
94 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
95 std::string(" had their message filtered, target was ")+
97 // this form of SendTo (with the source as NuLL) sends a server notice
98 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
99 " :Your message has been filtered and opers notified: "+reason);
102 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
103 std::string(" had their message filtered, target was ")+
104 target+": "+reason+" Action: "+do_action);
106 if (do_action == "kill")
108 Srv->QuitUser(user,reason);
116 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
118 std::string text2 = text+" ";
119 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
121 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
122 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
124 std::string target = "";
125 std::string reason = MyConf->ReadValue("keyword","reason",index);
126 std::string do_action = MyConf->ReadValue("keyword","action",index);
131 if (target_type == TYPE_USER)
133 userrec* t = (userrec*)dest;
134 target = std::string(t->nick);
136 else if (target_type == TYPE_CHANNEL)
138 chanrec* t = (chanrec*)dest;
139 target = std::string(t->name);
141 if (do_action == "block")
143 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
144 std::string(" had their notice filtered, target was ")+
146 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
147 " :Your notice has been filtered and opers notified: "+reason);
149 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
150 std::string(" had their notice filtered, target was ")+
151 target+": "+reason+" Action: "+do_action);
153 if (do_action == "kill")
155 Srv->QuitUser(user,reason);
163 virtual void OnRehash(std::string parameter)
165 // reload our config file on rehash - we must destroy and re-allocate the classes
166 // to call the constructor again and re-read our data.
169 Conf = new ConfigReader;
170 std::string filterfile = Conf->ReadValue("filter","file",0);
171 // this automatically re-reads the configuration file into the class
172 MyConf = new ConfigReader(filterfile);
173 if ((filterfile == "") || (!MyConf->Verify()))
175 // bail if the user forgot to create a config file
176 printf("Error, could not find <filter file=\"\"> definition in your config file!");
177 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
180 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
183 virtual Version GetVersion()
185 // This is version 2 because version 1.x is the unreleased unrealircd module
186 return Version(2,0,0,1,VF_VENDOR);
191 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
193 class ModuleFilterFactory : public ModuleFactory
196 ModuleFilterFactory()
200 ~ModuleFilterFactory()
204 virtual Module * CreateModule()
206 return new ModuleFilter;
212 extern "C" void * init_module( void )
214 return new ModuleFilterFactory;