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