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