2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2018 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 enum BlockAction { IBLOCK_KILL, IBLOCK_KILLOPERS, IBLOCK_NOTICE, IBLOCK_NOTICEOPERS, IBLOCK_SILENT };
28 /* IBLOCK_NOTICE - Send a notice to the user informing them of what happened.
29 * IBLOCK_NOTICEOPERS - Send a notice to the user informing them and send an oper notice.
30 * IBLOCK_SILENT - Generate no output, silently drop messages.
31 * IBLOCK_KILL - Kill the user with the reason "Global message (/amsg or /ame) detected".
32 * IBLOCK_KILLOPERS - As above, but send an oper notice as well. This is the default.
35 /** Holds a blocked message's details
44 BlockedMessage(const std::string& msg, const std::string& tgt, time_t when)
45 : message(msg), target(tgt), sent(when)
50 class ModuleBlockAmsg : public Module
52 unsigned int ForgetDelay;
54 SimpleExtItem<BlockedMessage> blockamsg;
58 : blockamsg("blockamsg", ExtensionItem::EXT_USER, this)
62 Version GetVersion() CXX11_OVERRIDE
64 return Version("Blocks mass messages sent using the /AME and /AMSG commands that exist in clients such as mIRC and HexChat.", VF_VENDOR);
67 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
69 ConfigTag* tag = ServerInstance->Config->ConfValue("blockamsg");
70 ForgetDelay = tag->getDuration("delay", 3);
71 std::string act = tag->getString("action");
73 if (stdalgo::string::equalsci(act, "notice"))
74 action = IBLOCK_NOTICE;
75 else if (stdalgo::string::equalsci(act, "noticeopers"))
76 action = IBLOCK_NOTICEOPERS;
77 else if (stdalgo::string::equalsci(act, "silent"))
78 action = IBLOCK_SILENT;
79 else if (stdalgo::string::equalsci(act, "kill"))
82 action = IBLOCK_KILLOPERS;
85 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
87 // Don't do anything with unregistered users
88 if (user->registered != REG_ALL)
89 return MOD_RES_PASSTHRU;
91 if ((validated) && (parameters.size() >= 2) && ((command == "PRIVMSG") || (command == "NOTICE")))
93 // parameters[0] is the target list, count how many channels are there
94 unsigned int targets = 0;
95 // Is the first target a channel?
96 if (*parameters[0].c_str() == '#')
99 for (const char* c = parameters[0].c_str(); *c; c++)
101 if ((*c == ',') && (*(c+1) == '#'))
105 /* targets should now contain the number of channel targets the msg/notice was pointed at.
106 * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
107 * We don't want to block PMs so...
110 return MOD_RES_PASSTHRU;
112 // Check that this message wasn't already sent within a few seconds.
113 BlockedMessage* m = blockamsg.get(user);
115 // If the message is identical and within the time.
116 // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
118 // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
119 // Check it's more than 1 too, or else users on one channel would have fun.
120 if ((m && (m->message == parameters[1]) && (!irc::equals(m->target, parameters[0])) && ForgetDelay && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == user->chans.size())))
123 if (action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
124 ServerInstance->SNO->WriteToSnoMask('a', "%s had an /amsg or /ame blocked", user->nick.c_str());
126 if (action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
127 ServerInstance->Users->QuitUser(user, "Attempted to global message (/amsg or /ame)");
128 else if (action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
129 user->WriteNotice("Global message (/amsg or /ame) blocked");
136 // If there's already a BlockedMessage allocated, use it.
137 m->message = parameters[1];
138 m->target = parameters[0];
139 m->sent = ServerInstance->Time();
143 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
144 blockamsg.set(user, m);
147 return MOD_RES_PASSTHRU;
151 MODULE_INIT(ModuleBlockAmsg)