]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
Add the override keyword in places that it is missing.
[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 /** Holds a CBAN item
27  */
28 class CBan : public XLine
29 {
30 private:
31         std::string matchtext;
32
33 public:
34         CBan(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& ch)
35                 : XLine(s_time, d, src, re, "CBAN")
36                 , matchtext(ch)
37         {
38         }
39
40         // XXX I shouldn't have to define this
41         bool Matches(User* u) CXX11_OVERRIDE
42         {
43                 return false;
44         }
45
46         bool Matches(const std::string& s) CXX11_OVERRIDE
47         {
48                 return irc::equals(matchtext, s);
49         }
50
51         const std::string& Displayable() CXX11_OVERRIDE
52         {
53                 return matchtext;
54         }
55 };
56
57 /** An XLineFactory specialized to generate cban pointers
58  */
59 class CBanFactory : public XLineFactory
60 {
61  public:
62         CBanFactory() : XLineFactory("CBAN") { }
63
64         /** Generate a CBAN
65         */
66         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
67         {
68                 return new CBan(set_time, duration, source, reason, xline_specific_mask);
69         }
70
71         bool AutoApplyToUserList(XLine* x) CXX11_OVERRIDE
72         {
73                 return false; // No, we apply to channels.
74         }
75 };
76
77 /** Handle /CBAN
78  */
79 class CommandCBan : public Command
80 {
81  public:
82         CommandCBan(Module* Creator) : Command(Creator, "CBAN", 1, 3)
83         {
84                 flags_needed = 'o'; this->syntax = "<channel> [<duration> :<reason>]";
85         }
86
87         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
88         {
89                 /* syntax: CBAN #channel time :reason goes here */
90                 /* 'time' is a human-readable timestring, like 2d3h2s. */
91
92                 if (parameters.size() == 1)
93                 {
94                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "CBAN", user))
95                         {
96                                 ServerInstance->SNO->WriteGlobalSno('x', "%s removed CBan on %s.",user->nick.c_str(),parameters[0].c_str());
97                         }
98                         else
99                         {
100                                 user->WriteNotice("*** CBan " + parameters[0] + " not found in list, try /stats C.");
101                                 return CMD_FAILURE;
102                         }
103                 }
104                 else
105                 {
106                         // Adding - XXX todo make this respect <insane> tag perhaps..
107                         unsigned long duration = InspIRCd::Duration(parameters[1]);
108                         const char *reason = (parameters.size() > 2) ? parameters[2].c_str() : "No reason supplied";
109                         CBan* r = new CBan(ServerInstance->Time(), duration, user->nick.c_str(), reason, parameters[0].c_str());
110
111                         if (ServerInstance->XLines->AddLine(r, user))
112                         {
113                                 if (!duration)
114                                 {
115                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent CBan for %s: %s", user->nick.c_str(), parameters[0].c_str(), reason);
116                                 }
117                                 else
118                                 {
119                                         time_t c_requires_crap = duration + ServerInstance->Time();
120                                         std::string timestr = InspIRCd::TimeString(c_requires_crap);
121                                         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);
122                                 }
123                         }
124                         else
125                         {
126                                 delete r;
127                                 user->WriteNotice("*** CBan for " + parameters[0] + " already exists");
128                                 return CMD_FAILURE;
129                         }
130                 }
131                 return CMD_SUCCESS;
132         }
133
134         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
135         {
136                 if (IS_LOCAL(user))
137                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
138
139                 return ROUTE_BROADCAST;
140         }
141 };
142
143 class ModuleCBan : public Module
144 {
145         CommandCBan mycommand;
146         CBanFactory f;
147
148  public:
149         ModuleCBan() : mycommand(this)
150         {
151         }
152
153         void init() CXX11_OVERRIDE
154         {
155                 ServerInstance->XLines->RegisterFactory(&f);
156         }
157
158         ~ModuleCBan()
159         {
160                 ServerInstance->XLines->DelAll("CBAN");
161                 ServerInstance->XLines->UnregisterFactory(&f);
162         }
163
164         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
165         {
166                 if (stats.GetSymbol() != 'C')
167                         return MOD_RES_PASSTHRU;
168
169                 ServerInstance->XLines->InvokeStats("CBAN", 210, stats);
170                 return MOD_RES_DENY;
171         }
172
173         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
174         {
175                 XLine *rl = ServerInstance->XLines->MatchesLine("CBAN", cname);
176
177                 if (rl)
178                 {
179                         // Channel is banned.
180                         user->WriteNumeric(384, cname, InspIRCd::Format("Cannot join channel, CBANed (%s)", rl->reason.c_str()));
181                         ServerInstance->SNO->WriteGlobalSno('a', "%s tried to join %s which is CBANed (%s)",
182                                  user->nick.c_str(), cname.c_str(), rl->reason.c_str());
183                         return MOD_RES_DENY;
184                 }
185
186                 return MOD_RES_PASSTHRU;
187         }
188
189         Version GetVersion() CXX11_OVERRIDE
190         {
191                 return Version("Gives /cban, aka C:lines. Think Q:lines, for channels.", VF_COMMON | VF_VENDOR);
192         }
193 };
194
195 MODULE_INIT(ModuleCBan)