]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
b78364b0b1d5a190372c0f7d42c5ee9899f912b2
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2005, 2007-2008 Robin Burchell <robin+git@viroteck.net>
12  *   Copyright (C) 2004, 2006, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29 #include "modules/exemption.h"
30
31 typedef insp::flat_map<std::string, std::string, irc::insensitive_swo> CensorMap;
32
33 class ModuleCensor : public Module
34 {
35         CheckExemption::EventProvider exemptionprov;
36         CensorMap censors;
37         SimpleUserModeHandler cu;
38         SimpleChannelModeHandler cc;
39
40  public:
41         ModuleCensor()
42                 : exemptionprov(this)
43                 , cu(this, "u_censor", 'G')
44                 , cc(this, "censor", 'G')
45         {
46         }
47
48         // format of a config entry is <badword text="shit" replace="poo">
49         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
50         {
51                 if (!IS_LOCAL(user))
52                         return MOD_RES_PASSTHRU;
53
54                 switch (target.type)
55                 {
56                         case MessageTarget::TYPE_USER:
57                         {
58                                 User* targuser = target.Get<User>();
59                                 if (!targuser->IsModeSet(cu))
60                                         return MOD_RES_PASSTHRU;
61                                 break;
62                         }
63
64                         case MessageTarget::TYPE_CHANNEL:
65                         {
66                                 Channel* targchan = target.Get<Channel>();
67                                 if (!targchan->IsModeSet(cc))
68                                         return MOD_RES_PASSTHRU;
69
70                                 ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
71                                 if (result == MOD_RES_ALLOW)
72                                         return MOD_RES_PASSTHRU;
73                                 break;
74                         }
75
76                         default:
77                                 return MOD_RES_PASSTHRU;
78                 }
79
80                 for (CensorMap::const_iterator index = censors.begin(); index != censors.end(); ++index)
81                 {
82                         size_t censorpos;
83                         while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
84                         {
85                                 if (index->second.empty())
86                                 {
87                                         const std::string msg = InspIRCd::Format("Your message to this channel contained a banned phrase (%s) and was blocked.", index->first.c_str());
88                                         if (target.type == MessageTarget::TYPE_CHANNEL)
89                                                 user->WriteNumeric(Numerics::CannotSendTo(target.Get<Channel>(), msg));
90                                         else
91                                                 user->WriteNumeric(Numerics::CannotSendTo(target.Get<User>(), msg));
92                                         return MOD_RES_DENY;
93                                 }
94
95                                 details.text.replace(censorpos, index->first.size(), index->second);
96                         }
97                 }
98                 return MOD_RES_PASSTHRU;
99         }
100
101         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
102         {
103                 /*
104                  * reload our config file on rehash - we must destroy and re-allocate the classes
105                  * to call the constructor again and re-read our data.
106                  */
107                 CensorMap newcensors;
108                 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
109                 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
110                 {
111                         ConfigTag* tag = i->second;
112                         const std::string text = tag->getString("text");
113                         if (text.empty())
114                                 throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
115
116                         const std::string replace = tag->getString("replace");
117                         newcensors[text] = replace;
118                 }
119                 censors.swap(newcensors);
120         }
121
122         Version GetVersion() CXX11_OVERRIDE
123         {
124                 return Version("Allows the server administrator to define inappropriate phrases that are not allowed to be used in private or channel messages.", VF_VENDOR);
125         }
126
127 };
128
129 MODULE_INIT(ModuleCensor)