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