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