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(const std::string &nn, const std::string &sb, const time_t so, const long ln, const std::string &rs) : nickname(nn), set_by(sb), set_on(so), length(ln), reason(rs)
48 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2);
50 typedef std::vector<SVSHold> SVSHoldlist;
52 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
57 class cmd_svshold : public command_t
60 cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
62 this->source = "m_svshold.so";
64 syntax = "<nickname> [<duration> :<reason>]";
67 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
69 /* syntax: svshold nickname time :reason goes here */
70 /* 'time' is a human-readable timestring, like 2d3h2s. */
74 /* form: svshold nickname removes a hold. */
75 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
77 if (irc::string(parameters[0]) == irc::string(iter->nickname.c_str()))
79 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
80 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());
88 /* full form to add a SVSHold */
89 if (ServerInstance->IsNick(parameters[0]))
91 // parameters[0] = w00t
92 // parameters[1] = 1h3m2s
93 // parameters[2] = Registered nickname
94 long length = ServerInstance->Duration(parameters[1]);
95 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
97 SVSHolds.push_back(SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason));
99 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
103 user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
104 ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
108 user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
109 ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
114 /* as this is primarily a Services command, do not provide an error */
123 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2)
125 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
128 class ModuleSVSHold : public Module
130 cmd_svshold *mycommand;
134 ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
136 mycommand = new cmd_svshold(Me);
137 ServerInstance->AddCommand(mycommand);
140 void Implements(char* List)
142 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
145 virtual int OnStats(char symbol, userrec* user, string_list &results)
151 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
153 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
154 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);
161 virtual int OnUserPreNick(userrec *user, const std::string &newnick)
165 /* check SVSHolds in here, and apply as necessary. */
166 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
168 if (irc::string(iter->nickname.c_str()) == irc::string(newnick.c_str()))
171 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), iter->reason.c_str());
178 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
180 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
182 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(*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 == "SVSHold"))
190 SVSHolds.push_back(DecodeSVSHold(extdata));
191 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
195 virtual ~ModuleSVSHold()
199 virtual Version GetVersion()
201 return Version(1,1,0,1,VF_VENDOR|VF_COMMON,API_VERSION);
204 std::string EncodeSVSHold(const SVSHold &ban)
206 std::ostringstream stream;
207 stream << ban.nickname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
211 SVSHold DecodeSVSHold(const std::string &data)
214 std::istringstream stream(data);
215 stream >> res.nickname;
216 stream >> res.set_by;
217 stream >> res.set_on;
218 stream >> res.length;
219 res.reason = stream.str();
226 bool go_again = true;
232 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
234 /* 0 == permanent, don't mess with them! -- w00t */
235 if (iter->length != 0)
237 if (iter->set_on + iter->length <= ServerInstance->Time())
239 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", iter->nickname.c_str());
240 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);
241 SVSHolds.erase(iter);
246 if (go_again == true)
253 class ModuleSVSHoldFactory : public ModuleFactory
256 ModuleSVSHoldFactory()
260 ~ModuleSVSHoldFactory()
264 virtual Module * CreateModule(InspIRCd* Me)
266 return new ModuleSVSHold(Me);
272 extern "C" void * init_module( void )
274 return new ModuleSVSHoldFactory;