]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
Filtering should leave ulines alone, and not filter uline notices.
[user/henk/code/inspircd.git] / src / modules / m_filter.h
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 #include "xline.h"
18
19 class FilterResult : public classbase
20 {
21  public:
22         std::string freeform;
23         std::string reason;
24         std::string action;
25         long gline_time;
26
27         FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt) : freeform(free), reason(rea), action(act), gline_time(gt)
28         {
29         }
30
31         FilterResult()
32         {
33         }
34
35         virtual ~FilterResult()
36         {
37         }
38 };
39
40 class cmd_filter;
41
42 class FilterBase : public Module
43 {
44         cmd_filter* filtcommand;
45  public:
46         FilterBase(InspIRCd* Me, const std::string &source);
47         virtual ~FilterBase();
48         virtual void Implements(char* List);
49         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status);
50
51         virtual FilterResult* FilterMatch(const std::string &text) = 0;
52         virtual bool DeleteFilter(const std::string &freeform) = 0;
53         virtual void SyncFilters(Module* proto, void* opaque) = 0;
54
55         virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
56         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration) = 0;
57         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status);
58         virtual void OnRehash(const std::string &parameter);
59         virtual Version GetVersion();
60         std::string EncodeFilter(FilterResult* filter);
61         FilterResult DecodeFilter(const std::string &data);
62         virtual void OnSyncOtherMetaData(Module* proto, void* opaque);
63         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata);
64
65         virtual int OnStats(char symbol, userrec* user, string_list &results) = 0;
66 };
67
68 class cmd_filter : public command_t
69 {
70         FilterBase* Base;
71  public:
72         cmd_filter(FilterBase* f, InspIRCd* Me, const std::string &source) : command_t(Me, "FILTER", 'o', 1), Base(f)
73         {
74                 this->source = source;
75                 this->syntax = "<filter-definition> <type> [<gline-duration>] :<reason>";
76         }
77
78         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
79         {
80                 if (pcnt == 1)
81                 {
82                         /* Deleting a filter */
83                         if (Base->DeleteFilter(parameters[0]))
84                         {
85                                 user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]);
86                                 return CMD_SUCCESS;
87                         }
88                         else
89                         {
90                                 user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]);
91                                 return CMD_FAILURE;
92                         }
93                 }
94                 else
95                 {
96                         /* Adding a filter */
97                         if (pcnt >= 3)
98                         {
99                                 std::string freeform = parameters[0];
100                                 std::string type = parameters[1];
101                                 std::string reason;
102                                 long duration = 0;
103
104                                 if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill"))
105                                 {
106                                         user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', and 'kill'.", user->nick, freeform.c_str());
107                                         return CMD_FAILURE;
108                                 }
109
110                                 if (type == "gline")
111                                 {
112                                         if (pcnt >= 4)
113                                         {
114                                                 duration = ServerInstance->Duration(parameters[2]);
115                                                 reason = parameters[3];
116                                         }
117                                         else
118                                         {
119                                                 this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter.");
120                                                 return CMD_FAILURE;
121                                         }
122                                 }
123                                 else
124                                 {
125                                         reason = parameters[2];
126                                 }
127                                 std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration);
128                                 if (result.first)
129                                 {
130                                         user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, reason: '%s'", user->nick, freeform.c_str(),
131                                                         type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[2] : ""),
132                                                         reason.c_str());
133                                         return CMD_SUCCESS;
134                                 }
135                                 else
136                                 {
137                                         user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str());
138                                         return CMD_FAILURE;
139                                 }
140                         }
141                         else
142                         {
143                                 this->TooFewParams(user, ".");
144                                 return CMD_FAILURE;
145                         }
146
147                 }
148         }
149
150         void TooFewParams(userrec* user, const std::string &extra_text)
151         {
152                 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str());
153         }
154 };
155
156 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module::Module(Me)
157 {
158         filtcommand = new cmd_filter(this, Me, source);
159         ServerInstance->AddCommand(filtcommand);
160 }
161
162 FilterBase::~FilterBase()
163 {
164 }
165
166 void FilterBase::Implements(char* List)
167 {
168         List[I_OnStats] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
169 }
170
171 int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
172 {
173         return OnUserPreNotice(user,dest,target_type,text,status);
174 }
175
176 int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
177 {
178         /* Leave ulines alone */
179         if (ServerInstance->ULine(user->server))
180                 return 0;
181
182         FilterResult* f = this->FilterMatch(text);
183         if (f)
184         {
185                 std::string target = "";
186                 if (target_type == TYPE_USER)
187                 {
188                         userrec* t = (userrec*)dest;
189                         target = std::string(t->nick);
190                 }
191                 else if (target_type == TYPE_CHANNEL)
192                 {
193                         chanrec* t = (chanrec*)dest;
194                         target = std::string(t->name);
195                 }
196                 if (f->action == "block")
197                 {       
198                         ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason);
199                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason);
200                 }
201                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
202                 if (f->action == "kill")
203                 {
204                         userrec::QuitUser(ServerInstance,user,f->reason);
205                 }
206                 if (f->action == "gline")
207                 {
208                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
209                         {
210                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
211                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
212                         }
213                 }
214                 return 1;
215         }
216         return 0;
217 }
218
219 void FilterBase::OnRehash(const std::string &parameter)
220 {
221 }
222         
223 Version FilterBase::GetVersion()
224 {
225         return Version(1,1,0,2,VF_VENDOR|VF_COMMON,API_VERSION);
226 }
227
228
229 std::string FilterBase::EncodeFilter(FilterResult* filter)
230 {
231         std::ostringstream stream;
232         stream << filter->freeform << " " << filter->action << " " << filter->gline_time << " " << filter->reason;
233         return stream.str();
234 }
235
236 FilterResult FilterBase::DecodeFilter(const std::string &data)
237 {
238         FilterResult res;
239         std::istringstream stream(data);
240         stream >> res.freeform;
241         stream >> res.action;
242         stream >> res.gline_time;
243         res.reason = stream.str();
244         return res;
245 }
246
247 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque)
248 {
249         this->SyncFilters(proto, opaque);
250 }
251
252 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
253 {
254         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
255 }
256
257 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
258 {
259         if ((target_type == TYPE_OTHER) && (extname == "filter"))
260         {
261                 FilterResult data = DecodeFilter(extdata);
262                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time);
263         }
264 }
265