1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "configreader.h"
21 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
23 /** Holds a SVSHold item
25 class SVSHold : public classbase
38 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)
44 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2);
46 typedef std::vector<SVSHold> SVSHoldlist;
48 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
53 class cmd_svshold : public command_t
56 cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
58 this->source = "m_svshold.so";
60 syntax = "<nickname> [<duration> :<reason>]";
63 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
65 /* syntax: svshold nickname time :reason goes here */
66 /* 'time' is a human-readable timestring, like 2d3h2s. */
70 /* form: svshold nickname removes a hold. */
71 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
73 if (irc::string(parameters[0]) == irc::string(iter->nickname.c_str()))
75 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
76 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());
84 /* full form to add a SVSHold */
85 if (ServerInstance->IsNick(parameters[0]))
87 // parameters[0] = w00t
88 // parameters[1] = 1h3m2s
89 // parameters[2] = Registered nickname
90 long length = ServerInstance->Duration(parameters[1]);
91 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
93 SVSHolds.push_back(SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason));
95 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
99 user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
100 ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
104 user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
105 ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
110 /* as this is primarily a Services command, do not provide an error */
119 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2)
121 return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
124 class ModuleSVSHold : public Module
126 cmd_svshold *mycommand;
130 ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
132 mycommand = new cmd_svshold(Me);
133 ServerInstance->AddCommand(mycommand);
136 void Implements(char* List)
138 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
141 virtual int OnStats(char symbol, userrec* user, string_list &results)
147 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
149 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
150 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);
157 virtual int OnUserPreNick(userrec *user, const std::string &newnick)
161 /* check SVSHolds in here, and apply as necessary. */
162 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
164 if (irc::string(iter->nickname.c_str()) == irc::string(newnick.c_str()))
167 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), iter->reason.c_str());
174 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
176 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
178 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(*iter));
182 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
184 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
186 SVSHolds.push_back(DecodeSVSHold(extdata));
187 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
191 virtual ~ModuleSVSHold()
195 virtual Version GetVersion()
197 return Version(1,1,0,1,VF_VENDOR|VF_COMMON,API_VERSION);
200 std::string EncodeSVSHold(const SVSHold &ban)
202 std::ostringstream stream;
203 stream << ban.nickname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
207 SVSHold DecodeSVSHold(const std::string &data)
210 std::istringstream stream(data);
211 stream >> res.nickname;
212 stream >> res.set_by;
213 stream >> res.set_on;
214 stream >> res.length;
215 res.reason = stream.str();
222 bool go_again = true;
228 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
230 /* 0 == permanent, don't mess with them! -- w00t */
231 if (iter->length != 0)
233 if (iter->set_on + iter->length <= ServerInstance->Time())
235 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", iter->nickname.c_str());
236 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);
237 SVSHolds.erase(iter);
242 if (go_again == true)
249 class ModuleSVSHoldFactory : public ModuleFactory
252 ModuleSVSHoldFactory()
256 ~ModuleSVSHoldFactory()
260 virtual Module * CreateModule(InspIRCd* Me)
262 return new ModuleSVSHold(Me);
268 extern "C" void * init_module( void )
270 return new ModuleSVSHoldFactory;