1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #define _CRT_SECURE_NO_DEPRECATE
15 #define _SCL_SECURE_NO_DEPRECATE
20 typedef std::map<irc::string,irc::string> censor_t;
22 /* $ModDesc: Provides user and channel +G mode */
24 /** Handles usermode +G
26 class CensorUser : public SimpleUserModeHandler
29 CensorUser(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'G') { }
32 /** Handles channel mode +G
34 class CensorChannel : public SimpleChannelModeHandler
37 CensorChannel(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'G') { }
40 class ModuleCensor : public Module
47 ModuleCensor(InspIRCd* Me)
50 /* Read the configuration file on startup.
53 cu = new CensorUser(ServerInstance);
54 cc = new CensorChannel(ServerInstance);
55 if (!ServerInstance->Modes->AddMode(cu) || !ServerInstance->Modes->AddMode(cc))
59 throw ModuleException("Could not add new modes!");
61 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRunTestSuite };
62 ServerInstance->Modules->Attach(eventlist, this, 4);
66 virtual ~ModuleCensor()
68 ServerInstance->Modes->DelMode(cu);
69 ServerInstance->Modes->DelMode(cc);
74 virtual void OnRunTestSuite()
76 std::cout << "Test suite for m_censor:" << std::endl;
78 irc::string text = "original text";
79 irc::string pattern = "text";
80 irc::string replace = "new";
81 std::cout << (ReplaceLine(text, pattern, replace) == "original new" ? "\nSUCCESS!\n" : "\nFAILURE '" + text + "' \n");
82 text = "original text here";
85 std::cout << (ReplaceLine(text, pattern, replace) == "original texts here" ? "\nSUCCESS!\n" : "\nFAILURE: '" + text + "' \n");
86 text = "original text";
89 std::cout << (ReplaceLine(text, pattern, replace) == "new text" ? "\nSUCCESS!\n" : "\nFAILURE '" + text + "' \n");
90 std::cout << "end of test suite for m_censor" << std::endl;
93 /* This version of ReplaceLine won't loop forever if the replacement string contains the source pattern */
94 virtual const irc::string& ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
96 irc::string replacement;
97 if ((!pattern.empty()) && (!text.empty()))
99 for (std::string::size_type n = 0; n != text.length(); ++n)
101 if (text.length() >= pattern.length() && text.substr(n, pattern.length()) == pattern)
103 /* Found the pattern in the text, replace it, and advance */
104 replacement.append(replace);
105 n = n + pattern.length() - 1;
109 replacement += text[n];
117 // format of a config entry is <badword text="shit" replace="poo">
118 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
125 if (target_type == TYPE_USER)
126 active = ((User*)dest)->IsModeSet('G');
127 else if (target_type == TYPE_CHANNEL)
129 active = ((Channel*)dest)->IsModeSet('G');
130 Channel* c = (Channel*)dest;
131 if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
140 irc::string text2 = text.c_str();
141 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
143 if (text2.find(index->first) != irc::string::npos)
145 if (index->second.empty())
147 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());
151 this->ReplaceLine(text2,index->first,index->second);
154 text = text2.c_str();
158 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
160 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
163 virtual void OnRehash(User* user, const std::string ¶meter)
166 * reload our config file on rehash - we must destroy and re-allocate the classes
167 * to call the constructor again and re-read our data.
169 ConfigReader* MyConf = new ConfigReader(ServerInstance);
172 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
174 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
175 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
176 censors[pattern] = replace;
182 virtual Version GetVersion()
184 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
189 MODULE_INIT(ModuleCensor)