]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
4b0319d87aab55c92e21eedf84840c6dff4d68db
[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, const char* src, const char* re, const char *ch) : XLine(Instance, s_time, d, src, re, "CBAN")
27         {
28                 this->matchtext = ch;
29         }
30
31         ~CBan()
32         {
33         }
34
35         // XXX I shouldn't have to define this
36         bool Matches(User *u)
37         {
38                 return false;
39         }
40
41         bool Matches(const std::string &s)
42         {
43                 if (matchtext == s)
44                         return true;
45                 return false;
46         }
47
48         void DisplayExpiry()
49         {
50                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired CBan %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, (long int)(ServerInstance->Time() - this->set_time));
51                 ServerInstance->PI->SendSNONotice("x", "Removing expired CBan " + assign(this->matchtext) + " (set by " + std::string(this->source) + " " + ConvToStr(ServerInstance->Time() - this->set_time) + " seconds ago)");
52         }
53
54         const char* Displayable()
55         {
56                 return matchtext.c_str();
57         }
58 };
59
60 /** An XLineFactory specialized to generate cban pointers
61  */
62 class CBanFactory : public XLineFactory
63 {
64  public:
65         CBanFactory(InspIRCd* Instance) : XLineFactory(Instance, "CBAN") { }
66
67         /** Generate a shun
68         */
69         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
70         {
71                 return new CBan(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
72         }
73
74         bool AutoApplyToUserList(XLine *x)
75         {
76                 return false; // No, we apply to channels.
77         }
78 };
79
80 /** Handle /CBAN
81  */
82 class CommandCBan : public Command
83 {
84  public:
85         CommandCBan(InspIRCd* Me) : Command(Me, "CBAN", "o", 1, 3)
86         {
87                 this->source = "m_cban.so";
88                 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->WriteToSnoMask('x',"%s removed CBan on %s.",user->nick.c_str(),parameters[0].c_str());
102                                 ServerInstance->PI->SendSNONotice("x", user->nick + " removed CBan on " + parameters[0]);
103                         }
104                         else
105                         {
106                                 user->WriteServ("NOTICE %s :*** CBan %s not found in list, try /stats C.",user->nick.c_str(),parameters[0].c_str());
107                         }
108
109                         return CMD_SUCCESS;
110                 }
111                 else if (parameters.size() >= 2)
112                 {
113                         // Adding - XXX todo make this respect <insane> tag perhaps..
114                         long duration = ServerInstance->Duration(parameters[1]);
115                         CBan *r = NULL;
116                         const char *reason = (parameters.size() > 2) ? parameters[2].c_str() : "No reason supplied";
117
118                         try
119                         {
120                                 r = new CBan(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), reason, parameters[0].c_str());
121                         }
122                         catch (...)
123                         {
124                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
125                         }
126
127                         if (r)
128                         {
129                                 if (ServerInstance->XLines->AddLine(r, user))
130                                 {
131                                         if (!duration)
132                                         {
133                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent CBan for %s: %s", user->nick.c_str(), parameters[0].c_str(), reason);
134                                                 ServerInstance->PI->SendSNONotice("x", user->nick + " added permenant CBan for " + parameters[0] + ": " + std::string(reason));
135                                         }
136                                         else
137                                         {
138                                                 time_t c_requires_crap = duration + ServerInstance->Time();
139                                                 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);
140                                                 ServerInstance->PI->SendSNONotice("x", user->nick + " added timed CBan for " + parameters[0] + ", expires on " + ServerInstance->TimeString(c_requires_crap) + ": " + std::string(reason));
141                                         }
142
143                                         ServerInstance->XLines->ApplyLines();
144                                 }
145                                 else
146                                 {
147                                         delete r;
148                                         user->WriteServ("NOTICE %s :*** CBan for %s already exists", user->nick.c_str(), parameters[0].c_str());
149                                 }
150                         }
151                 }
152
153                 return CMD_FAILURE;
154         }
155 };
156
157 class ModuleCBan : public Module
158 {
159         CommandCBan mycommand;
160         CBanFactory f;
161
162  public:
163         ModuleCBan(InspIRCd* Me) : Module(Me), mycommand(Me), f(Me)
164         {
165                 ServerInstance->XLines->RegisterFactory(&f);
166
167                 ServerInstance->AddCommand(&mycommand);
168                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnStats };
169                 ServerInstance->Modules->Attach(eventlist, this, 4);
170         }
171
172         virtual ~ModuleCBan()
173         {
174                 ServerInstance->XLines->DelAll("CBAN");
175                 ServerInstance->XLines->UnregisterFactory(&f);
176         }
177
178         virtual int OnStats(char symbol, User* user, string_list &out)
179         {
180                 if (symbol != 'C')
181                         return 0;
182
183                 ServerInstance->XLines->InvokeStats("CBAN", 210, user, out);
184                 return 1;
185         }
186
187         virtual int OnUserPreJoin(User *user, Channel *chan, const char *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, rl->reason);
195                         ServerInstance->SNO->WriteToSnoMask('a', "%s tried to join %s which is CBANed (%s)", user->nick.c_str(), cname, rl->reason);
196                         ServerInstance->PI->SendSNONotice("A", user->nick + " tried to join " + std::string(cname) + " which is CBANed (" + std::string(rl->reason) + ")");
197                         return 1;
198                 }
199
200                 return 0;
201         }
202
203         virtual Version GetVersion()
204         {
205                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
206         }
207 };
208
209 MODULE_INIT(ModuleCBan)
210