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 SilenceExtItem : public SimpleExtItem<SilenceList>
186 unsigned int maxsilence;
188 SilenceExtItem(Module* Creator)
189 : SimpleExtItem<SilenceList>("silence_list", ExtensionItem::EXT_USER, Creator)
193 void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
195 LocalUser* user = IS_LOCAL(static_cast<User*>(container));
199 // Remove the old list and create a new one.
201 SilenceList* list = new SilenceList();
203 irc::spacesepstream ts(value);
204 while (!ts.StreamEnd())
206 // Check we have space for another entry.
207 if (list->size() >= maxsilence)
209 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Oversized silence list received for %s: %s",
210 user->uuid.c_str(), value.c_str());
215 // Extract the mask and the flags.
218 if (!ts.GetToken(mask) || !ts.GetToken(flagstr))
220 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed silence list received for %s: %s",
221 user->uuid.c_str(), value.c_str());
226 // Try to parse the flags.
228 if (!SilenceEntry::FlagsToBits(flagstr, flags))
230 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed silence flags received for %s: %s",
231 user->uuid.c_str(), flagstr.c_str());
236 // Store the silence entry.
237 list->insert(SilenceEntry(flags, mask));
240 // The value was well formed.
244 std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
246 SilenceList* list = static_cast<SilenceList*>(item);
248 for (SilenceList::const_iterator iter = list->begin(); iter != list->end(); ++iter)
250 if (iter != list->begin())
253 buf.append(iter->mask);
255 buf.append(SilenceEntry::BitsToFlags(iter->flags));
261 class SilenceMessage : public ClientProtocol::Message
264 SilenceMessage(const std::string& mask, const std::string& flags)
265 : ClientProtocol::Message("SILENCE")
272 class CommandSilence : public SplitCommand
275 ClientProtocol::EventProvider msgprov;
277 CmdResult AddSilence(LocalUser* user, const std::string& mask, uint32_t flags)
279 SilenceList* list = ext.get(user);
280 if (list && list->size() > ext.maxsilence)
282 user->WriteNumeric(ERR_SILELISTFULL, mask, SilenceEntry::BitsToFlags(flags), "Your SILENCE list is full");
287 // There is no list; create it.
288 list = new SilenceList();
292 if (!list->insert(SilenceEntry(flags, mask)).second)
294 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified already exists");
298 SilenceMessage msg("+" + mask, SilenceEntry::BitsToFlags(flags));
299 user->Send(msgprov, msg);
303 CmdResult RemoveSilence(LocalUser* user, const std::string& mask, uint32_t flags)
305 SilenceList* list = ext.get(user);
308 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
310 if (!irc::equals(iter->mask, mask) || iter->flags != flags)
314 SilenceMessage msg("-" + mask, SilenceEntry::BitsToFlags(flags));
315 user->Send(msgprov, msg);
320 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified could not be found");
324 CmdResult ShowSilenceList(LocalUser* user)
326 SilenceList* list = ext.get(user);
329 for (SilenceList::const_iterator iter = list->begin(); iter != list->end(); ++iter)
331 user->WriteNumeric(RPL_SILELIST, iter->mask, SilenceEntry::BitsToFlags(iter->flags));
334 user->WriteNumeric(RPL_ENDOFSILELIST, "End of SILENCE list");
341 CommandSilence(Module* Creator)
342 : SplitCommand(Creator, "SILENCE")
343 , msgprov(Creator, "SILENCE")
346 allow_empty_last_param = false;
347 syntax = "[(+|-)<mask> [CcdiNnPpTtx]]";
350 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
352 if (parameters.empty())
353 return ShowSilenceList(user);
355 // If neither add nor remove are specified we default to add.
356 bool is_remove = parameters[0][0] == '-';
358 // If a prefix mask has been given then strip it and clean it up.
359 std::string mask = parameters[0];
360 if (mask[0] == '-' || mask[0] == '+')
365 ModeParser::CleanMask(mask);
368 // If the user specified a flags then use that. Otherwise, default to blocking
369 // all CTCPs, invites, notices, privmsgs, and invites.
370 uint32_t flags = SilenceEntry::SF_DEFAULT;
371 if (parameters.size() > 1)
373 if (!SilenceEntry::FlagsToBits(parameters[1], flags))
375 user->WriteNumeric(ERR_SILENCE, mask, parameters[1], "You specified one or more invalid SILENCE flags");
378 else if (flags == SilenceEntry::SF_EXEMPT)
380 // The user specified "x" with no other flags which does not make sense; add the "d" flag.
381 flags |= SilenceEntry::SF_DEFAULT;
385 return is_remove ? RemoveSilence(user, mask, flags) : AddSilence(user, mask, flags);
391 , public CTCTags::EventListener
397 ModResult BuildChannelExempts(User* source, Channel* channel, SilenceEntry::SilenceFlags flag, CUList& exemptions)
399 const Channel::MemberMap& members = channel->GetUsers();
400 for (Channel::MemberMap::const_iterator member = members.begin(); member != members.end(); ++member)
402 if (!CanReceiveMessage(source, member->first, flag))
403 exemptions.insert(member->first);
405 return MOD_RES_PASSTHRU;
408 bool CanReceiveMessage(User* source, User* target, SilenceEntry::SilenceFlags flag)
410 // Servers handle their own clients.
411 if (!IS_LOCAL(target))
414 if (exemptuline && source->server->IsULine())
417 SilenceList* list = cmd.ext.get(target);
421 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
423 if (!(iter->flags & flag))
426 if (InspIRCd::Match(source->GetFullHost(), iter->mask))
427 return iter->flags & SilenceEntry::SF_EXEMPT;
435 : CTCTags::EventListener(this)
440 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
442 ConfigTag* tag = ServerInstance->Config->ConfValue("silence");
443 exemptuline = tag->getBool("exemptuline", true);
444 cmd.ext.maxsilence = tag->getUInt("maxentries", 32, 1);
447 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
449 tokens["ESILENCE"] = "CcdiNnPpTtx";
450 tokens["SILENCE"] = ConvToStr(cmd.ext.maxsilence);
453 ModResult OnUserPreInvite(User* source, User* dest, Channel* channel, time_t timeout) CXX11_OVERRIDE
455 return CanReceiveMessage(source, dest, SilenceEntry::SF_INVITE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
458 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
460 std::string ctcpname;
461 bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION");
463 SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE;
466 case MessageTarget::TYPE_CHANNEL:
469 flag = SilenceEntry::SF_CTCP_CHANNEL;
470 else if (details.type == MSG_NOTICE)
471 flag = SilenceEntry::SF_NOTICE_CHANNEL;
472 else if (details.type == MSG_PRIVMSG)
473 flag = SilenceEntry::SF_PRIVMSG_CHANNEL;
475 return BuildChannelExempts(user, target.Get<Channel>(), flag, details.exemptions);
478 case MessageTarget::TYPE_USER:
481 flag = SilenceEntry::SF_CTCP_USER;
482 else if (details.type == MSG_NOTICE)
483 flag = SilenceEntry::SF_NOTICE_USER;
484 else if (details.type == MSG_PRIVMSG)
485 flag = SilenceEntry::SF_PRIVMSG_USER;
487 if (!CanReceiveMessage(user, target.Get<User>(), flag))
489 details.echo_original = true;
494 case MessageTarget::TYPE_SERVER:
498 return MOD_RES_PASSTHRU;
501 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
503 if (target.type == MessageTarget::TYPE_CHANNEL)
504 return BuildChannelExempts(user, target.Get<Channel>(), SilenceEntry::SF_TAGMSG_CHANNEL, details.exemptions);
506 if (target.type == MessageTarget::TYPE_USER && !CanReceiveMessage(user, target.Get<User>(), SilenceEntry::SF_TAGMSG_USER))
508 details.echo_original = true;
512 return MOD_RES_PASSTHRU;
515 Version GetVersion() CXX11_OVERRIDE
517 return Version("Provides support for blocking users with the SILENCE command", VF_OPTCOMMON | VF_VENDOR);
521 MODULE_INIT(ModuleSilence)