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