2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2006 John Brooks <john.brooks@dereferenced.net>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /* $ModDesc: Provides support for the /SILENCE command */
28 /* Improved drop-in replacement for the /SILENCE command
29 * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
31 * example that blocks all except private messages
35 * example that blocks all invites except from channel services
37 * /SILENCE +chanserv!services@chatters.net ix
39 * example that blocks some bad dude from private, notice and inviting you
40 * /SILENCE +*!kiddie@lamerz.net pin
42 * TODO: possibly have add and remove check for existing host and only modify flags according to
43 * what's been changed instead of having to remove first, then add if you want to change
47 // pair of hostmask and flags
48 typedef std::pair<std::string, int> silenceset;
50 // deque list of pairs
51 typedef std::deque<silenceset> silencelist;
54 static int SILENCE_PRIVATE = 0x0001; /* p private messages */
55 static int SILENCE_CHANNEL = 0x0002; /* c channel messages */
56 static int SILENCE_INVITE = 0x0004; /* i invites */
57 static int SILENCE_NOTICE = 0x0008; /* n notices */
58 static int SILENCE_CNOTICE = 0x0010; /* t channel notices */
59 static int SILENCE_ALL = 0x0020; /* a all, (pcint) */
60 static int SILENCE_EXCLUDE = 0x0040; /* x exclude this pattern */
63 class CommandSVSSilence : public Command
66 CommandSVSSilence(Module* Creator) : Command(Creator,"SVSSILENCE", 2)
68 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
69 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
72 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
75 * XXX: thought occurs to me
76 * We may want to change the syntax of this command to
77 * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
78 * style command so services can modify lots of entries at once.
79 * leaving it backwards compatible for now as it's late. -- w
81 if (!ServerInstance->ULine(user->server))
84 User *u = ServerInstance->FindNick(parameters[0]);
90 ServerInstance->Parser->CallHandler("SILENCE", std::vector<std::string>(++parameters.begin(), parameters.end()), u);
96 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
98 User* target = ServerInstance->FindNick(parameters[0]);
100 return ROUTE_OPT_UCAST(target->server);
101 return ROUTE_LOCALONLY;
105 class CommandSilence : public Command
107 unsigned int& maxsilence;
109 SimpleExtItem<silencelist> ext;
110 CommandSilence(Module* Creator, unsigned int &max) : Command(Creator, "SILENCE", 0),
111 maxsilence(max), ext("silence_list", Creator)
113 allow_empty_last_param = false;
114 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
115 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
118 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
120 if (!parameters.size())
122 // no parameters, show the current silence list.
123 silencelist* sl = ext.get(user);
124 // if the user has a silence list associated with their user record, show it
127 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
129 std::string decomppattern = DecompPattern(c->second);
130 user->WriteNumeric(271, "%s %s %s %s",user->nick.c_str(), user->nick.c_str(),c->first.c_str(), decomppattern.c_str());
133 user->WriteNumeric(272, "%s :End of Silence List",user->nick.c_str());
137 else if (parameters.size() > 0)
139 // one or more parameters, add or delete entry from the list (only the first parameter is used)
140 std::string mask = parameters[0].substr(1);
141 char action = parameters[0][0];
142 // Default is private and notice so clients do not break
143 int pattern = CompilePattern("pn");
145 // if pattern supplied, use it
146 if (parameters.size() > 1) {
147 pattern = CompilePattern(parameters[1].c_str());
152 user->WriteServ("NOTICE %s :Bad SILENCE pattern",user->nick.c_str());
158 // 'SILENCE +' or 'SILENCE -', assume *!*@*
162 ModeParser::CleanMask(mask);
166 std::string decomppattern = DecompPattern(pattern);
167 // fetch their silence list
168 silencelist* sl = ext.get(user);
169 // does it contain any entries and does it exist?
172 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
174 // search through for the item
175 irc::string listitem = i->first.c_str();
176 if (listitem == mask && i->second == pattern)
179 user->WriteNumeric(950, "%s %s :Removed %s %s from silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
188 user->WriteNumeric(952, "%s %s :%s %s does not exist on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
190 else if (action == '+')
192 // fetch the user's current silence list
193 silencelist* sl = ext.get(user);
196 sl = new silencelist;
199 if (sl->size() > maxsilence)
201 user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick.c_str(), user->nick.c_str());
205 std::string decomppattern = DecompPattern(pattern);
206 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
208 irc::string listitem = n->first.c_str();
209 if (listitem == mask && n->second == pattern)
211 user->WriteNumeric(952, "%s %s :%s %s is already on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
215 if (((pattern & SILENCE_EXCLUDE) > 0))
217 sl->push_front(silenceset(mask,pattern));
221 sl->push_back(silenceset(mask,pattern));
223 user->WriteNumeric(951, "%s %s :Added %s %s to silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), decomppattern.c_str());
230 /* turn the nice human readable pattern into a mask */
231 int CompilePattern(const char* pattern)
234 for (const char* n = pattern; *n; n++)
239 p |= SILENCE_PRIVATE;
242 p |= SILENCE_CHANNEL;
251 p |= SILENCE_CNOTICE;
258 p |= SILENCE_EXCLUDE;
267 /* turn the mask into a nice human readable format */
268 std::string DecompPattern (const int pattern)
271 if (pattern & SILENCE_PRIVATE)
272 out += ",privatemessages";
273 if (pattern & SILENCE_CHANNEL)
274 out += ",channelmessages";
275 if (pattern & SILENCE_INVITE)
277 if (pattern & SILENCE_NOTICE)
278 out += ",privatenotices";
279 if (pattern & SILENCE_CNOTICE)
280 out += ",channelnotices";
281 if (pattern & SILENCE_ALL)
283 if (pattern & SILENCE_EXCLUDE)
286 return "<" + out.substr(1) + ">";
293 class ModuleSilence : public Module
295 unsigned int maxsilence;
296 CommandSilence cmdsilence;
297 CommandSVSSilence cmdsvssilence;
301 : maxsilence(32), cmdsilence(this, maxsilence), cmdsvssilence(this)
308 ServerInstance->Modules->AddService(cmdsilence);
309 ServerInstance->Modules->AddService(cmdsvssilence);
310 ServerInstance->Modules->AddService(cmdsilence.ext);
312 Implementation eventlist[] = { I_OnRehash, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
313 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
316 void OnRehash(User* user)
318 maxsilence = ServerInstance->Config->ConfValue("showwhois")->getInt("maxentries", 32);
323 void On005Numeric(std::string &output)
325 // we don't really have a limit...
326 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
329 void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
331 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
332 const UserMembList *ulist = chan->GetUsers();
334 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
336 if (IS_LOCAL(i->first))
338 if (MatchPattern(i->first, sender, public_silence) == MOD_RES_DENY)
340 exempt_list.insert(i->first);
346 ModResult PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
348 if (target_type == TYPE_USER && IS_LOCAL(((User*)dest)))
350 return MatchPattern((User*)dest, user, silence_type);
352 else if (target_type == TYPE_CHANNEL)
354 Channel* chan = (Channel*)dest;
357 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
360 return MOD_RES_PASSTHRU;
363 ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
365 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
368 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
370 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
373 ModResult OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
375 return MatchPattern(dest, source, SILENCE_INVITE);
378 ModResult MatchPattern(User* dest, User* source, int pattern)
381 if (!source || !dest)
382 return MOD_RES_ALLOW;
384 silencelist* sl = cmdsilence.ext.get(dest);
387 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
389 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
390 return (c->second & SILENCE_EXCLUDE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
393 return MOD_RES_PASSTHRU;
398 return Version("Provides support for the /SILENCE command", VF_OPTCOMMON | VF_VENDOR);
402 MODULE_INIT(ModuleSilence)