]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockamsg.cpp
fixed some indentation and spacing in modules
[user/henk/code/inspircd.git] / src / modules / m_blockamsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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) : Module(Me)
48         {
49                 this->OnRehash(NULL,"");
50                 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand, I_OnCleanup };
51                 ServerInstance->Modules->Attach(eventlist, this, 3);
52         }
53
54
55         virtual ~ModuleBlockAmsg()
56         {
57         }
58
59         virtual Version GetVersion()
60         {
61                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
62         }
63
64         virtual void OnRehash(User* user, const std::string &parameter)
65         {
66                 ConfigReader Conf(ServerInstance);
67
68                 ForgetDelay = Conf.ReadInteger("blockamsg", "delay", 0, false);
69
70                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
71                         ForgetDelay = -1;
72
73                 std::string act = Conf.ReadValue("blockamsg", "action", 0);
74
75                 if(act == "notice")
76                         action = IBLOCK_NOTICE;
77                 else if(act == "noticeopers")
78                         action = IBLOCK_NOTICEOPERS;
79                 else if(act == "silent")
80                         action = IBLOCK_SILENT;
81                 else if(act == "kill")
82                         action = IBLOCK_KILL;
83                 else
84                         action = IBLOCK_KILLOPERS;
85         }
86
87         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
88         {
89                 // Don't do anything with unregistered users, or remote ones.
90                 if(!user || (user->registered != REG_ALL) || !IS_LOCAL(user))
91                         return 0;
92
93                 // We want case insensitive command comparison.
94                 // Add std::string contructor for irc::string :x
95                 irc::string cmd = command.c_str();
96
97                 if(validated && (cmd == "PRIVMSG" || cmd == "NOTICE") && (parameters.size() >= 2))
98                 {
99                         // parameters[0] should have the target(s) in it.
100                         // I think it will be faster to first check if there are any commas, and if there are then try and parse it out.
101                         // Most messages have a single target so...
102
103                         int targets = 1;
104                         int userchans = 0;
105
106                         if(*parameters[0].c_str() != '#')
107                         {
108                                 // Decrement if the first target wasn't a channel.
109                                 targets--;
110                         }
111
112                         for(const char* c = parameters[0].c_str(); *c; c++)
113                                 if((*c == ',') && *(c+1) && (*(c+1) == '#'))
114                                         targets++;
115
116                         /* targets should now contain the number of channel targets the msg/notice was pointed at.
117                          * If the msg/notice was a PM there should be no channel targets and 'targets' should = 0.
118                          * We don't want to block PMs so...
119                          */
120                         if(targets == 0)
121                         {
122                                 return 0;
123                         }
124
125                         userchans = user->chans.size();
126
127                         // Check that this message wasn't already sent within a few seconds.
128                         BlockedMessage* m;
129                         user->GetExt("amsgblock", m);
130
131                         // If the message is identical and within the time.
132                         // We check the target is *not* identical, that'd straying into the realms of flood control. Which isn't what we're doing...
133                         // OR
134                         // The number of target channels is equal to the number of channels the sender is on..a little suspicious.
135                         // Check it's more than 1 too, or else users on one channel would have fun.
136                         if((m && (m->message == parameters[1]) && (m->target != parameters[0]) && (ForgetDelay != -1) && (m->sent >= ServerInstance->Time()-ForgetDelay)) || ((targets > 1) && (targets == userchans)))
137                         {
138                                 // Block it...
139                                 if(action == IBLOCK_KILLOPERS || action == IBLOCK_NOTICEOPERS)
140                                         ServerInstance->SNO->WriteToSnoMask('A', "%s had an /amsg or /ame denied", user->nick.c_str());
141
142                                 if(action == IBLOCK_KILL || action == IBLOCK_KILLOPERS)
143                                         ServerInstance->Users->QuitUser(user, "Global message (/amsg or /ame) detected");
144                                 else if(action == IBLOCK_NOTICE || action == IBLOCK_NOTICEOPERS)
145                                         user->WriteServ( "NOTICE %s :Global message (/amsg or /ame) detected", user->nick.c_str());
146
147                                 return 1;
148                         }
149
150                         if(m)
151                         {
152                                 // If there's already a BlockedMessage allocated, use it.
153                                 m->message = parameters[1];
154                                 m->target = parameters[0].c_str();
155                                 m->sent = ServerInstance->Time();
156                         }
157                         else
158                         {
159                                 m = new BlockedMessage(parameters[1], parameters[0].c_str(), ServerInstance->Time());
160                                 user->Extend("amsgblock", (char*)m);
161                         }
162                 }
163                 return 0;
164         }
165
166         void OnCleanup(int target_type, void* item)
167         {
168                 if(target_type == TYPE_USER)
169                 {
170                         User* user = (User*)item;
171                         BlockedMessage* m;
172                         user->GetExt("amsgblock", m);
173                         if(m)
174                         {
175                                 delete m;
176                                 user->Shrink("amsgblock");
177                         }
178                 }
179         }
180 };
181
182
183 MODULE_INIT(ModuleBlockAmsg)