]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
Annotations
[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 "hashcomp.h"
26 #include "u_listmode.h"
27 #include "inspircd.h"
28
29 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
30
31 /** Handles channel mode +g
32  */
33 class ChanFilter : public ListModeBase
34 {
35  public:
36         ChanFilter(InspIRCd* Instance) : ListModeBase(Instance, 'g', "End of channel spamfilter list", "941", "940", false, "chanfilter") { }
37         
38         virtual bool ValidateParam(userrec* user, chanrec* chan, std::string &word)
39         {
40                 if (word.length() > 35)
41                 {
42                         user->WriteServ( "935 %s %s %s :word is too long for censor list",user->nick, chan->name,word.c_str());
43                         return false;
44                 }
45                 
46                 return true;
47         }
48         
49         virtual bool TellListTooLong(userrec* user, chanrec* chan, std::string &word)
50         {
51                 user->WriteServ("939 %s %s %s :Channel spamfilter list is full",user->nick, chan->name, word.c_str());
52                 return true;
53         }
54         
55         virtual void TellAlreadyOnList(userrec* user, chanrec* chan, std::string &word)
56         {
57                 user->WriteServ("937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
58         }
59         
60         virtual void TellNotSet(userrec* user, chanrec* chan, std::string &word)
61         {
62                 user->WriteServ("938 %s %s :No such spamfilter word is set",user->nick, chan->name);
63         }
64 };
65
66 class ModuleChanFilter : public Module
67 {
68         
69         ChanFilter* cf;
70         
71  public:
72  
73         ModuleChanFilter(InspIRCd* Me)
74                 : Module::Module(Me)
75         {
76                 cf = new ChanFilter(ServerInstance);
77                 ServerInstance->AddMode(cf, 'g');
78         }
79
80         void Implements(char* List) 
81         { 
82                 cf->DoImplements(List);
83                 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
84         }
85
86         virtual void OnChannelDelete(chanrec* chan)
87         {
88                 cf->DoChannelDelete(chan);
89         }
90
91         virtual void OnRehash(const std::string &parameter)
92         {
93                 cf->DoRehash();
94         }
95
96         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
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                 return 0;
116         }
117
118         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
119         {
120                 if (target_type == TYPE_CHANNEL)
121                 {
122                         return ProcessMessages(user,(chanrec*)dest,text);
123                 }
124                 else return 0;
125         }
126
127         virtual void OnCleanup(int target_type, void* item)
128         {
129                 cf->DoCleanup(target_type, item);
130         }
131         
132         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
133         {
134                 return OnUserPreMessage(user,dest,target_type,text,status);
135         }
136         
137         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
138         {
139                 cf->DoSyncChannel(chan, proto, opaque);
140         }
141
142         virtual Version GetVersion()
143         {
144                 return Version(1, 0, 0, 1, VF_COMMON | VF_VENDOR);
145         }
146         
147         virtual ~ModuleChanFilter()
148         {
149                 ServerInstance->Modes->DelMode(cf);
150                 DELETE(cf);
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(InspIRCd* 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 }