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