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);
33 printf("Error, could not find <filter file=\"\"> definition in your config file!");
36 MyConf = new ConfigReader(filterfile);
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">
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 if (target_type == TYPE_USER)
60 userrec* t = (userrec*)dest;
61 target = std::string(t->nick);
63 else if (target_type == TYPE_CHANNEL)
65 chanrec* t = (chanrec*)dest;
66 target = std::string(t->name);
68 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
69 std::string(" had their message filtered, target was ")+
70 target+": "+MyConf->ReadValue("keyword","reason",index));
71 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
72 std::string(" had their message filtered, target was ")+
73 target+": "+MyConf->ReadValue("keyword","reason",index));
80 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text)
83 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
85 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
86 if (Srv->MatchText(text,pattern))
88 std::string target = "";
89 if (target_type == TYPE_USER)
91 userrec* t = (userrec*)dest;
92 target = std::string(t->nick);
94 else if (target_type == TYPE_CHANNEL)
96 chanrec* t = (chanrec*)dest;
97 target = std::string(t->name);
99 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
100 std::string(" had their notice filtered, target was ")+
101 target+": "+MyConf->ReadValue("keyword","reason",index));
102 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
103 std::string(" had their notice filtered, target was ")+
104 target+": "+MyConf->ReadValue("keyword","reason",index));
111 virtual void OnRehash()
113 // reload our config file on rehash - we must destroy and re-allocate the classes
114 // to call the constructor again and re-read our data.
117 Conf = new ConfigReader;
118 std::string filterfile = Conf->ReadValue("filter","file",0);
119 if (filterfile == "")
121 // bail if the user forgot to create a config file
122 printf("Error, could not find <filter file=\"\"> definition in your config file!");
125 // this automatically re-reads the configuration file into the class
126 MyConf = new ConfigReader(filterfile);
127 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
130 virtual Version GetVersion()
132 // This is version 2 because version 1.x is the unreleased unrealircd module
133 return Version(2,0,0,0);
138 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
140 class ModuleFilterFactory : public ModuleFactory
143 ModuleFilterFactory()
147 ~ModuleFilterFactory()
151 virtual Module * CreateModule()
153 return new ModuleFilter;
159 extern "C" void * init_module( void )
161 return new ModuleFilterFactory;