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