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