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