]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Change /CHECK <#channel> to correctly report timestamp since it might have been TS...
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #define _CRT_SECURE_NO_DEPRECATE
15 #define _SCL_SECURE_NO_DEPRECATE
16
17 #include "inspircd.h"
18 #include <iostream>
19
20 typedef std::map<irc::string,irc::string> censor_t;
21
22 /* $ModDesc: Provides user and channel +G mode */
23
24 /** Handles usermode +G
25  */
26 class CensorUser : public SimpleUserModeHandler
27 {
28  public:
29         CensorUser(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'G') { }
30 };
31
32 /** Handles channel mode +G
33  */
34 class CensorChannel : public SimpleChannelModeHandler
35 {
36  public:
37         CensorChannel(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'G') { }
38 };
39
40 class ModuleCensor : public Module
41 {
42         censor_t censors;
43         CensorUser *cu;
44         CensorChannel *cc;
45
46  public:
47         ModuleCensor(InspIRCd* Me)
48                 : Module(Me)
49         {
50                 /* Read the configuration file on startup.
51                  */
52                 OnRehash(NULL,"");
53                 cu = new CensorUser(ServerInstance);
54                 cc = new CensorChannel(ServerInstance);
55                 if (!ServerInstance->Modes->AddMode(cu) || !ServerInstance->Modes->AddMode(cc))
56                 {
57                         delete cu;
58                         delete cc;
59                         throw ModuleException("Could not add new modes!");
60                 }
61                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRunTestSuite };
62                 ServerInstance->Modules->Attach(eventlist, this, 4);
63         }
64
65
66         virtual ~ModuleCensor()
67         {
68                 ServerInstance->Modes->DelMode(cu);
69                 ServerInstance->Modes->DelMode(cc);
70                 delete cu;
71                 delete cc;
72         }
73
74         virtual void OnRunTestSuite()
75         {
76                 std::cout << "Test suite for m_censor:" << std::endl;
77
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";
83                 pattern = "text";
84                 replace = "texts";
85                 std::cout << (ReplaceLine(text, pattern, replace) == "original texts here" ? "\nSUCCESS!\n" : "\nFAILURE: '" + text + "' \n");
86                 text = "original text";
87                 pattern = "original";
88                 replace = "new";
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;
91         }
92
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)
95         {
96                 irc::string replacement;
97                 if ((!pattern.empty()) && (!text.empty()))
98                 {
99                         for (std::string::size_type n = 0; n != text.length(); ++n)
100                         {
101                                 if (text.length() >= pattern.length() && text.substr(n, pattern.length()) == pattern)
102                                 {
103                                         /* Found the pattern in the text, replace it, and advance */
104                                         replacement.append(replace);
105                                         n = n + pattern.length() - 1;
106                                 }
107                                 else
108                                 {
109                                         replacement += text[n];
110                                 }
111                         }
112                 }
113                 text = replacement;
114                 return text;
115         }
116
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)
119         {
120                 if (!IS_LOCAL(user))
121                         return 0;
122
123                 bool active = false;
124
125                 if (target_type == TYPE_USER)
126                         active = ((User*)dest)->IsModeSet('G');
127                 else if (target_type == TYPE_CHANNEL)
128                 {
129                         active = ((Channel*)dest)->IsModeSet('G');
130                         Channel* c = (Channel*)dest;
131                         if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
132                         {
133                                 return 0;
134                         }
135                 }
136
137                 if (!active)
138                         return 0;
139
140                 irc::string text2 = text.c_str();
141                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
142                 {
143                         if (text2.find(index->first) != irc::string::npos)
144                         {
145                                 if (index->second.empty())
146                                 {
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());
148                                         return 1;
149                                 }
150
151                                 this->ReplaceLine(text2,index->first,index->second);
152                         }
153                 }
154                 text = text2.c_str();
155                 return 0;
156         }
157
158         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
159         {
160                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
161         }
162
163         virtual void OnRehash(User* user, const std::string &parameter)
164         {
165                 /*
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.
168                  */
169                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
170                 censors.clear();
171
172                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
173                 {
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;
177                 }
178
179                 delete MyConf;
180         }
181
182         virtual Version GetVersion()
183         {
184                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
185         }
186
187 };
188
189 MODULE_INIT(ModuleCensor)