2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "modules/ctctags.h"
27 RPL_ENDOFSILELIST = 272,
28 ERR_SILELISTFULL = 511,
39 // Does nothing; for internal use only.
42 // Exclude users who match this flags ("x").
45 // 2, 4, 8, 16 are reserved for future use.
47 // Matches a NOTICE targeted at a channel ("n").
48 SF_NOTICE_CHANNEL = 32,
50 // Matches a NOTICE targeted at a user ("N").
53 // Matches a PRIVMSG targeted at a channel ("p").
54 SF_PRIVMSG_CHANNEL = 128,
56 // Matches a PRIVMSG targeted at a user ("P").
57 SF_PRIVMSG_USER = 256,
59 // Matches a TAGMSG targeted at a channel ("t").
60 SF_TAGMSG_CHANNEL = 512,
62 // Matches a TAGMSG targeted at a user ("T").
63 SF_TAGMSG_USER = 1024,
65 // Matches a CTCP targeted at a channel ("c").
66 SF_CTCP_CHANNEL = 2048,
68 // Matches a CTCP targeted at a user ("C").
71 // Matches an invite to a channel ("i").
74 // The default if no flags have been specified.
75 SF_DEFAULT = SF_NOTICE_CHANNEL | SF_NOTICE_USER | SF_PRIVMSG_CHANNEL | SF_PRIVMSG_USER | SF_TAGMSG_CHANNEL |
76 SF_TAGMSG_USER | SF_CTCP_CHANNEL | SF_CTCP_USER | SF_INVITE
79 // The flags that this mask is silenced for.
82 // The mask which is silenced (e.g. *!*@example.com).
85 SilenceEntry(uint32_t Flags, const std::string& Mask)
91 bool operator <(const SilenceEntry& other) const
93 if (flags & SF_EXEMPT && other.flags & ~SF_EXEMPT)
95 if (other.flags & SF_EXEMPT && flags & ~SF_EXEMPT)
97 if (flags < other.flags)
99 if (other.flags < flags)
101 return mask < other.mask;
104 // Converts a flag list to a bitmask.
105 static bool FlagsToBits(const std::string& flags, uint32_t& out)
108 for (std::string::const_iterator flag = flags.begin(); flag != flags.end(); ++flag)
116 out |= SF_CTCP_CHANNEL;
125 out |= SF_NOTICE_USER;
128 out |= SF_NOTICE_CHANNEL;
131 out |= SF_PRIVMSG_USER;
134 out |= SF_PRIVMSG_CHANNEL;
137 out |= SF_TAGMSG_USER;
140 out |= SF_TAGMSG_CHANNEL;
153 // Converts a bitmask to a flag list.
154 static std::string BitsToFlags(uint32_t flags)
157 if (flags & SF_CTCP_USER)
159 if (flags & SF_CTCP_CHANNEL)
161 if (flags & SF_INVITE)
163 if (flags & SF_NOTICE_USER)
165 if (flags & SF_NOTICE_CHANNEL)
167 if (flags & SF_PRIVMSG_USER)
169 if (flags & SF_PRIVMSG_CHANNEL)
171 if (flags & SF_TAGMSG_CHANNEL)
173 if (flags & SF_TAGMSG_USER)
175 if (flags & SF_EXEMPT)
181 typedef insp::flat_set<SilenceEntry> SilenceList;
183 class SilenceMessage : public ClientProtocol::Message
186 SilenceMessage(const std::string& mask, const std::string& flags)
187 : ClientProtocol::Message("SILENCE")
194 class CommandSilence : public SplitCommand
197 ClientProtocol::EventProvider msgprov;
199 CmdResult AddSilence(LocalUser* user, const std::string& mask, uint32_t flags)
201 SilenceList* list = ext.get(user);
202 if (list && list->size() > maxsilence)
204 user->WriteNumeric(ERR_SILELISTFULL, mask, SilenceEntry::BitsToFlags(flags), "Your SILENCE list is full");
209 // There is no list; create it.
210 list = new SilenceList();
214 if (!list->insert(SilenceEntry(flags, mask)).second)
216 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified already exists");
220 SilenceMessage msg("+" + mask, SilenceEntry::BitsToFlags(flags));
221 user->Send(msgprov, msg);
225 CmdResult RemoveSilence(LocalUser* user, const std::string& mask, uint32_t flags)
227 SilenceList* list = ext.get(user);
230 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
232 if (!irc::equals(iter->mask, mask) || iter->flags != flags)
236 SilenceMessage msg("-" + mask, SilenceEntry::BitsToFlags(flags));
237 user->Send(msgprov, msg);
242 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified could not be found");
246 CmdResult ShowSilenceList(LocalUser* user)
248 SilenceList* list = ext.get(user);
251 for (SilenceList::const_iterator iter = list->begin(); iter != list->end(); ++iter)
253 user->WriteNumeric(RPL_SILELIST, iter->mask, SilenceEntry::BitsToFlags(iter->flags));
256 user->WriteNumeric(RPL_ENDOFSILELIST, "End of SILENCE list");
261 SimpleExtItem<SilenceList> ext;
262 unsigned int maxsilence;
264 CommandSilence(Module* Creator)
265 : SplitCommand(Creator, "SILENCE")
266 , msgprov(Creator, "SILENCE")
267 , ext("silence_list", ExtensionItem::EXT_USER, Creator)
269 allow_empty_last_param = false;
270 syntax = "[(+|-)<mask> [CcdiNnPpTtx]]";
273 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
275 if (parameters.empty())
276 return ShowSilenceList(user);
278 // If neither add nor remove are specified we default to add.
279 bool is_remove = parameters[0][0] == '-';
281 // If a prefix mask has been given then strip it and clean it up.
282 std::string mask = parameters[0];
283 if (mask[0] == '-' || mask[0] == '+')
288 ModeParser::CleanMask(mask);
291 // If the user specified a flags then use that. Otherwise, default to blocking
292 // all CTCPs, invites, notices, privmsgs, and invites.
293 uint32_t flags = SilenceEntry::SF_DEFAULT;
294 if (parameters.size() > 1)
296 if (!SilenceEntry::FlagsToBits(parameters[1], flags))
298 user->WriteNumeric(ERR_SILENCE, mask, parameters[1], "You specified one or more invalid SILENCE flags");
301 else if (flags == SilenceEntry::SF_EXEMPT)
303 // The user specified "x" with no other flags which does not make sense; add the "d" flag.
304 flags |= SilenceEntry::SF_DEFAULT;
308 return is_remove ? RemoveSilence(user, mask, flags) : AddSilence(user, mask, flags);
314 , public CTCTags::EventListener
320 ModResult BuildChannelExempts(User* source, Channel* channel, SilenceEntry::SilenceFlags flag, CUList& exemptions)
322 const Channel::MemberMap& members = channel->GetUsers();
323 for (Channel::MemberMap::const_iterator member = members.begin(); member != members.end(); ++member)
325 if (!CanReceiveMessage(source, member->first, flag))
326 exemptions.insert(member->first);
328 return MOD_RES_PASSTHRU;
331 bool CanReceiveMessage(User* source, User* target, SilenceEntry::SilenceFlags flag)
333 // Servers handle their own clients.
334 if (!IS_LOCAL(target))
337 if (exemptuline && source->server->IsULine())
340 SilenceList* list = cmd.ext.get(target);
344 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
346 if (!(iter->flags & flag))
349 if (InspIRCd::Match(source->GetFullHost(), iter->mask))
350 return iter->flags & SilenceEntry::SF_EXEMPT;
358 : CTCTags::EventListener(this)
363 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
365 ConfigTag* tag = ServerInstance->Config->ConfValue("silence");
366 exemptuline = tag->getBool("exemptuline", true);
367 cmd.maxsilence = tag->getUInt("maxentries", 32, 1);
370 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
372 tokens["ESILENCE"] = "CcdiNnPpTtx";
373 tokens["SILENCE"] = ConvToStr(cmd.maxsilence);
376 ModResult OnUserPreInvite(User* source, User* dest, Channel* channel, time_t timeout) CXX11_OVERRIDE
378 return CanReceiveMessage(source, dest, SilenceEntry::SF_INVITE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
381 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
383 std::string ctcpname;
384 bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION");
386 SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE;
387 if (target.type == MessageTarget::TYPE_CHANNEL)
390 flag = SilenceEntry::SF_CTCP_CHANNEL;
391 else if (details.type == MSG_NOTICE)
392 flag = SilenceEntry::SF_NOTICE_CHANNEL;
393 else if (details.type == MSG_PRIVMSG)
394 flag = SilenceEntry::SF_PRIVMSG_CHANNEL;
396 return BuildChannelExempts(user, target.Get<Channel>(), flag, details.exemptions);
399 if (target.type == MessageTarget::TYPE_USER)
402 flag = SilenceEntry::SF_CTCP_USER;
403 else if (details.type == MSG_NOTICE)
404 flag = SilenceEntry::SF_NOTICE_USER;
405 else if (details.type == MSG_PRIVMSG)
406 flag = SilenceEntry::SF_PRIVMSG_USER;
408 if (!CanReceiveMessage(user, target.Get<User>(), flag))
410 details.echo_original = true;
415 return MOD_RES_PASSTHRU;
418 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
420 if (target.type == MessageTarget::TYPE_CHANNEL)
421 return BuildChannelExempts(user, target.Get<Channel>(), SilenceEntry::SF_TAGMSG_CHANNEL, details.exemptions);
423 if (target.type == MessageTarget::TYPE_USER && !CanReceiveMessage(user, target.Get<User>(), SilenceEntry::SF_TAGMSG_USER))
425 details.echo_original = true;
429 return MOD_RES_PASSTHRU;
432 Version GetVersion() CXX11_OVERRIDE
434 return Version("Provides support for blocking users with the SILENCE command", VF_OPTCOMMON | VF_VENDOR);
438 MODULE_INIT(ModuleSilence)