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