]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
Const refs
[user/henk/code/inspircd.git] / src / modules / m_blockamsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *                                              <omster@gmail.com>
10  *     
11  * Written by Craig Edwards, Craig McLure, and others.
12  * This program is free but copyrighted software; see
13  *            the file COPYING for details.
14  *
15  * ---------------------------------------------------
16  */
17  
18 /* <3's to Lauren for original info on mIRC's weird behaviour */
19
20 #include <string>
21 #include <time.h>
22 #include "inspircd_config.h"
23 #include "users.h"
24 #include "channels.h"
25 #include "modules.h"
26
27 #include "hashcomp.h"
28 #include "inspircd.h"
29
30 /* $ModDesc: Attempt to block /amsg, at least some of the irritating mIRC scripts. */
31
32 enum BlockAction { IBLOCK_KILL, IBLOCK_KILLOPERS, IBLOCK_NOTICE, IBLOCK_NOTICEOPERS, IBLOCK_SILENT };
33
34 /** Holds a blocked message's details
35  */
36 class BlockedMessage : public classbase
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) : message(msg), target(tgt), sent(when)
44         {
45         }
46 };
47
48 class ModuleBlockAmsg : public Module
49 {
50         
51         int ForgetDelay;
52         BlockAction action;
53 public:
54         ModuleBlockAmsg(InspIRCd* Me) : Module::Module(Me)
55         {
56                 
57                 this->OnRehash("");
58         }
59
60         void Implements(char* List)
61         {
62                 List[I_OnRehash] = List[I_OnPreCommand] = List[I_OnCleanup] = 1;
63         }
64         
65         virtual ~ModuleBlockAmsg()
66         {
67         }
68         
69         virtual Version GetVersion()
70         {
71                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
72         }
73         
74         virtual void OnRehash(const std::string &parameter)
75         {
76                 ConfigReader* Conf = new ConfigReader(ServerInstance);
77                 
78                 ForgetDelay = Conf->ReadInteger("blockamsg", "delay", 0, false);
79                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
80                         ForgetDelay = -1;
81                         
82                 std::string act = Conf->ReadValue("blockamsg", "action", 0);
83                 
84                 if(act == "notice")
85                         action = IBLOCK_NOTICE;
86                 else if(act == "noticeopers")
87                         action = IBLOCK_NOTICEOPERS;
88                 else if(act == "silent")
89                         action = IBLOCK_SILENT;
90                 else if(act == "kill")
91                         action = IBLOCK_KILL;
92                 else
93                         action = IBLOCK_KILLOPERS;
94
95                 DELETE(Conf);
96         }
97
98         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
99         {
100                 // Don't do anything with unregistered users, or remote ones.
101                 if(!user || (user->registered != REG_ALL) || !IS_LOCAL(user))
102                         return 0;
103                         
104                 // We want case insensitive command comparison.
105                 // Add std::string contructor for irc::string :x
106                 irc::string cmd = command.c_str();
107                 
108                 if(validated && (cmd == "PRIVMSG" || cmd == "NOTICE") && (pcnt >= 2))
109                 {
110                         // parameters[0] should have the target(s) in it.
111                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
112                         // Most messages have a single target so...
113                         
114                         int targets = 1;
115                         int userchans = 0;
116                         
117                         // Decrement if the first target wasn't a channel.
118                         if(*parameters[0] != '#')
119                                 targets--;
120                         
121                         for(const char* c = parameters[0]; *c; c++)
122                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
123                                         targets++;
124                                         
125                         for(std::vector<ucrec*>::iterator f = user->chans.begin(); f != user->chans.end(); f++)
126                                 if(((ucrec*)(*f))->channel)
127                                         userchans++;
128
129                         // Check that this message wasn't already sent within a few seconds.
130                         BlockedMessage* m;
131                         user->GetExt("amsgblock", m);
132                         
133                         // If the message is identical and within the time.
134                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
135                         // OR
136                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
137                         // Check it's more than 1 too, or else users on one channel would have fun.
138                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
139                         {
140                                 // Block it...
141                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
142                                         ServerInstance->WriteOpers("*** %s had an /amsg or /ame denied", user->nick);
143
144                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
145                                         userrec::QuitUser(ServerInstance, user, "Global message (/amsg or /ame) detected");
146                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
147                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) detected", user->nick);
148                                                                         
149                                 return 1;
150                         }
151                         
152                         if(m)
153                         {
154                                 // If there's already a BlockedMessage allocated, use it.
155                                 m->message = parameters[1];
156                                 m->target = parameters[0];
157                                 m->sent = ServerInstance->Time();
158                         }
159                         else
160                         {
161                                 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
162                                 user->Extend("amsgblock", (char*)m);
163                         }
164                 }                                       
165                 return 0;
166         }
167         
168         void OnCleanup(int target_type, void* item)
169         {
170                 if(target_type == TYPE_USER)
171                 {
172                         userrec* user = (userrec*)item;
173                         BlockedMessage* m;
174                         user->GetExt("amsgblock", m);
175                         if(m)
176                         {
177                                 DELETE(m);
178                                 user->Shrink("amsgblock");
179                         }
180                 }
181         }
182 };
183
184
185 class ModuleBlockAmsgFactory : public ModuleFactory
186 {
187         public:
188                 ModuleBlockAmsgFactory()
189                 {
190                 }
191         
192                 ~ModuleBlockAmsgFactory()
193                 {
194                 }
195         
196                 virtual Module * CreateModule(InspIRCd* Me)
197                 {
198                         return new ModuleBlockAmsg(Me);
199                 }
200 };
201
202
203 extern "C" void * init_module( void )
204 {
205         return new ModuleBlockAmsgFactory;
206 }