]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Wheee, mass commit! this adds const stafety, throwing a compile error if anyone does...
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support for /KNOCK and mode +K */
17
18 /** Handles the /KNOCK command
19  */
20 class CommandKnock : public Command
21 {
22  public:
23         CommandKnock (InspIRCd* Instance) : Command(Instance,"KNOCK", 0, 2)
24         {
25                 this->source = "m_knock.so";
26                 syntax = "<channel> <reason>";
27                 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
28         }
29         
30         CmdResult Handle (const char* const* parameters, int pcnt, User *user)
31         {
32                 Channel* c = ServerInstance->FindChan(parameters[0]);
33                 std::string line;
34
35                 if (!c)
36                 {
37                         user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
38                         return CMD_FAILURE;
39                 }
40
41                 if (c->HasUser(user))
42                 {
43                         user->WriteServ("480 %s :Can't KNOCK on %s, you are already on that channel.", user->nick, c->name);
44                         return CMD_FAILURE;
45                 }
46
47                 if (c->IsModeSet('K'))
48                 {
49                         user->WriteServ("480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
50                         return CMD_FAILURE;
51                 }
52
53                 if (!c->modes[CM_INVITEONLY])
54                 {
55                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
56                         return CMD_FAILURE;
57                 }
58
59                 for (int i = 1; i < pcnt - 1; i++)
60                 {
61                         line = line + std::string(parameters[i]) + " ";
62                 }
63                 line = line + std::string(parameters[pcnt-1]);
64
65                 c->WriteChannelWithServ((char*)ServerInstance->Config->ServerName,  "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name, user->nick, c->name, line.c_str());
66                 user->WriteServ("NOTICE %s :KNOCKing on %s",user->nick,c->name);
67                 return CMD_SUCCESS;
68         }
69 };
70
71 /** Handles channel mode +K
72  */
73 class Knock : public ModeHandler
74 {
75  public:
76         Knock(InspIRCd* Instance) : ModeHandler(Instance, 'K', 0, 0, false, MODETYPE_CHANNEL, false) { }
77
78         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
79         {
80                 if (adding)
81                 {
82                         if (!channel->IsModeSet('K'))
83                         {
84                                 channel->SetMode('K',true);
85                                 return MODEACTION_ALLOW;
86                         }
87                 }
88                 else
89                 {
90                         if (channel->IsModeSet('K'))
91                         {
92                                 channel->SetMode('K',false);
93                                 return MODEACTION_ALLOW;
94                         }
95                 }
96
97                 return MODEACTION_DENY;
98         }
99 };
100
101 class ModuleKnock : public Module
102 {
103         CommandKnock* mycommand;
104         Knock* kn;
105  public:
106         ModuleKnock(InspIRCd* Me) : Module(Me)
107         {
108                 kn = new Knock(ServerInstance);
109
110                 if (!ServerInstance->Modes->AddMode(kn))
111                         throw ModuleException("Could not add new modes!");
112
113                 mycommand = new CommandKnock(ServerInstance);
114                 ServerInstance->AddCommand(mycommand);
115
116         }
117
118
119         virtual ~ModuleKnock()
120         {
121                 ServerInstance->Modes->DelMode(kn);
122                 delete kn;
123         }
124
125         virtual Version GetVersion()
126         {
127                 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
128         }
129 };
130
131 MODULE_INIT(ModuleKnock)