]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
So much stuff changed in this one, i forgot most of it.
[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 #include "inspircd.h"
29
30 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
31
32 extern InspIRCd* ServerInstance;
33
34 class ChanFilter : public ListModeBase
35 {
36  public:
37         ChanFilter(InspIRCd* Instance, Server* serv) : ListModeBase(Instance, serv, 'g', "End of channel spamfilter list", "941", "940", false, "chanfilter") { }
38         
39         virtual bool ValidateParam(userrec* user, chanrec* chan, std::string &word)
40         {
41                 if (word.length() > 35)
42                 {
43                         user->WriteServ( "935 %s %s %s :word is too long for censor list",user->nick, chan->name,word.c_str());
44                         return false;
45                 }
46                 
47                 return true;
48         }
49         
50         virtual bool TellListTooLong(userrec* user, chanrec* chan, std::string &word)
51         {
52                 user->WriteServ("939 %s %s %s :Channel spamfilter list is full",user->nick, chan->name, word.c_str());
53                 return true;
54         }
55         
56         virtual void TellAlreadyOnList(userrec* user, chanrec* chan, std::string &word)
57         {
58                 user->WriteServ("937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
59         }
60         
61         virtual void TellNotSet(userrec* user, chanrec* chan, std::string &word)
62         {
63                 user->WriteServ("938 %s %s :No such spamfilter word is set",user->nick, chan->name);
64         }
65 };
66
67 class ModuleChanFilter : public Module
68 {
69         Server *Srv;
70         ChanFilter* cf;
71         
72  public:
73  
74         ModuleChanFilter(Server* Me)
75         : Module::Module(Me), Srv(Me)
76         {
77                 cf = new ChanFilter(ServerInstance, Srv);
78                 Srv->AddMode(cf, 'g');
79         }
80
81         void Implements(char* List) 
82         { 
83                 cf->DoImplements(List);
84                 List[I_OnCleanup] = List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
85         }
86         
87         virtual void On005Numeric(std::string &output)
88         {
89                 ServerInstance->ModeGrok->InsertMode(output,"g",1);
90         }
91
92         virtual void OnChannelDelete(chanrec* chan)
93         {
94                 cf->DoChannelDelete(chan);
95         }
96
97         virtual void OnRehash(const std::string &parameter)
98         {
99                 cf->DoRehash();
100         }
101
102         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
103         {
104                 // Create a copy of the string in irc::string
105                 irc::string line = text.c_str();
106
107                 modelist* list;
108                 chan->GetExt(cf->GetInfoKey(), list);
109
110                 if (list)
111                 {
112                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
113                         {
114                                 if (line.find(i->mask.c_str()) != std::string::npos)
115                                 {
116                                         user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->mask.c_str());
117                                         return 1;
118                                 }
119                         }
120                 }
121                 return 0;
122         }
123
124         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
125         {
126                 if (target_type == TYPE_CHANNEL)
127                 {
128                         return ProcessMessages(user,(chanrec*)dest,text);
129                 }
130                 else return 0;
131         }
132
133         virtual void OnCleanup(int target_type, void* item)
134         {
135                 cf->DoCleanup(target_type, item);
136         }
137         
138         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
139         {
140                 return OnUserPreMessage(user,dest,target_type,text,status);
141         }
142         
143         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
144         {
145                 cf->DoSyncChannel(chan, proto, opaque);
146         }
147
148         virtual Version GetVersion()
149         {
150                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
151         }
152         
153         virtual ~ModuleChanFilter()
154         {
155                 DELETE(cf);
156         }
157 };
158
159
160 class ModuleChanFilterFactory : public ModuleFactory
161 {
162  public:
163         ModuleChanFilterFactory()
164         {
165         }
166         
167         ~ModuleChanFilterFactory()
168         {
169         }
170         
171         virtual Module * CreateModule(Server* Me)
172         {
173                 return new ModuleChanFilter(Me);
174         }
175         
176 };
177
178
179 extern "C" void * init_module( void )
180 {
181         return new ModuleChanFilterFactory;
182 }