]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Add a helper function for calling the OnCheckExemption event.
[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<irc::string, irc::string> censor_t;
27
28 /** Handles usermode +G
29  */
30 class CensorUser : public SimpleUserModeHandler
31 {
32  public:
33         CensorUser(Module* Creator) : SimpleUserModeHandler(Creator, "u_censor", 'G') { }
34 };
35
36 /** Handles channel mode +G
37  */
38 class CensorChannel : public SimpleChannelModeHandler
39 {
40  public:
41         CensorChannel(Module* Creator) : SimpleChannelModeHandler(Creator, "censor", 'G') { }
42 };
43
44 class ModuleCensor : public Module
45 {
46         CheckExemption::EventProvider exemptionprov;
47         censor_t censors;
48         CensorUser cu;
49         CensorChannel cc;
50
51  public:
52         ModuleCensor()
53                 : exemptionprov(this)
54                 , cu(this)
55                 , cc(this)
56         {
57         }
58
59         // format of a config entry is <badword text="shit" replace="poo">
60         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
61         {
62                 if (!IS_LOCAL(user))
63                         return MOD_RES_PASSTHRU;
64
65                 bool active = false;
66
67                 if (target_type == TYPE_USER)
68                         active = ((User*)dest)->IsModeSet(cu);
69                 else if (target_type == TYPE_CHANNEL)
70                 {
71                         Channel* c = (Channel*)dest;
72                         active = c->IsModeSet(cc);
73                         ModResult res = CheckExemption::Call(exemptionprov, user, c, "censor");
74
75                         if (res == MOD_RES_ALLOW)
76                                 return MOD_RES_PASSTHRU;
77                 }
78
79                 if (!active)
80                         return MOD_RES_PASSTHRU;
81
82                 irc::string text2 = text.c_str();
83                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
84                 {
85                         if (text2.find(index->first) != irc::string::npos)
86                         {
87                                 if (index->second.empty())
88                                 {
89                                         user->WriteNumeric(ERR_WORDFILTERED, ((target_type == TYPE_CHANNEL) ? ((Channel*)dest)->name : ((User*)dest)->nick), index->first.c_str(), "Your message contained a censored word, and was blocked");
90                                         return MOD_RES_DENY;
91                                 }
92
93                                 stdalgo::string::replace_all(text2, index->first, index->second);
94                         }
95                 }
96                 text = text2.c_str();
97                 return MOD_RES_PASSTHRU;
98         }
99
100         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
101         {
102                 /*
103                  * reload our config file on rehash - we must destroy and re-allocate the classes
104                  * to call the constructor again and re-read our data.
105                  */
106                 censors.clear();
107
108                 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
109                 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
110                 {
111                         ConfigTag* tag = i->second;
112                         std::string str = tag->getString("text");
113                         irc::string pattern(str.c_str());
114                         str = tag->getString("replace");
115                         censors[pattern] = irc::string(str.c_str());
116                 }
117         }
118
119         Version GetVersion() CXX11_OVERRIDE
120         {
121                 return Version("Provides user and channel +G mode",VF_VENDOR);
122         }
123
124 };
125
126 MODULE_INIT(ModuleCensor)