]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
40a42c9d7e6d67b6dcde16611c0b66a181920f51
[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 /* $ModDesc: Provides support for the /SILENCE command */
27
28 /* Improved drop-in replacement for the /SILENCE command
29  * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
30  *
31  * example that blocks all except private messages
32  *  /SILENCE +*!*@* a
33  *  /SILENCE +*!*@* px
34  *
35  * example that blocks all invites except from channel services
36  *  /SILENCE +*!*@* i
37  *  /SILENCE +chanserv!services@chatters.net ix
38  *
39  * example that blocks some bad dude from private, notice and inviting you
40  *  /SILENCE +*!kiddie@lamerz.net pin
41  *
42  * TODO: possibly have add and remove check for existing host and only modify flags according to
43  *       what's been changed instead of having to remove first, then add if you want to change
44  *       an entry.
45  */
46
47 // pair of hostmask and flags
48 typedef std::pair<std::string, int> silenceset;
49
50 // deque list of pairs
51 typedef std::deque<silenceset> silencelist;
52
53 // intmasks for flags
54 static int SILENCE_PRIVATE      = 0x0001; /* p  private messages      */
55 static int SILENCE_CHANNEL      = 0x0002; /* c  channel messages      */
56 static int SILENCE_INVITE       = 0x0004; /* i  invites               */
57 static int SILENCE_NOTICE       = 0x0008; /* n  notices               */
58 static int SILENCE_CNOTICE      = 0x0010; /* t  channel notices       */
59 static int SILENCE_ALL          = 0x0020; /* a  all, (pcint)          */
60 static int SILENCE_EXCLUDE      = 0x0040; /* x  exclude this pattern  */
61
62
63 class CommandSVSSilence : public Command
64 {
65  public:
66         CommandSVSSilence(Module* Creator) : Command(Creator,"SVSSILENCE", 2)
67         {
68                 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
69                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
70         }
71
72         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
73         {
74                 /*
75                  * XXX: thought occurs to me
76                  * We may want to change the syntax of this command to
77                  * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
78                  * style command so services can modify lots of entries at once.
79                  * leaving it backwards compatible for now as it's late. -- w
80                  */
81                 if (!ServerInstance->ULine(user->server))
82                         return CMD_FAILURE;
83
84                 User *u = ServerInstance->FindNick(parameters[0]);
85                 if (!u)
86                         return CMD_FAILURE;
87
88                 if (IS_LOCAL(u))
89                 {
90                         ServerInstance->Parser->CallHandler("SILENCE", std::vector<std::string>(++parameters.begin(), parameters.end()), u);
91                 }
92
93                 return CMD_SUCCESS;
94         }
95
96         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
97         {
98                 User* target = ServerInstance->FindNick(parameters[0]);
99                 if (target)
100                         return ROUTE_OPT_UCAST(target->server);
101                 return ROUTE_LOCALONLY;
102         }
103 };
104
105 class CommandSilence : public Command
106 {
107         unsigned int& maxsilence;
108  public:
109         SimpleExtItem<silencelist> ext;
110         CommandSilence(Module* Creator, unsigned int &max) : Command(Creator, "SILENCE", 0),
111                 maxsilence(max), ext("silence_list", Creator)
112         {
113                 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
114                 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
115         }
116
117         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
118         {
119                 if (!parameters.size())
120                 {
121                         // no parameters, show the current silence list.
122                         silencelist* sl = ext.get(user);
123                         // if the user has a silence list associated with their user record, show it
124                         if (sl)
125                         {
126                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
127                                 {
128                                         std::string decomppattern = DecompPattern(c->second);
129                                         user->WriteNumeric(271, "%s %s %s %s",user->nick.c_str(), user->nick.c_str(),c->first.c_str(), decomppattern.c_str());
130                                 }
131                         }
132                         user->WriteNumeric(272, "%s :End of Silence List",user->nick.c_str());
133
134                         return CMD_SUCCESS;
135                 }
136                 else if (parameters.size() > 0)
137                 {
138                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
139                         std::string mask = parameters[0].substr(1);
140                         char action = parameters[0][0];
141                         // Default is private and notice so clients do not break
142                         int pattern = CompilePattern("pn");
143
144                         // if pattern supplied, use it
145                         if (parameters.size() > 1) {
146                                 pattern = CompilePattern(parameters[1].c_str());
147                         }
148
149                         if (pattern == 0)
150                         {
151                                 user->WriteServ("NOTICE %s :Bad SILENCE pattern",user->nick.c_str());
152                                 return CMD_INVALID;
153                         }
154
155                         if (!mask.length())
156                         {
157                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
158                                 mask = "*!*@*";
159                         }
160
161                         ModeParser::CleanMask(mask);
162
163                         if (action == '-')
164                         {
165                                 std::string decomppattern = DecompPattern(pattern);
166                                 // fetch their silence list
167                                 silencelist* sl = ext.get(user);
168                                 // does it contain any entries and does it exist?
169                                 if (sl)
170                                 {
171                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
172                                         {
173                                                 // search through for the item
174                                                 irc::string listitem = i->first.c_str();
175                                                 if (listitem == mask && i->second == pattern)
176                                                 {
177                                                         sl->erase(i);
178                                                         user->WriteNumeric(950, "%s %s :Removed %s %s from silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
179                                                         if (!sl->size())
180                                                         {
181                                                                 ext.unset(user);
182                                                         }
183                                                         return CMD_SUCCESS;
184                                                 }
185                                         }
186                                 }
187                                 user->WriteNumeric(952, "%s %s :%s %s does not exist on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
188                         }
189                         else if (action == '+')
190                         {
191                                 // fetch the user's current silence list
192                                 silencelist* sl = ext.get(user);
193                                 if (!sl)
194                                 {
195                                         sl = new silencelist;
196                                         ext.set(user, sl);
197                                 }
198                                 if (sl->size() > maxsilence)
199                                 {
200                                         user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick.c_str(), user->nick.c_str());
201                                         return CMD_FAILURE;
202                                 }
203
204                                 std::string decomppattern = DecompPattern(pattern);
205                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
206                                 {
207                                         irc::string listitem = n->first.c_str();
208                                         if (listitem == mask && n->second == pattern)
209                                         {
210                                                 user->WriteNumeric(952, "%s %s :%s %s is already on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
211                                                 return CMD_FAILURE;
212                                         }
213                                 }
214                                 if (((pattern & SILENCE_EXCLUDE) > 0))
215                                 {
216                                         sl->push_front(silenceset(mask,pattern));
217                                 }
218                                 else
219                                 {
220                                         sl->push_back(silenceset(mask,pattern));
221                                 }
222                                 user->WriteNumeric(951, "%s %s :Added %s %s to silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
223                                 return CMD_SUCCESS;
224                         }
225                 }
226                 return CMD_SUCCESS;
227         }
228
229         /* turn the nice human readable pattern into a mask */
230         int CompilePattern(const char* pattern)
231         {
232                 int p = 0;
233                 for (const char* n = pattern; *n; n++)
234                 {
235                         switch (*n)
236                         {
237                                 case 'p':
238                                         p |= SILENCE_PRIVATE;
239                                         break;
240                                 case 'c':
241                                         p |= SILENCE_CHANNEL;
242                                         break;
243                                 case 'i':
244                                         p |= SILENCE_INVITE;
245                                         break;
246                                 case 'n':
247                                         p |= SILENCE_NOTICE;
248                                         break;
249                                 case 't':
250                                         p |= SILENCE_CNOTICE;
251                                         break;
252                                 case 'a':
253                                 case '*':
254                                         p |= SILENCE_ALL;
255                                         break;
256                                 case 'x':
257                                         p |= SILENCE_EXCLUDE;
258                                         break;
259                                 default:
260                                         break;
261                         }
262                 }
263                 return p;
264         }
265
266         /* turn the mask into a nice human readable format */
267         std::string DecompPattern (const int pattern)
268         {
269                 std::string out;
270                 if (pattern & SILENCE_PRIVATE)
271                         out += ",privatemessages";
272                 if (pattern & SILENCE_CHANNEL)
273                         out += ",channelmessages";
274                 if (pattern & SILENCE_INVITE)
275                         out += ",invites";
276                 if (pattern & SILENCE_NOTICE)
277                         out += ",privatenotices";
278                 if (pattern & SILENCE_CNOTICE)
279                         out += ",channelnotices";
280                 if (pattern & SILENCE_ALL)
281                         out = ",all";
282                 if (pattern & SILENCE_EXCLUDE)
283                         out += ",exclude";
284                 if (out.length())
285                         return "<" + out.substr(1) + ">";
286                 else
287                         return "<none>";
288         }
289
290 };
291
292 class ModuleSilence : public Module
293 {
294         unsigned int maxsilence;
295         CommandSilence cmdsilence;
296         CommandSVSSilence cmdsvssilence;
297  public:
298
299         ModuleSilence()
300                 : maxsilence(32), cmdsilence(this, maxsilence), cmdsvssilence(this)
301         {
302         }
303
304         void init()
305         {
306                 OnRehash(NULL);
307                 ServerInstance->AddCommand(&cmdsilence);
308                 ServerInstance->AddCommand(&cmdsvssilence);
309                 ServerInstance->Extensions.Register(&cmdsilence.ext);
310
311                 Implementation eventlist[] = { I_OnRehash, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
312                 ServerInstance->Modules->Attach(eventlist, this, 5);
313         }
314
315         void OnRehash(User* user)
316         {
317                 maxsilence = ServerInstance->Config->ConfValue("showwhois")->getInt("maxentries", 32);
318                 if (!maxsilence)
319                         maxsilence = 32;
320         }
321
322         void On005Numeric(std::string &output)
323         {
324                 // we don't really have a limit...
325                 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
326         }
327
328         void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
329         {
330                 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
331                 const UserMembList *ulist = chan->GetUsers();
332
333                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
334                 {
335                         if (IS_LOCAL(i->first))
336                         {
337                                 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_DENY)
338                                 {
339                                         exempt_list.insert(i->first);
340                                 }
341                         }
342                 }
343         }
344
345         ModResult PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
346         {
347                 if (target_type == TYPE_USER && IS_LOCAL(((User*)dest)))
348                 {
349                         return MatchPattern((User*)dest, user, silence_type);
350                 }
351                 else if (target_type == TYPE_CHANNEL)
352                 {
353                         Channel* chan = (Channel*)dest;
354                         if (chan)
355                         {
356                                 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
357                         }
358                 }
359                 return MOD_RES_PASSTHRU;
360         }
361
362         ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
363         {
364                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
365         }
366
367         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
368         {
369                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
370         }
371
372         ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
373         {
374                 return MatchPattern(dest, source, SILENCE_INVITE);
375         }
376
377         ModResult MatchPattern(User* dest, User* source, int pattern)
378         {
379                 /* Server source */
380                 if (!source || !dest)
381                         return MOD_RES_ALLOW;
382
383                 silencelist* sl = cmdsilence.ext.get(dest);
384                 if (sl)
385                 {
386                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
387                         {
388                                 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
389                                         return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
390                         }
391                 }
392                 return MOD_RES_PASSTHRU;
393         }
394
395         ~ModuleSilence()
396         {
397         }
398
399         Version GetVersion()
400         {
401                 return Version("Provides support for the /SILENCE command", VF_OPTCOMMON | VF_VENDOR);
402         }
403 };
404
405 MODULE_INIT(ModuleSilence)