]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
d216bd11f87a87accdccc74b8b79a9be9db40158
[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-2008 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 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> censor_t;
32
33 class ModuleCensor : public Module
34 {
35         CheckExemption::EventProvider exemptionprov;
36         censor_t 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                 int numeric = 0;
55                 switch (target.type)
56                 {
57                         case MessageTarget::TYPE_USER:
58                         {
59                                 User* targuser = target.Get<User>();
60                                 if (!targuser->IsModeSet(cu))
61                                         return MOD_RES_PASSTHRU;
62
63                                 numeric = ERR_CANTSENDTOUSER;
64                                 break;
65                         }
66
67                         case MessageTarget::TYPE_CHANNEL:
68                         {
69                                 Channel* targchan = target.Get<Channel>();
70                                 if (!targchan->IsModeSet(cc))
71                                         return MOD_RES_PASSTHRU;
72
73                                 ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
74                                 if (result == MOD_RES_ALLOW)
75                                         return MOD_RES_PASSTHRU;
76
77                                 numeric = ERR_CANNOTSENDTOCHAN;
78                                 break;
79                         }
80
81                         default:
82                                 return MOD_RES_PASSTHRU;
83                 }
84
85                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
86                 {
87                         size_t censorpos;
88                         while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
89                         {
90                                 if (index->second.empty())
91                                 {
92                                         user->WriteNumeric(numeric, target.GetName(), "Your message contained a censored word (" + index->first + "), and was blocked");
93                                         return MOD_RES_DENY;
94                                 }
95
96                                 details.text.replace(censorpos, index->first.size(), index->second);
97                         }
98                 }
99                 return MOD_RES_PASSTHRU;
100         }
101
102         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
103         {
104                 /*
105                  * reload our config file on rehash - we must destroy and re-allocate the classes
106                  * to call the constructor again and re-read our data.
107                  */
108                 censor_t newcensors;
109
110                 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
111                 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
112                 {
113                         ConfigTag* tag = i->second;
114                         const std::string text = tag->getString("text");
115                         if (text.empty())
116                                 throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
117
118                         const std::string replace = tag->getString("replace");
119                         newcensors[text] = replace;
120                 }
121                 censors.swap(newcensors);
122         }
123
124         Version GetVersion() CXX11_OVERRIDE
125         {
126                 return Version("Provides user and channel mode +G", VF_VENDOR);
127         }
128
129 };
130
131 MODULE_INIT(ModuleCensor)