]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
9553b619e17b31a96acb989c1d6923f577998f60
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 #include <stdio.h>
18 #include <string>
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "helperfuncs.h"
23
24 /* $ModDesc: Provides support for /KNOCK and mode +K */
25
26 Server *Srv;
27          
28 void handle_knock(char **parameters, int pcnt, userrec *user)
29 {
30         chanrec* c = Srv->FindChannel(parameters[0]);
31         std::string line = "";
32
33         for (int i = 1; i < pcnt - 1; i++)
34         {
35                 line = line + std::string(parameters[i]) + " ";
36         }
37         line = line + std::string(parameters[pcnt-1]);
38
39         if (c->IsCustomModeSet('K'))
40         {
41                 WriteServ(user->fd,"480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
42                 return;
43         }
44         if (c->binarymodes & CM_INVITEONLY)
45         {
46                 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());
47                 WriteServ(user->fd,"NOTICE %s :KNOCKing on %s",user->nick,c->name);
48                 return;
49         }
50         else
51         {
52                 WriteServ(user->fd,"480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
53                 return;
54         }
55 }
56
57
58 class ModuleKnock : public Module
59 {
60  public:
61         ModuleKnock()
62         {
63                 Srv = new Server;
64                 
65                 Srv->AddExtendedMode('K',MT_CHANNEL,false,0,0);
66                 Srv->AddCommand("KNOCK",handle_knock,0,2,"m_knock.so");
67         }
68
69         virtual void On005Numeric(std::string &output)
70         {
71                 std::stringstream line(output);
72                 std::string temp1, temp2;
73                 while (!line.eof())
74                 {
75                         line >> temp1;
76                         if (temp1.substr(0,10) == "CHANMODES=")
77                         {
78                                 // append the chanmode to the end
79                                 temp1 = temp1.substr(10,temp1.length());
80                                 temp1 = "CHANMODES=" + temp1 + "K";
81                         }
82                         temp2 = temp2 + temp1 + " ";
83                 }
84                 if (temp2.length())
85                 {
86                         output = temp2.substr(0,temp2.length()-1) + std::string(" KNOCK");
87                 }
88                 else output = output + std::string(" KNOCK");
89         }
90
91         virtual ~ModuleKnock()
92         {
93                 delete Srv;
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()
130         {
131                 return new ModuleKnock;
132         }
133         
134 };
135
136
137 extern "C" void * init_module( void )
138 {
139         return new ModuleKnockFactory;
140 }
141