]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2004-2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /** Handles the /KNOCK command
25  */
26 class CommandKnock : public Command
27 {
28         SimpleChannelModeHandler& noknockmode;
29         ChanModeReference inviteonlymode;
30
31  public:
32         bool sendnotice;
33         bool sendnumeric;
34         CommandKnock(Module* Creator, SimpleChannelModeHandler& Noknockmode)
35                 : Command(Creator,"KNOCK", 2, 2)
36                 , noknockmode(Noknockmode)
37                 , inviteonlymode(Creator, "inviteonly")
38         {
39                 syntax = "<channel> <reason>";
40                 Penalty = 5;
41         }
42
43         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
44         {
45                 Channel* c = ServerInstance->FindChan(parameters[0]);
46                 if (!c)
47                 {
48                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
49                         return CMD_FAILURE;
50                 }
51
52                 if (c->HasUser(user))
53                 {
54                         user->WriteNumeric(ERR_KNOCKONCHAN, c->name, InspIRCd::Format("Can't KNOCK on %s, you are already on that channel.", c->name.c_str()));
55                         return CMD_FAILURE;
56                 }
57
58                 if (c->IsModeSet(noknockmode))
59                 {
60                         user->WriteNumeric(480, InspIRCd::Format("Can't KNOCK on %s, +K is set.", c->name.c_str()));
61                         return CMD_FAILURE;
62                 }
63
64                 if (!c->IsModeSet(inviteonlymode))
65                 {
66                         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()));
67                         return CMD_FAILURE;
68                 }
69
70                 if (sendnotice)
71                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name.c_str(), user->nick.c_str(), c->name.c_str(), parameters[1].c_str());
72
73                 if (sendnumeric)
74                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "710 %s %s %s :is KNOCKing: %s", c->name.c_str(), c->name.c_str(), user->GetFullHost().c_str(), parameters[1].c_str());
75
76                 user->WriteNotice("KNOCKing on " + c->name);
77                 return CMD_SUCCESS;
78         }
79
80         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
81         {
82                 return ROUTE_OPT_BCAST;
83         }
84 };
85
86 class ModuleKnock : public Module
87 {
88         SimpleChannelModeHandler kn;
89         CommandKnock cmd;
90
91  public:
92         ModuleKnock()
93                 : kn(this, "noknock", 'K')
94                 , cmd(this, kn)
95         {
96         }
97
98         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
99         {
100                 std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
101                 irc::string notify(knocknotify.c_str());
102
103                 if (notify == "numeric")
104                 {
105                         cmd.sendnotice = false;
106                         cmd.sendnumeric = true;
107                 }
108                 else if (notify == "both")
109                 {
110                         cmd.sendnotice = true;
111                         cmd.sendnumeric = true;
112                 }
113                 else
114                 {
115                         cmd.sendnotice = true;
116                         cmd.sendnumeric = false;
117                 }
118         }
119
120         Version GetVersion() CXX11_OVERRIDE
121         {
122                 return Version("Provides support for /KNOCK and channel mode +K", VF_OPTCOMMON | VF_VENDOR);
123         }
124 };
125
126 MODULE_INIT(ModuleKnock)