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