]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Add support for blocking tag messages with the deaf mode.
[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 #include "modules/ctctags.h"
30
31 // User mode +d - filter out channel messages and channel notices
32 class DeafMode : public ModeHandler
33 {
34  public:
35         DeafMode(Module* Creator) : ModeHandler(Creator, "deaf", 'd', PARAM_NONE, MODETYPE_USER) { }
36
37         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
38         {
39                 if (adding == dest->IsModeSet(this))
40                         return MODEACTION_DENY;
41
42                 if (adding)
43                         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.");
44
45                 dest->SetMode(this, adding);
46                 return MODEACTION_ALLOW;
47         }
48 };
49
50 // User mode +D - filter out user messages and user notices
51 class PrivDeafMode : public ModeHandler
52 {
53  public:
54         PrivDeafMode(Module* Creator) : ModeHandler(Creator, "privdeaf", 'D', PARAM_NONE, MODETYPE_USER)
55         {
56                 if (!ServerInstance->Config->ConfValue("deaf")->getBool("enableprivdeaf"))
57                         DisableAutoRegister();
58         }
59
60         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
61         {
62                 if (adding == dest->IsModeSet(this))
63                         return MODEACTION_DENY;
64
65                 if (adding)
66                         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.");
67
68                 dest->SetMode(this, adding);
69                 return MODEACTION_ALLOW;
70         }
71 };
72
73 class ModuleDeaf
74         : public Module
75         , public CTCTags::EventListener
76 {
77  private:
78         DeafMode deafmode;
79         PrivDeafMode privdeafmode;
80         std::string deaf_bypasschars;
81         std::string deaf_bypasschars_uline;
82         bool privdeafuline;
83
84         ModResult HandleChannel(User* source, Channel* target, CUList& exemptions, bool is_bypasschar, bool is_bypasschar_uline)
85         {
86                 const Channel::MemberMap& ulist = target->GetUsers();
87                 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
88                 {
89                         User* member = i->first;
90
91                         // Allow if the user doesn't have the mode set.
92                         if (!member->IsModeSet(deafmode))
93                                 continue;
94
95                         // Allow if the message begins with a uline char and the
96                         // user is on a ulined server.
97                         if (is_bypasschar_uline && member->server->IsULine())
98                                 continue;
99
100                         // Allow if the prefix begins with a normal char and the
101                         // user is not on a ulined server.
102                         if (is_bypasschar && !member->server->IsULine())
103                                 continue;
104
105                         exemptions.insert(member);
106                 }
107
108                 return MOD_RES_PASSTHRU;
109         }
110
111         ModResult HandleUser(User* source, User* target)
112         {
113                 // Allow if the mode is not set.
114                 if (!target->IsModeSet(privdeafmode))
115                         return MOD_RES_PASSTHRU;
116
117                 // Reject if the source is ulined and privdeafuline is disaled.
118                 if (!privdeafuline && source->server->IsULine())
119                         return MOD_RES_DENY;
120
121                 // Reject if the source doesn't have the right priv.
122                 if (!source->HasPrivPermission("users/ignore-privdeaf"))
123                         return MOD_RES_DENY;
124
125                 return MOD_RES_ALLOW;
126         }
127
128  public:
129         ModuleDeaf()
130                 : CTCTags::EventListener(this)
131                 , deafmode(this)
132                 , privdeafmode(this)
133         {
134         }
135
136         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
137         {
138                 ConfigTag* tag = ServerInstance->Config->ConfValue("deaf");
139                 deaf_bypasschars = tag->getString("bypasschars");
140                 deaf_bypasschars_uline = tag->getString("bypasscharsuline");
141                 privdeafuline = tag->getBool("privdeafuline", true);
142         }
143
144         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
145         {
146                 switch (target.type)
147                 {
148                         case MessageTarget::TYPE_CHANNEL:
149                                 return HandleChannel(user, target.Get<Channel>(), details.exemptions, false, false);
150
151                         case MessageTarget::TYPE_USER:
152                                 return HandleUser(user, target.Get<User>());
153
154                         case MessageTarget::TYPE_SERVER:
155                                 break;
156                 }
157
158                 return MOD_RES_PASSTHRU;
159         }
160
161         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
162         {
163                 switch (target.type)
164                 {
165                         case MessageTarget::TYPE_CHANNEL:
166                         {
167                                 // If we have no bypasschars_uline in config, and this is a bypasschar (regular)
168                                 // Then it is obviously going to get through +d, no exemption list required
169                                 bool is_bypasschar = (deaf_bypasschars.find(details.text[0]) != std::string::npos);
170                                 if (deaf_bypasschars_uline.empty() && is_bypasschar)
171                                         return MOD_RES_PASSTHRU;
172
173                                 // If it matches both bypasschar and bypasschar_uline, it will get through.
174                                 bool is_bypasschar_uline = (deaf_bypasschars_uline.find(details.text[0]) != std::string::npos);
175                                 if (is_bypasschar && is_bypasschar_uline)
176                                         return MOD_RES_PASSTHRU;
177
178                                 return HandleChannel(user, target.Get<Channel>(), details.exemptions, is_bypasschar, is_bypasschar_uline);
179                         }
180
181                         case MessageTarget::TYPE_USER:
182                                 return HandleUser(user, target.Get<User>());
183
184                         case MessageTarget::TYPE_SERVER:
185                                 break;
186                 }
187
188                 return MOD_RES_PASSTHRU;
189         }
190
191         Version GetVersion() CXX11_OVERRIDE
192         {
193                 return Version("Adds user mode d (deaf) which prevents users from receiving channel messages.", VF_VENDOR);
194         }
195 };
196
197 MODULE_INIT(ModuleDeaf)