]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[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                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
62                         return CMD_FAILURE;
63                 }
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(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
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         cmd_knock* mycommand;
104         Knock* kn;
105  public:
106         ModuleKnock(InspIRCd* Me) : Module(Me)
107         {
108                 
109                 kn = new Knock(ServerInstance);
110                 if (!ServerInstance->AddMode(kn, 'K'))
111                         throw ModuleException("Could not add new modes!");
112                 mycommand = new cmd_knock(ServerInstance);
113                 ServerInstance->AddCommand(mycommand);
114         }
115
116         void Implements(char* List)
117         {
118         }
119
120         virtual ~ModuleKnock()
121         {
122                 ServerInstance->Modes->DelMode(kn);
123                 DELETE(kn);
124         }
125
126         virtual Version GetVersion()
127         {
128                 return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
129         }
130 };
131
132 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
133
134 class ModuleKnockFactory : public ModuleFactory
135 {
136  public:
137         ModuleKnockFactory()
138         {
139         }
140         
141         ~ModuleKnockFactory()
142         {
143         }
144         
145         virtual Module * CreateModule(InspIRCd* Me)
146         {
147                 return new ModuleKnock(Me);
148         }
149         
150 };
151
152
153 extern "C" DllExport void * init_module( void )
154 {
155         return new ModuleKnockFactory;
156 }
157