]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
4978f66292cb4af4def649f9066e50a2cf192589
[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 /* $ModDesc: Provides support for /KNOCK and channel mode +K */
25
26 /** Handles the /KNOCK command
27  */
28 class CommandKnock : public Command
29 {
30  public:
31         bool sendnotice;
32         bool sendnumeric;
33         CommandKnock(Module* Creator) : Command(Creator,"KNOCK", 2, 2)
34         {
35                 syntax = "<channel> <reason>";
36                 Penalty = 5;
37         }
38
39         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
40         {
41                 Channel* c = ServerInstance->FindChan(parameters[0]);
42                 if (!c)
43                 {
44                         user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
45                         return CMD_FAILURE;
46                 }
47
48                 if (c->HasUser(user))
49                 {
50                         user->WriteNumeric(480, "%s :Can't KNOCK on %s, you are already on that channel.", user->nick.c_str(), c->name.c_str());
51                         return CMD_FAILURE;
52                 }
53
54                 if (c->IsModeSet('K'))
55                 {
56                         user->WriteNumeric(480, "%s :Can't KNOCK on %s, +K is set.",user->nick.c_str(), c->name.c_str());
57                         return CMD_FAILURE;
58                 }
59
60                 if (!c->IsModeSet('i'))
61                 {
62                         user->WriteNumeric(480, "%s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick.c_str(), c->name.c_str());
63                         return CMD_FAILURE;
64                 }
65
66                 if (sendnotice)
67                         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());
68
69                 if (sendnumeric)
70                         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());
71
72                 user->WriteNotice("KNOCKing on " + c->name);
73                 return CMD_SUCCESS;
74         }
75
76         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
77         {
78                 return ROUTE_OPT_BCAST;
79         }
80 };
81
82 /** Handles channel mode +K
83  */
84 class Knock : public SimpleChannelModeHandler
85 {
86  public:
87         Knock(Module* Creator) : SimpleChannelModeHandler(Creator, "noknock", 'K') { }
88 };
89
90 class ModuleKnock : public Module
91 {
92         CommandKnock cmd;
93         Knock kn;
94  public:
95         ModuleKnock() : cmd(this), kn(this)
96         {
97         }
98
99         void init() CXX11_OVERRIDE
100         {
101                 ServerInstance->Modules->AddService(kn);
102                 ServerInstance->Modules->AddService(cmd);
103
104                 ServerInstance->Modules->Attach(I_OnRehash, this);
105                 OnRehash(NULL);
106         }
107
108         void OnRehash(User* user) CXX11_OVERRIDE
109         {
110                 std::string knocknotify = ServerInstance->Config->ConfValue("knock")->getString("notify");
111                 irc::string notify(knocknotify.c_str());
112
113                 if (notify == "numeric")
114                 {
115                         cmd.sendnotice = false;
116                         cmd.sendnumeric = true;
117                 }
118                 else if (notify == "both")
119                 {
120                         cmd.sendnotice = true;
121                         cmd.sendnumeric = true;
122                 }
123                 else
124                 {
125                         cmd.sendnotice = true;
126                         cmd.sendnumeric = false;
127                 }
128         }
129
130         Version GetVersion() CXX11_OVERRIDE
131         {
132                 return Version("Provides support for /KNOCK and channel mode +K", VF_OPTCOMMON | VF_VENDOR);
133         }
134 };
135
136 MODULE_INIT(ModuleKnock)