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