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