1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
30 /* $ModDesc: Provides support for the /SILENCE command */
32 /* Improved drop-in replacement for the /SILENCE command
33 * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
35 * example that blocks all except private messages
39 * example that blocks all invites except from channel services
41 * /SILENCE +chanserv!services@chatters.net ix
43 * example that blocks some bad dude from private, notice and inviting you
44 * /SILENCE +*!kiddie@lamerz.net pin
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
51 // pair of hostmask and flags
52 typedef std::pair<std::string, int> silenceset;
54 // deque list of pairs
55 typedef std::deque<silenceset> silencelist;
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_CNOTICE = 0x0010; /* t channel notices */
63 static int SILENCE_ALL = 0x0020; /* a all, (pcint) */
64 static int SILENCE_EXCLUDE = 0x0040; /* x exclude this pattern */
67 class cmd_silence : public command_t
70 cmd_silence (InspIRCd* Instance) : command_t(Instance,"SILENCE", 0, 0)
72 this->source = "m_silence_ext.so";
73 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
76 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
80 // no parameters, show the current silence list.
81 // Use Extensible::GetExt to fetch the silence list
83 user->GetExt("silence_list", sl);
84 // if the user has a silence list associated with their user record, show it
87 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
89 user->WriteServ("271 %s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
92 user->WriteServ("272 %s :End of Silence List",user->nick);
98 // one or more parameters, add or delete entry from the list (only the first parameter is used)
99 std::string mask = parameters[0] + 1;
100 char action = *parameters[0];
101 // Default is private and notice so clients do not break
102 int pattern = CompilePattern("pn");
104 // if pattern supplied, use it
106 pattern = CompilePattern(parameters[1]);
111 // 'SILENCE +' or 'SILENCE -', assume *!*@*
115 ModeParser::CleanMask(mask);
119 // fetch their silence list
121 user->GetExt("silence_list", sl);
122 // does it contain any entries and does it exist?
127 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
129 // search through for the item
130 irc::string listitem = i->first.c_str();
131 if (listitem == mask && i->second == pattern)
134 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
141 // tidy up -- if a user's list is empty, theres no use having it
142 // hanging around in the user record.
144 user->Shrink("silence_list");
148 else if (action == '+')
150 // fetch the user's current silence list
152 user->GetExt("silence_list", sl);
153 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
156 sl = new silencelist;
157 user->Extend("silence_list", sl);
159 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
161 irc::string listitem = n->first.c_str();
162 if (listitem == mask && n->second == pattern)
164 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
168 if (((pattern & SILENCE_EXCLUDE) > 0))
170 sl->push_front(silenceset(mask,pattern));
174 sl->push_back(silenceset(mask,pattern));
176 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
183 /* turn the nice human readable pattern into a mask */
184 int CompilePattern(const char* pattern)
187 for (const char* n = pattern; *n; n++)
192 p |= SILENCE_PRIVATE;
195 p |= SILENCE_CHANNEL;
204 p |= SILENCE_CNOTICE;
210 p |= SILENCE_EXCLUDE;
219 /* turn the mask into a nice human readable format */
220 std::string DecompPattern (const int pattern)
222 std::string out = "";
223 if ((pattern & SILENCE_PRIVATE) > 0)
224 out += ",privatemessages";
225 if ((pattern & SILENCE_CHANNEL) > 0)
226 out += ",channelmessages";
227 if ((pattern & SILENCE_INVITE) > 0)
229 if ((pattern & SILENCE_NOTICE) > 0)
230 out += ",privatenotices";
231 if ((pattern & SILENCE_CNOTICE) > 0)
232 out += ",channelnotices";
233 if ((pattern & SILENCE_ALL) > 0)
235 if ((pattern & SILENCE_EXCLUDE) > 0)
237 return "<" + out.substr(1) + ">";
242 class ModuleSilence : public Module
245 cmd_silence* mycommand;
248 ModuleSilence(InspIRCd* Me)
252 mycommand = new cmd_silence(ServerInstance);
253 ServerInstance->AddCommand(mycommand);
256 void Implements(char* List)
258 List[I_OnBuildExemptList] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = List[I_OnUserPreInvite] = 1;
261 virtual void OnUserQuit(userrec* user, const std::string &reason)
263 // when the user quits tidy up any silence list they might have just to keep things tidy
265 user->GetExt("silence_list", sl);
269 user->Shrink("silence_list");
273 virtual void On005Numeric(std::string &output)
275 // we don't really have a limit...
276 output = output + " ESILENCE SILENCE=999";
279 virtual void OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list)
281 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
286 ulist = chan->GetOppedUsers();
289 ulist = chan->GetHalfoppedUsers();
292 ulist = chan->GetVoicedUsers();
295 ulist = chan->GetUsers();
299 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
301 if (IS_LOCAL(i->second))
303 if (MatchPattern(i->second, sender, public_silence) == 1)
305 exempt_list[i->second] = i->second;
311 virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
313 if (target_type == TYPE_USER)
315 return MatchPattern((userrec*)dest, user, silence_type);
317 else if (target_type == TYPE_CHANNEL)
319 chanrec* chan = (chanrec*)dest;
322 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list);
328 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
330 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
333 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
335 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
338 virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
340 return MatchPattern(dest, source, SILENCE_INVITE);
343 int MatchPattern(userrec* dest, userrec* source, int pattern)
346 dest->GetExt("silence_list", sl);
349 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
351 if ((match(source->GetFullHost(), c->first.c_str())) && ( ((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0))
353 if (((c->second & SILENCE_EXCLUDE) > 0))
366 virtual ~ModuleSilence()
370 virtual Version GetVersion()
372 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
377 class ModuleSilenceFactory : public ModuleFactory
380 ModuleSilenceFactory()
384 ~ModuleSilenceFactory()
388 virtual Module * CreateModule(InspIRCd* Me)
390 return new ModuleSilence(Me);
396 extern "C" void * init_module( void )
398 return new ModuleSilenceFactory;