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 */
39 typedef std::map<std::string,Filter*> filter_t;
41 class FilterException : public ModuleException
44 virtual char* GetReason()
46 return "Could not find <filter file=\"\"> definition in your config file!";
50 class ModuleFilter : public Module
56 ModuleFilter(Server* Me)
59 // read the configuration file on startup.
60 // it is perfectly valid to set <filter file> to the value of the
61 // main config file, then append your <keyword> tags to the bottom
62 // of the main config... but rather messy. That's why the capability
63 // of using a seperate config file is provided.
68 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 (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
89 if ((Srv->MatchText(text2,index->first)) || (Srv->MatchText(text,index->first)))
91 Filter* f = (Filter*)index->second;
92 std::string target = "";
94 if (target_type == TYPE_USER)
96 userrec* t = (userrec*)dest;
97 target = std::string(t->nick);
99 else if (target_type == TYPE_CHANNEL)
101 chanrec* t = (chanrec*)dest;
102 target = std::string(t->name);
105 if (f->action == "block")
107 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
108 std::string(" had their notice filtered, target was ")+
109 target+": "+f->reason);
110 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
111 " :Your notice has been filtered and opers notified: "+f->reason);
113 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
114 std::string(" had their notice filtered, target was ")+
115 target+": "+f->reason+" Action: "+f->action);
117 if (f->action == "kill")
119 Srv->QuitUser(user,f->reason);
127 virtual void OnRehash(std::string parameter)
129 // reload our config file on rehash - we must destroy and re-allocate the classes
130 // to call the constructor again and re-read our data.
131 ConfigReader* Conf = new ConfigReader;
132 std::string filterfile = Conf->ReadValue("filter","file",0);
133 // this automatically re-reads the configuration file into the class
134 ConfigReader* MyConf = new ConfigReader(filterfile);
135 if ((filterfile == "") || (!MyConf->Verify()))
137 // bail if the user forgot to create a config file
141 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
146 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
148 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
149 std::string reason = MyConf->ReadValue("keyword","reason",index);
150 std::string do_action = MyConf->ReadValue("keyword","action",index);
153 Filter* x = new Filter;
155 x->action = do_action;
156 filters[pattern] = x;
158 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
163 virtual Version GetVersion()
165 // This is version 2 because version 1.x is the unreleased unrealircd module
166 return Version(2,0,0,2,VF_VENDOR);
171 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
173 class ModuleFilterFactory : public ModuleFactory
176 ModuleFilterFactory()
180 ~ModuleFilterFactory()
184 virtual Module * CreateModule(Server* Me)
186 return new ModuleFilter(Me);
192 extern "C" void * init_module( void )
194 return new ModuleFilterFactory;