]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Update all wiki links to point to the new wiki. This was done automatically with...
[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://wiki.inspircd.org/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         // format of a config entry is <badword text="shit" replace="poo">
75         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
76         {
77                 if (!IS_LOCAL(user))
78                         return 0;
79
80                 bool active = false;
81
82                 if (target_type == TYPE_USER)
83                         active = ((User*)dest)->IsModeSet('G');
84                 else if (target_type == TYPE_CHANNEL)
85                 {
86                         active = ((Channel*)dest)->IsModeSet('G');
87                         Channel* c = (Channel*)dest;
88                         if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
89                         {
90                                 return 0;
91                         }
92                 }
93
94                 if (!active)
95                         return 0;
96
97                 irc::string text2 = text.c_str();
98                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
99                 {
100                         if (text2.find(index->first) != irc::string::npos)
101                         {
102                                 if (index->second.empty())
103                                 {
104                                         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());
105                                         return 1;
106                                 }
107
108                                 SearchAndReplace(text2, index->first, index->second);
109                         }
110                 }
111                 text = text2.c_str();
112                 return 0;
113         }
114
115         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
116         {
117                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
118         }
119
120         virtual void OnRehash(User* user, const std::string &parameter)
121         {
122                 /*
123                  * reload our config file on rehash - we must destroy and re-allocate the classes
124                  * to call the constructor again and re-read our data.
125                  */
126                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
127                 censors.clear();
128
129                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
130                 {
131                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
132                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
133                         censors[pattern] = replace;
134                 }
135
136                 delete MyConf;
137         }
138
139         virtual Version GetVersion()
140         {
141                 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
142         }
143
144 };
145
146 MODULE_INIT(ModuleCensor)