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