]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
b7113eb3174e1c4776edba3fb93ea698acb9a7f5
[user/henk/code/inspircd.git] / src / modules / m_cban.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2005-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2005-2006 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25
26 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
27
28 /** Holds a CBAN item
29  */
30 class CBan : public XLine
31 {
32 private:
33         std::string displaytext;
34         irc::string matchtext;
35
36 public:
37         CBan(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& ch)
38                 : XLine(s_time, d, src, re, "CBAN")
39         {
40                 this->displaytext = ch;
41                 this->matchtext = ch.c_str();
42         }
43
44         // XXX I shouldn't have to define this
45         bool Matches(User *u)
46         {
47                 return false;
48         }
49
50         bool Matches(const std::string &s)
51         {
52                 if (matchtext == s)
53                         return true;
54                 return false;
55         }
56
57         const std::string& Displayable()
58         {
59                 return displaytext;
60         }
61 };
62
63 /** An XLineFactory specialized to generate cban pointers
64  */
65 class CBanFactory : public XLineFactory
66 {
67  public:
68         CBanFactory() : XLineFactory("CBAN") { }
69
70         /** Generate a CBAN
71         */
72         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
73         {
74                 return new CBan(set_time, duration, source, reason, xline_specific_mask);
75         }
76
77         bool AutoApplyToUserList(XLine *x)
78         {
79                 return false; // No, we apply to channels.
80         }
81 };
82
83 /** Handle /CBAN
84  */
85 class CommandCBan : public Command
86 {
87  public:
88         CommandCBan(Module* Creator) : Command(Creator, "CBAN", 1, 3)
89         {
90                 flags_needed = 'o'; this->syntax = "<channel> [<duration> :<reason>]";
91         }
92
93         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
94         {
95                 /* syntax: CBAN #channel time :reason goes here */
96                 /* 'time' is a human-readable timestring, like 2d3h2s. */
97
98                 if (parameters.size() == 1)
99                 {
100                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "CBAN", user))
101                         {
102                                 ServerInstance->SNO->WriteGlobalSno('x', "%s removed CBan on %s.",user->nick.c_str(),parameters[0].c_str());
103                         }
104                         else
105                         {
106                                 user->WriteNotice("*** CBan " + parameters[0] + " not found in list, try /stats C.");
107                                 return CMD_FAILURE;
108                         }
109                 }
110                 else
111                 {
112                         // Adding - XXX todo make this respect <insane> tag perhaps..
113                         unsigned long duration = InspIRCd::Duration(parameters[1]);
114                         const char *reason = (parameters.size() > 2) ? parameters[2].c_str() : "No reason supplied";
115                         CBan* r = new CBan(ServerInstance->Time(), duration, user->nick.c_str(), reason, parameters[0].c_str());
116
117                         if (ServerInstance->XLines->AddLine(r, user))
118                         {
119                                 if (!duration)
120                                 {
121                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent CBan for %s: %s", user->nick.c_str(), parameters[0].c_str(), reason);
122                                 }
123                                 else
124                                 {
125                                         time_t c_requires_crap = duration + ServerInstance->Time();
126                                         std::string timestr = ServerInstance->TimeString(c_requires_crap);
127                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added timed CBan for %s, expires on %s: %s", user->nick.c_str(), parameters[0].c_str(), timestr.c_str(), reason);
128                                 }
129                         }
130                         else
131                         {
132                                 delete r;
133                                 user->WriteNotice("*** CBan for " + parameters[0] + " already exists");
134                                 return CMD_FAILURE;
135                         }
136                 }
137                 return CMD_SUCCESS;
138         }
139
140         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
141         {
142                 if (IS_LOCAL(user))
143                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
144
145                 return ROUTE_BROADCAST;
146         }
147 };
148
149 class ModuleCBan : public Module
150 {
151         CommandCBan mycommand;
152         CBanFactory f;
153
154  public:
155         ModuleCBan() : mycommand(this)
156         {
157         }
158
159         void init() CXX11_OVERRIDE
160         {
161                 ServerInstance->XLines->RegisterFactory(&f);
162
163                 ServerInstance->Modules->AddService(mycommand);
164                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnStats };
165                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
166         }
167
168         ~ModuleCBan()
169         {
170                 ServerInstance->XLines->DelAll("CBAN");
171                 ServerInstance->XLines->UnregisterFactory(&f);
172         }
173
174         ModResult OnStats(char symbol, User* user, string_list &out) CXX11_OVERRIDE
175         {
176                 if (symbol != 'C')
177                         return MOD_RES_PASSTHRU;
178
179                 ServerInstance->XLines->InvokeStats("CBAN", 210, user, out);
180                 return MOD_RES_DENY;
181         }
182
183         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
184         {
185                 XLine *rl = ServerInstance->XLines->MatchesLine("CBAN", cname);
186
187                 if (rl)
188                 {
189                         // Channel is banned.
190                         user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick.c_str(), cname.c_str(), rl->reason.c_str());
191                         ServerInstance->SNO->WriteGlobalSno('a', "%s tried to join %s which is CBANed (%s)",
192                                  user->nick.c_str(), cname.c_str(), rl->reason.c_str());
193                         return MOD_RES_DENY;
194                 }
195
196                 return MOD_RES_PASSTHRU;
197         }
198
199         Version GetVersion() CXX11_OVERRIDE
200         {
201                 return Version("Gives /cban, aka C:lines. Think Q:lines, for channels.", VF_COMMON | VF_VENDOR);
202         }
203 };
204
205 MODULE_INIT(ModuleCBan)