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|a|x> as in <private|channel|invites|notices|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_ALL = 0x0010; /* a all, (pcin) */
63 static int SILENCE_EXCLUDE = 0x0020; /* x exclude this pattern */
66 class cmd_silence : public command_t
69 cmd_silence (InspIRCd* Instance) : command_t(Instance,"SILENCE", 0, 0)
71 this->source = "m_silence_ext.so";
72 syntax = "{[+|-]<mask> <p|c|i|n|a|x>}";
75 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
79 // no parameters, show the current silence list.
80 // Use Extensible::GetExt to fetch the silence list
82 user->GetExt("silence_list", sl);
83 // if the user has a silence list associated with their user record, show it
86 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
88 user->WriteServ("271 %s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
91 user->WriteServ("272 %s :End of Silence List",user->nick);
97 // one or more parameters, add or delete entry from the list (only the first parameter is used)
98 std::string mask = parameters[0] + 1;
99 char action = *parameters[0];
100 // Default is private and notice so clients do not break
101 int pattern = CompilePattern("pn");
103 // if pattern supplied, use it
105 pattern = CompilePattern(parameters[1]);
110 // 'SILENCE +' or 'SILENCE -', assume *!*@*
114 ModeParser::CleanMask(mask);
118 // fetch their silence list
120 user->GetExt("silence_list", sl);
121 // does it contain any entries and does it exist?
126 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
128 // search through for the item
129 irc::string listitem = i->first.c_str();
130 if (listitem == mask && i->second == pattern)
133 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
140 // tidy up -- if a user's list is empty, theres no use having it
141 // hanging around in the user record.
143 user->Shrink("silence_list");
147 else if (action == '+')
149 // fetch the user's current silence list
151 user->GetExt("silence_list", sl);
152 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
155 sl = new silencelist;
156 user->Extend("silence_list", sl);
158 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
160 irc::string listitem = n->first.c_str();
161 if (listitem == mask && n->second == pattern)
163 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
167 if (((pattern & SILENCE_EXCLUDE) > 0))
169 sl->push_front(silenceset(mask,pattern));
173 sl->push_back(silenceset(mask,pattern));
175 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
182 /* turn the nice human readable pattern into a mask */
183 int CompilePattern(const char* pattern)
186 for (uint n = 0; n < strlen(pattern); n++)
191 p |= SILENCE_PRIVATE;
194 p |= SILENCE_CHANNEL;
206 p |= SILENCE_EXCLUDE;
215 /* turn the mask into a nice human readable format */
216 std::string DecompPattern (const int pattern)
218 std::string out = "";
219 if ((pattern & SILENCE_PRIVATE) > 0)
221 if ((pattern & SILENCE_CHANNEL) > 0)
223 if ((pattern & SILENCE_INVITE) > 0)
225 if ((pattern & SILENCE_NOTICE) > 0)
227 if ((pattern & SILENCE_ALL) > 0)
229 if ((pattern & SILENCE_EXCLUDE) > 0)
231 return "<" + out.substr(1) + ">";
236 class ModuleSilence : public Module
239 cmd_silence* mycommand;
242 ModuleSilence(InspIRCd* Me)
246 mycommand = new cmd_silence(ServerInstance);
247 ServerInstance->AddCommand(mycommand);
250 void Implements(char* List)
252 List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = List[I_OnUserPreInvite] = 1;
255 virtual void OnUserQuit(userrec* user, const std::string &reason)
257 // when the user quits tidy up any silence list they might have just to keep things tidy
259 user->GetExt("silence_list", sl);
263 user->Shrink("silence_list");
267 virtual void On005Numeric(std::string &output)
269 // we don't really have a limit...
270 output = output + " ESILENCE SILENCE=999";
274 virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
276 if (target_type == TYPE_USER)
278 return MatchPattern((userrec*)dest, user, silence_type);
280 else if (target_type == TYPE_CHANNEL)
282 chanrec* chan = (chanrec*)dest;
289 ulist = chan->GetOppedUsers();
292 ulist = chan->GetHalfoppedUsers();
295 ulist = chan->GetVoicedUsers();
298 ulist = chan->GetUsers();
302 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
304 if ((IS_LOCAL(i->second)) && (user != i->second))
306 if (MatchPattern(i->second, user, SILENCE_CHANNEL) == 1)
308 exempt_list[i->second] = i->second;
317 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
319 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
322 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
324 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
327 virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
329 return MatchPattern(dest, source, SILENCE_INVITE);
332 int MatchPattern(userrec* dest, userrec* source, int pattern)
335 dest->GetExt("silence_list", sl);
338 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
340 if ((match(source->GetFullHost(), c->first.c_str())) && ( ((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0))
342 if (((c->second & SILENCE_EXCLUDE) > 0))
355 virtual ~ModuleSilence()
359 virtual Version GetVersion()
361 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
366 class ModuleSilenceFactory : public ModuleFactory
369 ModuleSilenceFactory()
373 ~ModuleSilenceFactory()
377 virtual Module * CreateModule(InspIRCd* Me)
379 return new ModuleSilence(Me);
385 extern "C" void * init_module( void )
387 return new ModuleSilenceFactory;