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