]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
Use ERR_BANLISTFULL in the chanfilter and exemptchanops modules.
[user/henk/code/inspircd.git] / src / modules / m_chanfilter.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *   Copyright (C) 2005 Craig McLure <craig@chatspike.net>
10  *   Copyright (C) 2005 Craig Edwards <craigedwards@brainbox.cc>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "listmode.h"
28 #include "modules/exemption.h"
29
30 enum
31 {
32         // InspIRCd-specific.
33         ERR_ALREADYCHANFILTERED = 937,
34         ERR_NOSUCHCHANFILTER = 938
35 };
36
37 /** Handles channel mode +g
38  */
39 class ChanFilter : public ListModeBase
40 {
41  public:
42         unsigned long maxlen;
43
44         ChanFilter(Module* Creator) : ListModeBase(Creator, "filter", 'g', "End of channel spamfilter list", 941, 940, false, "chanfilter") { }
45
46         bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
47         {
48                 if (word.length() > maxlen)
49                 {
50                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Word is too long for the spamfilter list"));
51                         return false;
52                 }
53
54                 return true;
55         }
56
57         void TellAlreadyOnList(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
58         {
59                 user->WriteNumeric(ERR_ALREADYCHANFILTERED, chan->name, InspIRCd::Format("The word %s is already on the spamfilter list", word.c_str()));
60         }
61
62         void TellNotSet(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
63         {
64                 user->WriteNumeric(ERR_NOSUCHCHANFILTER, chan->name, "No such spamfilter word is set");
65         }
66 };
67
68 class ModuleChanFilter : public Module
69 {
70         CheckExemption::EventProvider exemptionprov;
71         ChanFilter cf;
72         bool hidemask;
73         bool notifyuser;
74
75  public:
76
77         ModuleChanFilter()
78                 : exemptionprov(this)
79                 , cf(this)
80         {
81         }
82
83         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
84         {
85                 ConfigTag* tag = ServerInstance->Config->ConfValue("chanfilter");
86                 hidemask = tag->getBool("hidemask");
87                 cf.maxlen = tag->getUInt("maxlen", 35, 10, 100);
88                 notifyuser = tag->getBool("notifyuser", true);
89                 cf.DoRehash();
90         }
91
92         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
93         {
94                 if (target.type != MessageTarget::TYPE_CHANNEL)
95                         return MOD_RES_PASSTHRU;
96
97                 Channel* chan = target.Get<Channel>();
98                 ModResult res = CheckExemption::Call(exemptionprov, user, chan, "filter");
99
100                 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
101                         return MOD_RES_PASSTHRU;
102
103                 ListModeBase::ModeList* list = cf.GetList(chan);
104
105                 if (list)
106                 {
107                         for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); i++)
108                         {
109                                 if (InspIRCd::Match(details.text, i->mask))
110                                 {
111                                         if (!notifyuser)
112                                         {
113                                                 details.echo_original = true;
114                                                 return MOD_RES_DENY;
115                                         }
116
117                                         if (hidemask)
118                                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (your message contained a censored word)");
119                                         else
120                                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (your message contained a censored word: " + i->mask + ")");
121                                         return MOD_RES_DENY;
122                                 }
123                         }
124                 }
125
126                 return MOD_RES_PASSTHRU;
127         }
128
129         Version GetVersion() CXX11_OVERRIDE
130         {
131                 // We don't send any link data if the length is 35 for compatibility with the 2.0 branch.
132                 std::string maxfilterlen;
133                 if (cf.maxlen != 35)
134                         maxfilterlen.assign(ConvToStr(cf.maxlen));
135
136                 return Version("Provides channel-specific censor lists (like mode +G but varies from channel to channel)", VF_VENDOR, maxfilterlen);
137         }
138 };
139
140 MODULE_INIT(ModuleChanFilter)