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