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.
30 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
34 /** Holds a filter pattern and reason
36 class Filter : public classbase
43 typedef std::map<std::string,Filter*> filter_t;
45 /** Thrown by m_filter
47 class FilterException : public ModuleException
50 virtual const char* GetReason()
52 return "Could not find <filter file=\"\"> definition in your config file!";
56 class ModuleFilter : public Module
62 ModuleFilter(InspIRCd* Me)
65 // read the configuration file on startup.
66 // it is perfectly valid to set <filter file> to the value of the
67 // main config file, then append your <keyword> tags to the bottom
68 // of the main config... but rather messy. That's why the capability
69 // of using a seperate config file is provided.
74 virtual ~ModuleFilter()
78 void Implements(char* List)
80 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
83 // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
85 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
87 return OnUserPreNotice(user,dest,target_type,text,status);
90 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
92 std::string text2 = text+" ";
93 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
95 if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first)))
97 Filter* f = (Filter*)index->second;
98 std::string target = "";
100 if (target_type == TYPE_USER)
102 userrec* t = (userrec*)dest;
103 target = std::string(t->nick);
105 else if (target_type == TYPE_CHANNEL)
107 chanrec* t = (chanrec*)dest;
108 target = std::string(t->name);
111 if (f->action == "block")
113 ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason);
114 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason);
116 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
118 if (f->action == "kill")
120 userrec::QuitUser(ServerInstance,user,f->reason);
128 virtual void OnRehash(const std::string ¶meter)
130 // reload our config file on rehash - we must destroy and re-allocate the classes
131 // to call the constructor again and re-read our data.
132 ConfigReader* Conf = new ConfigReader(ServerInstance);
133 std::string filterfile = Conf->ReadValue("filter","file",0);
134 // this automatically re-reads the configuration file into the class
135 ConfigReader* MyConf = new ConfigReader(ServerInstance, filterfile);
136 if ((filterfile == "") || (!MyConf->Verify()))
138 // bail if the user forgot to create a config file
142 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
147 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
149 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
150 std::string reason = MyConf->ReadValue("keyword","reason",index);
151 std::string do_action = MyConf->ReadValue("keyword","action",index);
154 Filter* x = new Filter;
156 x->action = do_action;
157 filters[pattern] = x;
159 ServerInstance->Log(DEFAULT,"m_filter: read configuration from "+filterfile);
164 virtual Version GetVersion()
166 // This is version 2 because version 1.x is the unreleased unrealircd module
167 return Version(2,0,0,2,VF_VENDOR);
172 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
174 class ModuleFilterFactory : public ModuleFactory
177 ModuleFilterFactory()
181 ~ModuleFilterFactory()
185 virtual Module * CreateModule(InspIRCd* Me)
187 return new ModuleFilter(Me);
193 extern "C" void * init_module( void )
195 return new ModuleFilterFactory;