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