]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
5bb5a91b03a59cc72c3fc7ca2ef3d076a988005b
[user/henk/code/inspircd.git] / src / modules / m_chanfilter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #define _CRT_SECURE_NO_DEPRECATE
15 #define _SCL_SECURE_NO_DEPRECATE
16
17 #include "inspircd.h"
18 #include "u_listmode.h"
19
20 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
21
22 /** Handles channel mode +g
23  */
24 class ChanFilter : public ListModeBase
25 {
26  public:
27         ChanFilter(Module* Creator) : ListModeBase(Creator, "filter", 'g', "End of channel spamfilter list", 941, 940, false, "chanfilter") { }
28
29         virtual bool ValidateParam(User* user, Channel* chan, std::string &word)
30         {
31                 if ((word.length() > 35) || (word.empty()))
32                 {
33                         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"));
34                         return false;
35                 }
36
37                 return true;
38         }
39
40         virtual bool TellListTooLong(User* user, Channel* chan, std::string &word)
41         {
42                 user->WriteNumeric(939, "%s %s %s :Channel spamfilter list is full", user->nick.c_str(), chan->name.c_str(), word.c_str());
43                 return true;
44         }
45
46         virtual void TellAlreadyOnList(User* user, Channel* chan, std::string &word)
47         {
48                 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());
49         }
50
51         virtual void TellNotSet(User* user, Channel* chan, std::string &word)
52         {
53                 user->WriteNumeric(938, "%s %s :No such spamfilter word is set",user->nick.c_str(), chan->name.c_str());
54         }
55 };
56
57 class ModuleChanFilter : public Module
58 {
59         ChanFilter cf;
60         bool hidemask;
61
62  public:
63
64         ModuleChanFilter()
65                 : cf(this)
66         {
67                 if (!ServerInstance->Modes->AddMode(&cf))
68                         throw ModuleException("Could not add new modes!");
69
70                 cf.DoImplements(this);
71                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnSyncChannel };
72                 ServerInstance->Modules->Attach(eventlist, this, 4);
73
74                 OnRehash(NULL);
75         }
76
77         virtual void OnRehash(User* user)
78         {
79                 ConfigReader Conf;
80                 hidemask = Conf.ReadFlag("chanfilter", "hidemask", 0);
81                 cf.DoRehash();
82         }
83
84         virtual ModResult ProcessMessages(User* user,Channel* chan,std::string &text)
85         {
86                 ModResult res;
87                 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,chan,"filter"));
88
89                 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
90                         return MOD_RES_PASSTHRU;
91
92                 modelist* list = cf.extItem.get(chan);
93
94                 if (list)
95                 {
96                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
97                         {
98                                 if (InspIRCd::Match(text, i->mask))
99                                 {
100                                         if (hidemask)
101                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (your message contained a censored word)",user->nick.c_str(), chan->name.c_str());
102                                         else
103                                                 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());
104                                         return MOD_RES_DENY;
105                                 }
106                         }
107                 }
108
109                 return MOD_RES_PASSTHRU;
110         }
111
112         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
113         {
114                 if (target_type == TYPE_CHANNEL)
115                 {
116                         return ProcessMessages(user,(Channel*)dest,text);
117                 }
118                 return MOD_RES_PASSTHRU;
119         }
120
121         virtual void OnCleanup(int target_type, void* item)
122         {
123                 cf.DoCleanup(target_type, item);
124         }
125
126         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
127         {
128                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
129         }
130
131         virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
132         {
133                 cf.DoSyncChannel(chan, proto, opaque);
134         }
135
136         virtual Version GetVersion()
137         {
138                 return Version("Provides channel-specific censor lists (like mode +G but varies from channel to channel)", VF_COMMON | VF_VENDOR);
139         }
140
141         virtual ~ModuleChanFilter()
142         {
143         }
144 };
145
146 MODULE_INIT(ModuleChanFilter)