]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Made SANICK not collide the user (theres no need to in the new 1.1 now we have return...
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "configreader.h"
25 #include "inspircd.h"
26
27 /* $ModDesc: Provides support for /KNOCK and mode +K */
28
29
30
31 class cmd_knock : public command_t
32 {
33  public:
34         cmd_knock (InspIRCd* Instance) : command_t(Instance,"KNOCK", 0, 2)
35         {
36                 this->source = "m_knock.so";
37                 syntax = "<channel> <reason>";
38         }
39         
40         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
41         {
42                 chanrec* c = ServerInstance->FindChan(parameters[0]);
43
44                 if (!c)
45                 {
46                         user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
47                         return CMD_FAILURE;
48                 }
49
50                 std::string line = "";
51
52                 if (c->IsModeSet('K'))
53                 {
54                         user->WriteServ("480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
55                         return CMD_FAILURE;
56                 }
57
58                 for (int i = 1; i < pcnt - 1; i++)
59                 {
60                         line = line + std::string(parameters[i]) + " ";
61                 }
62                 line = line + std::string(parameters[pcnt-1]);
63
64                 if (c->modes[CM_INVITEONLY])
65                 {
66                         c->WriteChannelWithServ((char*)ServerInstance->Config->ServerName,  "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name, user->nick, c->name, line.c_str());
67                         user->WriteServ("NOTICE %s :KNOCKing on %s",user->nick,c->name);
68                         return CMD_SUCCESS;
69                 }
70                 else
71                 {
72                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
73                         return CMD_FAILURE;
74                 }
75
76                 return CMD_SUCCESS;
77         }
78 };
79
80 class Knock : public ModeHandler
81 {
82  public:
83         Knock(InspIRCd* Instance) : ModeHandler(Instance, 'K', 0, 0, false, MODETYPE_CHANNEL, false) { }
84
85         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
86         {
87                 if (adding)
88                 {
89                         if (!channel->IsModeSet('K'))
90                         {
91                                 channel->SetMode('K',true);
92                                 return MODEACTION_ALLOW;
93                         }
94                 }
95                 else
96                 {
97                         if (channel->IsModeSet('K'))
98                         {
99                                 channel->SetMode('K',false);
100                                 return MODEACTION_ALLOW;
101                         }
102                 }
103
104                 return MODEACTION_DENY;
105         }
106 };
107
108 class ModuleKnock : public Module
109 {
110         cmd_knock* mycommand;
111         Knock* kn;
112  public:
113         ModuleKnock(InspIRCd* Me) : Module::Module(Me)
114         {
115                 
116                 kn = new Knock(ServerInstance);
117                 ServerInstance->AddMode(kn, 'K');
118                 mycommand = new cmd_knock(ServerInstance);
119                 ServerInstance->AddCommand(mycommand);
120         }
121
122         void Implements(char* List)
123         {
124         }
125
126         virtual ~ModuleKnock()
127         {
128                 ServerInstance->Modes->DelMode(kn);
129                 DELETE(kn);
130         }
131
132         virtual Version GetVersion()
133         {
134                 return Version(1,0,0,1,VF_COMMON|VF_VENDOR);
135         }
136 };
137
138 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
139
140 class ModuleKnockFactory : public ModuleFactory
141 {
142  public:
143         ModuleKnockFactory()
144         {
145         }
146         
147         ~ModuleKnockFactory()
148         {
149         }
150         
151         virtual Module * CreateModule(InspIRCd* Me)
152         {
153                 return new ModuleKnock(Me);
154         }
155         
156 };
157
158
159 extern "C" void * init_module( void )
160 {
161         return new ModuleKnockFactory;
162 }
163