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