]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_botmode.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_botmode.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012, 2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2004, 2010 Craig Edwards <brain@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/ctctags.h"
28 #include "modules/who.h"
29 #include "modules/whois.h"
30
31 enum
32 {
33         // From UnrealIRCd.
34         RPL_WHOISBOT = 335
35 };
36
37 class BotTag : public ClientProtocol::MessageTagProvider
38 {
39  private:
40         SimpleUserModeHandler& botmode;
41         CTCTags::CapReference ctctagcap;
42
43  public:
44         BotTag(Module* mod, SimpleUserModeHandler& bm)
45                 : ClientProtocol::MessageTagProvider(mod)
46                 , botmode(bm)
47                 , ctctagcap(mod)
48         {
49         }
50
51         void OnPopulateTags(ClientProtocol::Message& msg) CXX11_OVERRIDE
52         {
53                 User* const user = msg.GetSourceUser();
54                 if (user && user->IsModeSet(botmode))
55                         msg.AddTag("inspircd.org/bot", this, "");
56         }
57
58         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
59         {
60                 return ctctagcap.get(user);
61         }
62 };
63
64 class ModuleBotMode
65         : public Module
66         , public Who::EventListener
67         , public Whois::EventListener
68 {
69  private:
70         SimpleUserModeHandler bm;
71         BotTag tag;
72         bool forcenotice;
73
74  public:
75         ModuleBotMode()
76                 : Who::EventListener(this)
77                 , Whois::EventListener(this)
78                 , bm(this, "bot", 'B')
79                 , tag(this, bm)
80         {
81         }
82
83         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
84         {
85                 forcenotice = ServerInstance->Config->ConfValue("botmode")->getBool("forcenotice");
86         }
87
88         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
89         {
90                 tokens["BOT"] = ConvToStr(bm.GetModeChar());
91         }
92
93         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
94         {
95                 // Allow sending if forcenotice is off, the user is not a bot, or if the message is a notice.
96                 if (!forcenotice || !user->IsModeSet(bm) || details.type == MSG_NOTICE)
97                         return MOD_RES_PASSTHRU;
98
99                 // Allow sending PRIVMSGs to services pseudoclients.
100                 if (target.type == MessageTarget::TYPE_USER && target.Get<User>()->server->IsULine())
101                         return MOD_RES_PASSTHRU;
102
103                 // Force the message to be broadcast as a NOTICE.
104                 details.type = MSG_NOTICE;
105                 return MOD_RES_PASSTHRU;
106         }
107
108         ModResult OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE
109         {
110                 size_t flag_index;
111                 if (!request.GetFieldIndex('f', flag_index))
112                         return MOD_RES_PASSTHRU;
113
114                 if (user->IsModeSet(bm))
115                         numeric.GetParams()[flag_index].push_back('B');
116
117                 return MOD_RES_PASSTHRU;
118         }
119
120         Version GetVersion() CXX11_OVERRIDE
121         {
122                 return Version("Adds user mode B (bot) which marks users with it set as bots in their /WHOIS response.",VF_VENDOR);
123         }
124
125         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
126         {
127                 if (whois.GetTarget()->IsModeSet(bm))
128                 {
129                         whois.SendLine(RPL_WHOISBOT, "is a bot on " + ServerInstance->Config->Network);
130                 }
131         }
132 };
133
134 MODULE_INIT(ModuleBotMode)