]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence_ext.cpp
Fix to work with channel notices.
[user/henk/code/inspircd.git] / src / modules / m_silence_ext.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <vector>
22 #include <stdarg.h>
23 #include "users.h"
24 #include "channels.h"
25 #include "modules.h"
26 #include "hashcomp.h"
27 #include "inspircd.h"
28 #include "wildcard.h"
29
30 /* $ModDesc: Provides support for the /SILENCE command */
31
32 /* Improved drop-in replacement for the /SILENCE command
33  * syntax: /SILENCE [+|-]<mask> <p|c|i|n|a|x> as in <private|channel|invites|notices|all|exclude>
34  *
35  * example that blocks all except private messages
36  *  /SILENCE +*!*@* a
37  *  /SILENCE +*!*@* px
38  *
39  * example that blocks all invites except from channel services
40  *  /SILENCE +*!*@* i
41  *  /SILENCE +chanserv!services@chatters.net ix
42  *
43  * example that blocks some bad dude from private, notice and inviting you
44  *  /SILENCE +*!kiddie@lamerz.net pin
45  *
46  * TODO: possibly have add and remove check for existing host and only modify flags according to
47  *       what's been changed instead of having to remove first, then add if you want to change
48  *       an entry.
49  */
50
51 // pair of hostmask and flags
52 typedef std::pair<std::string, int> silenceset;
53
54 // deque list of pairs
55 typedef std::deque<silenceset> silencelist;
56
57 // intmasks for flags
58 static int SILENCE_PRIVATE      = 0x0001; /* p  private messages      */
59 static int SILENCE_CHANNEL      = 0x0002; /* c  channel messages      */
60 static int SILENCE_INVITE       = 0x0004; /* i  invites               */
61 static int SILENCE_NOTICE       = 0x0008; /* n  notices               */
62 static int SILENCE_ALL          = 0x0010; /* a  all, (pcin)           */
63 static int SILENCE_EXCLUDE      = 0x0020; /* x  exclude this pattern  */
64
65
66 class cmd_silence : public command_t
67 {
68  public:
69         cmd_silence (InspIRCd* Instance) : command_t(Instance,"SILENCE", 0, 0)
70         {
71                 this->source = "m_silence_ext.so";
72                 syntax = "{[+|-]<mask> <p|c|i|n|a|x>}";
73         }
74
75         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
76         {
77                 if (!pcnt)
78                 {
79                         // no parameters, show the current silence list.
80                         // Use Extensible::GetExt to fetch the silence list
81                         silencelist* sl;
82                         user->GetExt("silence_list", sl);
83                         // if the user has a silence list associated with their user record, show it
84                         if (sl)
85                         {
86                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
87                                 {
88                                         user->WriteServ("271 %s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
89                                 }
90                         }
91                         user->WriteServ("272 %s :End of Silence List",user->nick);
92
93                         return CMD_SUCCESS;
94                 }
95                 else if (pcnt > 0)
96                 {
97                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
98                         std::string mask = parameters[0] + 1;
99                         char action = *parameters[0];
100                         // Default is private and notice so clients do not break
101                         int pattern = CompilePattern("pn");
102
103                         // if pattern supplied, use it
104                         if (pcnt > 1) {
105                                 pattern = CompilePattern(parameters[1]);
106                         }
107                         
108                         if (!mask.length())
109                         {
110                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
111                                 mask = "*!*@*";
112                         }
113                         
114                         ModeParser::CleanMask(mask);
115
116                         if (action == '-')
117                         {
118                                 // fetch their silence list
119                                 silencelist* sl;
120                                 user->GetExt("silence_list", sl);
121                                 // does it contain any entries and does it exist?
122                                 if (sl)
123                                 {
124                                         if (sl->size())
125                                         {
126                                                 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
127                                                 {
128                                                         // search through for the item
129                                                         irc::string listitem = i->first.c_str();
130                                                         if (listitem == mask && i->second == pattern)
131                                                         {
132                                                                 sl->erase(i);
133                                                                 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
134                                                                 break;
135                                                         }
136                                                 }
137                                         }
138                                         else
139                                         {
140                                                 // tidy up -- if a user's list is empty, theres no use having it
141                                                 // hanging around in the user record.
142                                                 DELETE(sl);
143                                                 user->Shrink("silence_list");
144                                         }
145                                 }
146                         }
147                         else if (action == '+')
148                         {
149                                 // fetch the user's current silence list
150                                 silencelist* sl;
151                                 user->GetExt("silence_list", sl);
152                                 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
153                                 if (!sl)
154                                 {
155                                         sl = new silencelist;
156                                         user->Extend("silence_list", sl);
157                                 }
158                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
159                                 {
160                                         irc::string listitem = n->first.c_str();
161                                         if (listitem == mask && n->second == pattern)
162                                         {
163                                                 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
164                                                 return CMD_SUCCESS;
165                                         }
166                                 }
167                                 if (((pattern & SILENCE_EXCLUDE) > 0))
168                                 {
169                                         sl->push_front(silenceset(mask,pattern));
170                                 }
171                                 else
172                                 {
173                                         sl->push_back(silenceset(mask,pattern));
174                                 }
175                                 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
176                                 return CMD_SUCCESS;
177                         }
178                 }
179                 return CMD_SUCCESS;
180         }
181
182         /* turn the nice human readable pattern into a mask */
183         int CompilePattern(const char* pattern)
184         {
185                 int p = 0;
186                 for (uint n = 0; n < strlen(pattern); n++)
187                 {
188                         switch (pattern[n])
189                         {
190                                 case 'p':
191                                         p |= SILENCE_PRIVATE;
192                                         break;
193                                 case 'c':
194                                         p |= SILENCE_CHANNEL;
195                                         break;
196                                 case 'i': 
197                                         p |= SILENCE_INVITE;
198                                         break;
199                                 case 'n':
200                                         p |= SILENCE_NOTICE;
201                                         break;
202                                 case 'a':
203                                         p |= SILENCE_ALL;
204                                         break;
205                                 case 'x':
206                                         p |= SILENCE_EXCLUDE;
207                                         break;
208                                 default:
209                                         break;
210                         }
211                 }
212                 return p;
213         }
214
215         /* turn the mask into a nice human readable format */
216         std::string DecompPattern (const int pattern)
217         {
218                 std::string out = "";
219                 if ((pattern & SILENCE_PRIVATE) > 0)
220                         out += ",private";
221                 if ((pattern & SILENCE_CHANNEL) > 0)
222                         out += ",channel";
223                 if ((pattern & SILENCE_INVITE) > 0)
224                         out += ",invites";
225                 if ((pattern & SILENCE_NOTICE) > 0)
226                         out += ",notices";
227                 if ((pattern & SILENCE_ALL) > 0)
228                         out = ",all";
229                 if ((pattern & SILENCE_EXCLUDE) > 0)
230                         out += ",exclude";
231                 return "<" + out.substr(1) + ">";
232         }
233
234 };
235
236 class ModuleSilence : public Module
237 {
238         
239         cmd_silence* mycommand;
240  public:
241  
242         ModuleSilence(InspIRCd* Me)
243                 : Module::Module(Me)
244         {
245                 
246                 mycommand = new cmd_silence(ServerInstance);
247                 ServerInstance->AddCommand(mycommand);
248         }
249
250         void Implements(char* List)
251         {
252                 List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = List[I_OnUserPreInvite] = 1;
253         }
254
255         virtual void OnUserQuit(userrec* user, const std::string &reason)
256         {
257                 // when the user quits tidy up any silence list they might have just to keep things tidy
258                 silencelist* sl;
259                 user->GetExt("silence_list", sl);
260                 if (sl)
261                 {
262                         DELETE(sl);
263                         user->Shrink("silence_list");
264                 }
265         }
266
267         virtual void On005Numeric(std::string &output)
268         {
269                 // we don't really have a limit...
270                 output = output + " ESILENCE SILENCE=999";
271         }
272
273
274         virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
275         {
276                 if (target_type == TYPE_USER)
277                 {
278                         return MatchPattern((userrec*)dest, user, silence_type);
279                 }
280                 else if (target_type == TYPE_CHANNEL)
281                 {
282                         chanrec* chan = (chanrec*)dest;
283                         if (chan)
284                         {
285                                 CUList *ulist;
286                                 switch (status)
287                                 {
288                                         case '@':
289                                                 ulist = chan->GetOppedUsers();
290                                                 break;
291                                         case '%':
292                                                 ulist = chan->GetHalfoppedUsers();
293                                                 break;
294                                         case '+':
295                                                 ulist = chan->GetVoicedUsers();
296                                                 break;
297                                         default:
298                                                 ulist = chan->GetUsers();
299                                                 break;
300                                 }
301         
302                                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
303                                 {
304                                         if ((IS_LOCAL(i->second)) && (user != i->second))
305                                         {
306                                                 if (MatchPattern(i->second, user, SILENCE_CHANNEL) == 1)
307                                                 {
308                                                         exempt_list[i->second] = i->second;
309                                                 }
310                                         }
311                                 }
312                         }
313                 }
314                 return 0;
315         }
316
317         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
318         {
319                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
320         }
321
322         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
323         {
324                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
325         }
326
327         virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
328         {
329                 return MatchPattern(dest, source, SILENCE_INVITE);
330         }
331
332         int MatchPattern(userrec* dest, userrec* source, int pattern)
333         {
334                 silencelist* sl;
335                 dest->GetExt("silence_list", sl);
336                 if (sl)
337                 {
338                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
339                         {
340                                 if ((match(source->GetFullHost(), c->first.c_str())) && ( ((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0))
341                                 {
342                                         if (((c->second & SILENCE_EXCLUDE) > 0))
343                                         {
344                                                 return 0;
345                                         }
346                                         else {
347                                                 return 1;
348                                         }
349                                 }
350                         }
351                 }
352                 return 0;
353         }
354
355         virtual ~ModuleSilence()
356         {
357         }
358         
359         virtual Version GetVersion()
360         {
361                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
362         }
363 };
364
365
366 class ModuleSilenceFactory : public ModuleFactory
367 {
368  public:
369         ModuleSilenceFactory()
370         {
371         }
372         
373         ~ModuleSilenceFactory()
374         {
375         }
376         
377         virtual Module * CreateModule(InspIRCd* Me)
378         {
379                 return new ModuleSilence(Me);
380         }
381         
382 };
383
384
385 extern "C" void * init_module( void )
386 {
387         return new ModuleSilenceFactory;
388 }
389