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