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