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