]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
Attach to events and register services in init()
[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 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
27
28 #define _CRT_SECURE_NO_DEPRECATE
29 #define _SCL_SECURE_NO_DEPRECATE
30
31 #include "inspircd.h"
32 #include "u_listmode.h"
33
34 /** Handles channel mode +g
35  */
36 class ChanFilter : public ListModeBase
37 {
38  public:
39         ChanFilter(Module* Creator) : ListModeBase(Creator, "filter", 'g', "End of channel spamfilter list", 941, 940, false, "chanfilter") { }
40
41         virtual bool ValidateParam(User* user, Channel* chan, std::string &word)
42         {
43                 if ((word.length() > 35) || (word.empty()))
44                 {
45                         user->WriteNumeric(935, "%s %s %s :word is too %s for censor list",user->nick.c_str(), chan->name.c_str(), word.c_str(), (word.empty() ? "short" : "long"));
46                         return false;
47                 }
48
49                 return true;
50         }
51
52         virtual bool TellListTooLong(User* user, Channel* chan, std::string &word)
53         {
54                 user->WriteNumeric(939, "%s %s %s :Channel spamfilter list is full", user->nick.c_str(), chan->name.c_str(), word.c_str());
55                 return true;
56         }
57
58         virtual void TellAlreadyOnList(User* user, Channel* chan, std::string &word)
59         {
60                 user->WriteNumeric(937, "%s %s :The word %s is already on the spamfilter list",user->nick.c_str(), chan->name.c_str(), word.c_str());
61         }
62
63         virtual void TellNotSet(User* user, Channel* chan, std::string &word)
64         {
65                 user->WriteNumeric(938, "%s %s :No such spamfilter word is set",user->nick.c_str(), chan->name.c_str());
66         }
67 };
68
69 class ModuleChanFilter : public Module
70 {
71         ChanFilter cf;
72         bool hidemask;
73
74  public:
75
76         ModuleChanFilter()
77                 : cf(this)
78         {
79         }
80
81         void init()
82         {
83                 if (!ServerInstance->Modes->AddMode(&cf))
84                         throw ModuleException("Could not add new modes!");
85
86                 cf.DoImplements(this);
87                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnSyncChannel };
88                 ServerInstance->Modules->Attach(eventlist, this, 4);
89
90                 OnRehash(NULL);
91         }
92
93         virtual void OnRehash(User* user)
94         {
95                 hidemask = ServerInstance->Config->ConfValue("chanfilter")->getBool("hidemask");
96                 cf.DoRehash();
97         }
98
99         virtual ModResult ProcessMessages(User* user,Channel* chan,std::string &text)
100         {
101                 ModResult res = ServerInstance->OnCheckExemption(user,chan,"filter");
102
103                 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
104                         return MOD_RES_PASSTHRU;
105
106                 modelist* list = cf.extItem.get(chan);
107
108                 if (list)
109                 {
110                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
111                         {
112                                 if (InspIRCd::Match(text, i->mask))
113                                 {
114                                         if (hidemask)
115                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (your message contained a censored word)",user->nick.c_str(), chan->name.c_str());
116                                         else
117                                                 user->WriteNumeric(404, "%s %s %s :Cannot send to channel (your message contained a censored word)",user->nick.c_str(), chan->name.c_str(), i->mask.c_str());
118                                         return MOD_RES_DENY;
119                                 }
120                         }
121                 }
122
123                 return MOD_RES_PASSTHRU;
124         }
125
126         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
127         {
128                 if (target_type == TYPE_CHANNEL)
129                 {
130                         return ProcessMessages(user,(Channel*)dest,text);
131                 }
132                 return MOD_RES_PASSTHRU;
133         }
134
135         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
136         {
137                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
138         }
139
140         virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
141         {
142                 cf.DoSyncChannel(chan, proto, opaque);
143         }
144
145         virtual Version GetVersion()
146         {
147                 return Version("Provides channel-specific censor lists (like mode +G but varies from channel to channel)", VF_VENDOR);
148         }
149
150         virtual ~ModuleChanFilter()
151         {
152         }
153 };
154
155 MODULE_INIT(ModuleChanFilter)