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 std::stringstream line(output);
59 std::string temp1, temp2;
63 if (temp1.substr(0,10) == "CHANMODES=")
65 // append the chanmode to the end
66 temp1 = temp1.substr(10,temp1.length());
67 temp1 = "CHANMODES=g" + temp1;
69 temp2 = temp2 + temp1 + " ";
72 output = temp2.substr(0,temp2.length()-1);
75 virtual void OnUserPart(userrec* user, chanrec* channel, std::string partreason)
77 // when the last user parts, delete the list
78 if (Srv->CountUsers(channel) == 1)
80 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
83 channel->Shrink("spam_list");
89 virtual void OnRehash(std::string parameter)
92 Conf = new ConfigReader;
93 // re-read our config options on a rehash
94 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
97 virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
100 // Create a copy of the string in irc::string
101 irc::string line = text.c_str();
103 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
106 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
108 if (line.find(*i) != std::string::npos)
110 WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
118 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
120 if (target_type == TYPE_CHANNEL)
122 return ProcessMessages(user,(chanrec*)dest,text);
127 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
129 if (target_type == TYPE_CHANNEL)
131 return ProcessMessages(user,(chanrec*)dest,text);
136 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
138 if ((modechar == 'g') && (type == MT_CHANNEL))
140 chanrec* chan = (chanrec*)target;
142 irc::string word = params[0].c_str();
146 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
149 spamlist = new SpamList;
150 chan->Extend("spam_list",(char*)spamlist);
152 if (spamlist->size() < (unsigned)MaxEntries)
154 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
158 WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
162 spamlist->push_back(word);
165 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
170 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
173 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
182 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
190 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
194 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
197 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
199 WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
202 WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
206 virtual ~ModuleChanFilter()
211 virtual Version GetVersion()
213 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
216 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
218 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
219 string_list commands;
222 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
224 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+std::string(i->c_str()));
232 class ModuleChanFilterFactory : public ModuleFactory
235 ModuleChanFilterFactory()
239 ~ModuleChanFilterFactory()
243 virtual Module * CreateModule(Server* Me)
245 return new ModuleChanFilter(Me);
251 extern "C" void * init_module( void )
253 return new ModuleChanFilterFactory;