]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
9c41403115b0f46da0905c500f8a566e846427f7
[user/henk/code/inspircd.git] / src / modules / m_blockamsg.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
10  *
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.
14  *
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
18  * details.
19  *
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/>.
22  */
23
24
25 #include "inspircd.h"
26
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.
33  */
34
35 /** Holds a blocked message's details
36  */
37 class BlockedMessage
38 {
39  public:
40         std::string message;
41         std::string target;
42         time_t sent;
43
44         BlockedMessage(const std::string& msg, const std::string& tgt, time_t when)
45                 : message(msg), target(tgt), sent(when)
46         {
47         }
48 };
49
50 class ModuleBlockAmsg : public Module
51 {
52         unsigned int ForgetDelay;
53         BlockAction action;
54         SimpleExtItem<BlockedMessage> blockamsg;
55
56  public:
57         ModuleBlockAmsg()
58                 : blockamsg("blockamsg", ExtensionItem::EXT_USER, this)
59         {
60         }
61
62         Version GetVersion() CXX11_OVERRIDE
63         {
64                 return Version("Blocks mass messages sent using the /AME and /AMSG commands that exist in clients such as mIRC and HexChat.", VF_VENDOR);
65         }
66
67         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
68         {
69                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockamsg");
70                 ForgetDelay = tag->getDuration("delay", 3);
71                 std::string act = tag->getString("action");
72
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"))
80                         action = IBLOCK_KILL;
81                 else
82                         action = IBLOCK_KILLOPERS;
83         }
84
85         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
86         {
87                 // Don't do anything with unregistered users
88                 if (user->registered != REG_ALL)
89                         return MOD_RES_PASSTHRU;
90
91                 if ((validated) && (parameters.size() >= 2) && ((command == "PRIVMSG") || (command == "NOTICE")))
92                 {
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() == '#')
97                                 targets = 1;
98
99                         for (const char* c = parameters[0].c_str(); *c; c++)
100                         {
101                                 if ((*c == ',') && (*(c+1) == '#'))
102                                         targets++;
103                         }
104
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...
108                          */
109                         if (targets == 0)
110                                 return MOD_RES_PASSTHRU;
111
112                         // Check that this message wasn't already sent within a few seconds.
113                         BlockedMessage* m = blockamsg.get(user);
114
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...
117                         // OR
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())))
121                         {
122                                 // Block it...
123                                 if (action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
124                                         ServerInstance->SNO->WriteToSnoMask('a', "%s had an /amsg or /ame blocked", user->nick.c_str());
125
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");
130
131                                 return MOD_RES_DENY;
132                         }
133
134                         if (m)
135                         {
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();
140                         }
141                         else
142                         {
143                                 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
144                                 blockamsg.set(user, m);
145                         }
146                 }
147                 return MOD_RES_PASSTHRU;
148         }
149 };
150
151 MODULE_INIT(ModuleBlockAmsg)