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.
26 #include "helperfuncs.h"
28 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
30 class ModuleFilter : public Module
33 ConfigReader *Conf, *MyConf;
38 // read the configuration file on startup.
39 // it is perfectly valid to set <filter file> to the value of the
40 // main config file, then append your <keyword> tags to the bottom
41 // of the main config... but rather messy. That's why the capability
42 // of using a seperate config file is provided.
44 Conf = new ConfigReader;
45 std::string filterfile = Conf->ReadValue("filter","file",0);
46 MyConf = new ConfigReader(filterfile);
47 if ((filterfile == "") || (!MyConf->Verify()))
49 printf("Error, could not find <filter file=\"\"> definition in your config file!");
50 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
53 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
56 virtual ~ModuleFilter()
63 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
65 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
67 std::string text2 = text+" ";
68 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
70 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
71 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
73 std::string target = "";
74 std::string reason = MyConf->ReadValue("keyword","reason",index);
75 std::string do_action = MyConf->ReadValue("keyword","action",index);
80 if (target_type == TYPE_USER)
82 userrec* t = (userrec*)dest;
83 target = std::string(t->nick);
85 else if (target_type == TYPE_CHANNEL)
87 chanrec* t = (chanrec*)dest;
88 target = std::string(t->name);
90 if (do_action == "block")
92 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
93 std::string(" had their message filtered, target was ")+
95 // this form of SendTo (with the source as NuLL) sends a server notice
96 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
97 " :Your message has been filtered and opers notified: "+reason);
100 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
101 std::string(" had their message filtered, target was ")+
102 target+": "+reason+" Action: "+do_action);
104 if (do_action == "kill")
106 Srv->QuitUser(user,reason);
114 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
116 std::string text2 = text+" ";
117 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
119 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
120 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
122 std::string target = "";
123 std::string reason = MyConf->ReadValue("keyword","reason",index);
124 std::string do_action = MyConf->ReadValue("keyword","action",index);
129 if (target_type == TYPE_USER)
131 userrec* t = (userrec*)dest;
132 target = std::string(t->nick);
134 else if (target_type == TYPE_CHANNEL)
136 chanrec* t = (chanrec*)dest;
137 target = std::string(t->name);
139 if (do_action == "block")
141 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
142 std::string(" had their notice filtered, target was ")+
144 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
145 " :Your notice has been filtered and opers notified: "+reason);
147 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
148 std::string(" had their notice filtered, target was ")+
149 target+": "+reason+" Action: "+do_action);
151 if (do_action == "kill")
153 Srv->QuitUser(user,reason);
161 virtual void OnRehash()
163 // reload our config file on rehash - we must destroy and re-allocate the classes
164 // to call the constructor again and re-read our data.
167 Conf = new ConfigReader;
168 std::string filterfile = Conf->ReadValue("filter","file",0);
169 // this automatically re-reads the configuration file into the class
170 MyConf = new ConfigReader(filterfile);
171 if ((filterfile == "") || (!MyConf->Verify()))
173 // bail if the user forgot to create a config file
174 printf("Error, could not find <filter file=\"\"> definition in your config file!");
175 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
178 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
181 virtual Version GetVersion()
183 // This is version 2 because version 1.x is the unreleased unrealircd module
184 return Version(2,0,0,1,VF_VENDOR);
189 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
191 class ModuleFilterFactory : public ModuleFactory
194 ModuleFilterFactory()
198 ~ModuleFilterFactory()
202 virtual Module * CreateModule()
204 return new ModuleFilter;
210 extern "C" void * init_module( void )
212 return new ModuleFilterFactory;