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