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