]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
Allow Timers to delete themselves in Tick()
[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         irc::string target;
41         time_t sent;
42
43         BlockedMessage(const std::string &msg, const irc::string &tgt, time_t when)
44         : message(msg), target(tgt), sent(when)
45         {
46         }
47 };
48
49 class ModuleBlockAmsg : public Module
50 {
51         int ForgetDelay;
52         BlockAction action;
53         SimpleExtItem<BlockedMessage> blockamsg;
54
55  public:
56         ModuleBlockAmsg() : blockamsg("blockamsg", this)
57         {
58         }
59
60         Version GetVersion() CXX11_OVERRIDE
61         {
62                 return Version("Attempt to block /amsg, at least some of the irritating mIRC scripts.",VF_VENDOR);
63         }
64
65         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
66         {
67                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockamsg");
68                 ForgetDelay = tag->getInt("delay", -1);
69                 std::string act = tag->getString("action");
70
71                 if(act == "notice")
72                         action = IBLOCK_NOTICE;
73                 else if(act == "noticeopers")
74                         action = IBLOCK_NOTICEOPERS;
75                 else if(act == "silent")
76                         action = IBLOCK_SILENT;
77                 else if(act == "kill")
78                         action = IBLOCK_KILL;
79                 else
80                         action = IBLOCK_KILLOPERS;
81         }
82
83         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
84         {
85                 // Don't do anything with unregistered users
86                 if (user->registered != REG_ALL)
87                         return MOD_RES_PASSTHRU;
88
89                 if ((validated) && (parameters.size() >= 2) && ((command == "PRIVMSG") || (command == "NOTICE")))
90                 {
91                         // parameters[0] should have the target(s) in it.
92                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
93                         // Most messages have a single target so...
94
95                         int targets = 1;
96                         int userchans = 0;
97
98                         if(*parameters[0].c_str() != '#')
99                         {
100                                 // Decrement if the first target wasn't a channel.
101                                 targets--;
102                         }
103
104                         for(const char* c = parameters[0].c_str(); *c; c++)
105                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
106                                         targets++;
107
108                         /* targets should now contain the number of channel targets the msg/notice was pointed at.
109                          * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
110                          * We don't want to block PMs so...
111                          */
112                         if(targets == 0)
113                         {
114                                 return MOD_RES_PASSTHRU;
115                         }
116
117                         userchans = user->chans.size();
118
119                         // Check that this message wasn't already sent within a few seconds.
120                         BlockedMessage* m = blockamsg.get(user);
121
122                         // If the message is identical and within the time.
123                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
124                         // OR
125                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
126                         // Check it's more than 1 too, or else users on one channel would have fun.
127                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
128                         {
129                                 // Block it...
130                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
131                                         ServerInstance->SNO->WriteToSnoMask('a', "%s had an /amsg or /ame denied", user->nick.c_str());
132
133                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
134                                         ServerInstance->Users->QuitUser(user, "Attempted to global message (/amsg or /ame)");
135                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
136                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) denied", user->nick.c_str());
137
138                                 return MOD_RES_DENY;
139                         }
140
141                         if(m)
142                         {
143                                 // If there's already a BlockedMessage allocated, use it.
144                                 m->message = parameters[1];
145                                 m->target = parameters[0].c_str();
146                                 m->sent = ServerInstance->Time();
147                         }
148                         else
149                         {
150                                 m = new BlockedMessage(parameters[1], parameters[0].c_str(), ServerInstance->Time());
151                                 blockamsg.set(user, m);
152                         }
153                 }
154                 return MOD_RES_PASSTHRU;
155         }
156 };
157
158 MODULE_INIT(ModuleBlockAmsg)