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. */
33 extern InspIRCd* ServerInstance;
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 InspIRCd* ServerInstance;
70 cmd_cban(InspIRCd* Me) : command_t("CBAN", 'o', 1), ServerInstance(Me)
72 this->source = "m_cban.so";
74 syntax = "<channel> [<duration> :<reason>]";
77 void Handle(const char** parameters, int pcnt, userrec *user)
79 /* syntax: CBAN #channel time :reason goes here */
80 /* 'time' is a human-readable timestring, like 2d3h2s. */
86 /* form: CBAN #channel removes a CBAN */
87 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
89 if (parameters[0] == iter->chname)
91 unsigned long remaining = (iter->set_on + iter->length) - TIME;
92 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());
100 /* full form to add a CBAN */
101 if (ServerInstance->IsChannel(parameters[0]))
103 // parameters[0] = #channel
104 // parameters[1] = 1h3m2s
105 // parameters[2] = Tortoise abuser
106 long length = duration(parameters[1]);
107 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
109 cbans.push_back(CBan(parameters[0], user->nick, TIME, length, reason));
111 std::sort(cbans.begin(), cbans.end(), CBanComp);
115 user->WriteServ( "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
116 ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
120 user->WriteServ( "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
121 ServerInstance->WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
126 user->WriteServ( "403 %s %s :Invalid channel name", user->nick, parameters[0]);
132 class ModuleCBan : public Module
138 ModuleCBan(InspIRCd* Me) : Module::Module(Me)
141 mycommand = new cmd_cban(Me);
142 ServerInstance->AddCommand(mycommand);
145 void Implements(char* List)
147 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
150 virtual int OnStats(char symbol, userrec* user, string_list &results)
156 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
158 unsigned long remaining = (iter->set_on + iter->length) - TIME;
159 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);
166 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
170 /* check cbans in here, and apply as necessary. */
171 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
173 if(iter->chname == cname && !user->modes[UM_OPERATOR])
175 // Channel is banned.
176 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
177 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
184 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
186 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
188 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
192 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
194 if((target_type == TYPE_OTHER) && (extname == "cban"))
196 cbans.push_back(DecodeCBan(extdata));
197 std::sort(cbans.begin(), cbans.end(), CBanComp);
201 virtual ~ModuleCBan()
205 virtual Version GetVersion()
207 return Version(1,0,0,1,VF_VENDOR);
211 std::string EncodeCBan(const CBan &ban)
213 std::ostringstream stream;
214 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
218 CBan DecodeCBan(const std::string &data)
221 std::istringstream stream(data);
222 stream >> res.chname;
223 stream >> res.set_by;
224 stream >> res.set_on;
225 stream >> res.length;
226 res.reason = stream.str();
231 bool CBanComp(const CBan &ban1, const CBan &ban2)
233 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
238 bool go_again = true;
244 for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
246 /* 0 == permanent, don't mess with them! -- w00t */
247 if (iter->length != 0)
249 if (iter->set_on + iter->length <= TIME)
251 log(DEBUG, "m_cban.so: Ban on %s expired, removing...", iter->chname.c_str());
252 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);
258 if (go_again == true)
264 class ModuleCBanFactory : public ModuleFactory
275 virtual Module * CreateModule(InspIRCd* Me)
277 return new ModuleCBan(Me);
283 extern "C" void * init_module( void )
285 return new ModuleCBanFactory;