1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
8 * <Craig@chatspike.net>
11 * Written by Craig Edwards, Craig McLure, and others.
12 * This program is free but copyrighted software; see
13 *the file COPYING for details.
15 * ---------------------------------------------------
25 #include "helperfuncs.h"
28 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
30 class CBan : public classbase
43 CBan(irc::string cn, std::string sb, time_t so, long ln, std::string rs) : chname(cn), set_by(sb), set_on(so), length(ln), reason(rs)
48 std::string EncodeCBan(const CBan &ban);
49 CBan DecodeCBan(const std::string &data);
50 bool CBanComp(const CBan &ban1, const CBan &ban2);
54 typedef std::vector<CBan> cbanlist;
56 /* cbans is declared here, as our type is right above. Don't try move it. */
59 class cmd_cban : public command_t
65 cmd_cban(Server* Me) : command_t("CBAN", 'o', 1)
67 this->source = "m_cban.so";
69 syntax = "<channel> [<duration> :<reason>]";
72 void Handle(const char** parameters, int pcnt, userrec *user)
74 /* syntax: CBAN #channel time :reason goes here */
75 /* 'time' is a human-readable timestring, like 2d3h2s. */
81 /* form: CBAN #channel removes a CBAN */
82 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
84 if (parameters[0] == iter->chname)
86 unsigned long remaining = (iter->set_on + iter->length) - TIME;
87 WriteServ(user->fd, "386 %s %s :Removed CBAN with %lu seconds left before expiry (%s)", user->nick, iter->chname.c_str(), remaining, iter->reason.c_str());
95 /* full form to add a CBAN */
96 if (IsValidChannelName(parameters[0]))
98 // parameters[0] = #channel
99 // parameters[1] = 1h3m2s
100 // parameters[2] = Tortoise abuser
101 long length = Srv->CalcDuration(parameters[1]);
102 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
104 cbans.push_back(CBan(parameters[0], user->nick, TIME, length, reason));
106 std::sort(cbans.begin(), cbans.end(), CBanComp);
110 WriteServ(user->fd, "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
111 WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
115 WriteServ(user->fd, "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
116 WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
121 WriteServ(user->fd, "403 %s %s :Invalid channel name", user->nick, parameters[0]);
127 class ModuleCBan : public Module
133 ModuleCBan(Server* Me) : Module::Module(Me)
136 mycommand = new cmd_cban(Srv);
137 Srv->AddCommand(mycommand);
140 void Implements(char* List)
142 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
145 virtual int OnStats(char symbol, userrec* user, string_list &results)
151 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
153 unsigned long remaining = (iter->set_on + iter->length) - TIME;
154 results.push_back(Srv->GetServerName()+" 210 "+user->nick+" "+iter->chname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
161 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
165 /* check cbans in here, and apply as necessary. */
166 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
168 if(iter->chname == cname && !user->modes[UM_OPERATOR])
170 // Channel is banned.
171 WriteServ(user->fd, "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
172 WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
179 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
181 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
183 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
187 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
189 if((target_type == TYPE_OTHER) && (extname == "cban"))
191 cbans.push_back(DecodeCBan(extdata));
192 std::sort(cbans.begin(), cbans.end(), CBanComp);
196 virtual ~ModuleCBan()
200 virtual Version GetVersion()
202 return Version(1,0,0,1,VF_VENDOR);
206 std::string EncodeCBan(const CBan &ban)
208 std::ostringstream stream;
209 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
213 CBan DecodeCBan(const std::string &data)
216 std::istringstream stream(data);
217 stream >> res.chname;
218 stream >> res.set_by;
219 stream >> res.set_on;
220 stream >> res.length;
221 res.reason = stream.str();
226 bool CBanComp(const CBan &ban1, const CBan &ban2)
228 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
233 bool go_again = true;
239 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
241 /* 0 == permanent, don't mess with them! -- w00t */
242 if (iter->length != 0)
244 if (iter->set_on + iter->length <= TIME)
246 log(DEBUG, "m_cban.so: Ban on %s expired, removing...", iter->chname.c_str());
247 WriteOpers("*** %li second CBAN on %s (%s) set %u seconds ago expired", iter->length, iter->chname.c_str(), iter->reason.c_str(), TIME - iter->set_on);
253 if (go_again == true)
259 class ModuleCBanFactory : public ModuleFactory
270 virtual Module * CreateModule(Server* Me)
272 return new ModuleCBan(Me);
278 extern "C" void * init_module( void )
280 return new ModuleCBanFactory;