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