]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Added <options:notimesync> to the example config
[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 class ModuleFilter : public Module
46 {
47  
48  filter_t filters;
49  
50  public:
51         ModuleFilter(InspIRCd* Me)
52                 : Module::Module(Me)
53         {
54                 // read the configuration file on startup.
55                 // it is perfectly valid to set <filter file> to the value of the
56                 // main config file, then append your <keyword> tags to the bottom
57                 // of the main config... but rather messy. That's why the capability
58                 // of using a seperate config file is provided.
59                 
60                 OnRehash("");
61         }
62         
63         virtual ~ModuleFilter()
64         {
65         }
66
67         void Implements(char* List)
68         {
69                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
70         }
71         
72         // format of a config entry is <keyword pattern="*glob*" reason="Some reason here" action="kill/block">
73         
74         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
75         {
76                 return OnUserPreNotice(user,dest,target_type,text,status);
77         }
78         
79         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
80         {
81                 std::string text2 = text+" ";
82                 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
83                 {
84                         if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first)))
85                         {
86                                 Filter* f = (Filter*)index->second;
87                                 std::string target = "";
88
89                                 if (target_type == TYPE_USER)
90                                 {
91                                         userrec* t = (userrec*)dest;
92                                         target = std::string(t->nick);
93                                 }
94                                 else if (target_type == TYPE_CHANNEL)
95                                 {
96                                         chanrec* t = (chanrec*)dest;
97                                         target = std::string(t->name);
98                                 }
99
100                                 if (f->action == "block")
101                                 {       
102                                         ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason);
103                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason);
104                                 }
105                                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
106
107                                 if (f->action == "kill")
108                                 {
109                                         userrec::QuitUser(ServerInstance,user,f->reason);
110                                 }
111                                 return 1;
112                         }
113                 }
114                 return 0;
115         }
116         
117         virtual void OnRehash(const std::string &parameter)
118         {
119                 // this automatically re-reads the configuration file into the class
120                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
121                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
122                 {
123                         DELETE(n->second);
124                 }
125                 filters.clear();
126                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
127                 {
128                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
129                         std::string reason = MyConf->ReadValue("keyword","reason",index);
130                         std::string do_action = MyConf->ReadValue("keyword","action",index);
131                         if (do_action == "")
132                                 do_action = "none";
133                         Filter* x = new Filter;
134                         x->reason = reason;
135                         x->action = do_action;
136                         filters[pattern] = x;
137                 }
138                 DELETE(MyConf);
139         }
140         
141         virtual Version GetVersion()
142         {
143                 // This is version 2 because version 1.x is the unreleased unrealircd module
144                 return Version(2,1,0,2,VF_VENDOR,API_VERSION);
145         }
146         
147 };
148
149 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
150
151 class ModuleFilterFactory : public ModuleFactory
152 {
153  public:
154         ModuleFilterFactory()
155         {
156         }
157         
158         ~ModuleFilterFactory()
159         {
160         }
161         
162         virtual Module * CreateModule(InspIRCd* Me)
163         {
164                 return new ModuleFilter(Me);
165         }
166         
167 };
168
169
170 extern "C" void * init_module( void )
171 {
172         return new ModuleFilterFactory;
173 }
174