1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
25 #include "helperfuncs.h"
28 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
30 typedef std::vector<irc::string> SpamList;
32 class ModuleChanFilter : public Module
40 ModuleChanFilter(Server* Me)
44 Conf = new ConfigReader;
45 Srv->AddExtendedListMode('g');
46 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
51 void Implements(char* List)
53 List[I_On005Numeric] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = List[I_OnSendList] = List[I_OnSyncChannel] = 1;
56 virtual void On005Numeric(std::string &output)
58 InsertMode(output,"g",1);
61 virtual void OnUserPart(userrec* user, chanrec* channel, std::string partreason)
63 // when the last user parts, delete the list
64 if (Srv->CountUsers(channel) == 1)
66 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
69 channel->Shrink("spam_list");
75 virtual void OnRehash(std::string parameter)
78 Conf = new ConfigReader;
79 // re-read our config options on a rehash
80 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
83 virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
86 // Create a copy of the string in irc::string
87 irc::string line = text.c_str();
89 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
92 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
94 if (line.find(*i) != std::string::npos)
96 WriteServ(user->fd,"936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->c_str());
104 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
106 if (target_type == TYPE_CHANNEL)
108 return ProcessMessages(user,(chanrec*)dest,text);
113 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
115 return OnUserPreMessage(user,dest,target_type,text,status);
118 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
120 if ((modechar == 'g') && (type == MT_CHANNEL))
122 chanrec* chan = (chanrec*)target;
124 irc::string word = params[0].c_str();
128 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
131 spamlist = new SpamList;
132 chan->Extend("spam_list",(char*)spamlist);
134 if (spamlist->size() < (unsigned)MaxEntries)
136 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
140 WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
144 spamlist->push_back(word);
147 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
152 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
155 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
164 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
172 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
176 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
179 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
181 WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
184 WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
188 virtual ~ModuleChanFilter()
193 virtual Version GetVersion()
195 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
198 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
200 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
201 string_list commands;
204 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
206 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+std::string(i->c_str()));
214 class ModuleChanFilterFactory : public ModuleFactory
217 ModuleChanFilterFactory()
221 ~ModuleChanFilterFactory()
225 virtual Module * CreateModule(Server* Me)
227 return new ModuleChanFilter(Me);
233 extern "C" void * init_module( void )
235 return new ModuleChanFilterFactory;