]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_censor.cpp
Merge pull request #971 from SaberUK/master+numeric-xline
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
index a7aa2f8b13fe691a45afad70b552114ef655a324..da22b5153627745b2e44824406610123ca9df013 100644 (file)
@@ -1 +1,119 @@
-/*       +------------------------------------+\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) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2004, 2008-2009 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2005, 2007 Robin Burchell <robin+git@viroteck.net>
+ *
+ * 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"
+
+typedef insp::flat_map<irc::string, irc::string> censor_t;
+
+/** Handles usermode +G
+ */
+class CensorUser : public SimpleUserModeHandler
+{
+ public:
+       CensorUser(Module* Creator) : SimpleUserModeHandler(Creator, "u_censor", 'G') { }
+};
+
+/** Handles channel mode +G
+ */
+class CensorChannel : public SimpleChannelModeHandler
+{
+ public:
+       CensorChannel(Module* Creator) : SimpleChannelModeHandler(Creator, "censor", 'G') { }
+};
+
+class ModuleCensor : public Module
+{
+       censor_t censors;
+       CensorUser cu;
+       CensorChannel cc;
+
+ public:
+       ModuleCensor() : cu(this), cc(this) { }
+
+       // format of a config entry is <badword text="shit" replace="poo">
+       ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
+       {
+               if (!IS_LOCAL(user))
+                       return MOD_RES_PASSTHRU;
+
+               bool active = false;
+
+               if (target_type == TYPE_USER)
+                       active = ((User*)dest)->IsModeSet(cu);
+               else if (target_type == TYPE_CHANNEL)
+               {
+                       Channel* c = (Channel*)dest;
+                       active = c->IsModeSet(cc);
+                       ModResult res = ServerInstance->OnCheckExemption(user,c,"censor");
+
+                       if (res == MOD_RES_ALLOW)
+                               return MOD_RES_PASSTHRU;
+               }
+
+               if (!active)
+                       return MOD_RES_PASSTHRU;
+
+               irc::string text2 = text.c_str();
+               for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
+               {
+                       if (text2.find(index->first) != irc::string::npos)
+                       {
+                               if (index->second.empty())
+                               {
+                                       user->WriteNumeric(ERR_WORDFILTERED, "%s %s :Your message contained a censored word, and was blocked", ((Channel*)dest)->name.c_str(), index->first.c_str());
+                                       return MOD_RES_DENY;
+                               }
+
+                               SearchAndReplace(text2, index->first, index->second);
+                       }
+               }
+               text = text2.c_str();
+               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.
+                */
+               censors.clear();
+
+               ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
+               for (ConfigIter i = badwords.first; i != badwords.second; ++i)
+               {
+                       ConfigTag* tag = i->second;
+                       std::string str = tag->getString("text");
+                       irc::string pattern(str.c_str());
+                       str = tag->getString("replace");
+                       censors[pattern] = irc::string(str.c_str());
+               }
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Provides user and channel +G mode",VF_VENDOR);
+       }
+
+};
+
+MODULE_INIT(ModuleCensor)