]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_censor.cpp
Include explicit routing information in Command, will replace CMD_LOCALONLY return...
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
index a7aa2f8b13fe691a45afad70b552114ef655a324..9c68be7fdcb23a62771d0d9deb7377fbe58ec063 100644 (file)
@@ -1 +1,138 @@
-/*       +------------------------------------+\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
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
+ *
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+#define _CRT_SECURE_NO_DEPRECATE
+#define _SCL_SECURE_NO_DEPRECATE
+
+#include "inspircd.h"
+#include <iostream>
+
+typedef std::map<irc::string,irc::string> censor_t;
+
+/* $ModDesc: Provides user and channel +G mode */
+
+/** Handles usermode +G
+ */
+class CensorUser : public SimpleUserModeHandler
+{
+ public:
+       CensorUser(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'G') { }
+};
+
+/** Handles channel mode +G
+ */
+class CensorChannel : public SimpleChannelModeHandler
+{
+ public:
+       CensorChannel(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'G') { }
+};
+
+class ModuleCensor : public Module
+{
+       censor_t censors;
+       CensorUser cu;
+       CensorChannel cc;
+
+ public:
+       ModuleCensor(InspIRCd* Me)
+               : Module(Me), cu(Me), cc(Me)
+       {
+               /* Read the configuration file on startup.
+                */
+               OnRehash(NULL);
+               if (!ServerInstance->Modes->AddMode(&cu) || !ServerInstance->Modes->AddMode(&cc))
+                       throw ModuleException("Could not add new modes!");
+               Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRunTestSuite };
+               ServerInstance->Modules->Attach(eventlist, this, 4);
+       }
+
+
+       virtual ~ModuleCensor()
+       {
+               ServerInstance->Modes->DelMode(&cu);
+               ServerInstance->Modes->DelMode(&cc);
+       }
+
+       // format of a config entry is <badword text="shit" replace="poo">
+       virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
+       {
+               if (!IS_LOCAL(user))
+                       return 0;
+
+               bool active = false;
+
+               if (target_type == TYPE_USER)
+                       active = ((User*)dest)->IsModeSet('G');
+               else if (target_type == TYPE_CHANNEL)
+               {
+                       active = ((Channel*)dest)->IsModeSet('G');
+                       Channel* c = (Channel*)dest;
+                       if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
+                       {
+                               return 0;
+                       }
+               }
+
+               if (!active)
+                       return 0;
+
+               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 %s :Your message contained a censored word, and was blocked", user->nick.c_str(), ((Channel*)dest)->name.c_str(), index->first.c_str());
+                                       return 1;
+                               }
+
+                               SearchAndReplace(text2, index->first, index->second);
+                       }
+               }
+               text = text2.c_str();
+               return 0;
+       }
+
+       virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
+       {
+               return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
+       }
+
+       virtual void OnRehash(User* user)
+       {
+               /*
+                * reload our config file on rehash - we must destroy and re-allocate the classes
+                * to call the constructor again and re-read our data.
+                */
+               ConfigReader* MyConf = new ConfigReader(ServerInstance);
+               censors.clear();
+
+               for (int index = 0; index < MyConf->Enumerate("badword"); index++)
+               {
+                       irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
+                       irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
+                       censors[pattern] = replace;
+               }
+
+               delete MyConf;
+       }
+
+       virtual Version GetVersion()
+       {
+               return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
+       }
+
+};
+
+MODULE_INIT(ModuleCensor)