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