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 if (word.length() > 35)
138 WriteServ(user->fd,"935 %s %s %s :word is too long for censor list",user->nick, chan->name,word.c_str());
141 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
145 WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
149 spamlist->push_back(word);
152 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
157 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
160 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
169 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
177 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
181 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
184 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
186 WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
189 WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
193 virtual ~ModuleChanFilter()
198 virtual Version GetVersion()
200 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
203 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
205 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
206 string_list commands;
209 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
211 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+std::string(i->c_str()));
219 class ModuleChanFilterFactory : public ModuleFactory
222 ModuleChanFilterFactory()
226 ~ModuleChanFilterFactory()
230 virtual Module * CreateModule(Server* Me)
232 return new ModuleChanFilter(Me);
238 extern "C" void * init_module( void )
240 return new ModuleChanFilterFactory;