]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
e360e2a2e6a117cb320423e57dc3f22ebf4eb78c
[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 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.");
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 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.");
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                 switch (target.type)
93                 {
94                         case MessageTarget::TYPE_CHANNEL:
95                         {
96                                 Channel* chan = target.Get<Channel>();
97                                 bool is_bypasschar = (deaf_bypasschars.find(details.text[0]) != std::string::npos);
98                                 bool is_bypasschar_uline = (deaf_bypasschars_uline.find(details.text[0]) != std::string::npos);
99
100                                 // If we have no bypasschars_uline in config, and this is a bypasschar (regular)
101                                 // Then it is obviously going to get through +d, no exemption list required
102                                 if (deaf_bypasschars_uline.empty() && is_bypasschar)
103                                         return MOD_RES_PASSTHRU;
104                                 // If it matches both bypasschar and bypasschar_uline, it will get through.
105                                 if (is_bypasschar && is_bypasschar_uline)
106                                         return MOD_RES_PASSTHRU;
107
108                                 const Channel::MemberMap& ulist = chan->GetUsers();
109                                 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
110                                 {
111                                         // not +d
112                                         if (!i->first->IsModeSet(deafmode))
113                                                 continue;
114
115                                         bool is_a_uline = i->first->server->IsULine();
116                                         // matched a U-line only bypass
117                                         if (is_bypasschar_uline && is_a_uline)
118                                                 continue;
119                                         // matched a regular bypass
120                                         if (is_bypasschar && !is_a_uline)
121                                                 continue;
122
123                                         // don't deliver message!
124                                         details.exemptions.insert(i->first);
125                                 }
126                                 break;
127                         }
128                         case MessageTarget::TYPE_USER:
129                         {
130                                 User* targ = target.Get<User>();
131                                 if (!targ->IsModeSet(privdeafmode))
132                                         return MOD_RES_PASSTHRU;
133
134                                 if (!privdeafuline && user->server->IsULine())
135                                         return MOD_RES_DENY;
136
137                                 if (!user->HasPrivPermission("users/ignore-privdeaf"))
138                                         return MOD_RES_DENY;
139
140                                 break;
141                         }
142                         case MessageTarget::TYPE_SERVER:
143                                 break;
144                 }
145
146                 return MOD_RES_PASSTHRU;
147         }
148
149         Version GetVersion() CXX11_OVERRIDE
150         {
151                 return Version("Provides user modes +d and +D to block channel and user messages/notices", VF_VENDOR);
152         }
153 };
154
155 MODULE_INIT(ModuleDeaf)