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