1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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"
27 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
29 typedef std::vector<std::string> SpamList;
31 class ModuleChanFilter : public Module
39 ModuleChanFilter(Server* Me)
43 Conf = new ConfigReader;
44 Srv->AddExtendedListMode('g');
45 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
50 void Implements(char* List)
52 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;
55 virtual void On005Numeric(std::string &output)
57 std::stringstream line(output);
58 std::string temp1, temp2;
62 if (temp1.substr(0,10) == "CHANMODES=")
64 // append the chanmode to the end
65 temp1 = temp1.substr(10,temp1.length());
66 temp1 = "CHANMODES=g" + temp1;
68 temp2 = temp2 + temp1 + " ";
71 output = temp2.substr(0,temp2.length()-1);
74 virtual void OnUserPart(userrec* user, chanrec* channel)
76 // when the last user parts, delete the list
77 if (Srv->CountUsers(channel) == 1)
79 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
82 channel->Shrink("spam_list");
88 virtual void OnRehash(std::string parameter)
91 Conf = new ConfigReader;
92 // re-read our config options on a rehash
93 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
96 virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
99 strlcpy(buffer,text.c_str(),MAXBUF);
100 for (unsigned int j = 0; j < strlen(buffer); j++)
101 buffer[j] = tolower(buffer[j]);
102 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
105 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
107 if (strstr(buffer,i->c_str()))
109 WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
117 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
119 if (target_type == TYPE_CHANNEL)
121 return ProcessMessages(user,(chanrec*)dest,text);
126 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
128 if (target_type == TYPE_CHANNEL)
130 return ProcessMessages(user,(chanrec*)dest,text);
135 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
137 if ((modechar == 'g') && (type == MT_CHANNEL))
139 chanrec* chan = (chanrec*)target;
141 for (unsigned int j = 0; j < params[0].length(); j++)
142 params[0][j] = tolower(params[0][j]);
144 std::string param = params[0];
148 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
151 spamlist = new SpamList;
152 chan->Extend("spam_list",(char*)spamlist);
154 if (spamlist->size() < (unsigned)MaxEntries)
156 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
160 WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,params[0].c_str());
164 spamlist->push_back(params[0]);
167 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
172 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
175 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
184 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
192 virtual void OnSendList(userrec* user, chanrec* channel, char mode)
196 SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
199 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
201 WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
204 WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
208 virtual ~ModuleChanFilter()
213 virtual Version GetVersion()
215 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
218 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
220 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
221 string_list commands;
224 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
226 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+*i);
234 class ModuleChanFilterFactory : public ModuleFactory
237 ModuleChanFilterFactory()
241 ~ModuleChanFilterFactory()
245 virtual Module * CreateModule(Server* Me)
247 return new ModuleChanFilter(Me);
253 extern "C" void * init_module( void )
255 return new ModuleChanFilterFactory;