]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
Just to mess with om's head, remove helperfuncs.h from everywhere
[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 class BlockedMessage : public classbase
35 {
36 public:
37         std::string message;
38         irc::string target;
39         time_t sent;
40         
41         BlockedMessage(std::string msg, irc::string tgt, time_t when) : message(msg), target(tgt), sent(when)
42         {
43         }
44 };
45
46 class ModuleBlockAmsg : public Module
47 {
48         
49         int ForgetDelay;
50         BlockAction action;
51 public:
52         ModuleBlockAmsg(InspIRCd* Me) : Module::Module(Me)
53         {
54                 
55                 this->OnRehash("");
56         }
57
58         void Implements(char* List)
59         {
60                 List[I_OnRehash] = List[I_OnPreCommand] = List[I_OnCleanup] = 1;
61         }
62         
63         virtual ~ModuleBlockAmsg()
64         {
65         }
66         
67         virtual Version GetVersion()
68         {
69                 return Version(1,0,0,0,VF_VENDOR);
70         }
71         
72         virtual void OnRehash(const std::string &parameter)
73         {
74                 ConfigReader* Conf = new ConfigReader(ServerInstance);
75                 
76                 ForgetDelay = Conf->ReadInteger("blockamsg", "delay", 0, false);
77                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
78                         ForgetDelay = -1;
79                         
80                 std::string act = Conf->ReadValue("blockamsg", "action", 0);
81                 
82                 if(act == "notice")
83                         action = IBLOCK_NOTICE;
84                 else if(act == "noticeopers")
85                         action = IBLOCK_NOTICEOPERS;
86                 else if(act == "silent")
87                         action = IBLOCK_SILENT;
88                 else if(act == "kill")
89                         action = IBLOCK_KILL;
90                 else
91                         action = IBLOCK_KILLOPERS;
92
93                 DELETE(Conf);
94         }
95
96         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
97         {
98                 // Don't do anything with unregistered users, or remote ones.
99                 if(!user || (user->registered != REG_ALL) || !IS_LOCAL(user))
100                         return 0;
101                         
102                 // We want case insensitive command comparison.
103                 // Add std::string contructor for irc::string :x
104                 irc::string cmd = command.c_str();
105                 
106                 if(validated && (cmd == "PRIVMSG" || cmd == "NOTICE") && (pcnt >= 2))
107                 {
108                         // parameters[0] should have the target(s) in it.
109                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
110                         // Most messages have a single target so...
111                         
112                         int targets = 1;
113                         int userchans = 0;
114                         
115                         // Decrement if the first target wasn't a channel.
116                         if(*parameters[0] != '#')
117                                 targets--;
118                         
119                         for(const char* c = parameters[0]; *c; c++)
120                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
121                                         targets++;
122                                         
123                         for(std::vector<ucrec*>::iterator f = user->chans.begin(); f != user->chans.end(); f++)
124                                 if(((ucrec*)(*f))->channel)
125                                         userchans++;
126
127                         // Check that this message wasn't already sent within a few seconds.
128                         BlockedMessage* m;
129                         user->GetExt("amsgblock", m);
130                         
131                         // If the message is identical and within the time.
132                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
133                         // OR
134                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
135                         // Check it's more than 1 too, or else users on one channel would have fun.
136                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
137                         {
138                                 // Block it...
139                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
140                                         ServerInstance->WriteOpers("*** %s had an /amsg or /ame denied", user->nick);
141
142                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
143                                         userrec::QuitUser(ServerInstance, user, "Global message (/amsg or /ame) detected");
144                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
145                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) detected", user->nick);
146                                                                         
147                                 return 1;
148                         }
149                         
150                         if(m)
151                         {
152                                 // If there's already a BlockedMessage allocated, use it.
153                                 m->message = parameters[1];
154                                 m->target = parameters[0];
155                                 m->sent = ServerInstance->Time();
156                         }
157                         else
158                         {
159                                 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
160                                 user->Extend("amsgblock", (char*)m);
161                         }
162                 }                                       
163                 return 0;
164         }
165         
166         void OnCleanup(int target_type, void* item)
167         {
168                 if(target_type == TYPE_USER)
169                 {
170                         userrec* user = (userrec*)item;
171                         BlockedMessage* m;
172                         user->GetExt("amsgblock", m);
173                         if(m)
174                         {
175                                 DELETE(m);
176                                 user->Shrink("amsgblock");
177                         }
178                 }
179         }
180 };
181
182
183 class ModuleBlockAmsgFactory : public ModuleFactory
184 {
185         public:
186                 ModuleBlockAmsgFactory()
187                 {
188                 }
189         
190                 ~ModuleBlockAmsgFactory()
191                 {
192                 }
193         
194                 virtual Module * CreateModule(InspIRCd* Me)
195                 {
196                         return new ModuleBlockAmsg(Me);
197                 }
198 };
199
200
201 extern "C" void * init_module( void )
202 {
203         return new ModuleBlockAmsgFactory;
204 }