]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2013, 2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2013, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2004, 2006, 2008, 2010 Craig Edwards <brain@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 enum
31 {
32         // From UnrealIRCd.
33         ERR_CANNOTKNOCK = 480,
34
35         // From ircd-ratbox.
36         RPL_KNOCK = 710,
37         RPL_KNOCKDLVR = 711,
38         ERR_CHANOPEN = 713,
39         ERR_KNOCKONCHAN = 714
40 };
41
42 /** Handles the /KNOCK command
43  */
44 class CommandKnock : public Command
45 {
46         SimpleChannelModeHandler& noknockmode;
47         ChanModeReference inviteonlymode;
48
49  public:
50         bool sendnotice;
51         bool sendnumeric;
52         CommandKnock(Module* Creator, SimpleChannelModeHandler& Noknockmode)
53                 : Command(Creator,"KNOCK", 2, 2)
54                 , noknockmode(Noknockmode)
55                 , inviteonlymode(Creator, "inviteonly")
56         {
57                 syntax = "<channel> :<reason>";
58                 Penalty = 5;
59         }
60
61         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
62         {
63                 Channel* c = ServerInstance->FindChan(parameters[0]);
64                 if (!c)
65                 {
66                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
67                         return CMD_FAILURE;
68                 }
69
70                 if (c->HasUser(user))
71                 {
72                         user->WriteNumeric(ERR_KNOCKONCHAN, c->name, InspIRCd::Format("Can't KNOCK on %s, you are already on that channel.", c->name.c_str()));
73                         return CMD_FAILURE;
74                 }
75
76                 if (c->IsModeSet(noknockmode))
77                 {
78                         user->WriteNumeric(ERR_CANNOTKNOCK, InspIRCd::Format("Can't KNOCK on %s, +K is set.", c->name.c_str()));
79                         return CMD_FAILURE;
80                 }
81
82                 if (!c->IsModeSet(inviteonlymode))
83                 {
84                         user->WriteNumeric(ERR_CHANOPEN, c->name, InspIRCd::Format("Can't KNOCK on %s, channel is not invite only so knocking is pointless!", c->name.c_str()));
85                         return CMD_FAILURE;
86                 }
87
88                 if (sendnotice)
89                 {
90                         c->WriteNotice(InspIRCd::Format("User %s is KNOCKing on %s (%s)", user->nick.c_str(), c->name.c_str(), parameters[1].c_str()));
91                         user->WriteNotice("KNOCKing on " + c->name);
92                 }
93
94                 if (sendnumeric)
95                 {
96                         Numeric::Numeric numeric(RPL_KNOCK);
97                         numeric.push(c->name).push(user->GetFullHost()).push("is KNOCKing: " + parameters[1]);
98
99                         ClientProtocol::Messages::Numeric numericmsg(numeric, c->name);
100                         c->Write(ServerInstance->GetRFCEvents().numeric, numericmsg);
101
102                         user->WriteNumeric(RPL_KNOCKDLVR, c->name, "KNOCKing on channel");
103                 }
104
105                 return CMD_SUCCESS;
106         }
107
108         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
109         {
110                 return ROUTE_OPT_BCAST;
111         }
112 };
113
114 class ModuleKnock : public Module
115 {
116         SimpleChannelModeHandler kn;
117         CommandKnock cmd;
118
119  public:
120         ModuleKnock()
121                 : kn(this, "noknock", 'K')
122                 , cmd(this, kn)
123         {
124         }
125
126         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
127         {
128                 std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
129                 if (stdalgo::string::equalsci(knocknotify, "numeric"))
130                 {
131                         cmd.sendnotice = false;
132                         cmd.sendnumeric = true;
133                 }
134                 else if (stdalgo::string::equalsci(knocknotify, "both"))
135                 {
136                         cmd.sendnotice = true;
137                         cmd.sendnumeric = true;
138                 }
139                 else
140                 {
141                         cmd.sendnotice = true;
142                         cmd.sendnumeric = false;
143                 }
144         }
145
146         Version GetVersion() CXX11_OVERRIDE
147         {
148                 return Version("Adds the /KNOCK command which allows users to request access to an invite-only channel and channel mode K (noknock) which allows channels to disable usage of this command.", VF_OPTCOMMON | VF_VENDOR);
149         }
150 };
151
152 MODULE_INIT(ModuleKnock)