]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
m_callerid, m_dccallow Use OnUserPostNick hook instead of OnUserPreNick
[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 /* $ModDesc: Attempt to block /amsg, at least some of the irritating mIRC scripts. */
27
28 enum BlockAction { IBLOCK_KILL, IBLOCK_KILLOPERS, IBLOCK_NOTICE, IBLOCK_NOTICEOPERS, IBLOCK_SILENT };
29 /*      IBLOCK_NOTICE           - Send a notice to the user informing them of what happened.
30  *      IBLOCK_NOTICEOPERS      - Send a notice to the user informing them and send an oper notice.
31  *      IBLOCK_SILENT           - Generate no output, silently drop messages.
32  *      IBLOCK_KILL                     - Kill the user with the reason "Global message (/amsg or /ame) detected".
33  *      IBLOCK_KILLOPERS        - As above, but send an oper notice as well. This is the default.
34  */
35
36 /** Holds a blocked message's details
37  */
38 class BlockedMessage
39 {
40 public:
41         std::string message;
42         irc::string target;
43         time_t sent;
44
45         BlockedMessage(const std::string &msg, const irc::string &tgt, time_t when)
46         : message(msg), target(tgt), sent(when)
47         {
48         }
49 };
50
51 class ModuleBlockAmsg : public Module
52 {
53         int ForgetDelay;
54         BlockAction action;
55         SimpleExtItem<BlockedMessage> blockamsg;
56
57  public:
58         ModuleBlockAmsg() : blockamsg("blockamsg", this)
59         {
60                 this->OnRehash(NULL);
61                 ServerInstance->Extensions.Register(&blockamsg);
62                 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
63                 ServerInstance->Modules->Attach(eventlist, this, 2);
64         }
65
66
67         virtual ~ModuleBlockAmsg()
68         {
69         }
70
71         virtual Version GetVersion()
72         {
73                 return Version("Attempt to block /amsg, at least some of the irritating mIRC scripts.",VF_VENDOR);
74         }
75
76         virtual void OnRehash(User* user)
77         {
78                 ConfigReader Conf;
79
80                 ForgetDelay = Conf.ReadInteger("blockamsg", "delay", 0, false);
81
82                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
83                         ForgetDelay = -1;
84
85                 std::string act = Conf.ReadValue("blockamsg", "action", 0);
86
87                 if(act == "notice")
88                         action = IBLOCK_NOTICE;
89                 else if(act == "noticeopers")
90                         action = IBLOCK_NOTICEOPERS;
91                 else if(act == "silent")
92                         action = IBLOCK_SILENT;
93                 else if(act == "kill")
94                         action = IBLOCK_KILL;
95                 else
96                         action = IBLOCK_KILLOPERS;
97         }
98
99         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
100         {
101                 // Don't do anything with unregistered users, or remote ones.
102                 if(!user || (user->registered != REG_ALL) || !IS_LOCAL(user))
103                         return MOD_RES_PASSTHRU;
104
105                 // We want case insensitive command comparison.
106                 // Add std::string contructor for irc::string :x
107                 irc::string cmd = command.c_str();
108
109                 if(validated && (cmd == "PRIVMSG" || cmd == "NOTICE") && (parameters.size() >= 2))
110                 {
111                         // parameters[0] should have the target(s) in it.
112                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
113                         // Most messages have a single target so...
114
115                         int targets = 1;
116                         int userchans = 0;
117
118                         if(*parameters[0].c_str() != '#')
119                         {
120                                 // Decrement if the first target wasn't a channel.
121                                 targets--;
122                         }
123
124                         for(const char* c = parameters[0].c_str(); *c; c++)
125                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
126                                         targets++;
127
128                         /* targets should now contain the number of channel targets the msg/notice was pointed at.
129                          * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
130                          * We don't want to block PMs so...
131                          */
132                         if(targets == 0)
133                         {
134                                 return MOD_RES_PASSTHRU;
135                         }
136
137                         userchans = user->chans.size();
138
139                         // Check that this message wasn't already sent within a few seconds.
140                         BlockedMessage* m = blockamsg.get(user);
141
142                         // If the message is identical and within the time.
143                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
144                         // OR
145                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
146                         // Check it's more than 1 too, or else users on one channel would have fun.
147                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
148                         {
149                                 // Block it...
150                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
151                                         ServerInstance->SNO->WriteToSnoMask('a', "%s had an /amsg or /ame denied", user->nick.c_str());
152
153                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
154                                         ServerInstance->Users->QuitUser(user, "Attempted to global message (/amsg or /ame)");
155                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
156                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) denied", user->nick.c_str());
157
158                                 return MOD_RES_DENY;
159                         }
160
161                         if(m)
162                         {
163                                 // If there's already a BlockedMessage allocated, use it.
164                                 m->message = parameters[1];
165                                 m->target = parameters[0].c_str();
166                                 m->sent = ServerInstance->Time();
167                         }
168                         else
169                         {
170                                 m = new BlockedMessage(parameters[1], parameters[0].c_str(), ServerInstance->Time());
171                                 blockamsg.set(user, m);
172                         }
173                 }
174                 return MOD_RES_PASSTHRU;
175         }
176 };
177
178
179 MODULE_INIT(ModuleBlockAmsg)