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