1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Provides support for the /SILENCE command */
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>
22 * example that blocks all except private messages
26 * example that blocks all invites except from channel services
28 * /SILENCE +chanserv!services@chatters.net ix
30 * example that blocks some bad dude from private, notice and inviting you
31 * /SILENCE +*!kiddie@lamerz.net pin
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
38 // pair of hostmask and flags
39 typedef std::pair<std::string, int> silenceset;
41 // deque list of pairs
42 typedef std::deque<silenceset> silencelist;
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 */
54 class CommandSVSSilence : public Command
57 CommandSVSSilence(InspIRCd* Instance) : Command(Instance,"SVSSILENCE", 0, 2)
59 this->source = "m_silence.so";
60 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
61 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
64 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
67 * XXX: thought occurs to me
68 * We may want to change the syntax of this command to
69 * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
70 * style command so services can modify lots of entries at once.
71 * leaving it backwards compatible for now as it's late. -- w
73 if (!ServerInstance->ULine(user->server))
76 User *u = ServerInstance->FindNick(parameters[0]);
82 ServerInstance->Parser->CallHandler("SILENCE", ¶meters[1], 1, u);
89 class CommandSilence : public Command
91 unsigned int& maxsilence;
93 CommandSilence (InspIRCd* Instance, unsigned int &max) : Command(Instance,"SILENCE", 0, 0), maxsilence(max)
95 this->source = "m_silence_ext.so";
96 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
97 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
100 CmdResult Handle (const char* const* parameters, int pcnt, User *user)
104 // no parameters, show the current silence list.
105 // Use Extensible::GetExt to fetch the silence list
107 user->GetExt("silence_list", sl);
108 // if the user has a silence list associated with their user record, show it
111 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
113 user->WriteNumeric(271, "%s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
116 user->WriteNumeric(272, "%s :End of Silence List",user->nick);
118 return CMD_LOCALONLY;
122 // one or more parameters, add or delete entry from the list (only the first parameter is used)
123 std::string mask = parameters[0] + 1;
124 char action = *parameters[0];
125 // Default is private and notice so clients do not break
126 int pattern = CompilePattern("pn");
128 // if pattern supplied, use it
130 pattern = CompilePattern(parameters[1]);
135 // 'SILENCE +' or 'SILENCE -', assume *!*@*
139 ModeParser::CleanMask(mask);
143 // fetch their silence list
145 user->GetExt("silence_list", sl);
146 // does it contain any entries and does it exist?
149 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
151 // search through for the item
152 irc::string listitem = i->first.c_str();
153 if (listitem == mask && i->second == pattern)
156 user->WriteNumeric(950, "%s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
160 user->Shrink("silence_list");
166 user->WriteNumeric(952, "%s %s :%s %s does not exist on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
168 else if (action == '+')
170 // fetch the user's current silence list
172 user->GetExt("silence_list", sl);
173 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
176 sl = new silencelist;
177 user->Extend("silence_list", sl);
179 if (sl->size() > maxsilence)
181 user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick, user->nick);
184 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
186 irc::string listitem = n->first.c_str();
187 if (listitem == mask && n->second == pattern)
189 user->WriteNumeric(952, "%s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
193 if (((pattern & SILENCE_EXCLUDE) > 0))
195 sl->push_front(silenceset(mask,pattern));
199 sl->push_back(silenceset(mask,pattern));
201 user->WriteNumeric(951, "%s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
205 return CMD_LOCALONLY;
208 /* turn the nice human readable pattern into a mask */
209 int CompilePattern(const char* pattern)
212 for (const char* n = pattern; *n; n++)
217 p |= SILENCE_PRIVATE;
220 p |= SILENCE_CHANNEL;
229 p |= SILENCE_CNOTICE;
236 p |= SILENCE_EXCLUDE;
245 /* turn the mask into a nice human readable format */
246 std::string DecompPattern (const int pattern)
249 if ((pattern & SILENCE_PRIVATE) > 0)
250 out += ",privatemessages";
251 if ((pattern & SILENCE_CHANNEL) > 0)
252 out += ",channelmessages";
253 if ((pattern & SILENCE_INVITE) > 0)
255 if ((pattern & SILENCE_NOTICE) > 0)
256 out += ",privatenotices";
257 if ((pattern & SILENCE_CNOTICE) > 0)
258 out += ",channelnotices";
259 if ((pattern & SILENCE_ALL) > 0)
261 if ((pattern & SILENCE_EXCLUDE) > 0)
263 return "<" + out.substr(1) + ">";
268 class ModuleSilence : public Module
270 CommandSilence* cmdsilence;
271 CommandSVSSilence *cmdsvssilence;
272 unsigned int maxsilence;
275 ModuleSilence(InspIRCd* Me)
276 : Module(Me), maxsilence(32)
279 cmdsilence = new CommandSilence(ServerInstance,maxsilence);
280 cmdsvssilence = new CommandSVSSilence(ServerInstance);
281 ServerInstance->AddCommand(cmdsilence);
282 ServerInstance->AddCommand(cmdsvssilence);
284 Implementation eventlist[] = { I_OnRehash, I_OnBuildExemptList, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
285 ServerInstance->Modules->Attach(eventlist, this, 7);
288 virtual void OnRehash(User* user, const std::string ¶meter)
290 ConfigReader Conf(ServerInstance);
291 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
297 virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
299 // when the user quits tidy up any silence list they might have just to keep things tidy
301 user->GetExt("silence_list", sl);
305 user->Shrink("silence_list");
309 virtual void On005Numeric(std::string &output)
311 // we don't really have a limit...
312 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
315 virtual void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
317 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
322 ulist = chan->GetOppedUsers();
325 ulist = chan->GetHalfoppedUsers();
328 ulist = chan->GetVoicedUsers();
331 ulist = chan->GetUsers();
335 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
337 if (IS_LOCAL(i->first))
339 if (MatchPattern(i->first, sender, public_silence) == 1)
341 exempt_list[i->first] = i->first->nick;
347 virtual int PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
352 if (target_type == TYPE_USER)
354 return MatchPattern((User*)dest, user, silence_type);
356 else if (target_type == TYPE_CHANNEL)
358 Channel* chan = (Channel*)dest;
361 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
367 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
369 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
372 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
374 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
377 virtual int OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
379 return MatchPattern(dest, source, SILENCE_INVITE);
382 int MatchPattern(User* dest, User* source, int pattern)
385 dest->GetExt("silence_list", sl);
388 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
390 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (ServerInstance->MatchText(source->GetFullHost(), c->first)))
391 return !(((c->second & SILENCE_EXCLUDE) > 0));
397 virtual ~ModuleSilence()
401 virtual Version GetVersion()
403 return Version(1, 2, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
407 MODULE_INIT(ModuleSilence)