]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.h
Whoops, make /stats s work
[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         FilterResult* f = this->FilterMatch(text);
179         if (f)
180         {
181                 std::string target = "";
182                 if (target_type == TYPE_USER)
183                 {
184                         userrec* t = (userrec*)dest;
185                         target = std::string(t->nick);
186                 }
187                 else if (target_type == TYPE_CHANNEL)
188                 {
189                         chanrec* t = (chanrec*)dest;
190                         target = std::string(t->name);
191                 }
192                 if (f->action == "block")
193                 {       
194                         ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason);
195                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason);
196                 }
197                 ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action);
198                 if (f->action == "kill")
199                 {
200                         userrec::QuitUser(ServerInstance,user,f->reason);
201                 }
202                 if (f->action == "gline")
203                 {
204                         if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP()))
205                         {
206                                 ServerInstance->XLines->apply_lines(APPLY_GLINES);
207                                 FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP()));
208                         }
209                 }
210                 return 1;
211         }
212         return 0;
213 }
214
215 void FilterBase::OnRehash(const std::string &parameter)
216 {
217 }
218         
219 Version FilterBase::GetVersion()
220 {
221         return Version(1,1,0,2,VF_VENDOR,API_VERSION);
222 }
223
224
225 std::string FilterBase::EncodeFilter(FilterResult* filter)
226 {
227         std::ostringstream stream;
228         stream << filter->freeform << " " << filter->action << " " << filter->gline_time << " " << filter->reason;
229         return stream.str();
230 }
231
232 FilterResult FilterBase::DecodeFilter(const std::string &data)
233 {
234         FilterResult res;
235         std::istringstream stream(data);
236         stream >> res.freeform;
237         stream >> res.action;
238         stream >> res.gline_time;
239         res.reason = stream.str();
240         return res;
241 }
242
243 void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque)
244 {
245         this->SyncFilters(proto, opaque);
246 }
247
248 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
249 {
250         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter));
251 }
252
253 void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
254 {
255         if ((target_type == TYPE_OTHER) && (extname == "filter"))
256         {
257                 FilterResult data = DecodeFilter(extdata);
258                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time);
259         }
260 }
261