]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Use CommandBase::Params instead of std::vector<std::string>.
[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 // list of pairs
49 typedef std::vector<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 enum
61 {
62         // From ircu?
63         RPL_SILELIST = 271,
64         RPL_ENDOFSILELIST = 272,
65
66         // InspIRCd-specific.
67         RPL_UNSILENCED = 950,
68         RPL_SILENCED = 951,
69         ERR_NOTSILENCED = 952
70 };
71
72 class CommandSVSSilence : public Command
73 {
74  public:
75         CommandSVSSilence(Module* Creator) : Command(Creator,"SVSSILENCE", 2)
76         {
77                 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
78                 TRANSLATE3(TR_NICK, TR_TEXT, TR_TEXT);
79         }
80
81         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
82         {
83                 /*
84                  * XXX: thought occurs to me
85                  * We may want to change the syntax of this command to
86                  * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
87                  * style command so services can modify lots of entries at once.
88                  * leaving it backwards compatible for now as it's late. -- w
89                  */
90                 if (!user->server->IsULine())
91                         return CMD_FAILURE;
92
93                 User *u = ServerInstance->FindNick(parameters[0]);
94                 if (!u)
95                         return CMD_FAILURE;
96
97                 if (IS_LOCAL(u))
98                 {
99                         CommandBase::Params params(parameters.begin() + 1, parameters.end());
100                         ServerInstance->Parser.CallHandler("SILENCE", params, u);
101                 }
102
103                 return CMD_SUCCESS;
104         }
105
106         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
107         {
108                 return ROUTE_OPT_UCAST(parameters[0]);
109         }
110 };
111
112 class CommandSilence : public Command
113 {
114         unsigned int& maxsilence;
115  public:
116         SimpleExtItem<silencelist> ext;
117         CommandSilence(Module* Creator, unsigned int &max) : Command(Creator, "SILENCE", 0),
118                 maxsilence(max)
119                 , ext("silence_list", ExtensionItem::EXT_USER, Creator)
120         {
121                 allow_empty_last_param = false;
122                 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
123         }
124
125         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
126         {
127                 if (parameters.empty())
128                 {
129                         // no parameters, show the current silence list.
130                         silencelist* sl = ext.get(user);
131                         // if the user has a silence list associated with their user record, show it
132                         if (sl)
133                         {
134                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
135                                 {
136                                         std::string decomppattern = DecompPattern(c->second);
137                                         user->WriteNumeric(RPL_SILELIST, user->nick, c->first, decomppattern);
138                                 }
139                         }
140                         user->WriteNumeric(RPL_ENDOFSILELIST, "End of Silence List");
141
142                         return CMD_SUCCESS;
143                 }
144                 else
145                 {
146                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
147                         std::string mask(parameters[0], 1);
148                         char action = parameters[0][0];
149                         // Default is private and notice so clients do not break
150                         int pattern = CompilePattern("pn");
151
152                         // if pattern supplied, use it
153                         if (parameters.size() > 1) {
154                                 pattern = CompilePattern(parameters[1].c_str());
155                         }
156
157                         if (pattern == 0)
158                         {
159                                 user->WriteNotice("Bad SILENCE pattern");
160                                 return CMD_INVALID;
161                         }
162
163                         if (!mask.length())
164                         {
165                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
166                                 mask = "*!*@*";
167                         }
168
169                         ModeParser::CleanMask(mask);
170
171                         if (action == '-')
172                         {
173                                 std::string decomppattern = DecompPattern(pattern);
174                                 // fetch their silence list
175                                 silencelist* sl = ext.get(user);
176                                 // does it contain any entries and does it exist?
177                                 if (sl)
178                                 {
179                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
180                                         {
181                                                 // search through for the item
182                                                 const std::string& listitem = i->first;
183                                                 if ((irc::equals(listitem, mask)) && (i->second == pattern))
184                                                 {
185                                                         sl->erase(i);
186                                                         user->WriteNumeric(RPL_UNSILENCED, user->nick, InspIRCd::Format("Removed %s %s from silence list", mask.c_str(), decomppattern.c_str()));
187                                                         if (!sl->size())
188                                                         {
189                                                                 ext.unset(user);
190                                                         }
191                                                         return CMD_SUCCESS;
192                                                 }
193                                         }
194                                 }
195                                 user->WriteNumeric(ERR_NOTSILENCED, user->nick, InspIRCd::Format("%s %s does not exist on your silence list", mask.c_str(), decomppattern.c_str()));
196                         }
197                         else if (action == '+')
198                         {
199                                 // fetch the user's current silence list
200                                 silencelist* sl = ext.get(user);
201                                 if (!sl)
202                                 {
203                                         sl = new silencelist;
204                                         ext.set(user, sl);
205                                 }
206                                 if (sl->size() > maxsilence)
207                                 {
208                                         user->WriteNumeric(ERR_NOTSILENCED, user->nick, "Your silence list is full");
209                                         return CMD_FAILURE;
210                                 }
211
212                                 std::string decomppattern = DecompPattern(pattern);
213                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
214                                 {
215                                         const std::string& listitem = n->first;
216                                         if ((irc::equals(listitem, mask)) && (n->second == pattern))
217                                         {
218                                                 user->WriteNumeric(ERR_NOTSILENCED, user->nick, InspIRCd::Format("%s %s is already on your silence list", mask.c_str(), decomppattern.c_str()));
219                                                 return CMD_FAILURE;
220                                         }
221                                 }
222                                 if (((pattern & SILENCE_EXCLUDE) > 0))
223                                 {
224                                         sl->insert(sl->begin(), silenceset(mask, pattern));
225                                 }
226                                 else
227                                 {
228                                         sl->push_back(silenceset(mask,pattern));
229                                 }
230                                 user->WriteNumeric(RPL_SILENCED, user->nick, InspIRCd::Format("Added %s %s to silence list", mask.c_str(), decomppattern.c_str()));
231                                 return CMD_SUCCESS;
232                         }
233                 }
234                 return CMD_SUCCESS;
235         }
236
237         /* turn the nice human readable pattern into a mask */
238         int CompilePattern(const char* pattern)
239         {
240                 int p = 0;
241                 for (const char* n = pattern; *n; n++)
242                 {
243                         switch (*n)
244                         {
245                                 case 'p':
246                                         p |= SILENCE_PRIVATE;
247                                         break;
248                                 case 'c':
249                                         p |= SILENCE_CHANNEL;
250                                         break;
251                                 case 'i':
252                                         p |= SILENCE_INVITE;
253                                         break;
254                                 case 'n':
255                                         p |= SILENCE_NOTICE;
256                                         break;
257                                 case 't':
258                                         p |= SILENCE_CNOTICE;
259                                         break;
260                                 case 'a':
261                                 case '*':
262                                         p |= SILENCE_ALL;
263                                         break;
264                                 case 'x':
265                                         p |= SILENCE_EXCLUDE;
266                                         break;
267                                 default:
268                                         break;
269                         }
270                 }
271                 return p;
272         }
273
274         /* turn the mask into a nice human readable format */
275         std::string DecompPattern (const int pattern)
276         {
277                 std::string out;
278                 if (pattern & SILENCE_PRIVATE)
279                         out += ",privatemessages";
280                 if (pattern & SILENCE_CHANNEL)
281                         out += ",channelmessages";
282                 if (pattern & SILENCE_INVITE)
283                         out += ",invites";
284                 if (pattern & SILENCE_NOTICE)
285                         out += ",privatenotices";
286                 if (pattern & SILENCE_CNOTICE)
287                         out += ",channelnotices";
288                 if (pattern & SILENCE_ALL)
289                         out = ",all";
290                 if (pattern & SILENCE_EXCLUDE)
291                         out += ",exclude";
292                 if (out.length())
293                         return "<" + out.substr(1) + ">";
294                 else
295                         return "<none>";
296         }
297
298 };
299
300 class ModuleSilence : public Module
301 {
302         unsigned int maxsilence;
303         bool ExemptULine;
304         CommandSilence cmdsilence;
305         CommandSVSSilence cmdsvssilence;
306  public:
307
308         ModuleSilence()
309                 : maxsilence(32), cmdsilence(this, maxsilence), cmdsvssilence(this)
310         {
311         }
312
313         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
314         {
315                 ConfigTag* tag = ServerInstance->Config->ConfValue("silence");
316
317                 maxsilence = tag->getUInt("maxentries", 32, 1);
318                 ExemptULine = tag->getBool("exemptuline", true);
319         }
320
321         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
322         {
323                 tokens["ESILENCE"];
324                 tokens["SILENCE"] = ConvToStr(maxsilence);
325         }
326
327         void BuildExemptList(MessageType message_type, Channel* chan, User* sender, CUList& exempt_list)
328         {
329                 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
330
331                 const Channel::MemberMap& ulist = chan->GetUsers();
332                 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
333                 {
334                         if (IS_LOCAL(i->first))
335                         {
336                                 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_DENY)
337                                 {
338                                         exempt_list.insert(i->first);
339                                 }
340                         }
341                 }
342         }
343
344         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
345         {
346                 if (target.type == MessageTarget::TYPE_USER && IS_LOCAL(target.Get<User>()))
347                 {
348                         return MatchPattern(target.Get<User>(), user, ((details.type == MSG_PRIVMSG) ? SILENCE_PRIVATE : SILENCE_NOTICE));
349                 }
350                 else if (target.type == MessageTarget::TYPE_CHANNEL)
351                 {
352                         Channel* chan = target.Get<Channel>();
353                         BuildExemptList(details.type, chan, user, details.exemptions);
354                 }
355                 return MOD_RES_PASSTHRU;
356         }
357
358         ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout) CXX11_OVERRIDE
359         {
360                 return MatchPattern(dest, source, SILENCE_INVITE);
361         }
362
363         ModResult MatchPattern(User* dest, User* source, int pattern)
364         {
365                 if (ExemptULine && source->server->IsULine())
366                         return MOD_RES_PASSTHRU;
367
368                 silencelist* sl = cmdsilence.ext.get(dest);
369                 if (sl)
370                 {
371                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
372                         {
373                                 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
374                                         return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
375                         }
376                 }
377                 return MOD_RES_PASSTHRU;
378         }
379
380         Version GetVersion() CXX11_OVERRIDE
381         {
382                 return Version("Provides support for the /SILENCE command", VF_OPTCOMMON | VF_VENDOR);
383         }
384 };
385
386 MODULE_INIT(ModuleSilence)