]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence_ext.cpp
370452fd6b8a047f8d01fc6ed3c054bb95be7e84
[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                                                 silencelist::iterator i,safei;
127                                                 for (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                                                                 safei = i;
134                                                                 --i;
135                                                         sl->erase(safei);
136                                                                 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
137                                                                 break;
138                                                         }
139                                                 }
140                                         }
141                                         else
142                                         {
143                                                 // tidy up -- if a user's list is empty, theres no use having it
144                                                 // hanging around in the user record.
145                                                 DELETE(sl);
146                                                 user->Shrink("silence_list");
147                                         }
148                                 }
149                         }
150                         else if (action == '+')
151                         {
152                                 // fetch the user's current silence list
153                                 silencelist* sl;
154                                 user->GetExt("silence_list", sl);
155                                 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
156                                 if (!sl)
157                                 {
158                                         sl = new silencelist;
159                                         user->Extend("silence_list", sl);
160                                 }
161                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
162                                 {
163                                         irc::string listitem = n->first.c_str();
164                                         if (listitem == mask && n->second == pattern)
165                                         {
166                                                 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
167                                                 return CMD_SUCCESS;
168                                         }
169                                 }
170                                 if (((pattern & SILENCE_EXCLUDE) > 0))
171                                 {
172                                         sl->push_front(silenceset(mask,pattern));
173                                 }
174                                 else
175                                 {
176                                         sl->push_back(silenceset(mask,pattern));
177                                 }
178                                 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
179                                 return CMD_SUCCESS;
180                         }
181                 }
182                 return CMD_SUCCESS;
183         }
184
185         /* turn the nice human readable pattern into a mask */
186         int CompilePattern(const char* pattern)
187         {
188                 int p = 0;
189                 for (uint n = 0; n < strlen(pattern); n++)
190                 {
191                         switch (pattern[n])
192                         {
193                                 case 'p':
194                                         p |= SILENCE_PRIVATE;
195                                         break;
196                                 case 'c':
197                                         p |= SILENCE_CHANNEL;
198                                         break;
199                                 case 'i': 
200                                         p |= SILENCE_INVITE;
201                                         break;
202                                 case 'n':
203                                         p |= SILENCE_NOTICE;
204                                         break;
205                                 case 'a':
206                                         p |= SILENCE_ALL;
207                                         break;
208                                 case 'x':
209                                         p |= SILENCE_EXCLUDE;
210                                         break;
211                                 default:
212                                         break;
213                         }
214                 }
215                 return p;
216         }
217
218         /* turn the mask into a nice human readable format */
219         std::string DecompPattern (const int pattern)
220         {
221                 std::string out = "";
222                 if ((pattern & SILENCE_PRIVATE) > 0)
223                         out += ",private";
224                 if ((pattern & SILENCE_CHANNEL) > 0)
225                         out += ",channel";
226                 if ((pattern & SILENCE_INVITE) > 0)
227                         out += ",invites";
228                 if ((pattern & SILENCE_NOTICE) > 0)
229                         out += ",notices";
230                 if ((pattern & SILENCE_ALL) > 0)
231                         out = ",all";
232                 if ((pattern & SILENCE_EXCLUDE) > 0)
233                         out += ",exclude";
234                 return "<" + out.substr(1) + ">";
235         }
236
237 };
238
239 class ModuleSilence : public Module
240 {
241         
242         cmd_silence* mycommand;
243  public:
244  
245         ModuleSilence(InspIRCd* Me)
246                 : Module::Module(Me)
247         {
248                 
249                 mycommand = new cmd_silence(ServerInstance);
250                 ServerInstance->AddCommand(mycommand);
251         }
252
253         void Implements(char* List)
254         {
255                 List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = 1;
256                 List[I_OnUserPreInvite] = 1;
257                 List[I_OnPreCommand] = 1;
258         }
259
260         virtual void OnUserQuit(userrec* user, const std::string &reason)
261         {
262                 // when the user quits tidy up any silence list they might have just to keep things tidy
263                 // and to prevent a HONKING BIG MEMORY LEAK!
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 + " SILENCE=999";
277         }
278
279         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
280         {
281                 if (target_type == TYPE_USER)
282                 {
283                         return MatchPattern((userrec*)dest, user, SILENCE_PRIVATE);
284                 }
285                 return 0;
286         }
287
288         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
289         {
290                 return MatchPattern((userrec*)dest, user, SILENCE_NOTICE);
291         }
292
293         virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
294         {
295                 return MatchPattern(dest, source, SILENCE_INVITE);
296         }
297
298         int MatchPattern(userrec* dest, userrec* source, int pattern)
299         {
300                 silencelist* sl;
301                 dest->GetExt("silence_list", sl);
302                 if (sl)
303                 {
304                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
305                         {
306                                 if ((match(source->GetFullHost(), c->first.c_str())) && ( ((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0))
307                                 {
308                                         if (((c->second & SILENCE_EXCLUDE) > 0))
309                                         {
310                                                 return 0;
311                                         }
312                                         else {
313                                                 return 1;
314                                         }
315                                 }
316                         }
317                 }
318                 return 0;
319         }
320
321         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
322         {
323                 /* Implement the part of cmd_privmsg.cpp that handles *channel* messages, if cmd_privmsg.cpp
324                  * is changed this probably needs updating too. Also implement the actual write to the users
325                  * on the channel. This code is from channels.cpp, and should also be changed if channels.cpp
326                  * updates it's corresponding code
327                  */
328                 if ((validated) && (command == "PRIVMSG"))
329                 {
330                         char status = 0;
331                         if ((*parameters[0] == '@') || (*parameters[0] == '%') || (*parameters[0] == '+'))
332                         {
333                                 status = *parameters[0];
334                                 parameters[0]++;
335                         }
336                         if (parameters[0][0] == '#')
337                         {
338                                 chanrec *chan;
339                                 user->idle_lastmsg = ServerInstance->Time();
340                                 chan = ServerInstance->FindChan(parameters[0]);
341                                 if (chan)
342                                 {
343                                         if (IS_LOCAL(user))
344                                         {
345                                                 if ((chan->modes[CM_NOEXTERNAL]) && (!chan->HasUser(user)))
346                                                 {
347                                                         user->WriteServ("404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
348                                                         return 1;
349                                                 }
350                                                 if ((chan->modes[CM_MODERATED]) && (chan->GetStatus(user) < STATUS_VOICE))
351                                                 {
352                                                         user->WriteServ("404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
353                                                         return 1;
354                                                 }
355                                         }
356                                         int MOD_RESULT = 0;
357
358                                         std::string temp = parameters[1];
359                                         FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,chan,TYPE_CHANNEL,temp,status));
360                                         if (MOD_RESULT) {
361                                                 return 1;
362                                         }
363                                         parameters[1] = temp.c_str();
364
365                                         if (temp == "")
366                                         {
367                                                 user->WriteServ("412 %s No text to send", user->nick);
368                                                 return 1;
369                                         }
370
371                                         /* This next call into channel.cpp is the one that gets replaced by our modified method
372                                          * chan->WriteAllExceptSender(user, false, status, "PRIVMSG %s :%s", chan->name, parameters[1]);
373                                          */
374                                         WriteAllExceptSenderAndSilenced(chan, user, false, status, "PRIVMSG %s :%s", chan->name, parameters[1]);
375
376                                         FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,parameters[1],status));
377                                         return 1;
378                                 }
379                                 else
380                                 {
381                                         /* no such nick/channel */
382                                         user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
383                                         return 1;
384                                 }
385                                 return 1;
386                         }
387                         else
388                         {
389                                 command_t* privmsg_command = ServerInstance->Parser->GetHandler("PRIVMSG");
390                                 if (privmsg_command)
391                                 {
392                                         privmsg_command->Handle(parameters, pcnt, user);
393                                         return 1;
394                                 }
395                                 else
396                                 {
397                                         ServerInstance->Log(DEBUG, "Could not find PRIVMSG Command!");
398                                 }
399                         }
400                 }
401                 return 0;
402         }
403
404         /* Taken from channels.cpp and slightly modified, see OnPreCommand above*/
405         void WriteAllExceptSenderAndSilenced(chanrec* chan, userrec* user, bool serversource, char status, char* text, ...)
406         {
407                 char textbuffer[MAXBUF];
408                 va_list argsPtr;
409
410                 if (!text)
411                         return;
412
413                 va_start(argsPtr, text);
414                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
415                 va_end(argsPtr);
416
417                 this->WriteAllExceptSenderAndSilenced(chan, user, serversource, status, std::string(textbuffer));
418         }
419
420         /* Taken from channels.cpp and slightly modified, see OnPreCommand above*/
421         void WriteAllExceptSenderAndSilenced(chanrec* chan, userrec* user, bool serversource, char status, const std::string& text)
422         {
423                 CUList *ulist;
424
425                 switch (status)
426                 {
427                         case '@':
428                                 ulist = chan->GetOppedUsers();
429                                 break;
430                         case '%':
431                                 ulist = chan->GetHalfoppedUsers();
432                                 break;
433                         case '+':
434                                 ulist = chan->GetVoicedUsers();
435                                 break;
436                         default:
437                                 ulist = chan->GetUsers();
438                                 break;
439                 }
440         
441                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
442                 {
443                         if ((IS_LOCAL(i->second)) && (user != i->second))
444                         {
445                                 if (serversource)
446                                 {
447                                         i->second->WriteServ(text);
448                                 }
449                                 else
450                                 {
451                                         if (MatchPattern(i->second, user, SILENCE_CHANNEL) == 0)
452                                         {
453                                                 i->second->WriteFrom(user,text);
454                                         }
455                                 }
456                         }
457                 }
458         }
459
460         virtual ~ModuleSilence()
461         {
462         }
463         
464         virtual Version GetVersion()
465         {
466                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
467         }
468 };
469
470
471 class ModuleSilenceFactory : public ModuleFactory
472 {
473  public:
474         ModuleSilenceFactory()
475         {
476         }
477         
478         ~ModuleSilenceFactory()
479         {
480         }
481         
482         virtual Module * CreateModule(InspIRCd* Me)
483         {
484                 return new ModuleSilence(Me);
485         }
486         
487 };
488
489
490 extern "C" void * init_module( void )
491 {
492         return new ModuleSilenceFactory;
493 }
494