]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
0ceac731881b85ef6c0dac5d76531830ab943eaa
[user/henk/code/inspircd.git] / src / modules / m_chanfilter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26 #include "hashcomp.h"
27 #include "u_listmode.h"
28
29 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
30
31 class ChanFilter : public ListModeBase
32 {
33  public:
34         ChanFilter(Server* serv) : ListModeBase(serv, 'g', "End of channel spamfilter list", "941", "940", "chanfilter") { }
35         
36         virtual bool ValidateParam(userrec* user, chanrec* chan, std::string &word)
37         {
38                 if (word.length() > 35)
39                 {
40                         WriteServ(user->fd, "935 %s %s %s :word is too long for censor list",user->nick, chan->name,word.c_str());
41                         return false;
42                 }
43                 
44                 return true;
45         }
46         
47         virtual bool TellListTooLong(userrec* user, chanrec* chan, std::string &word)
48         {
49                 WriteServ(user->fd,"939 %s %s %s :Channel spamfilter list is full",user->nick, chan->name, word.c_str());
50                 return true;
51         }
52         
53         virtual void TellAlreadyOnList(userrec* user, chanrec* chan, std::string &word)
54         {
55                 WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
56         }
57         
58         virtual void TellNotSet(userrec* user, chanrec* chan, std::string &word)
59         {
60                 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
61         }
62 };
63
64 class ModuleChanFilter : public Module
65 {
66         Server *Srv;
67         ChanFilter* cf;
68         
69  public:
70  
71         ModuleChanFilter(Server* Me)
72         : Module::Module(Me), Srv(Me)
73         {
74                 cf = new ChanFilter(Srv);
75                 Srv->AddMode(cf, 'g');
76         }
77
78         void Implements(char* List) 
79         { 
80                 cf->DoImplements(List);
81                 List[I_OnCleanup] = List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
82         }
83         
84         virtual void On005Numeric(std::string &output)
85         {
86                 InsertMode(output,"g",1);
87         }
88
89         virtual void OnChannelDelete(chanrec* chan)
90         {
91                 cf->DoChannelDelete(chan);
92         }
93
94         virtual void OnRehash(const std::string &parameter)
95         {
96                 cf->DoRehash();
97         }
98
99         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
100         {
101                 // Create a copy of the string in irc::string
102                 irc::string line = text.c_str();
103
104                 modelist* list = (modelist*)chan->GetExt(cf->GetInfoKey());
105
106                 if (list)
107                 {
108                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
109                         {
110                                 if (line.find(i->mask.c_str()) != std::string::npos)
111                                 {
112                                         WriteServ(user->fd,"936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->mask.c_str());
113                                         return 1;
114                                 }
115                         }
116                 }
117                 return 0;
118         }
119
120         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
121         {
122                 if (target_type == TYPE_CHANNEL)
123                 {
124                         return ProcessMessages(user,(chanrec*)dest,text);
125                 }
126                 else return 0;
127         }
128
129         virtual void OnCleanup(int target_type, void* item)
130         {
131                 cf->DoCleanup(target_type, item);
132         }
133         
134         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
135         {
136                 return OnUserPreMessage(user,dest,target_type,text,status);
137         }
138         
139         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
140         {
141                 cf->DoSyncChannel(chan, proto, opaque);
142         }
143
144         virtual Version GetVersion()
145         {
146                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
147         }
148         
149         virtual ~ModuleChanFilter()
150         {
151         }
152 };
153
154
155 class ModuleChanFilterFactory : public ModuleFactory
156 {
157  public:
158         ModuleChanFilterFactory()
159         {
160         }
161         
162         ~ModuleChanFilterFactory()
163         {
164         }
165         
166         virtual Module * CreateModule(Server* Me)
167         {
168                 return new ModuleChanFilter(Me);
169         }
170         
171 };
172
173
174 extern "C" void * init_module( void )
175 {
176         return new ModuleChanFilterFactory;
177 }