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