]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
d2714ee018d1940a3d9022206aaa24e6b1eb9132
[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">
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                                 if (target_type == TYPE_USER)
59                                 {
60                                         userrec* t = (userrec*)dest;
61                                         target = std::string(t->nick);
62                                 }
63                                 else if (target_type == TYPE_CHANNEL)
64                                 {
65                                         chanrec* t = (chanrec*)dest;
66                                         target = std::string(t->name);
67                                 }
68                                 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
69                                                 std::string(" had their message filtered, target was ")+
70                                                 target+": "+MyConf->ReadValue("keyword","reason",index));
71                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
72                                                 std::string(" had their message filtered, target was ")+
73                                                 target+": "+MyConf->ReadValue("keyword","reason",index));
74                                 return 1;
75                         }
76                 }
77                 return 0;
78         }
79         
80         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string text)
81         {
82                 text = text + " ";
83                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
84                 {
85                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
86                         if (Srv->MatchText(text,pattern))
87                         {
88                                 std::string target = "";
89                                 if (target_type == TYPE_USER)
90                                 {
91                                         userrec* t = (userrec*)dest;
92                                         target = std::string(t->nick);
93                                 }
94                                 else if (target_type == TYPE_CHANNEL)
95                                 {
96                                         chanrec* t = (chanrec*)dest;
97                                         target = std::string(t->name);
98                                 }
99                                 Srv->SendOpers(std::string("FILTER: ")+std::string(user->nick)+
100                                                 std::string(" had their notice filtered, target was ")+
101                                                 target+": "+MyConf->ReadValue("keyword","reason",index));
102                                 Srv->Log(DEFAULT,std::string("FILTER: ")+std::string(user->nick)+
103                                                 std::string(" had their notice filtered, target was ")+
104                                                 target+": "+MyConf->ReadValue("keyword","reason",index));
105                                 return 1;
106                         }
107                 }
108                 return 0;
109         }
110         
111         virtual void OnRehash()
112         {
113                 // reload our config file on rehash - we must destroy and re-allocate the classes
114                 // to call the constructor again and re-read our data.
115                 delete Conf;
116                 delete MyConf;
117                 Conf = new ConfigReader;
118                 std::string filterfile = Conf->ReadValue("filter","file",0);
119                 if (filterfile == "")
120                 {
121                         // bail if the user forgot to create a config file
122                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
123                         exit(0);
124                 }
125                 // this automatically re-reads the configuration file into the class
126                 MyConf = new ConfigReader(filterfile);
127                 Srv->Log(DEFAULT,std::string("m_filter: read configuration from ")+filterfile);
128         }
129         
130         virtual Version GetVersion()
131         {
132                 // This is version 2 because version 1.x is the unreleased unrealircd module
133                 return Version(2,0,0,0);
134         }
135         
136 };
137
138 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
139
140 class ModuleFilterFactory : public ModuleFactory
141 {
142  public:
143         ModuleFilterFactory()
144         {
145         }
146         
147         ~ModuleFilterFactory()
148         {
149         }
150         
151         virtual Module * CreateModule()
152         {
153                 return new ModuleFilter;
154         }
155         
156 };
157
158
159 extern "C" void * init_module( void )
160 {
161         return new ModuleFilterFactory;
162 }
163