]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
1724f0e5f2592331e0983d043a21e0c2b3c77fbe
[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 "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "hashcomp.h"
19
20 /* $ModDesc: Attempt to block /amsg, at least some of the irritating mIRC scripts. */
21
22 enum BlockAction { IBLOCK_KILL, IBLOCK_KILLOPERS, IBLOCK_NOTICE, IBLOCK_NOTICEOPERS, IBLOCK_SILENT };
23 /*      IBLOCK_NOTICE           - Send a notice to the user informing them of what happened.
24  *      IBLOCK_NOTICEOPERS      - Send a notice to the user informing them and send an oper notice.
25  *      IBLOCK_SILENT           - Generate no output, silently drop messages.
26  *      IBLOCK_KILL                     - Kill the user with the reason "Global message (/amsg or /ame) detected".
27  *      IBLOCK_KILLOPERS        - As above, but send an oper notice as well. This is the default.
28  */
29
30 /** Holds a blocked message's details
31  */
32 class BlockedMessage : public classbase
33 {
34 public:
35         std::string message;
36         irc::string target;
37         time_t sent;
38
39         BlockedMessage(const std::string &msg, const irc::string &tgt, time_t when)
40         : message(msg), target(tgt), sent(when)
41         {
42         }
43 };
44
45 class ModuleBlockAmsg : public Module
46 {
47         int ForgetDelay;
48         BlockAction action;
49         
50  public:
51         ModuleBlockAmsg(InspIRCd* Me)
52         : Module(Me)
53         {
54                 
55                 this->OnRehash(NULL,"");
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,1,0,0,VF_VENDOR,API_VERSION);
70         }
71         
72         virtual void OnRehash(userrec* user, const std::string &parameter)
73         {
74                 ConfigReader Conf(ServerInstance);
75                 
76                 ForgetDelay = Conf.ReadInteger("blockamsg", "delay", 0, false);
77                 
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
95         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
96         {
97                 // Don't do anything with unregistered users, or remote ones.
98                 if(!user || (user->registered != REG_ALL) || !IS_LOCAL(user))
99                         return 0;
100                         
101                 // We want case insensitive command comparison.
102                 // Add std::string contructor for irc::string :x
103                 irc::string cmd = command.c_str();
104                 
105                 if(validated && (cmd == "PRIVMSG" || cmd == "NOTICE") && (pcnt >= 2))
106                 {
107                         // parameters[0] should have the target(s) in it.
108                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
109                         // Most messages have a single target so...
110                         
111                         int targets = 1;
112                         int userchans = 0;
113                 
114                         if(*parameters[0] != '#')
115                         {
116                                 // Decrement if the first target wasn't a channel.
117                                 targets--;
118                         }
119                         
120                         for(const char* c = parameters[0]; *c; c++)
121                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
122                                         targets++;
123                                 
124                         /* targets should now contain the number of channel targets the msg/notice was pointed at.
125                          * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
126                          * We don't want to block PMs so...
127                          */
128                         if(targets == 0)
129                         {
130                                 return 0;
131                         }
132                                         
133                         userchans = user->chans.size();
134
135                         // Check that this message wasn't already sent within a few seconds.
136                         BlockedMessage* m;
137                         user->GetExt("amsgblock", m);
138                         
139                         // If the message is identical and within the time.
140                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
141                         // OR
142                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
143                         // Check it's more than 1 too, or else users on one channel would have fun.
144                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
145                         {
146                                 // Block it...
147                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
148                                         ServerInstance->WriteOpers("*** %s had an /amsg or /ame denied", user->nick);
149
150                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
151                                         userrec::QuitUser(ServerInstance, user, "Global message (/amsg or /ame) detected");
152                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
153                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) detected", user->nick);
154                                                                         
155                                 return 1;
156                         }
157                         
158                         if(m)
159                         {
160                                 // If there's already a BlockedMessage allocated, use it.
161                                 m->message = parameters[1];
162                                 m->target = parameters[0];
163                                 m->sent = ServerInstance->Time();
164                         }
165                         else
166                         {
167                                 m = new BlockedMessage(parameters[1], parameters[0], ServerInstance->Time());
168                                 user->Extend("amsgblock", (char*)m);
169                         }
170                 }                                       
171                 return 0;
172         }
173         
174         void OnCleanup(int target_type, void* item)
175         {
176                 if(target_type == TYPE_USER)
177                 {
178                         userrec* user = (userrec*)item;
179                         BlockedMessage* m;
180                         user->GetExt("amsgblock", m);
181                         if(m)
182                         {
183                                 DELETE(m);
184                                 user->Shrink("amsgblock");
185                         }
186                 }
187         }
188 };
189
190
191 class ModuleBlockAmsgFactory : public ModuleFactory
192 {
193         public:
194                 ModuleBlockAmsgFactory()
195                 {
196                 }
197         
198                 ~ModuleBlockAmsgFactory()
199                 {
200                 }
201         
202                 virtual Module * CreateModule(InspIRCd* Me)
203                 {
204                         return new ModuleBlockAmsg(Me);
205                 }
206 };
207
208
209 extern "C" DllExport void * init_module( void )
210 {
211         return new ModuleBlockAmsgFactory;
212 }