]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
fa54e08c79d28ccdc28d71f531ef9d517443cd03
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Provides support for /KNOCK and mode +K */
20
21 /** Handles the /KNOCK command
22  */
23 class cmd_knock : public command_t
24 {
25  public:
26         cmd_knock (InspIRCd* Instance) : command_t(Instance,"KNOCK", 0, 2)
27         {
28                 this->source = "m_knock.so";
29                 syntax = "<channel> <reason>";
30         }
31         
32         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
33         {
34                 chanrec* c = ServerInstance->FindChan(parameters[0]);
35
36                 if (!c)
37                 {
38                         user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
39                         return CMD_FAILURE;
40                 }
41
42                 std::string line;
43
44                 if (c->IsModeSet('K'))
45                 {
46                         user->WriteServ("480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
47                         return CMD_FAILURE;
48                 }
49
50                 for (int i = 1; i < pcnt - 1; i++)
51                 {
52                         line = line + std::string(parameters[i]) + " ";
53                 }
54                 line = line + std::string(parameters[pcnt-1]);
55
56                 if (!c->modes[CM_INVITEONLY])
57                 {
58                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
59                         return CMD_FAILURE;
60                 }
61
62                 c->WriteChannelWithServ((char*)ServerInstance->Config->ServerName,  "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name, user->nick, c->name, line.c_str());
63                 user->WriteServ("NOTICE %s :KNOCKing on %s",user->nick,c->name);
64                 return CMD_SUCCESS;
65         }
66 };
67
68 /** Handles channel mode +K
69  */
70 class Knock : public ModeHandler
71 {
72  public:
73         Knock(InspIRCd* Instance) : ModeHandler(Instance, 'K', 0, 0, false, MODETYPE_CHANNEL, false) { }
74
75         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
76         {
77                 if (adding)
78                 {
79                         if (!channel->IsModeSet('K'))
80                         {
81                                 channel->SetMode('K',true);
82                                 return MODEACTION_ALLOW;
83                         }
84                 }
85                 else
86                 {
87                         if (channel->IsModeSet('K'))
88                         {
89                                 channel->SetMode('K',false);
90                                 return MODEACTION_ALLOW;
91                         }
92                 }
93
94                 return MODEACTION_DENY;
95         }
96 };
97
98 class ModuleKnock : public Module
99 {
100         cmd_knock* mycommand;
101         Knock* kn;
102  public:
103         ModuleKnock(InspIRCd* Me) : Module(Me)
104         {
105                 
106                 kn = new Knock(ServerInstance);
107                 if (!ServerInstance->AddMode(kn, 'K'))
108                         throw ModuleException("Could not add new modes!");
109                 mycommand = new cmd_knock(ServerInstance);
110                 ServerInstance->AddCommand(mycommand);
111         }
112
113         void Implements(char* List)
114         {
115         }
116
117         virtual ~ModuleKnock()
118         {
119                 ServerInstance->Modes->DelMode(kn);
120                 DELETE(kn);
121         }
122
123         virtual Version GetVersion()
124         {
125                 return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
126         }
127 };
128
129 MODULE_INIT(ModuleKnock);