]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Update copyright headers.
[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) 2008 Craig Edwards <brain@inspircd.org>
11  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
12  *   Copyright (C) 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 enum
31 {
32         // From UnrealIRCd.
33         ERR_CANNOTKNOCK = 480,
34
35         // From ircd-ratbox.
36         ERR_CHANOPEN = 713,
37         ERR_KNOCKONCHAN = 714
38 };
39
40 /** Handles the /KNOCK command
41  */
42 class CommandKnock : public Command
43 {
44         SimpleChannelModeHandler& noknockmode;
45         ChanModeReference inviteonlymode;
46
47  public:
48         bool sendnotice;
49         bool sendnumeric;
50         CommandKnock(Module* Creator, SimpleChannelModeHandler& Noknockmode)
51                 : Command(Creator,"KNOCK", 2, 2)
52                 , noknockmode(Noknockmode)
53                 , inviteonlymode(Creator, "inviteonly")
54         {
55                 syntax = "<channel> :<reason>";
56                 Penalty = 5;
57         }
58
59         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
60         {
61                 Channel* c = ServerInstance->FindChan(parameters[0]);
62                 if (!c)
63                 {
64                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
65                         return CMD_FAILURE;
66                 }
67
68                 if (c->HasUser(user))
69                 {
70                         user->WriteNumeric(ERR_KNOCKONCHAN, c->name, InspIRCd::Format("Can't KNOCK on %s, you are already on that channel.", c->name.c_str()));
71                         return CMD_FAILURE;
72                 }
73
74                 if (c->IsModeSet(noknockmode))
75                 {
76                         user->WriteNumeric(ERR_CANNOTKNOCK, InspIRCd::Format("Can't KNOCK on %s, +K is set.", c->name.c_str()));
77                         return CMD_FAILURE;
78                 }
79
80                 if (!c->IsModeSet(inviteonlymode))
81                 {
82                         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()));
83                         return CMD_FAILURE;
84                 }
85
86                 if (sendnotice)
87                         c->WriteNotice(InspIRCd::Format("User %s is KNOCKing on %s (%s)", user->nick.c_str(), c->name.c_str(), parameters[1].c_str()));
88
89                 if (sendnumeric)
90                 {
91                         Numeric::Numeric numeric(710);
92                         numeric.push(c->name).push(user->GetFullHost()).push("is KNOCKing: " + parameters[1]);
93
94                         ClientProtocol::Messages::Numeric numericmsg(numeric, c->name);
95                         c->Write(ServerInstance->GetRFCEvents().numeric, numericmsg);
96                 }
97
98                 user->WriteNotice("KNOCKing on " + c->name);
99                 return CMD_SUCCESS;
100         }
101
102         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
103         {
104                 return ROUTE_OPT_BCAST;
105         }
106 };
107
108 class ModuleKnock : public Module
109 {
110         SimpleChannelModeHandler kn;
111         CommandKnock cmd;
112
113  public:
114         ModuleKnock()
115                 : kn(this, "noknock", 'K')
116                 , cmd(this, kn)
117         {
118         }
119
120         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
121         {
122                 std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
123                 if (stdalgo::string::equalsci(knocknotify, "numeric"))
124                 {
125                         cmd.sendnotice = false;
126                         cmd.sendnumeric = true;
127                 }
128                 else if (stdalgo::string::equalsci(knocknotify, "both"))
129                 {
130                         cmd.sendnotice = true;
131                         cmd.sendnumeric = true;
132                 }
133                 else
134                 {
135                         cmd.sendnotice = true;
136                         cmd.sendnumeric = false;
137                 }
138         }
139
140         Version GetVersion() CXX11_OVERRIDE
141         {
142                 return Version("Provides the KNOCK command and channel mode +K", VF_OPTCOMMON | VF_VENDOR);
143         }
144 };
145
146 MODULE_INIT(ModuleKnock)