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 * ---------------------------------------------------
22 #include "configreader.h"
25 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
27 /** Holds a SVSHold item
29 class SVSHold : public classbase
42 SVSHold(std::string nn, std::string sb, time_t so, long ln, std::string rs) : nickname(nn), set_by(sb), set_on(so), length(ln), reason(rs)
47 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2);
49 typedef std::vector<SVSHold> SVSHoldlist;
51 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
56 class cmd_svshold : public command_t
59 cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
61 this->source = "m_svshold.so";
63 syntax = "<nickname> [<duration> :<reason>]";
66 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
68 /* syntax: svshold nickname time :reason goes here */
69 /* 'time' is a human-readable timestring, like 2d3h2s. */
73 /* form: svshold nickname removes a hold. */
74 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
76 if (parameters[0] == iter->nickname)
78 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
79 user->WriteServ( "386 %s %s :Removed SVSHOLD with %lu seconds left before expiry (%s)", user->nick, iter->nickname.c_str(), remaining, iter->reason.c_str());
87 /* full form to add a SVSHold */
88 if (ServerInstance->IsNick(parameters[0]))
90 // parameters[0] = w00t
91 // parameters[1] = 1h3m2s
92 // parameters[2] = Registered nickname
93 long length = ServerInstance->Duration(parameters[1]);
94 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
96 SVSHolds.push_back(SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason));
98 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
102 user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
103 ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
107 user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
108 ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
113 /* as this is primarily a Services command, do not provide an error */
122 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2)
124 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
127 class ModuleSVSHold : public Module
129 cmd_svshold *mycommand;
133 ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
135 mycommand = new cmd_svshold(Me);
136 ServerInstance->AddCommand(mycommand);
139 void Implements(char* List)
141 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
144 virtual int OnStats(char symbol, userrec* user, string_list &results)
150 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
152 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
153 results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->nickname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
160 virtual int OnUserPreNick(userrec *user, const std::string &newnick)
164 /* check SVSHolds in here, and apply as necessary. */
165 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
167 if (iter->nickname == newnick)
170 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), iter->reason.c_str());
177 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
179 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
181 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(*iter));
185 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
187 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
189 SVSHolds.push_back(DecodeSVSHold(extdata));
190 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
194 virtual ~ModuleSVSHold()
198 virtual Version GetVersion()
200 return Version(1,0,0,1,VF_VENDOR);
203 std::string EncodeSVSHold(const SVSHold &ban)
205 std::ostringstream stream;
206 stream << ban.nickname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
210 SVSHold DecodeSVSHold(const std::string &data)
213 std::istringstream stream(data);
214 stream >> res.nickname;
215 stream >> res.set_by;
216 stream >> res.set_on;
217 stream >> res.length;
218 res.reason = stream.str();
225 bool go_again = true;
231 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
233 /* 0 == permanent, don't mess with them! -- w00t */
234 if (iter->length != 0)
236 if (iter->set_on + iter->length <= ServerInstance->Time())
238 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", iter->nickname.c_str());
239 ServerInstance->WriteOpers("*** %li second SVSHOLD on %s (%s) set %u seconds ago expired", iter->length, iter->nickname.c_str(), iter->reason.c_str(), ServerInstance->Time() - iter->set_on);
240 SVSHolds.erase(iter);
245 if (go_again == true)
252 class ModuleSVSHoldFactory : public ModuleFactory
255 ModuleSVSHoldFactory()
259 ~ModuleSVSHoldFactory()
263 virtual Module * CreateModule(InspIRCd* Me)
265 return new ModuleSVSHold(Me);
271 extern "C" void * init_module( void )
273 return new ModuleSVSHoldFactory;