]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
5018899d208533773711db7e1e01a268872164bd
[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 <stdio.h>
15 #include <string>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "configreader.h"
20 #include "inspircd.h"
21
22 /* $ModDesc: Provides support for /KNOCK and mode +K */
23
24 /** Handles the /KNOCK command
25  */
26 class cmd_knock : public command_t
27 {
28  public:
29         cmd_knock (InspIRCd* Instance) : command_t(Instance,"KNOCK", 0, 2)
30         {
31                 this->source = "m_knock.so";
32                 syntax = "<channel> <reason>";
33         }
34         
35         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
36         {
37                 chanrec* c = ServerInstance->FindChan(parameters[0]);
38
39                 if (!c)
40                 {
41                         user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
42                         return CMD_FAILURE;
43                 }
44
45                 std::string line = "";
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                 for (int i = 1; i < pcnt - 1; i++)
54                 {
55                         line = line + std::string(parameters[i]) + " ";
56                 }
57                 line = line + std::string(parameters[pcnt-1]);
58
59                 if (c->modes[CM_INVITEONLY])
60                 {
61                         c->WriteChannelWithServ((char*)ServerInstance->Config->ServerName,  "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name, user->nick, c->name, line.c_str());
62                         user->WriteServ("NOTICE %s :KNOCKing on %s",user->nick,c->name);
63                         return CMD_SUCCESS;
64                 }
65                 else
66                 {
67                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
68                         return CMD_FAILURE;
69                 }
70
71                 return CMD_SUCCESS;
72         }
73 };
74
75 /** Handles channel mode +K
76  */
77 class Knock : public ModeHandler
78 {
79  public:
80         Knock(InspIRCd* Instance) : ModeHandler(Instance, 'K', 0, 0, false, MODETYPE_CHANNEL, false) { }
81
82         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
83         {
84                 if (adding)
85                 {
86                         if (!channel->IsModeSet('K'))
87                         {
88                                 channel->SetMode('K',true);
89                                 return MODEACTION_ALLOW;
90                         }
91                 }
92                 else
93                 {
94                         if (channel->IsModeSet('K'))
95                         {
96                                 channel->SetMode('K',false);
97                                 return MODEACTION_ALLOW;
98                         }
99                 }
100
101                 return MODEACTION_DENY;
102         }
103 };
104
105 class ModuleKnock : public Module
106 {
107         cmd_knock* mycommand;
108         Knock* kn;
109  public:
110         ModuleKnock(InspIRCd* Me) : Module::Module(Me)
111         {
112                 
113                 kn = new Knock(ServerInstance);
114                 ServerInstance->AddMode(kn, 'K');
115                 mycommand = new cmd_knock(ServerInstance);
116                 ServerInstance->AddCommand(mycommand);
117         }
118
119         void Implements(char* List)
120         {
121         }
122
123         virtual ~ModuleKnock()
124         {
125                 ServerInstance->Modes->DelMode(kn);
126                 DELETE(kn);
127         }
128
129         virtual Version GetVersion()
130         {
131                 return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
132         }
133 };
134
135 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
136
137 class ModuleKnockFactory : public ModuleFactory
138 {
139  public:
140         ModuleKnockFactory()
141         {
142         }
143         
144         ~ModuleKnockFactory()
145         {
146         }
147         
148         virtual Module * CreateModule(InspIRCd* Me)
149         {
150                 return new ModuleKnock(Me);
151         }
152         
153 };
154
155
156 extern "C" void * init_module( void )
157 {
158         return new ModuleKnockFactory;
159 }
160