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