]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
c22fe5ff01674522f019b6ca132ded8d9736fc61
[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 "helperfuncs.h"
25
26 /* $ModDesc: Provides support for /KNOCK and mode +K */
27
28 static Server *Srv;
29
30 class cmd_knock : public command_t
31 {
32  public:
33         cmd_knock () : command_t("KNOCK", 0, 2)
34         {
35                 this->source = "m_knock.so";
36         }
37         
38         void Handle (char **parameters, int pcnt, userrec *user)
39         {
40                 chanrec* c = Srv->FindChannel(parameters[0]);
41                 std::string line = "";
42
43                 if (c->IsModeSet('K'))
44                 {
45                         WriteServ(user->fd,"480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
46                         return;
47                 }
48
49                 for (int i = 1; i < pcnt - 1; i++)
50                 {
51                         line = line + std::string(parameters[i]) + " ";
52                 }
53                 line = line + std::string(parameters[pcnt-1]);
54
55                 if (c->modes[CM_INVITEONLY])
56                 {
57                         WriteChannelWithServ((char*)Srv->GetServerName().c_str(),c,"NOTICE %s :User %s is KNOCKing on %s (%s)",c->name,user->nick,c->name,line.c_str());
58                         WriteServ(user->fd,"NOTICE %s :KNOCKing on %s",user->nick,c->name);
59                         return;
60                 }
61                 else
62                 {
63                         WriteServ(user->fd,"480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
64                         return;
65                 }
66         }
67 };
68
69 class ModuleKnock : public Module
70 {
71         cmd_knock* mycommand;
72  public:
73         ModuleKnock(Server* Me)
74                 : Module::Module(Me)
75         {
76                 Srv = Me;
77                 Srv->AddExtendedMode('K',MT_CHANNEL,false,0,0);
78                 mycommand = new cmd_knock();
79                 Srv->AddCommand(mycommand);
80         }
81
82         void Implements(char* List)
83         {
84                 List[I_On005Numeric] = List[I_OnExtendedMode] = 1;
85         }
86
87         virtual void On005Numeric(std::string &output)
88         {
89                 InsertMode(output,"K",4);
90         }
91
92         virtual ~ModuleKnock()
93         {
94         }
95         
96         virtual Version GetVersion()
97         {
98                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
99         }
100         
101         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
102         {
103                 // check if this is our mode character...
104                 if ((modechar == 'K') && (type == MT_CHANNEL))
105                 {
106                         return 1;
107                 }
108                 else
109                 {
110                         return 0;
111                 }
112         }
113
114 };
115
116 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
117
118 class ModuleKnockFactory : public ModuleFactory
119 {
120  public:
121         ModuleKnockFactory()
122         {
123         }
124         
125         ~ModuleKnockFactory()
126         {
127         }
128         
129         virtual Module * CreateModule(Server* Me)
130         {
131                 return new ModuleKnock(Me);
132         }
133         
134 };
135
136
137 extern "C" void * init_module( void )
138 {
139         return new ModuleKnockFactory;
140 }
141