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