]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Change allocation of InspIRCd::Parser to be physically part of the object containing it
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2006 John Brooks <john.brooks@dereferenced.net>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 /* Improved drop-in replacement for the /SILENCE command
27  * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
28  *
29  * example that blocks all except private messages
30  *  /SILENCE +*!*@* a
31  *  /SILENCE +*!*@* px
32  *
33  * example that blocks all invites except from channel services
34  *  /SILENCE +*!*@* i
35  *  /SILENCE +chanserv!services@chatters.net ix
36  *
37  * example that blocks some bad dude from private, notice and inviting you
38  *  /SILENCE +*!kiddie@lamerz.net pin
39  *
40  * TODO: possibly have add and remove check for existing host and only modify flags according to
41  *       what's been changed instead of having to remove first, then add if you want to change
42  *       an entry.
43  */
44
45 // pair of hostmask and flags
46 typedef std::pair<std::string, int> silenceset;
47
48 // deque list of pairs
49 typedef std::deque<silenceset> silencelist;
50
51 // intmasks for flags
52 static int SILENCE_PRIVATE      = 0x0001; /* p  private messages      */
53 static int SILENCE_CHANNEL      = 0x0002; /* c  channel messages      */
54 static int SILENCE_INVITE       = 0x0004; /* i  invites               */
55 static int SILENCE_NOTICE       = 0x0008; /* n  notices               */
56 static int SILENCE_CNOTICE      = 0x0010; /* t  channel notices       */
57 static int SILENCE_ALL          = 0x0020; /* a  all, (pcint)          */
58 static int SILENCE_EXCLUDE      = 0x0040; /* x  exclude this pattern  */
59
60
61 class CommandSVSSilence : public Command
62 {
63  public:
64         CommandSVSSilence(Module* Creator) : Command(Creator,"SVSSILENCE", 2)
65         {
66                 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
67                 TRANSLATE3(TR_NICK, TR_TEXT, TR_TEXT);
68         }
69
70         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
71         {
72                 /*
73                  * XXX: thought occurs to me
74                  * We may want to change the syntax of this command to
75                  * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
76                  * style command so services can modify lots of entries at once.
77                  * leaving it backwards compatible for now as it's late. -- w
78                  */
79                 if (!user->server->IsULine())
80                         return CMD_FAILURE;
81
82                 User *u = ServerInstance->FindNick(parameters[0]);
83                 if (!u)
84                         return CMD_FAILURE;
85
86                 if (IS_LOCAL(u))
87                 {
88                         ServerInstance->Parser.CallHandler("SILENCE", std::vector<std::string>(parameters.begin() + 1, parameters.end()), u);
89                 }
90
91                 return CMD_SUCCESS;
92         }
93
94         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
95         {
96                 User* target = ServerInstance->FindNick(parameters[0]);
97                 if (target)
98                         return ROUTE_OPT_UCAST(target->server);
99                 return ROUTE_LOCALONLY;
100         }
101 };
102
103 class CommandSilence : public Command
104 {
105         unsigned int& maxsilence;
106  public:
107         SimpleExtItem<silencelist> ext;
108         CommandSilence(Module* Creator, unsigned int &max) : Command(Creator, "SILENCE", 0),
109                 maxsilence(max), ext("silence_list", Creator)
110         {
111                 allow_empty_last_param = false;
112                 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
113         }
114
115         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
116         {
117                 if (!parameters.size())
118                 {
119                         // no parameters, show the current silence list.
120                         silencelist* sl = ext.get(user);
121                         // if the user has a silence list associated with their user record, show it
122                         if (sl)
123                         {
124                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
125                                 {
126                                         std::string decomppattern = DecompPattern(c->second);
127                                         user->WriteNumeric(271, "%s %s %s", user->nick.c_str(),c->first.c_str(), decomppattern.c_str());
128                                 }
129                         }
130                         user->WriteNumeric(272, ":End of Silence List");
131
132                         return CMD_SUCCESS;
133                 }
134                 else if (parameters.size() > 0)
135                 {
136                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
137                         std::string mask = parameters[0].substr(1);
138                         char action = parameters[0][0];
139                         // Default is private and notice so clients do not break
140                         int pattern = CompilePattern("pn");
141
142                         // if pattern supplied, use it
143                         if (parameters.size() > 1) {
144                                 pattern = CompilePattern(parameters[1].c_str());
145                         }
146
147                         if (pattern == 0)
148                         {
149                                 user->WriteNotice("Bad SILENCE pattern");
150                                 return CMD_INVALID;
151                         }
152
153                         if (!mask.length())
154                         {
155                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
156                                 mask = "*!*@*";
157                         }
158
159                         ModeParser::CleanMask(mask);
160
161                         if (action == '-')
162                         {
163                                 std::string decomppattern = DecompPattern(pattern);
164                                 // fetch their silence list
165                                 silencelist* sl = ext.get(user);
166                                 // does it contain any entries and does it exist?
167                                 if (sl)
168                                 {
169                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
170                                         {
171                                                 // search through for the item
172                                                 irc::string listitem = i->first.c_str();
173                                                 if (listitem == mask && i->second == pattern)
174                                                 {
175                                                         sl->erase(i);
176                                                         user->WriteNumeric(950, "%s :Removed %s %s from silence list", user->nick.c_str(), mask.c_str(), decomppattern.c_str());
177                                                         if (!sl->size())
178                                                         {
179                                                                 ext.unset(user);
180                                                         }
181                                                         return CMD_SUCCESS;
182                                                 }
183                                         }
184                                 }
185                                 user->WriteNumeric(952, "%s :%s %s does not exist on your silence list", user->nick.c_str(), mask.c_str(), decomppattern.c_str());
186                         }
187                         else if (action == '+')
188                         {
189                                 // fetch the user's current silence list
190                                 silencelist* sl = ext.get(user);
191                                 if (!sl)
192                                 {
193                                         sl = new silencelist;
194                                         ext.set(user, sl);
195                                 }
196                                 if (sl->size() > maxsilence)
197                                 {
198                                         user->WriteNumeric(952, "%s :Your silence list is full",user->nick.c_str());
199                                         return CMD_FAILURE;
200                                 }
201
202                                 std::string decomppattern = DecompPattern(pattern);
203                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
204                                 {
205                                         irc::string listitem = n->first.c_str();
206                                         if (listitem == mask && n->second == pattern)
207                                         {
208                                                 user->WriteNumeric(952, "%s :%s %s is already on your silence list", user->nick.c_str(), mask.c_str(), decomppattern.c_str());
209                                                 return CMD_FAILURE;
210                                         }
211                                 }
212                                 if (((pattern & SILENCE_EXCLUDE) > 0))
213                                 {
214                                         sl->push_front(silenceset(mask,pattern));
215                                 }
216                                 else
217                                 {
218                                         sl->push_back(silenceset(mask,pattern));
219                                 }
220                                 user->WriteNumeric(951, "%s :Added %s %s to silence list", user->nick.c_str(), mask.c_str(), decomppattern.c_str());
221                                 return CMD_SUCCESS;
222                         }
223                 }
224                 return CMD_SUCCESS;
225         }
226
227         /* turn the nice human readable pattern into a mask */
228         int CompilePattern(const char* pattern)
229         {
230                 int p = 0;
231                 for (const char* n = pattern; *n; n++)
232                 {
233                         switch (*n)
234                         {
235                                 case 'p':
236                                         p |= SILENCE_PRIVATE;
237                                         break;
238                                 case 'c':
239                                         p |= SILENCE_CHANNEL;
240                                         break;
241                                 case 'i':
242                                         p |= SILENCE_INVITE;
243                                         break;
244                                 case 'n':
245                                         p |= SILENCE_NOTICE;
246                                         break;
247                                 case 't':
248                                         p |= SILENCE_CNOTICE;
249                                         break;
250                                 case 'a':
251                                 case '*':
252                                         p |= SILENCE_ALL;
253                                         break;
254                                 case 'x':
255                                         p |= SILENCE_EXCLUDE;
256                                         break;
257                                 default:
258                                         break;
259                         }
260                 }
261                 return p;
262         }
263
264         /* turn the mask into a nice human readable format */
265         std::string DecompPattern (const int pattern)
266         {
267                 std::string out;
268                 if (pattern & SILENCE_PRIVATE)
269                         out += ",privatemessages";
270                 if (pattern & SILENCE_CHANNEL)
271                         out += ",channelmessages";
272                 if (pattern & SILENCE_INVITE)
273                         out += ",invites";
274                 if (pattern & SILENCE_NOTICE)
275                         out += ",privatenotices";
276                 if (pattern & SILENCE_CNOTICE)
277                         out += ",channelnotices";
278                 if (pattern & SILENCE_ALL)
279                         out = ",all";
280                 if (pattern & SILENCE_EXCLUDE)
281                         out += ",exclude";
282                 if (out.length())
283                         return "<" + out.substr(1) + ">";
284                 else
285                         return "<none>";
286         }
287
288 };
289
290 class ModuleSilence : public Module
291 {
292         unsigned int maxsilence;
293         CommandSilence cmdsilence;
294         CommandSVSSilence cmdsvssilence;
295  public:
296
297         ModuleSilence()
298                 : maxsilence(32), cmdsilence(this, maxsilence), cmdsvssilence(this)
299         {
300         }
301
302         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
303         {
304                 maxsilence = ServerInstance->Config->ConfValue("silence")->getInt("maxentries", 32);
305                 if (!maxsilence)
306                         maxsilence = 32;
307         }
308
309         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
310         {
311                 tokens["ESILENCE"];
312                 tokens["SILENCE"] = ConvToStr(maxsilence);
313         }
314
315         void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
316         {
317                 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
318                 const UserMembList *ulist = chan->GetUsers();
319
320                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
321                 {
322                         if (IS_LOCAL(i->first))
323                         {
324                                 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_DENY)
325                                 {
326                                         exempt_list.insert(i->first);
327                                 }
328                         }
329                 }
330         }
331
332         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
333         {
334                 if (target_type == TYPE_USER && IS_LOCAL(((User*)dest)))
335                 {
336                         return MatchPattern((User*)dest, user, ((msgtype == MSG_PRIVMSG) ? SILENCE_PRIVATE : SILENCE_NOTICE));
337                 }
338                 else if (target_type == TYPE_CHANNEL)
339                 {
340                         Channel* chan = (Channel*)dest;
341                         this->OnBuildExemptList(msgtype, chan, user, status, exempt_list, "");
342                 }
343                 return MOD_RES_PASSTHRU;
344         }
345
346         ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout) CXX11_OVERRIDE
347         {
348                 return MatchPattern(dest, source, SILENCE_INVITE);
349         }
350
351         ModResult MatchPattern(User* dest, User* source, int pattern)
352         {
353                 silencelist* sl = cmdsilence.ext.get(dest);
354                 if (sl)
355                 {
356                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
357                         {
358                                 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
359                                         return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
360                         }
361                 }
362                 return MOD_RES_PASSTHRU;
363         }
364
365         Version GetVersion() CXX11_OVERRIDE
366         {
367                 return Version("Provides support for the /SILENCE command", VF_OPTCOMMON | VF_VENDOR);
368         }
369 };
370
371 MODULE_INIT(ModuleSilence)