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