]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
5ab627c71bedc2964705dfd4a9f4f0c79039b027
[user/henk/code/inspircd.git] / src / modules / m_blockamsg.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8  *   Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 enum BlockAction { IBLOCK_KILL, IBLOCK_KILLOPERS, IBLOCK_NOTICE, IBLOCK_NOTICEOPERS, IBLOCK_SILENT };
27 /*      IBLOCK_NOTICE           - Send a notice to the user informing them of what happened.
28  *      IBLOCK_NOTICEOPERS      - Send a notice to the user informing them and send an oper notice.
29  *      IBLOCK_SILENT           - Generate no output, silently drop messages.
30  *      IBLOCK_KILL                     - Kill the user with the reason "Global message (/amsg or /ame) detected".
31  *      IBLOCK_KILLOPERS        - As above, but send an oper notice as well. This is the default.
32  */
33
34 /** Holds a blocked message's details
35  */
36 class BlockedMessage
37 {
38  public:
39         std::string message;
40         std::string target;
41         time_t sent;
42
43         BlockedMessage(const std::string& msg, const std::string& tgt, time_t when)
44                 : message(msg), target(tgt), sent(when)
45         {
46         }
47 };
48
49 class ModuleBlockAmsg : public Module
50 {
51         unsigned int ForgetDelay;
52         BlockAction action;
53         SimpleExtItem<BlockedMessage> blockamsg;
54
55  public:
56         ModuleBlockAmsg()
57                 : blockamsg("blockamsg", ExtensionItem::EXT_USER, this)
58         {
59         }
60
61         Version GetVersion() CXX11_OVERRIDE
62         {
63                 return Version("Attempt to block /amsg or /ame, at least some of the irritating client scripts", VF_VENDOR);
64         }
65
66         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
67         {
68                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockamsg");
69                 ForgetDelay = tag->getDuration("delay", 3);
70                 std::string act = tag->getString("action");
71
72                 if (stdalgo::string::equalsci(act, "notice"))
73                         action = IBLOCK_NOTICE;
74                 else if (stdalgo::string::equalsci(act, "noticeopers"))
75                         action = IBLOCK_NOTICEOPERS;
76                 else if (stdalgo::string::equalsci(act, "silent"))
77                         action = IBLOCK_SILENT;
78                 else if (stdalgo::string::equalsci(act, "kill"))
79                         action = IBLOCK_KILL;
80                 else
81                         action = IBLOCK_KILLOPERS;
82         }
83
84         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
85         {
86                 // Don't do anything with unregistered users
87                 if (user->registered != REG_ALL)
88                         return MOD_RES_PASSTHRU;
89
90                 if ((validated) && (parameters.size() >= 2) && ((command == "PRIVMSG") || (command == "NOTICE")))
91                 {
92                         // parameters[0] is the target list, count how many channels are there
93                         unsigned int targets = 0;
94                         // Is the first target a channel?
95                         if (*parameters[0].c_str() == '#')
96                                 targets = 1;
97
98                         for (const char* c = parameters[0].c_str(); *c; c++)
99                         {
100                                 if ((*c == ',') && (*(c+1) == '#'))
101                                         targets++;
102                         }
103
104                         /* targets should now contain the number of channel targets the msg/notice was pointed at.
105                          * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
106                          * We don't want to block PMs so...
107                          */
108                         if (targets == 0)
109                                 return MOD_RES_PASSTHRU;
110
111                         // Check that this message wasn't already sent within a few seconds.
112                         BlockedMessage* m = blockamsg.get(user);
113
114                         // If the message is identical and within the time.
115                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
116                         // OR
117                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
118                         // Check it's more than 1 too, or else users on one channel would have fun.
119                         if ((m && (m->message == parameters[1]) && (!irc::equals(m->target, parameters[0])) && ForgetDelay && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == user->chans.size())))
120                         {
121                                 // Block it...
122                                 if (action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
123                                         ServerInstance->SNO->WriteToSnoMask('a', "%s had an /amsg or /ame blocked", user->nick.c_str());
124
125                                 if (action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
126                                         ServerInstance->Users->QuitUser(user, "Attempted to global message (/amsg or /ame)");
127                                 else if (action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
128                                         user->WriteNotice("Global message (/amsg or /ame) blocked");
129
130                                 return MOD_RES_DENY;
131                         }
132
133                         if (m)
134                         {
135                                 // If there's already a BlockedMessage allocated, use it.
136                                 m->message = parameters[1];
137                                 m->target = parameters[0];
138                                 m->sent = ServerInstance->Time();
139                         }
140                         else
141                         {
142                                 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
143                                 blockamsg.set(user, m);
144                         }
145                 }
146                 return MOD_RES_PASSTHRU;
147         }
148 };
149
150 MODULE_INIT(ModuleBlockAmsg)