-/* +------------------------------------+
- * | Inspire Internet Relay Chat Daemon |
- * +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
*
- * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * Written by Craig Edwards, Craig McLure, and others.
- * This program is free but copyrighted software; see
- * the file COPYING for details.
+ * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
+ * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
+ * Copyright (C) 2005 Craig McLure <craig@chatspike.net>
+ * Copyright (C) 2005 Craig Edwards <craigedwards@brainbox.cc>
*
- * ---------------------------------------------------
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-using namespace std;
-
-#include <stdio.h>
-#include <string>
-#include <vector>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "helperfuncs.h"
-/* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
+#include "inspircd.h"
+#include "listmode.h"
-typedef std::vector<std::string> SpamList;
-
-class ModuleChanFilter : public Module
+/** Handles channel mode +g
+ */
+class ChanFilter : public ListModeBase
{
- Server *Srv;
- ConfigReader *Conf;
- long MaxEntries;
-
public:
-
- ModuleChanFilter(Server* Me)
- : Module::Module(Me)
- {
- Srv = Me;
- Conf = new ConfigReader;
- Srv->AddExtendedListMode('g');
- MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
- if (MaxEntries == 0)
- MaxEntries = 32;
- }
-
- virtual void On005Numeric(std::string &output)
- {
- std::stringstream line(output);
- std::string temp1, temp2;
- while (!line.eof())
- {
- line >> temp1;
- if (temp1.substr(0,10) == "CHANMODES=")
- {
- // append the chanmode to the end
- temp1 = temp1.substr(10,temp1.length());
- temp1 = "CHANMODES=g" + temp1;
- }
- temp2 = temp2 + temp1 + " ";
- }
- if (temp2.length())
- output = temp2.substr(0,temp2.length()-1);
- }
-
- virtual void OnUserPart(userrec* user, chanrec* channel)
+ ChanFilter(Module* Creator) : ListModeBase(Creator, "filter", 'g', "End of channel spamfilter list", 941, 940, false, "chanfilter") { }
+
+ bool ValidateParam(User* user, Channel* chan, std::string &word)
{
- // when the last user parts, delete the list
- if (Srv->CountUsers(channel) == 1)
+ if (word.length() > 35)
{
- SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
- if (spamlist)
- {
- channel->Shrink("spam_list");
- delete spamlist;
- }
+ user->WriteNumeric(935, "%s %s :word is too long for censor list", chan->name.c_str(), word.c_str());
+ return false;
}
- }
- virtual void OnRehash(std::string parameter)
- {
- delete Conf;
- Conf = new ConfigReader;
- // re-read our config options on a rehash
- MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
+ return true;
}
- virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
- {
- char buffer[MAXBUF];
- strlcpy(buffer,text.c_str(),MAXBUF);
- for (unsigned int j = 0; j < strlen(buffer); j++)
- buffer[j] = tolower(buffer[j]);
- SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
- if (spamlist)
- {
- for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
- {
- if (strstr(text.c_str(),i->c_str()))
- {
- WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
- return 1;
- }
- }
- }
- return 0;
+ void TellListTooLong(User* user, Channel* chan, std::string &word)
+ {
+ user->WriteNumeric(939, "%s %s :Channel spamfilter list is full", chan->name.c_str(), word.c_str());
}
- virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
+ void TellAlreadyOnList(User* user, Channel* chan, std::string &word)
{
- if (target_type == TYPE_CHANNEL)
- {
- return ProcessMessages(user,(chanrec*)dest,text);
- }
- else return 0;
+ user->WriteNumeric(937, "%s :The word %s is already on the spamfilter list", chan->name.c_str(), word.c_str());
}
- virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
+ void TellNotSet(User* user, Channel* chan, std::string &word)
{
- if (target_type == TYPE_CHANNEL)
- {
- return ProcessMessages(user,(chanrec*)dest,text);
- }
- else return 0;
+ user->WriteNumeric(938, "%s :No such spamfilter word is set", chan->name.c_str());
}
-
- virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
- {
- if ((modechar == 'g') && (type == MT_CHANNEL))
- {
- chanrec* chan = (chanrec*)target;
+};
- for (unsigned int j = 0; j < params[0].length(); j++)
- params[0][j] = tolower(params[0][j]);
+class ModuleChanFilter : public Module
+{
+ ChanFilter cf;
+ bool hidemask;
- std::string param = params[0];
-
- if (mode_on)
- {
- SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
- if (!spamlist)
- {
- spamlist = new SpamList;
- chan->Extend("spam_list",(char*)spamlist);
- }
- if (spamlist->size() < (unsigned)MaxEntries)
- {
- for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
- {
- if (*i == params[0])
- {
- WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,params[0].c_str());
- return -1;
- }
- }
- spamlist->push_back(params[0]);
- return 1;
- }
- WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
- return -1;
- }
- else
- {
- SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
- if (spamlist)
- {
- for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
- {
- if (*i == params[0])
- {
- spamlist->erase(i);
- return 1;
- }
- }
- }
- WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
- return -1;
- }
- return -1;
- }
- return 0;
- }
+ public:
- virtual void OnSendList(userrec* user, chanrec* channel, char mode)
- {
- if (mode == 'g')
- {
- SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
- if (spamlist)
- {
- for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
- {
- WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
- }
- }
- WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
- }
- }
-
- virtual ~ModuleChanFilter()
+ ModuleChanFilter()
+ : cf(this)
{
- delete Conf;
}
-
- virtual Version GetVersion()
+
+ void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
{
- return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
+ hidemask = ServerInstance->Config->ConfValue("chanfilter")->getBool("hidemask");
+ cf.DoRehash();
}
-
- virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
+
+ ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
{
- SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
- string_list commands;
- if (spamlist)
+ if (target_type != TYPE_CHANNEL)
+ return MOD_RES_PASSTHRU;
+
+ Channel* chan = static_cast<Channel*>(dest);
+ ModResult res = ServerInstance->OnCheckExemption(user,chan,"filter");
+
+ if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
+ return MOD_RES_PASSTHRU;
+
+ ListModeBase::ModeList* list = cf.GetList(chan);
+
+ if (list)
{
- for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
+ for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); i++)
{
- proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+*i);
+ if (InspIRCd::Match(text, i->mask))
+ {
+ if (hidemask)
+ user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (your message contained a censored word)", chan->name.c_str());
+ else
+ user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s %s :Cannot send to channel (your message contained a censored word)", chan->name.c_str(), i->mask.c_str());
+ return MOD_RES_DENY;
+ }
}
}
- }
-};
-
-
-class ModuleChanFilterFactory : public ModuleFactory
-{
- public:
- ModuleChanFilterFactory()
- {
- }
-
- ~ModuleChanFilterFactory()
- {
+ return MOD_RES_PASSTHRU;
}
-
- virtual Module * CreateModule(Server* Me)
+
+ Version GetVersion() CXX11_OVERRIDE
{
- return new ModuleChanFilter(Me);
+ return Version("Provides channel-specific censor lists (like mode +G but varies from channel to channel)", VF_VENDOR);
}
-
};
-
-extern "C" void * init_module( void )
-{
- return new ModuleChanFilterFactory;
-}
-
+MODULE_INIT(ModuleChanFilter)