]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
c4a69cc10dfd4e5f90ee4dbd09737b3f84c43911
[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 #include <stdio.h>
15 #include <string>
16 #include <vector>
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "hashcomp.h"
21 #include "u_listmode.h"
22 #include "inspircd.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::Module(Me)
71         {
72                 cf = new ChanFilter(ServerInstance);
73                 ServerInstance->AddMode(cf, 'g');
74         }
75
76         void Implements(char* List) 
77         { 
78                 cf->DoImplements(List);
79                 List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
80         }
81
82         virtual void OnChannelDelete(chanrec* chan)
83         {
84                 cf->DoChannelDelete(chan);
85         }
86
87         virtual void OnRehash(const std::string &parameter)
88         {
89                 cf->DoRehash();
90         }
91
92         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
93         {
94                 // Create a copy of the string in irc::string
95                 irc::string line = text.c_str();
96
97                 modelist* list;
98                 chan->GetExt(cf->GetInfoKey(), list);
99
100                 if (list)
101                 {
102                         for (modelist::iterator i = list->begin(); i != list->end(); i++)
103                         {
104                                 if (line.find(i->mask.c_str()) != std::string::npos)
105                                 {
106                                         user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->mask.c_str());
107                                         return 1;
108                                 }
109                         }
110                 }
111                 return 0;
112         }
113
114         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
115         {
116                 if (target_type == TYPE_CHANNEL)
117                 {
118                         return ProcessMessages(user,(chanrec*)dest,text);
119                 }
120                 else return 0;
121         }
122
123         virtual void OnCleanup(int target_type, void* item)
124         {
125                 cf->DoCleanup(target_type, item);
126         }
127         
128         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
129         {
130                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
131         }
132         
133         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
134         {
135                 cf->DoSyncChannel(chan, proto, opaque);
136         }
137
138         virtual Version GetVersion()
139         {
140                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
141         }
142         
143         virtual ~ModuleChanFilter()
144         {
145                 ServerInstance->Modes->DelMode(cf);
146                 DELETE(cf);
147         }
148 };
149
150
151 class ModuleChanFilterFactory : public ModuleFactory
152 {
153  public:
154         ModuleChanFilterFactory()
155         {
156         }
157         
158         ~ModuleChanFilterFactory()
159         {
160         }
161         
162         virtual Module * CreateModule(InspIRCd* Me)
163         {
164                 return new ModuleChanFilter(Me);
165         }
166         
167 };
168
169
170 extern "C" void * init_module( void )
171 {
172         return new ModuleChanFilterFactory;
173 }