]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Updated copyrights in headers etc using perl inplace edit
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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         void Implements(char* List)
66         {
67                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
68         }
69         
70         // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
71         
72         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
73         {
74                 std::string text2 = text+" ";
75                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
76                 {
77                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
78                         if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
79                         {
80                                 std::string target = "";
81                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
82                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
83
84                                 if (do_action == "")
85                                         do_action = "none";
86
87                                 if (target_type == TYPE_USER)
88                                 {
89                                         userrec* t = (userrec*)dest;
90                                         target = std::string(t->nick);
91                                 }
92                                 else if (target_type == TYPE_CHANNEL)
93                                 {
94                                         chanrec* t = (chanrec*)dest;
95                                         target = std::string(t->name);
96                                 }
97                                 if (do_action == "block")
98                                 {       
99                                         Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
100                                                         std::string(" had their message filtered, target was ")+
101                                                         target+": "+reason);
102                                         // this form of SendTo (with the source as NuLL) sends a server notice
103                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
104                                                         " :Your message has been filtered and opers notified: "+reason);
105                                 }
106
107                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
108                                                 std::string(" had their message filtered, target was ")+
109                                                 target+": "+reason+" Action: "+do_action);
110
111                                 if (do_action == "kill")
112                                 {
113                                         Srv->QuitUser(user,reason);
114                                 }
115                                 return 1;
116                         }
117                 }
118                 return 0;
119         }
120         
121         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
122         {
123                 std::string text2 = text+" ";
124                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
125                 {
126                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
127                         if ((Srv->MatchText(text2,pattern)) || (Srv->MatchText(text,pattern)))
128                         {
129                                 std::string target = "";
130                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
131                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
132
133                                 if (do_action == "")
134                                         do_action = "none";
135                                         
136                                 if (target_type == TYPE_USER)
137                                 {
138                                         userrec* t = (userrec*)dest;
139                                         target = std::string(t->nick);
140                                 }
141                                 else if (target_type == TYPE_CHANNEL)
142                                 {
143                                         chanrec* t = (chanrec*)dest;
144                                         target = std::string(t->name);
145                                 }
146                                 if (do_action == "block")
147                                 {       
148                                         Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
149                                                         std::string(" had their notice filtered, target was ")+
150                                                         target+": "+reason);
151                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
152                                                         " :Your notice has been filtered and opers notified: "+reason);
153                                 }
154                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
155                                                 std::string(" had their notice filtered, target was ")+
156                                                 target+": "+reason+" Action: "+do_action);
157
158                                 if (do_action == "kill")
159                                 {
160                                         Srv->QuitUser(user,reason);
161                                 }
162                                 return 1;
163                         }
164                 }
165                 return 0;
166         }
167         
168         virtual void OnRehash(std::string parameter)
169         {
170                 // reload our config file on rehash - we must destroy and re-allocate the classes
171                 // to call the constructor again and re-read our data.
172                 delete Conf;
173                 delete MyConf;
174                 Conf = new ConfigReader;
175                 std::string filterfile = Conf->ReadValue("filter","file",0);
176                 // this automatically re-reads the configuration file into the class
177                 MyConf = new ConfigReader(filterfile);
178                 if ((filterfile == "") || (!MyConf->Verify()))
179                 {
180                         // bail if the user forgot to create a config file
181                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
182                         log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
183                         return;
184                 }
185                 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
186         }
187         
188         virtual Version GetVersion()
189         {
190                 // This is version 2 because version 1.x is the unreleased unrealircd module
191                 return Version(2,0,0,1,VF_VENDOR);
192         }
193         
194 };
195
196 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
197
198 class ModuleFilterFactory : public ModuleFactory
199 {
200  public:
201         ModuleFilterFactory()
202         {
203         }
204         
205         ~ModuleFilterFactory()
206         {
207         }
208         
209         virtual Module * CreateModule(Server* Me)
210         {
211                 return new ModuleFilter(Me);
212         }
213         
214 };
215
216
217 extern "C" void * init_module( void )
218 {
219         return new ModuleFilterFactory;
220 }
221