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