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