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