]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Some more text fixes and improvements (#1618).
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2004, 2008-2009 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005, 2007 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/exemption.h"
25
26 typedef insp::flat_map<std::string, std::string, irc::insensitive_swo> censor_t;
27
28 class ModuleCensor : public Module
29 {
30         CheckExemption::EventProvider exemptionprov;
31         censor_t censors;
32         SimpleUserModeHandler cu;
33         SimpleChannelModeHandler cc;
34
35  public:
36         ModuleCensor()
37                 : exemptionprov(this)
38                 , cu(this, "u_censor", 'G')
39                 , cc(this, "censor", 'G')
40         {
41         }
42
43         // format of a config entry is <badword text="shit" replace="poo">
44         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
45         {
46                 if (!IS_LOCAL(user))
47                         return MOD_RES_PASSTHRU;
48
49                 int numeric = 0;
50                 const char* targetname = NULL;
51
52                 switch (target.type)
53                 {
54                         case MessageTarget::TYPE_USER:
55                         {
56                                 User* targuser = target.Get<User>();
57                                 if (!targuser->IsModeSet(cu))
58                                         return MOD_RES_PASSTHRU;
59
60                                 numeric = ERR_CANTSENDTOUSER;
61                                 targetname = targuser->nick.c_str();
62                                 break;
63                         }
64
65                         case MessageTarget::TYPE_CHANNEL:
66                         {
67                                 Channel* targchan = target.Get<Channel>();
68                                 if (!targchan->IsModeSet(cc))
69                                         return MOD_RES_PASSTHRU;
70
71                                 ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
72                                 if (result == MOD_RES_ALLOW)
73                                         return MOD_RES_PASSTHRU;
74
75                                 numeric = ERR_CANNOTSENDTOCHAN;
76                                 targetname = targchan->name.c_str();
77                                 break;
78                         }
79
80                         default:
81                                 return MOD_RES_PASSTHRU;
82                 }
83
84                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
85                 {
86                         size_t censorpos;
87                         while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
88                         {
89                                 if (index->second.empty())
90                                 {
91                                         user->WriteNumeric(numeric, targetname, "Your message contained a censored word (" + index->first + "), and was blocked");
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                 censor_t newcensors;
108
109                 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
110                 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
111                 {
112                         ConfigTag* tag = i->second;
113                         const std::string text = tag->getString("text");
114                         if (text.empty())
115                                 throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
116
117                         const std::string replace = tag->getString("replace");
118                         newcensors[text] = replace;
119                 }
120                 censors.swap(newcensors);
121         }
122
123         Version GetVersion() CXX11_OVERRIDE
124         {
125                 return Version("Provides user and channel mode +G", VF_VENDOR);
126         }
127
128 };
129
130 MODULE_INIT(ModuleCensor)