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;
47 typedef std::map<irc::string, SVSHold*> SVSHoldMap;
49 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
55 class cmd_svshold : public command_t
58 cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
60 this->source = "m_svshold.so";
61 this->syntax = "<nickname> [<duration> :<reason>]";
64 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
66 /* syntax: svshold nickname time :reason goes here */
67 /* 'time' is a human-readable timestring, like 2d3h2s. */
71 SVSHoldMap::iterator n = HoldMap.find(parameters[0]);
72 if (n != HoldMap.end())
74 /* form: svshold nickname removes a hold. */
75 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
77 if (parameters[0] == assign((*iter)->nickname))
79 unsigned long remaining = 0;
82 remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
83 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 user->WriteServ( "386 %s %s :Removed permenant SVSHOLD (%s)", user->nick, (*iter)->nickname.c_str(), (*iter)->reason.c_str());
100 /* full form to add a SVSHold */
101 if (ServerInstance->IsNick(parameters[0]))
103 // parameters[0] = w00t
104 // parameters[1] = 1h3m2s
105 // parameters[2] = Registered nickname
107 /* Already exists? */
108 if (HoldMap.find(parameters[0]) != HoldMap.end())
110 user->WriteServ( "385 %s %s :SVSHOLD already exists", user->nick, parameters[0]);
114 long length = ServerInstance->Duration(parameters[1]);
115 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
117 SVSHold* S = new SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason);
118 SVSHolds.push_back(S);
119 HoldMap[parameters[0]] = S;
121 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
125 user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
126 ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
130 user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], parameters[0], reason.c_str());
131 ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
136 /* as this is primarily a Services command, do not provide an error */
145 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2)
147 return ((ban1->set_on + ban1->length) < (ban2->set_on + ban2->length));
150 class ModuleSVSHold : public Module
152 cmd_svshold *mycommand;
156 ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
158 mycommand = new cmd_svshold(Me);
159 ServerInstance->AddCommand(mycommand);
162 void Implements(char* List)
164 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
167 virtual int OnStats(char symbol, userrec* user, string_list &results)
173 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
175 unsigned long remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
176 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);
183 virtual int OnUserPreNick(userrec *user, const std::string &newnick)
187 /* check SVSHolds in here, and apply as necessary. */
188 SVSHoldMap::iterator n = HoldMap.find(assign(newnick));
189 if (n != HoldMap.end())
191 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), n->second->reason.c_str());
197 virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
199 for(SVSHoldMap::iterator iter = HoldMap.begin(); iter != HoldMap.end(); iter++)
201 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(iter->second));
205 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
207 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
209 SVSHold* S = DecodeSVSHold(extdata); /* NOTE: Allocates a new SVSHold* */
210 if (HoldMap.find(assign(S->nickname)) == HoldMap.end())
212 SVSHolds.push_back(S);
213 HoldMap[assign(S->nickname)] = S;
214 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
223 virtual ~ModuleSVSHold()
227 virtual Version GetVersion()
229 return Version(1,1,0,1,VF_VENDOR|VF_COMMON,API_VERSION);
232 std::string EncodeSVSHold(const SVSHold* ban)
234 std::ostringstream stream;
235 stream << ban->nickname << " " << ban->set_by << " " << ban->set_on << " " << ban->length << " " << ban->reason;
239 SVSHold* DecodeSVSHold(const std::string &data)
241 SVSHold* res = new SVSHold();
242 std::istringstream stream(data);
243 stream >> res->nickname;
244 stream >> res->set_by;
245 stream >> res->set_on;
246 stream >> res->length;
247 res->reason = stream.str();
253 SVSHoldlist::iterator iter,safeiter;
254 for (iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
256 /* 0 == permanent, don't mess with them! -- w00t */
257 if ((*iter)->length != 0)
259 if ((*iter)->set_on + (*iter)->length <= ServerInstance->Time())
261 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", (*iter)->nickname.c_str());
262 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);
263 HoldMap.erase(assign((*iter)->nickname));
267 SVSHolds.erase(safeiter);
274 class ModuleSVSHoldFactory : public ModuleFactory
277 ModuleSVSHoldFactory()
281 ~ModuleSVSHoldFactory()
285 virtual Module * CreateModule(InspIRCd* Me)
287 return new ModuleSVSHold(Me);
293 extern "C" void * init_module( void )
295 return new ModuleSVSHoldFactory;