]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Remove CMD_LOCALONLY, enforce use of GetRouting for routed commands
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"SVSSILENCE", 0, 2)
57         {
58                 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
59                 TRANSLATE3(TR_NICK, 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                 return ROUTE_BROADCAST;
89         }
90 };
91
92 class CommandSilence : public Command
93 {
94         unsigned int& maxsilence;
95  public:
96         CommandSilence (InspIRCd* Instance, Module* Creator, unsigned int &max) : Command(Instance, Creator, "SILENCE", 0, 0), maxsilence(max)
97         {
98                 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
99                 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
100         }
101
102         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
103         {
104                 if (!parameters.size())
105                 {
106                         // no parameters, show the current silence list.
107                         // Use Extensible::GetExt to fetch the silence list
108                         silencelist* sl;
109                         user->GetExt("silence_list", sl);
110                         // if the user has a silence list associated with their user record, show it
111                         if (sl)
112                         {
113                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
114                                 {
115                                         user->WriteNumeric(271, "%s %s %s %s",user->nick.c_str(), user->nick.c_str(),c->first.c_str(), DecompPattern(c->second).c_str());
116                                 }
117                         }
118                         user->WriteNumeric(272, "%s :End of Silence List",user->nick.c_str());
119
120                         return CMD_SUCCESS;
121                 }
122                 else if (parameters.size() > 0)
123                 {
124                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
125                         std::string mask = parameters[0].substr(1);
126                         char action = parameters[0][0];
127                         // Default is private and notice so clients do not break
128                         int pattern = CompilePattern("pn");
129
130                         // if pattern supplied, use it
131                         if (parameters.size() > 1) {
132                                 pattern = CompilePattern(parameters[1].c_str());
133                         }
134
135                         if (!mask.length())
136                         {
137                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
138                                 mask = "*!*@*";
139                         }
140
141                         ModeParser::CleanMask(mask);
142
143                         if (action == '-')
144                         {
145                                 // fetch their silence list
146                                 silencelist* sl;
147                                 user->GetExt("silence_list", sl);
148                                 // does it contain any entries and does it exist?
149                                 if (sl)
150                                 {
151                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
152                                         {
153                                                 // search through for the item
154                                                 irc::string listitem = i->first.c_str();
155                                                 if (listitem == mask && i->second == pattern)
156                                                 {
157                                                         sl->erase(i);
158                                                         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());
159                                                         if (!sl->size())
160                                                         {
161                                                                 delete sl;
162                                                                 user->Shrink("silence_list");
163                                                         }
164                                                         return CMD_SUCCESS;
165                                                 }
166                                         }
167                                 }
168                                 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());
169                         }
170                         else if (action == '+')
171                         {
172                                 // fetch the user's current silence list
173                                 silencelist* sl;
174                                 user->GetExt("silence_list", sl);
175                                 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
176                                 if (!sl)
177                                 {
178                                         sl = new silencelist;
179                                         user->Extend("silence_list", sl);
180                                 }
181                                 if (sl->size() > maxsilence)
182                                 {
183                                         user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick.c_str(), user->nick.c_str());
184                                         return CMD_FAILURE;
185                                 }
186                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
187                                 {
188                                         irc::string listitem = n->first.c_str();
189                                         if (listitem == mask && n->second == pattern)
190                                         {
191                                                 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());
192                                                 return CMD_FAILURE;
193                                         }
194                                 }
195                                 if (((pattern & SILENCE_EXCLUDE) > 0))
196                                 {
197                                         sl->push_front(silenceset(mask,pattern));
198                                 }
199                                 else
200                                 {
201                                         sl->push_back(silenceset(mask,pattern));
202                                 }
203                                 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());
204                                 return CMD_SUCCESS;
205                         }
206                 }
207                 return CMD_SUCCESS;
208         }
209
210         /* turn the nice human readable pattern into a mask */
211         int CompilePattern(const char* pattern)
212         {
213                 int p = 0;
214                 for (const char* n = pattern; *n; n++)
215                 {
216                         switch (*n)
217                         {
218                                 case 'p':
219                                         p |= SILENCE_PRIVATE;
220                                         break;
221                                 case 'c':
222                                         p |= SILENCE_CHANNEL;
223                                         break;
224                                 case 'i':
225                                         p |= SILENCE_INVITE;
226                                         break;
227                                 case 'n':
228                                         p |= SILENCE_NOTICE;
229                                         break;
230                                 case 't':
231                                         p |= SILENCE_CNOTICE;
232                                         break;
233                                 case 'a':
234                                 case '*':
235                                         p |= SILENCE_ALL;
236                                         break;
237                                 case 'x':
238                                         p |= SILENCE_EXCLUDE;
239                                         break;
240                                 default:
241                                         break;
242                         }
243                 }
244                 return p;
245         }
246
247         /* turn the mask into a nice human readable format */
248         std::string DecompPattern (const int pattern)
249         {
250                 std::string out;
251                 if ((pattern & SILENCE_PRIVATE) > 0)
252                         out += ",privatemessages";
253                 if ((pattern & SILENCE_CHANNEL) > 0)
254                         out += ",channelmessages";
255                 if ((pattern & SILENCE_INVITE) > 0)
256                         out += ",invites";
257                 if ((pattern & SILENCE_NOTICE) > 0)
258                         out += ",privatenotices";
259                 if ((pattern & SILENCE_CNOTICE) > 0)
260                         out += ",channelnotices";
261                 if ((pattern & SILENCE_ALL) > 0)
262                         out = ",all";
263                 if ((pattern & SILENCE_EXCLUDE) > 0)
264                         out += ",exclude";
265                 return "<" + out.substr(1) + ">";
266         }
267
268 };
269
270 class ModuleSilence : public Module
271 {
272         unsigned int maxsilence;
273         CommandSilence cmdsilence;
274         CommandSVSSilence cmdsvssilence;
275  public:
276
277         ModuleSilence(InspIRCd* Me)
278                 : Module(Me), maxsilence(32), cmdsilence(Me, this, maxsilence), cmdsvssilence(Me, this)
279         {
280                 OnRehash(NULL);
281                 ServerInstance->AddCommand(&cmdsilence);
282                 ServerInstance->AddCommand(&cmdsvssilence);
283
284                 Implementation eventlist[] = { I_OnRehash, I_OnBuildExemptList, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
285                 ServerInstance->Modules->Attach(eventlist, this, 7);
286         }
287
288         virtual void OnRehash(User* user)
289         {
290                 ConfigReader Conf(ServerInstance);
291                 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
292                 if (!maxsilence)
293                         maxsilence = 32;
294         }
295
296
297         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
298         {
299                 // when the user quits tidy up any silence list they might have just to keep things tidy
300                 silencelist* sl;
301                 user->GetExt("silence_list", sl);
302                 if (sl)
303                 {
304                         delete sl;
305                         user->Shrink("silence_list");
306                 }
307         }
308
309         virtual void On005Numeric(std::string &output)
310         {
311                 // we don't really have a limit...
312                 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
313         }
314
315         virtual void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
316         {
317                 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
318                 CUList *ulist;
319                 switch (status)
320                 {
321                         case '@':
322                                 ulist = chan->GetOppedUsers();
323                                 break;
324                         case '%':
325                                 ulist = chan->GetHalfoppedUsers();
326                                 break;
327                         case '+':
328                                 ulist = chan->GetVoicedUsers();
329                                 break;
330                         default:
331                                 ulist = chan->GetUsers();
332                                 break;
333                 }
334
335                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
336                 {
337                         if (IS_LOCAL(i->first))
338                         {
339                                 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_ALLOW)
340                                 {
341                                         exempt_list[i->first] = i->first->nick;
342                                 }
343                         }
344                 }
345         }
346
347         virtual ModResult PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
348         {
349                 if (target_type == TYPE_USER && IS_LOCAL(((User*)dest)))
350                 {
351                         return MatchPattern((User*)dest, user, silence_type);
352                 }
353                 else if (target_type == TYPE_CHANNEL)
354                 {
355                         Channel* chan = (Channel*)dest;
356                         if (chan)
357                         {
358                                 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
359                         }
360                 }
361                 return MOD_RES_PASSTHRU;
362         }
363
364         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
365         {
366                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
367         }
368
369         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
370         {
371                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
372         }
373
374         virtual ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
375         {
376                 return MatchPattern(dest, source, SILENCE_INVITE);
377         }
378
379         ModResult MatchPattern(User* dest, User* source, int pattern)
380         {
381                 /* Server source */
382                 if (!source || !dest)
383                         return MOD_RES_ALLOW;
384
385                 silencelist* sl;
386                 dest->GetExt("silence_list", sl);
387                 if (sl)
388                 {
389                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
390                         {
391                                 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
392                                         return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
393                         }
394                 }
395                 return MOD_RES_PASSTHRU;
396         }
397
398         virtual ~ModuleSilence()
399         {
400         }
401
402         virtual Version GetVersion()
403         {
404                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
405         }
406 };
407
408 MODULE_INIT(ModuleSilence)