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, char status)
74 return OnUserPreNotice(user,dest,target_type,text,status);
77 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
79 std::string text2 = text+" ";
80 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
82 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
83 if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
85 std::string target = "";
86 std::string reason = MyConf->ReadValue("keyword","reason",index);
87 std::string do_action = MyConf->ReadValue("keyword","action",index);
92 if (target_type == TYPE_USER)
94 userrec* t = (userrec*)dest;
95 target = std::string(t->nick);
97 else if (target_type == TYPE_CHANNEL)
99 chanrec* t = (chanrec*)dest;
100 target = std::string(t->name);
102 if (do_action == "block")
104 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
105 std::string(" had their notice filtered, target was ")+
107 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
108 " :Your notice has been filtered and opers notified: "+reason);
110 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
111 std::string(" had their notice filtered, target was ")+
112 target+": "+reason+" Action: "+do_action);
114 if (do_action == "kill")
116 Srv->QuitUser(user,reason);
124 virtual void OnRehash(std::string parameter)
126 // reload our config file on rehash - we must destroy and re-allocate the classes
127 // to call the constructor again and re-read our data.
130 Conf = new ConfigReader;
131 std::string filterfile = Conf->ReadValue("filter","file",0);
132 // this automatically re-reads the configuration file into the class
133 MyConf = new ConfigReader(filterfile);
134 if ((filterfile == "") || (!MyConf->Verify()))
136 // bail if the user forgot to create a config file
137 printf("Error, could not find <filter file=\"\"> definition in your config file!");
138 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
141 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
144 virtual Version GetVersion()
146 // This is version 2 because version 1.x is the unreleased unrealircd module
147 return Version(2,0,0,1,VF_VENDOR);
152 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
154 class ModuleFilterFactory : public ModuleFactory
157 ModuleFilterFactory()
161 ~ModuleFilterFactory()
165 virtual Module * CreateModule(Server* Me)
167 return new ModuleFilter(Me);
173 extern "C" void * init_module( void )
175 return new ModuleFilterFactory;