]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_censor.cpp
Rename censor_t to CensorMap as the _t suffix is reserved by POSIX.
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
index a7aa2f8b13fe691a45afad70b552114ef655a324..b78364b0b1d5a190372c0f7d42c5ee9899f912b2 100644 (file)
@@ -1 +1,129 @@
-/*       +------------------------------------+\r *       | Inspire Internet Relay Chat Daemon |\r *       +------------------------------------+\r *\r *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r *            the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#define _CRT_SECURE_NO_DEPRECATE\r#define _SCL_SECURE_NO_DEPRECATE\r\r#include "inspircd.h"\r#include "users.h"\r#include "channels.h"\r#include "modules.h"\r\rtypedef std::map<irc::string,irc::string> censor_t;\r\r/* $ModDesc: Provides user and channel +G mode */\r\r/** Handles usermode +G\r */\rclass CensorUser : public ModeHandler\r{\r public:\r      CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }\r\r    ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)\r {\r              if (adding)\r            {\r                      if (!dest->IsModeSet('G'))\r                     {\r                              dest->SetMode('G',true);\r                               return MODEACTION_ALLOW;\r                       }\r              }\r              else\r           {\r                      if (dest->IsModeSet('G'))\r                      {\r                              dest->SetMode('G',false);\r                              return MODEACTION_ALLOW;\r                       }\r              }\r\r             return MODEACTION_DENY;\r        }\r};\r\r/** Handles channel mode +G\r */\rclass CensorChannel : public ModeHandler\r{\r public:\r      CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }\r\r      ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)\r {\r              if (adding)\r            {\r                      if (!channel->IsModeSet('G'))\r                  {\r                              channel->SetMode('G',true);\r                            return MODEACTION_ALLOW;\r                       }\r              }\r              else\r           {\r                      if (channel->IsModeSet('G'))\r                   {\r                              channel->SetMode('G',false);\r                           return MODEACTION_ALLOW;\r                       }\r              }\r\r             return MODEACTION_ALLOW;\r       }\r};\r\rclass ModuleCensor : public Module\r{\r\r    \r       censor_t censors;\r      CensorUser *cu;\r        CensorChannel *cc;\r \r public:\r  ModuleCensor(InspIRCd* Me)\r             : Module(Me)\r   {\r              /* Read the configuration file on startup.\r              */\r            OnRehash(NULL,"");\r             cu = new CensorUser(ServerInstance);\r           cc = new CensorChannel(ServerInstance);\r                if (!ServerInstance->AddMode(cu, 'G') || !ServerInstance->AddMode(cc, 'G'))\r                    throw ModuleException("Could not add new modes!");\r     }\r\r     void Implements(char* List)\r    {\r              List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;\r     }\r\r     virtual ~ModuleCensor()\r        {\r              ServerInstance->Modes->DelMode(cu);\r            ServerInstance->Modes->DelMode(cc);\r            DELETE(cu);\r            DELETE(cc);\r    }\r\r     virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)\r  {\r              if ((!pattern.empty()) && (!text.empty()))\r             {\r                      std::string::size_type pos;\r                    while ((pos = text.find(pattern)) != irc::string::npos)\r                        {\r                              text.erase(pos,pattern.length());\r                              text.insert(pos,replace);\r                      }\r              }\r      }\r\r     // format of a config entry is <badword text="shit" replace="poo">\r     virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)\r    {\r              if (!IS_LOCAL(user))\r                   return 0;\r\r             bool active = false;\r\r          if (target_type == TYPE_USER)\r                  active = ((userrec*)dest)->IsModeSet('G');\r             else if (target_type == TYPE_CHANNEL)\r                  active = ((chanrec*)dest)->IsModeSet('G');\r\r            if (!active)\r                   return 0;\r\r             irc::string text2 = text.c_str();\r              for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)\r              { \r                     if (text2.find(index->first) != irc::string::npos)\r                     {\r                              if (index->second.empty())\r                             {\r                                      user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked", user->nick, ((chanrec*)dest)->name, index->first.c_str());\r                                    return 1;\r                              }\r                              \r                               this->ReplaceLine(text2,index->first,index->second);\r                   }\r              }\r              text = text2.c_str();\r          return 0;\r      }\r      \r       virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)\r     {\r              return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);\r        }\r      \r       virtual void OnRehash(userrec* user, const std::string &parameter)\r     {\r              /*\r              * reload our config file on rehash - we must destroy and re-allocate the classes\r               * to call the constructor again and re-read our data.\r          */\r            ConfigReader* MyConf = new ConfigReader(ServerInstance);\r               censors.clear();\r               for (int index = 0; index < MyConf->Enumerate("badword"); index++)\r             {\r                      irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();\r                     irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();\r                  censors[pattern] = replace;\r            }\r              DELETE(MyConf);\r        }\r      \r       virtual Version GetVersion()\r   {\r              return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);\r       }\r      \r};\r\rMODULE_INIT(ModuleCensor)\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2005, 2007-2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2004, 2006, 2010 Craig Edwards <brain@inspircd.org>
+ *
+ * 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/>.
+ */
+
+
+#include "inspircd.h"
+#include "modules/exemption.h"
+
+typedef insp::flat_map<std::string, std::string, irc::insensitive_swo> CensorMap;
+
+class ModuleCensor : public Module
+{
+       CheckExemption::EventProvider exemptionprov;
+       CensorMap censors;
+       SimpleUserModeHandler cu;
+       SimpleChannelModeHandler cc;
+
+ public:
+       ModuleCensor()
+               : exemptionprov(this)
+               , cu(this, "u_censor", 'G')
+               , cc(this, "censor", 'G')
+       {
+       }
+
+       // format of a config entry is <badword text="shit" replace="poo">
+       ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
+       {
+               if (!IS_LOCAL(user))
+                       return MOD_RES_PASSTHRU;
+
+               switch (target.type)
+               {
+                       case MessageTarget::TYPE_USER:
+                       {
+                               User* targuser = target.Get<User>();
+                               if (!targuser->IsModeSet(cu))
+                                       return MOD_RES_PASSTHRU;
+                               break;
+                       }
+
+                       case MessageTarget::TYPE_CHANNEL:
+                       {
+                               Channel* targchan = target.Get<Channel>();
+                               if (!targchan->IsModeSet(cc))
+                                       return MOD_RES_PASSTHRU;
+
+                               ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
+                               if (result == MOD_RES_ALLOW)
+                                       return MOD_RES_PASSTHRU;
+                               break;
+                       }
+
+                       default:
+                               return MOD_RES_PASSTHRU;
+               }
+
+               for (CensorMap::const_iterator index = censors.begin(); index != censors.end(); ++index)
+               {
+                       size_t censorpos;
+                       while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
+                       {
+                               if (index->second.empty())
+                               {
+                                       const std::string msg = InspIRCd::Format("Your message to this channel contained a banned phrase (%s) and was blocked.", index->first.c_str());
+                                       if (target.type == MessageTarget::TYPE_CHANNEL)
+                                               user->WriteNumeric(Numerics::CannotSendTo(target.Get<Channel>(), msg));
+                                       else
+                                               user->WriteNumeric(Numerics::CannotSendTo(target.Get<User>(), msg));
+                                       return MOD_RES_DENY;
+                               }
+
+                               details.text.replace(censorpos, index->first.size(), index->second);
+                       }
+               }
+               return MOD_RES_PASSTHRU;
+       }
+
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
+       {
+               /*
+                * reload our config file on rehash - we must destroy and re-allocate the classes
+                * to call the constructor again and re-read our data.
+                */
+               CensorMap newcensors;
+               ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
+               for (ConfigIter i = badwords.first; i != badwords.second; ++i)
+               {
+                       ConfigTag* tag = i->second;
+                       const std::string text = tag->getString("text");
+                       if (text.empty())
+                               throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
+
+                       const std::string replace = tag->getString("replace");
+                       newcensors[text] = replace;
+               }
+               censors.swap(newcensors);
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Allows the server administrator to define inappropriate phrases that are not allowed to be used in private or channel messages.", VF_VENDOR);
+       }
+
+};
+
+MODULE_INIT(ModuleCensor)