]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
d43ad7373b05bfe3edd4057ce9fb915866eb8109
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
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.
22
23 #include <stdio.h>
24 #include <string>
25 #include "users.h"
26 #include "channels.h"
27 #include "modules.h"
28 #include "helperfuncs.h"
29
30 /* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */
31
32 class ModuleFilter : public Module
33 {
34  Server *Srv;
35  ConfigReader *Conf, *MyConf;
36  
37  public:
38         ModuleFilter(Server* Me)
39                 : Module::Module(Me)
40         {
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.
46                 Srv = Me;
47                 Conf = new ConfigReader;
48                 std::string filterfile = Conf->ReadValue("filter","file",0);
49                 MyConf = new ConfigReader(filterfile);
50                 if ((filterfile == "") || (!MyConf->Verify()))
51                 {
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!");
54                         return;
55                 }
56                 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
57         }
58         
59         virtual ~ModuleFilter()
60         {
61                 delete MyConf;
62                 delete Conf;
63         }
64         
65         // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
66         
67         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
68         {
69                 std::string text2 = text+" ";
70                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
71                 {
72                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
73                         if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
74                         {
75                                 std::string target = "";
76                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
77                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
78
79                                 if (do_action == "")
80                                         do_action = "none";
81
82                                 if (target_type == TYPE_USER)
83                                 {
84                                         userrec* t = (userrec*)dest;
85                                         target = std::string(t->nick);
86                                 }
87                                 else if (target_type == TYPE_CHANNEL)
88                                 {
89                                         chanrec* t = (chanrec*)dest;
90                                         target = std::string(t->name);
91                                 }
92                                 if (do_action == "block")
93                                 {       
94                                         Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
95                                                         std::string(" had their message filtered, target was ")+
96                                                         target+": "+reason);
97                                         // this form of SendTo (with the source as NuLL) sends a server notice
98                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
99                                                         " :Your message has been filtered and opers notified: "+reason);
100                                 }
101
102                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
103                                                 std::string(" had their message filtered, target was ")+
104                                                 target+": "+reason+" Action: "+do_action);
105
106                                 if (do_action == "kill")
107                                 {
108                                         Srv->QuitUser(user,reason);
109                                 }
110                                 return 1;
111                         }
112                 }
113                 return 0;
114         }
115         
116         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
117         {
118                 std::string text2 = text+" ";
119                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
120                 {
121                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
122                         if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
123                         {
124                                 std::string target = "";
125                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
126                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
127
128                                 if (do_action == "")
129                                         do_action = "none";
130                                         
131                                 if (target_type == TYPE_USER)
132                                 {
133                                         userrec* t = (userrec*)dest;
134                                         target = std::string(t->nick);
135                                 }
136                                 else if (target_type == TYPE_CHANNEL)
137                                 {
138                                         chanrec* t = (chanrec*)dest;
139                                         target = std::string(t->name);
140                                 }
141                                 if (do_action == "block")
142                                 {       
143                                         Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
144                                                         std::string(" had their notice filtered, target was ")+
145                                                         target+": "+reason);
146                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
147                                                         " :Your notice has been filtered and opers notified: "+reason);
148                                 }
149                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
150                                                 std::string(" had their notice filtered, target was ")+
151                                                 target+": "+reason+" Action: "+do_action);
152
153                                 if (do_action == "kill")
154                                 {
155                                         Srv->QuitUser(user,reason);
156                                 }
157                                 return 1;
158                         }
159                 }
160                 return 0;
161         }
162         
163         virtual void OnRehash(std::string parameter)
164         {
165                 // reload our config file on rehash - we must destroy and re-allocate the classes
166                 // to call the constructor again and re-read our data.
167                 delete Conf;
168                 delete MyConf;
169                 Conf = new ConfigReader;
170                 std::string filterfile = Conf->ReadValue("filter","file",0);
171                 // this automatically re-reads the configuration file into the class
172                 MyConf = new ConfigReader(filterfile);
173                 if ((filterfile == "") || (!MyConf->Verify()))
174                 {
175                         // bail if the user forgot to create a config file
176                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
177                         log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
178                         return;
179                 }
180                 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
181         }
182         
183         virtual Version GetVersion()
184         {
185                 // This is version 2 because version 1.x is the unreleased unrealircd module
186                 return Version(2,0,0,1,VF_VENDOR);
187         }
188         
189 };
190
191 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
192
193 class ModuleFilterFactory : public ModuleFactory
194 {
195  public:
196         ModuleFilterFactory()
197         {
198         }
199         
200         ~ModuleFilterFactory()
201         {
202         }
203         
204         virtual Module * CreateModule(Server* Me)
205         {
206                 return new ModuleFilter(Me);
207         }
208         
209 };
210
211
212 extern "C" void * init_module( void )
213 {
214         return new ModuleFilterFactory;
215 }
216