]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Use a consistent naming scheme for operator privileges.
[user/henk/code/inspircd.git] / src / modules / m_deaf.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006-2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2012 satmd <satmd@satmd.dyndns.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 // User mode +d - filter out channel messages and channel notices
26 class DeafMode : public ModeHandler
27 {
28  public:
29         DeafMode(Module* Creator) : ModeHandler(Creator, "deaf", 'd', PARAM_NONE, MODETYPE_USER) { }
30
31         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
32         {
33                 if (adding == dest->IsModeSet(this))
34                         return MODEACTION_DENY;
35
36                 if (adding)
37                         dest->WriteNotice("*** You have enabled usermode +d, deaf mode. This mode means you WILL NOT receive any messages from any channels you are in. If you did NOT mean to do this, use /mode " + dest->nick + " -d.");
38
39                 dest->SetMode(this, adding);
40                 return MODEACTION_ALLOW;
41         }
42 };
43
44 // User mode +D - filter out user messages and user notices
45 class PrivDeafMode : public ModeHandler
46 {
47  public:
48         PrivDeafMode(Module* Creator) : ModeHandler(Creator, "privdeaf", 'D', PARAM_NONE, MODETYPE_USER)
49         {
50                 if (!ServerInstance->Config->ConfValue("deaf")->getBool("enableprivdeaf"))
51                         DisableAutoRegister();
52         }
53
54         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
55         {
56                 if (adding == dest->IsModeSet(this))
57                         return MODEACTION_DENY;
58
59                 if (adding)
60                         dest->WriteNotice("*** You have enabled usermode +D, private deaf mode. This mode means you WILL NOT receive any messages and notices from any nicks. If you did NOT mean to do this, use /mode " + dest->nick + " -D.");
61
62                 dest->SetMode(this, adding);
63                 return MODEACTION_ALLOW;
64         }
65 };
66
67 class ModuleDeaf : public Module
68 {
69         DeafMode deafmode;
70         PrivDeafMode privdeafmode;
71         std::string deaf_bypasschars;
72         std::string deaf_bypasschars_uline;
73         bool privdeafuline;
74
75  public:
76         ModuleDeaf()
77                 : deafmode(this)
78                 , privdeafmode(this)
79         {
80         }
81
82         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
83         {
84                 ConfigTag* tag = ServerInstance->Config->ConfValue("deaf");
85                 deaf_bypasschars = tag->getString("bypasschars");
86                 deaf_bypasschars_uline = tag->getString("bypasscharsuline");
87                 privdeafuline = tag->getBool("privdeafuline", true);
88         }
89
90         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
91         {
92                 if (target.type == MessageTarget::TYPE_CHANNEL)
93                 {
94                         Channel* chan = target.Get<Channel>();
95                         bool is_bypasschar = (deaf_bypasschars.find(details.text[0]) != std::string::npos);
96                         bool is_bypasschar_uline = (deaf_bypasschars_uline.find(details.text[0]) != std::string::npos);
97
98                         // If we have no bypasschars_uline in config, and this is a bypasschar (regular)
99                         // Then it is obviously going to get through +d, no exemption list required
100                         if (deaf_bypasschars_uline.empty() && is_bypasschar)
101                                 return MOD_RES_PASSTHRU;
102                         // If it matches both bypasschar and bypasschar_uline, it will get through.
103                         if (is_bypasschar && is_bypasschar_uline)
104                                 return MOD_RES_PASSTHRU;
105
106                         const Channel::MemberMap& ulist = chan->GetUsers();
107                         for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
108                         {
109                                 // not +d
110                                 if (!i->first->IsModeSet(deafmode))
111                                         continue;
112
113                                 bool is_a_uline = i->first->server->IsULine();
114                                 // matched a U-line only bypass
115                                 if (is_bypasschar_uline && is_a_uline)
116                                         continue;
117                                 // matched a regular bypass
118                                 if (is_bypasschar && !is_a_uline)
119                                         continue;
120
121                                 // don't deliver message!
122                                 details.exemptions.insert(i->first);
123                         }
124                 }
125                 else if (target.type == MessageTarget::TYPE_USER)
126                 {
127                         User* targ = target.Get<User>();
128                         if (!targ->IsModeSet(privdeafmode))
129                                 return MOD_RES_PASSTHRU;
130
131                         if (!privdeafuline && user->server->IsULine())
132                                 return MOD_RES_DENY;
133
134                         if (!user->HasPrivPermission("users/ignore-privdeaf"))
135                                 return MOD_RES_DENY;
136                 }
137
138                 return MOD_RES_PASSTHRU;
139         }
140
141         Version GetVersion() CXX11_OVERRIDE
142         {
143                 return Version("Provides usermodes +dD to block channel and/or user messages and notices", VF_VENDOR);
144         }
145 };
146
147 MODULE_INIT(ModuleDeaf)