2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2019 iwalkalone <iwalkalone69@gmail.com>
6 * Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "modules/ctctags.h"
30 RPL_ENDOFSILELIST = 272,
31 ERR_SILELISTFULL = 511,
42 // Does nothing; for internal use only.
45 // Exclude users who match this flags ("x").
48 // 2, 4, 8, 16 are reserved for future use.
50 // Matches a NOTICE targeted at a channel ("n").
51 SF_NOTICE_CHANNEL = 32,
53 // Matches a NOTICE targeted at a user ("N").
56 // Matches a PRIVMSG targeted at a channel ("p").
57 SF_PRIVMSG_CHANNEL = 128,
59 // Matches a PRIVMSG targeted at a user ("P").
60 SF_PRIVMSG_USER = 256,
62 // Matches a TAGMSG targeted at a channel ("t").
63 SF_TAGMSG_CHANNEL = 512,
65 // Matches a TAGMSG targeted at a user ("T").
66 SF_TAGMSG_USER = 1024,
68 // Matches a CTCP targeted at a channel ("c").
69 SF_CTCP_CHANNEL = 2048,
71 // Matches a CTCP targeted at a user ("C").
74 // Matches an invite to a channel ("i").
77 // The default if no flags have been specified.
78 SF_DEFAULT = SF_NOTICE_CHANNEL | SF_NOTICE_USER | SF_PRIVMSG_CHANNEL | SF_PRIVMSG_USER | SF_TAGMSG_CHANNEL |
79 SF_TAGMSG_USER | SF_CTCP_CHANNEL | SF_CTCP_USER | SF_INVITE
82 // The flags that this mask is silenced for.
85 // The mask which is silenced (e.g. *!*@example.com).
88 SilenceEntry(uint32_t Flags, const std::string& Mask)
94 bool operator <(const SilenceEntry& other) const
96 if (flags & SF_EXEMPT && other.flags & ~SF_EXEMPT)
98 if (other.flags & SF_EXEMPT && flags & ~SF_EXEMPT)
100 if (flags < other.flags)
102 if (other.flags < flags)
104 return mask < other.mask;
107 // Converts a flag list to a bitmask.
108 static bool FlagsToBits(const std::string& flags, uint32_t& out)
111 for (std::string::const_iterator flag = flags.begin(); flag != flags.end(); ++flag)
119 out |= SF_CTCP_CHANNEL;
128 out |= SF_NOTICE_USER;
131 out |= SF_NOTICE_CHANNEL;
134 out |= SF_PRIVMSG_USER;
137 out |= SF_PRIVMSG_CHANNEL;
140 out |= SF_TAGMSG_USER;
143 out |= SF_TAGMSG_CHANNEL;
156 // Converts a bitmask to a flag list.
157 static std::string BitsToFlags(uint32_t flags)
160 if (flags & SF_CTCP_USER)
162 if (flags & SF_CTCP_CHANNEL)
164 if (flags & SF_INVITE)
166 if (flags & SF_NOTICE_USER)
168 if (flags & SF_NOTICE_CHANNEL)
170 if (flags & SF_PRIVMSG_USER)
172 if (flags & SF_PRIVMSG_CHANNEL)
174 if (flags & SF_TAGMSG_CHANNEL)
176 if (flags & SF_TAGMSG_USER)
178 if (flags & SF_EXEMPT)
184 typedef insp::flat_set<SilenceEntry> SilenceList;
186 class SilenceExtItem : public SimpleExtItem<SilenceList>
189 unsigned int maxsilence;
191 SilenceExtItem(Module* Creator)
192 : SimpleExtItem<SilenceList>("silence_list", ExtensionItem::EXT_USER, Creator)
196 void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE
198 LocalUser* user = IS_LOCAL(static_cast<User*>(container));
202 // Remove the old list and create a new one.
204 SilenceList* list = new SilenceList();
206 irc::spacesepstream ts(value);
207 while (!ts.StreamEnd())
209 // Check we have space for another entry.
210 if (list->size() >= maxsilence)
212 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Oversized silence list received for %s: %s",
213 user->uuid.c_str(), value.c_str());
218 // Extract the mask and the flags.
221 if (!ts.GetToken(mask) || !ts.GetToken(flagstr))
223 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed silence list received for %s: %s",
224 user->uuid.c_str(), value.c_str());
229 // Try to parse the flags.
231 if (!SilenceEntry::FlagsToBits(flagstr, flags))
233 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Malformed silence flags received for %s: %s",
234 user->uuid.c_str(), flagstr.c_str());
239 // Store the silence entry.
240 list->insert(SilenceEntry(flags, mask));
243 // The value was well formed.
247 std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
249 SilenceList* list = static_cast<SilenceList*>(item);
251 for (SilenceList::const_iterator iter = list->begin(); iter != list->end(); ++iter)
253 if (iter != list->begin())
256 buf.append(iter->mask);
258 buf.append(SilenceEntry::BitsToFlags(iter->flags));
264 class SilenceMessage : public ClientProtocol::Message
267 SilenceMessage(const std::string& mask, const std::string& flags)
268 : ClientProtocol::Message("SILENCE")
275 class CommandSilence : public SplitCommand
278 ClientProtocol::EventProvider msgprov;
280 CmdResult AddSilence(LocalUser* user, const std::string& mask, uint32_t flags)
282 SilenceList* list = ext.get(user);
283 if (list && list->size() > ext.maxsilence)
285 user->WriteNumeric(ERR_SILELISTFULL, mask, SilenceEntry::BitsToFlags(flags), "Your SILENCE list is full");
290 // There is no list; create it.
291 list = new SilenceList();
295 if (!list->insert(SilenceEntry(flags, mask)).second)
297 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified already exists");
301 SilenceMessage msg("+" + mask, SilenceEntry::BitsToFlags(flags));
302 user->Send(msgprov, msg);
306 CmdResult RemoveSilence(LocalUser* user, const std::string& mask, uint32_t flags)
308 SilenceList* list = ext.get(user);
311 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
313 if (!irc::equals(iter->mask, mask) || iter->flags != flags)
317 SilenceMessage msg("-" + mask, SilenceEntry::BitsToFlags(flags));
318 user->Send(msgprov, msg);
323 user->WriteNumeric(ERR_SILENCE, mask, SilenceEntry::BitsToFlags(flags), "The SILENCE entry you specified could not be found");
327 CmdResult ShowSilenceList(LocalUser* user)
329 SilenceList* list = ext.get(user);
332 for (SilenceList::const_iterator iter = list->begin(); iter != list->end(); ++iter)
334 user->WriteNumeric(RPL_SILELIST, iter->mask, SilenceEntry::BitsToFlags(iter->flags));
337 user->WriteNumeric(RPL_ENDOFSILELIST, "End of SILENCE list");
344 CommandSilence(Module* Creator)
345 : SplitCommand(Creator, "SILENCE")
346 , msgprov(Creator, "SILENCE")
349 allow_empty_last_param = false;
350 syntax = "[(+|-)<mask> [CcdiNnPpTtx]]";
353 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
355 if (parameters.empty())
356 return ShowSilenceList(user);
358 // If neither add nor remove are specified we default to add.
359 bool is_remove = parameters[0][0] == '-';
361 // If a prefix mask has been given then strip it and clean it up.
362 std::string mask = parameters[0];
363 if (mask[0] == '-' || mask[0] == '+')
368 ModeParser::CleanMask(mask);
371 // If the user specified a flags then use that. Otherwise, default to blocking
372 // all CTCPs, invites, notices, privmsgs, and invites.
373 uint32_t flags = SilenceEntry::SF_DEFAULT;
374 if (parameters.size() > 1)
376 if (!SilenceEntry::FlagsToBits(parameters[1], flags))
378 user->WriteNumeric(ERR_SILENCE, mask, parameters[1], "You specified one or more invalid SILENCE flags");
381 else if (flags == SilenceEntry::SF_EXEMPT)
383 // The user specified "x" with no other flags which does not make sense; add the "d" flag.
384 flags |= SilenceEntry::SF_DEFAULT;
388 return is_remove ? RemoveSilence(user, mask, flags) : AddSilence(user, mask, flags);
394 , public CTCTags::EventListener
400 ModResult BuildChannelExempts(User* source, Channel* channel, SilenceEntry::SilenceFlags flag, CUList& exemptions)
402 const Channel::MemberMap& members = channel->GetUsers();
403 for (Channel::MemberMap::const_iterator member = members.begin(); member != members.end(); ++member)
405 if (!CanReceiveMessage(source, member->first, flag))
406 exemptions.insert(member->first);
408 return MOD_RES_PASSTHRU;
411 bool CanReceiveMessage(User* source, User* target, SilenceEntry::SilenceFlags flag)
413 // Servers handle their own clients.
414 if (!IS_LOCAL(target))
417 if (exemptuline && source->server->IsULine())
420 SilenceList* list = cmd.ext.get(target);
424 for (SilenceList::iterator iter = list->begin(); iter != list->end(); ++iter)
426 if (!(iter->flags & flag))
429 if (InspIRCd::Match(source->GetFullHost(), iter->mask))
430 return iter->flags & SilenceEntry::SF_EXEMPT;
438 : CTCTags::EventListener(this)
443 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
445 ConfigTag* tag = ServerInstance->Config->ConfValue("silence");
446 exemptuline = tag->getBool("exemptuline", true);
447 cmd.ext.maxsilence = tag->getUInt("maxentries", 32, 1);
450 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
452 tokens["ESILENCE"] = "CcdiNnPpTtx";
453 tokens["SILENCE"] = ConvToStr(cmd.ext.maxsilence);
456 ModResult OnUserPreInvite(User* source, User* dest, Channel* channel, time_t timeout) CXX11_OVERRIDE
458 return CanReceiveMessage(source, dest, SilenceEntry::SF_INVITE) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
461 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
463 std::string ctcpname;
464 bool is_ctcp = details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION");
466 SilenceEntry::SilenceFlags flag = SilenceEntry::SF_NONE;
469 case MessageTarget::TYPE_CHANNEL:
472 flag = SilenceEntry::SF_CTCP_CHANNEL;
473 else if (details.type == MSG_NOTICE)
474 flag = SilenceEntry::SF_NOTICE_CHANNEL;
475 else if (details.type == MSG_PRIVMSG)
476 flag = SilenceEntry::SF_PRIVMSG_CHANNEL;
478 return BuildChannelExempts(user, target.Get<Channel>(), flag, details.exemptions);
481 case MessageTarget::TYPE_USER:
484 flag = SilenceEntry::SF_CTCP_USER;
485 else if (details.type == MSG_NOTICE)
486 flag = SilenceEntry::SF_NOTICE_USER;
487 else if (details.type == MSG_PRIVMSG)
488 flag = SilenceEntry::SF_PRIVMSG_USER;
490 if (!CanReceiveMessage(user, target.Get<User>(), flag))
492 details.echo_original = true;
497 case MessageTarget::TYPE_SERVER:
501 return MOD_RES_PASSTHRU;
504 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
506 if (target.type == MessageTarget::TYPE_CHANNEL)
507 return BuildChannelExempts(user, target.Get<Channel>(), SilenceEntry::SF_TAGMSG_CHANNEL, details.exemptions);
509 if (target.type == MessageTarget::TYPE_USER && !CanReceiveMessage(user, target.Get<User>(), SilenceEntry::SF_TAGMSG_USER))
511 details.echo_original = true;
515 return MOD_RES_PASSTHRU;
518 Version GetVersion() CXX11_OVERRIDE
520 return Version("Adds the /SILENCE command which allows users to ignore other users on server-side.", VF_OPTCOMMON | VF_VENDOR);
524 MODULE_INIT(ModuleSilence)