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";
71 void Handle(char **parameters, int pcnt, userrec *user)
73 /* syntax: CBAN #channel time :reason goes here */
74 /* 'time' is a human-readable timestring, like 2d3h2s. */
80 /* form: CBAN #channel removes a CBAN */
81 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
83 if (parameters[0] == iter->chname)
85 unsigned long remaining = (iter->set_on + iter->length) - TIME;
86 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());
94 /* full form to add a CBAN */
95 if (IsValidChannelName(parameters[0]))
97 // parameters[0] = #channel
98 // parameters[1] = 1h3m2s
99 // parameters[2] = Tortoise abuser
100 long length = Srv->CalcDuration(parameters[1]);
101 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
103 cbans.push_back(CBan(parameters[0], user->nick, TIME, length, reason));
105 std::sort(cbans.begin(), cbans.end(), CBanComp);
109 WriteServ(user->fd, "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
110 WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
114 WriteServ(user->fd, "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
115 WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
120 WriteServ(user->fd, "403 %s %s :Invalid channel name", user->nick, parameters[0]);
126 class ModuleCBan : public Module
132 ModuleCBan(Server* Me) : Module::Module(Me)
135 mycommand = new cmd_cban(Srv);
136 Srv->AddCommand(mycommand);
139 void Implements(char* List)
141 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
144 virtual int OnStats(char symbol, userrec* user)
150 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
152 unsigned long remaining = (iter->set_on + iter->length) - TIME;
153 WriteServ(user->fd, "210 %s %s %s %lu %lu %lu :%s", user->nick, iter->chname.c_str(), iter->set_by.c_str(), iter->set_on, iter->length, remaining, iter->reason.c_str());
160 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
164 /* check cbans in here, and apply as necessary. */
165 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
167 if(iter->chname == cname && !user->modes[UM_OPERATOR])
169 // Channel is banned.
170 WriteServ(user->fd, "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
171 WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
178 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
180 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
182 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
186 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
188 if((target_type == TYPE_OTHER) && (extname == "cban"))
190 cbans.push_back(DecodeCBan(extdata));
191 std::sort(cbans.begin(), cbans.end(), CBanComp);
195 virtual ~ModuleCBan()
199 virtual Version GetVersion()
201 return Version(1,0,0,1,VF_VENDOR);
205 std::string EncodeCBan(const CBan &ban)
207 std::ostringstream stream;
208 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
212 CBan DecodeCBan(const std::string &data)
215 std::istringstream stream(data);
216 stream >> res.chname;
217 stream >> res.set_by;
218 stream >> res.set_on;
219 stream >> res.length;
220 res.reason = stream.str();
225 bool CBanComp(const CBan &ban1, const CBan &ban2)
227 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
232 bool go_again = true;
238 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
240 /* 0 == permanent, don't mess with them! -- w00t */
241 if (iter->length != 0)
243 if (iter->set_on + iter->length <= TIME)
245 log(DEBUG, "m_cban.so: Ban on %s expired, removing...", iter->chname.c_str());
246 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);
252 if (go_again == true)
258 class ModuleCBanFactory : public ModuleFactory
269 virtual Module * CreateModule(Server* Me)
271 return new ModuleCBan(Me);
277 extern "C" void * init_module( void )
279 return new ModuleCBanFactory;