1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
16 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
18 /** Holds a SVSHold item
20 class SVSHold : public classbase
33 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)
39 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2);
41 typedef std::vector<SVSHold*> SVSHoldlist;
42 typedef std::map<irc::string, SVSHold*> SVSHoldMap;
44 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
50 class CommandSvshold : public Command
53 CommandSvshold(InspIRCd* Me) : Command(Me, "SVSHOLD", "o", 1)
55 this->source = "m_svshold.so";
56 this->syntax = "<nickname> [<duration> :<reason>]";
57 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
60 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
62 /* syntax: svshold nickname time :reason goes here */
63 /* 'time' is a human-readable timestring, like 2d3h2s. */
65 if (!ServerInstance->ULine(user->server))
67 /* don't allow SVSHOLD from non-ulined clients */
71 if (parameters.size() == 1)
73 SVSHoldMap::iterator n = HoldMap.find(parameters[0].c_str());
74 if (n != HoldMap.end())
76 /* form: svshold nickname removes a hold. */
77 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
79 if (parameters[0] == assign((*iter)->nickname))
81 unsigned long remaining = 0;
84 remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
85 user->WriteServ( "386 %s %s :Removed SVSHOLD with %lu seconds left before expiry (%s)", user->nick.c_str(), (*iter)->nickname.c_str(), (unsigned long)remaining, (*iter)->reason.c_str());
89 user->WriteServ( "386 %s %s :Removed permanent SVSHOLD (%s)", user->nick.c_str(), (*iter)->nickname.c_str(), (*iter)->reason.c_str());
100 else if (parameters.size() >= 2)
102 /* full form to add a SVSHold */
104 /* NOTE: We check nicks up to 512 in length here, as a hax to allow
105 * remote nicks that are longer than our configuration to be held
107 if (ServerInstance->IsNick(parameters[0].c_str(), 512))
109 // parameters[0] = w00t
110 // parameters[1] = 1h3m2s
111 // parameters[2] = Registered nickname
113 /* Already exists? */
114 if (HoldMap.find(parameters[0].c_str()) != HoldMap.end())
116 user->WriteServ( "385 %s %s :SVSHOLD already exists", user->nick.c_str(), parameters[0].c_str());
120 unsigned long length = ServerInstance->Duration(parameters[1]);
121 std::string reason = (parameters.size() > 2) ? parameters[2] : "No reason supplied";
123 SVSHold* S = new SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason);
124 SVSHolds.push_back(S);
125 HoldMap[parameters[0].c_str()] = S;
127 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
131 user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick.c_str(), parameters[0].c_str(), length, reason.c_str());
132 ServerInstance->SNO->WriteToSnoMask('A', "%s added %lu second SVSHOLD on %s (%s)", user->nick.c_str(), length, parameters[0].c_str(), reason.c_str());
136 user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick.c_str(), parameters[0].c_str(), parameters[0].c_str(), reason.c_str());
137 ServerInstance->SNO->WriteToSnoMask('A', "%s added permanent SVSHOLD on %s (%s)", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
142 /* as this is primarily a Services command, do not provide an error */
151 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2)
153 return ((ban1->set_on + ban1->length) < (ban2->set_on + ban2->length));
156 class ModuleSVSHold : public Module
158 CommandSvshold *mycommand;
162 ModuleSVSHold(InspIRCd* Me) : Module(Me)
164 mycommand = new CommandSvshold(Me);
165 ServerInstance->AddCommand(mycommand);
166 Implementation eventlist[] = { I_OnUserPreNick, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnStats };
167 ServerInstance->Modules->Attach(eventlist, this, 4);
171 virtual int OnStats(char symbol, User* user, string_list &results)
177 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
179 unsigned long remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
180 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);
187 virtual int OnUserPreNick(User *user, const std::string &newnick)
191 /* check SVSHolds in here, and apply as necessary. */
192 SVSHoldMap::iterator n = HoldMap.find(assign(newnick));
193 if (n != HoldMap.end())
195 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick.c_str(), newnick.c_str(), n->second->reason.c_str());
201 virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
203 for(SVSHoldMap::iterator iter = HoldMap.begin(); iter != HoldMap.end(); iter++)
205 proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(iter->second));
209 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
211 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
213 SVSHold* S = DecodeSVSHold(extdata); /* NOTE: Allocates a new SVSHold* */
214 if (HoldMap.find(assign(S->nickname)) == HoldMap.end())
216 SVSHolds.push_back(S);
217 HoldMap[assign(S->nickname)] = S;
218 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
227 virtual ~ModuleSVSHold()
231 virtual Version GetVersion()
233 return Version(1, 2, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
236 std::string EncodeSVSHold(const SVSHold* ban)
238 std::ostringstream stream;
239 stream << ban->nickname << " " << ban->set_by << " " << ban->set_on << " " << ban->length << " :" << ban->reason;
243 SVSHold* DecodeSVSHold(const std::string &data)
245 SVSHold* res = new SVSHold();
247 irc::tokenstream tokens(data);
248 tokens.GetToken(res->nickname);
249 tokens.GetToken(res->set_by);
250 tokens.GetToken(set_on);
251 res->set_on = set_on;
252 tokens.GetToken(res->length);
253 tokens.GetToken(res->reason);
259 SVSHoldlist::iterator iter,safeiter;
260 for (iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
262 /* 0 == permanent, don't mess with them! -- w00t */
263 if ((*iter)->length != 0)
265 if ((*iter)->set_on + (*iter)->length <= ServerInstance->Time())
267 ServerInstance->Logs->Log("m_svshold",DEBUG, "m_svshold.so: hold on %s expired, removing...", (*iter)->nickname.c_str());
268 ServerInstance->SNO->WriteToSnoMask('A',"%lu second SVSHOLD on %s (%s) set %lu seconds ago expired", (unsigned long) (*iter)->length, (*iter)->nickname.c_str(), (*iter)->reason.c_str(), (unsigned long) ServerInstance->Time() - (*iter)->set_on);
269 HoldMap.erase(assign((*iter)->nickname));
273 SVSHolds.erase(safeiter);
280 MODULE_INIT(ModuleSVSHold)