1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
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 #include "configreader.h"
31 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
35 class CBan : public classbase
48 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)
53 std::string EncodeCBan(const CBan &ban);
54 CBan DecodeCBan(const std::string &data);
55 bool CBanComp(const CBan &ban1, const CBan &ban2);
59 typedef std::vector<CBan> cbanlist;
61 /* cbans is declared here, as our type is right above. Don't try move it. */
64 class cmd_cban : public command_t
67 cmd_cban(InspIRCd* Me) : command_t(Me, "CBAN", 'o', 1)
69 this->source = "m_cban.so";
71 syntax = "<channel> [<duration> :<reason>]";
74 void Handle(const char** parameters, int pcnt, userrec *user)
76 /* syntax: CBAN #channel time :reason goes here */
77 /* 'time' is a human-readable timestring, like 2d3h2s. */
83 /* form: CBAN #channel removes a CBAN */
84 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
86 if (parameters[0] == iter->chname)
88 unsigned long remaining = (iter->set_on + iter->length) - TIME;
89 user->WriteServ( "386 %s %s :Removed CBAN with %lu seconds left before expiry (%s)", user->nick, iter->chname.c_str(), remaining, iter->reason.c_str());
97 /* full form to add a CBAN */
98 if (ServerInstance->IsChannel(parameters[0]))
100 // parameters[0] = #channel
101 // parameters[1] = 1h3m2s
102 // parameters[2] = Tortoise abuser
103 long length = duration(parameters[1]);
104 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
106 cbans.push_back(CBan(parameters[0], user->nick, TIME, length, reason));
108 std::sort(cbans.begin(), cbans.end(), CBanComp);
112 user->WriteServ( "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
113 ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
117 user->WriteServ( "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
118 ServerInstance->WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
123 user->WriteServ( "403 %s %s :Invalid channel name", user->nick, parameters[0]);
129 bool CBanComp(const CBan &ban1, const CBan &ban2)
131 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
134 class ModuleCBan : public Module
140 ModuleCBan(InspIRCd* Me) : Module::Module(Me)
143 mycommand = new cmd_cban(Me);
144 ServerInstance->AddCommand(mycommand);
147 void Implements(char* List)
149 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
152 virtual int OnStats(char symbol, userrec* user, string_list &results)
158 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
160 unsigned long remaining = (iter->set_on + iter->length) - TIME;
161 results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->chname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
168 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
172 /* check cbans in here, and apply as necessary. */
173 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
175 if(iter->chname == cname && !user->modes[UM_OPERATOR])
177 // Channel is banned.
178 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
179 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
186 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
188 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
190 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
194 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
196 if((target_type == TYPE_OTHER) && (extname == "cban"))
198 cbans.push_back(DecodeCBan(extdata));
199 std::sort(cbans.begin(), cbans.end(), CBanComp);
203 virtual ~ModuleCBan()
207 virtual Version GetVersion()
209 return Version(1,0,0,1,VF_VENDOR);
212 std::string EncodeCBan(const CBan &ban)
214 std::ostringstream stream;
215 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
219 CBan DecodeCBan(const std::string &data)
222 std::istringstream stream(data);
223 stream >> res.chname;
224 stream >> res.set_by;
225 stream >> res.set_on;
226 stream >> res.length;
227 res.reason = stream.str();
234 bool go_again = true;
240 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
242 /* 0 == permanent, don't mess with them! -- w00t */
243 if (iter->length != 0)
245 if (iter->set_on + iter->length <= TIME)
247 log(DEBUG, "m_cban.so: Ban on %s expired, removing...", iter->chname.c_str());
248 ServerInstance->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);
254 if (go_again == true)
261 class ModuleCBanFactory : public ModuleFactory
272 virtual Module * CreateModule(InspIRCd* Me)
274 return new ModuleCBan(Me);
280 extern "C" void * init_module( void )
282 return new ModuleCBanFactory;