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 FilterException : public ModuleException
34 virtual char* GetReason()
36 return "Could not find <filter file=\"\"> definition in your config file!";
40 class ModuleFilter : public Module
43 ConfigReader *Conf, *MyConf;
46 ModuleFilter(Server* Me)
49 // read the configuration file on startup.
50 // it is perfectly valid to set <filter file> to the value of the
51 // main config file, then append your <keyword> tags to the bottom
52 // of the main config... but rather messy. That's why the capability
53 // of using a seperate config file is provided.
55 Conf = new ConfigReader;
56 std::string filterfile = Conf->ReadValue("filter","file",0);
57 MyConf = new ConfigReader(filterfile);
58 if ((filterfile == "") || (!MyConf->Verify()))
63 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
66 virtual ~ModuleFilter()
72 void Implements(char* List)
74 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
77 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
79 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
81 return OnUserPreNotice(user,dest,target_type,text,status);
84 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
86 std::string text2 = text+" ";
87 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
89 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
90 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
92 std::string target = "";
93 std::string reason = MyConf->ReadValue("keyword","reason",index);
94 std::string do_action = MyConf->ReadValue("keyword","action",index);
99 if (target_type == TYPE_USER)
101 userrec* t = (userrec*)dest;
102 target = std::string(t->nick);
104 else if (target_type == TYPE_CHANNEL)
106 chanrec* t = (chanrec*)dest;
107 target = std::string(t->name);
109 if (do_action == "block")
111 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
112 std::string(" had their notice filtered, target was ")+
114 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
115 " :Your notice has been filtered and opers notified: "+reason);
117 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
118 std::string(" had their notice filtered, target was ")+
119 target+": "+reason+" Action: "+do_action);
121 if (do_action == "kill")
123 Srv->QuitUser(user,reason);
131 virtual void OnRehash(std::string parameter)
133 // reload our config file on rehash - we must destroy and re-allocate the classes
134 // to call the constructor again and re-read our data.
137 Conf = new ConfigReader;
138 std::string filterfile = Conf->ReadValue("filter","file",0);
139 // this automatically re-reads the configuration file into the class
140 MyConf = new ConfigReader(filterfile);
141 if ((filterfile == "") || (!MyConf->Verify()))
143 // bail if the user forgot to create a config file
147 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
150 virtual Version GetVersion()
152 // This is version 2 because version 1.x is the unreleased unrealircd module
153 return Version(2,0,0,1,VF_VENDOR);
158 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
160 class ModuleFilterFactory : public ModuleFactory
163 ModuleFilterFactory()
167 ~ModuleFilterFactory()
171 virtual Module * CreateModule(Server* Me)
173 return new ModuleFilter(Me);
179 extern "C" void * init_module( void )
181 return new ModuleFilterFactory;