]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Remove dummy API_VERSION from Version constructor
[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(Module* Creator) : SimpleUserModeHandler(Creator, "censor", 'G') { }
30 };
31
32 /** Handles channel mode +G
33  */
34 class CensorChannel : public SimpleChannelModeHandler
35 {
36  public:
37         CensorChannel(Module* Creator) : SimpleChannelModeHandler(Creator, "censor", 'G') { }
38 };
39
40 class ModuleCensor : public Module
41 {
42         censor_t censors;
43         CensorUser cu;
44         CensorChannel cc;
45
46  public:
47         ModuleCensor()
48                 : cu(this), cc(this)
49         {
50                 /* Read the configuration file on startup.
51                  */
52                 OnRehash(NULL);
53                 if (!ServerInstance->Modes->AddMode(&cu) || !ServerInstance->Modes->AddMode(&cc))
54                         throw ModuleException("Could not add new modes!");
55                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRunTestSuite };
56                 ServerInstance->Modules->Attach(eventlist, this, 4);
57         }
58
59
60         virtual ~ModuleCensor()
61         {
62         }
63
64         // format of a config entry is <badword text="shit" replace="poo">
65         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
66         {
67                 if (!IS_LOCAL(user))
68                         return MOD_RES_PASSTHRU;
69
70                 bool active = false;
71
72                 if (target_type == TYPE_USER)
73                         active = ((User*)dest)->IsModeSet('G');
74                 else if (target_type == TYPE_CHANNEL)
75                 {
76                         active = ((Channel*)dest)->IsModeSet('G');
77                         Channel* c = (Channel*)dest;
78                         ModResult res;
79                         FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,c,"censor"));
80
81                         if (res == MOD_RES_ALLOW)
82                                 return MOD_RES_PASSTHRU;
83                 }
84
85                 if (!active)
86                         return MOD_RES_PASSTHRU;
87
88                 irc::string text2 = text.c_str();
89                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
90                 {
91                         if (text2.find(index->first) != irc::string::npos)
92                         {
93                                 if (index->second.empty())
94                                 {
95                                         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());
96                                         return MOD_RES_DENY;
97                                 }
98
99                                 SearchAndReplace(text2, index->first, index->second);
100                         }
101                 }
102                 text = text2.c_str();
103                 return MOD_RES_PASSTHRU;
104         }
105
106         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
107         {
108                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
109         }
110
111         virtual void OnRehash(User* user)
112         {
113                 /*
114                  * reload our config file on rehash - we must destroy and re-allocate the classes
115                  * to call the constructor again and re-read our data.
116                  */
117                 ConfigReader* MyConf = new ConfigReader;
118                 censors.clear();
119
120                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
121                 {
122                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
123                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
124                         censors[pattern] = replace;
125                 }
126
127                 delete MyConf;
128         }
129
130         virtual Version GetVersion()
131         {
132                 return Version("Provides user and channel +G mode",VF_COMMON|VF_VENDOR);
133         }
134
135 };
136
137 MODULE_INIT(ModuleCensor)