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