]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
375fbce9c5fdecd39e2b2329d6a60af32ccfae21
[user/henk/code/inspircd.git] / src / modules / m_chanfilter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "users.h"
19 #include "channels.h"
20 #include "modules.h"
21 #include "hashcomp.h"
22 #include "u_listmode.h"
23
24 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
25 /* $ModDep: ../../include/u_listmode.h */
26
27 /** Handles channel mode +g
28  */
29 class ChanFilter : public ListModeBase
30 {
31  public:
32         ChanFilter(InspIRCd* Instance) : ListModeBase(Instance, 'g', "End of channel spamfilter list", "941", "940", false, "chanfilter") { }
33         
34         virtual bool ValidateParam(userrec* user, chanrec* chan, std::string &word)
35         {
36                 if ((word.length() > 35) || (word.empty()))
37                 {
38                         user->WriteServ("935 %s %s %s :word is too %s for censor list",user->nick, chan->name,word.c_str(), (word.empty() ? "short" : "long"));
39                         return false;
40                 }
41                 
42                 return true;
43         }
44         
45         virtual bool TellListTooLong(userrec* user, chanrec* chan, std::string &word)
46         {
47                 user->WriteServ("939 %s %s %s :Channel spamfilter list is full",user->nick, chan->name, word.c_str());
48                 return true;
49         }
50         
51         virtual void TellAlreadyOnList(userrec* user, chanrec* chan, std::string &word)
52         {
53                 user->WriteServ("937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
54         }
55         
56         virtual void TellNotSet(userrec* user, chanrec* chan, std::string &word)
57         {
58                 user->WriteServ("938 %s %s :No such spamfilter word is set",user->nick, chan->name);
59         }
60 };
61
62 class ModuleChanFilter : public Module
63 {
64         
65         ChanFilter* cf;
66         
67  public:
68  
69         ModuleChanFilter(InspIRCd* Me)
70                 : Module(Me)
71         {
72                 cf = new ChanFilter(ServerInstance);
73                 if (!ServerInstance->AddMode(cf, 'g'))
74                         throw ModuleException("Could not add new modes!");
75         }
76
77         void Implements(char* List) 
78         { 
79                 cf->DoImplements(List);
80                 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
81         }
82
83         virtual void OnChannelDelete(chanrec* chan)
84         {
85                 cf->DoChannelDelete(chan);
86         }
87
88         virtual void OnRehash(userrec* user, const std::string &parameter)
89         {
90                 cf->DoRehash();
91         }
92
93         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
94         {
95                 if (!IS_LOCAL(user) || CHANOPS_EXEMPT(ServerInstance, 'g') && chan->GetStatus(user) == STATUS_OP)
96                         return 0;
97
98                 // Create a copy of the string in irc::string
99                 irc::string line = text.c_str();
100
101                 modelist* list;
102                 chan->GetExt(cf->GetInfoKey(), list);
103
104                 if (list)
105                 {
106                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
107                         {
108                                 if (line.find(i->mask.c_str()) != std::string::npos)
109                                 {
110                                         user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->mask.c_str());
111                                         return 1;
112                                 }
113                         }
114                 }
115
116                 return 0;
117         }
118
119         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
120         {
121                 if (target_type == TYPE_CHANNEL)
122                 {
123                         return ProcessMessages(user,(chanrec*)dest,text);
124                 }
125                 else return 0;
126         }
127
128         virtual void OnCleanup(int target_type, void* item)
129         {
130                 cf->DoCleanup(target_type, item);
131         }
132         
133         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
134         {
135                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
136         }
137         
138         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
139         {
140                 cf->DoSyncChannel(chan, proto, opaque);
141         }
142
143         virtual Version GetVersion()
144         {
145                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
146         }
147         
148         virtual ~ModuleChanFilter()
149         {
150                 ServerInstance->Modes->DelMode(cf);
151                 DELETE(cf);
152         }
153 };
154
155 MODULE_INIT(ModuleChanFilter)