1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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;
38 ModuleFilter(Server* Me)
41 // read the configuration file on startup.
42 // it is perfectly valid to set <filter file> to the value of the
43 // main config file, then append your <keyword> tags to the bottom
44 // of the main config... but rather messy. That's why the capability
45 // of using a seperate config file is provided.
47 Conf = new ConfigReader;
48 std::string filterfile = Conf->ReadValue("filter","file",0);
49 MyConf = new ConfigReader(filterfile);
50 if ((filterfile == "") || (!MyConf->Verify()))
52 printf("Error, could not find <filter file=\"\"> definition in your config file!\n");
53 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
56 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
59 virtual ~ModuleFilter()
65 void Implements(char* List)
67 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
70 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
72 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
74 std::string text2 = text+" ";
75 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
77 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
78 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
80 std::string target = "";
81 std::string reason = MyConf->ReadValue("keyword","reason",index);
82 std::string do_action = MyConf->ReadValue("keyword","action",index);
87 if (target_type == TYPE_USER)
89 userrec* t = (userrec*)dest;
90 target = std::string(t->nick);
92 else if (target_type == TYPE_CHANNEL)
94 chanrec* t = (chanrec*)dest;
95 target = std::string(t->name);
97 if (do_action == "block")
99 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
100 std::string(" had their message filtered, target was ")+
102 // this form of SendTo (with the source as NuLL) sends a server notice
103 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
104 " :Your message has been filtered and opers notified: "+reason);
107 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
108 std::string(" had their message filtered, target was ")+
109 target+": "+reason+" Action: "+do_action);
111 if (do_action == "kill")
113 Srv->QuitUser(user,reason);
121 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
123 std::string text2 = text+" ";
124 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
126 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
127 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
129 std::string target = "";
130 std::string reason = MyConf->ReadValue("keyword","reason",index);
131 std::string do_action = MyConf->ReadValue("keyword","action",index);
136 if (target_type == TYPE_USER)
138 userrec* t = (userrec*)dest;
139 target = std::string(t->nick);
141 else if (target_type == TYPE_CHANNEL)
143 chanrec* t = (chanrec*)dest;
144 target = std::string(t->name);
146 if (do_action == "block")
148 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
149 std::string(" had their notice filtered, target was ")+
151 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
152 " :Your notice has been filtered and opers notified: "+reason);
154 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
155 std::string(" had their notice filtered, target was ")+
156 target+": "+reason+" Action: "+do_action);
158 if (do_action == "kill")
160 Srv->QuitUser(user,reason);
168 virtual void OnRehash(std::string parameter)
170 // reload our config file on rehash - we must destroy and re-allocate the classes
171 // to call the constructor again and re-read our data.
174 Conf = new ConfigReader;
175 std::string filterfile = Conf->ReadValue("filter","file",0);
176 // this automatically re-reads the configuration file into the class
177 MyConf = new ConfigReader(filterfile);
178 if ((filterfile == "") || (!MyConf->Verify()))
180 // bail if the user forgot to create a config file
181 printf("Error, could not find <filter file=\"\"> definition in your config file!");
182 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
185 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
188 virtual Version GetVersion()
190 // This is version 2 because version 1.x is the unreleased unrealircd module
191 return Version(2,0,0,1,VF_VENDOR);
196 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
198 class ModuleFilterFactory : public ModuleFactory
201 ModuleFilterFactory()
205 ~ModuleFilterFactory()
209 virtual Module * CreateModule(Server* Me)
211 return new ModuleFilter(Me);
217 extern "C" void * init_module( void )
219 return new ModuleFilterFactory;