]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
a7aa2f8b13fe691a45afad70b552114ef655a324
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*       +------------------------------------+\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