]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Added version flags
[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                         exit(0);
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))
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))
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                         exit(0);
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,0,0);
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