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