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