From 0e758a2ac23dc4a001e8e126cec14588da9a9769 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 28 Jul 2016 21:29:35 -0400 Subject: Allow serializable fields to use storage in the respective objects. Split service management code nito a proper servicemanager. Make service references managed instead of lazy lookup. Also made events and serializable use service manager instead of their respective systems for management --- modules/hostserv/request.cpp | 99 ++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 45 deletions(-) (limited to 'modules/hostserv/request.cpp') diff --git a/modules/hostserv/request.cpp b/modules/hostserv/request.cpp index 8a064a84c..25f3de12b 100644 --- a/modules/hostserv/request.cpp +++ b/modules/hostserv/request.cpp @@ -1,7 +1,7 @@ /* * * - * (C) 2003-2014 Anope Team + * (C) 2003-2016 Anope Team * Contact us at team@anope.org * * Please read COPYING and README for further details. @@ -11,11 +11,17 @@ #include "module.h" #include "modules/memoserv.h" -static void req_send_memos(Module *me, CommandSource &source, const Anope::string &vIdent, const Anope::string &vHost); - class HostRequest : public Serialize::Object { + friend class HostRequestType; + + NickServ::Nick *na = nullptr; + Anope::string ident, host; + time_t time = 0; + public: + static constexpr const char *const NAME = "hostrequest"; + HostRequest(Serialize::TypeBase *type) : Serialize::Object(type) { } HostRequest(Serialize::TypeBase *type, Serialize::ID id) : Serialize::Object(type, id) { } @@ -39,11 +45,11 @@ class HostRequestType : public Serialize::Type Serialize::Field ident, host; Serialize::Field time; - HostRequestType(Module *me) : Serialize::Type(me, "HostRequest") - , na(this, "na", true) - , ident(this, "ident") - , host(this, "host") - , time(this, "time") + HostRequestType(Module *me) : Serialize::Type(me) + , na(this, "na", &HostRequest::na, true) + , ident(this, "ident", &HostRequest::ident) + , host(this, "host", &HostRequest::host) + , time(this, "time", &HostRequest::time) { } }; @@ -88,10 +94,32 @@ void HostRequest::SetTime(const time_t &t) Set(&HostRequestType::time, t); } -static Serialize::TypeReference hostrequest("HostRequest"); - class CommandHSRequest : public Command { + ServiceReference memoserv; + + void SendMemos(CommandSource &source, const Anope::string &vIdent, const Anope::string &vHost) + { + Anope::string host; + + if (!vIdent.empty()) + host = vIdent + "@" + vHost; + else + host = vHost; + + if (Config->GetModule(GetOwner())->Get("memooper") && memoserv) + for (Oper *o : Serialize::GetObjects()) + { + NickServ::Nick *na = NickServ::FindNick(o->GetName()); + if (!na) + continue; + + Anope::string message = Anope::printf(_("[auto memo] vHost \002%s\002 has been requested by %s."), host.c_str(), source.GetNick().c_str()); + + memoserv->Send(source.service->nick, na->GetNick(), message, true); + } + } + public: CommandHSRequest(Module *creator) : Command(creator, "hostserv/request", 1, 1) { @@ -174,21 +202,21 @@ class CommandHSRequest : public Command } time_t send_delay = Config->GetModule("memoserv")->Get("senddelay"); - if (Config->GetModule(this->owner)->Get("memooper") && send_delay > 0 && u && u->lastmemosend + send_delay > Anope::CurTime) + if (Config->GetModule(this->GetOwner())->Get("memooper") && send_delay > 0 && u && u->lastmemosend + send_delay > Anope::CurTime) { source.Reply(_("Please wait %d seconds before requesting a new vHost."), send_delay); u->lastmemosend = Anope::CurTime; return; } - HostRequest *req = hostrequest.Create(); + HostRequest *req = Serialize::New(); req->SetNick(na); req->SetIdent(user); req->SetHost(host); req->SetTime(Anope::CurTime); source.Reply(_("Your vhost has been requested.")); - req_send_memos(owner, source, user, host); + this->SendMemos(source, user, host); Log(LOG_COMMAND, source, this) << "to request new vhost " << (!user.empty() ? user + "@" : "") << host; } @@ -201,6 +229,8 @@ class CommandHSRequest : public Command class CommandHSActivate : public Command { + ServiceReference memoserv; + public: CommandHSActivate(Module *creator) : Command(creator, "hostserv/activate", 1, 1) { @@ -233,10 +263,10 @@ class CommandHSActivate : public Command } na->SetVhost(req->GetIdent(), req->GetHost(), source.GetNick(), req->GetTime()); - Event::OnSetVhost(&Event::SetVhost::OnSetVhost, na); + EventManager::Get()->Dispatch(&Event::SetVhost::OnSetVhost, na); - if (Config->GetModule(this->owner)->Get("memouser") && MemoServ::service) - MemoServ::service->Send(source.service->nick, na->GetNick(), _("[auto memo] Your requested vHost has been approved."), true); + if (Config->GetModule(this->GetOwner())->Get("memouser") && memoserv) + memoserv->Send(source.service->nick, na->GetNick(), _("[auto memo] Your requested vHost has been approved."), true); source.Reply(_("Vhost for \002{0}\002 has been activated."), na->GetNick()); Log(LOG_COMMAND, source, this) << "for " << na->GetNick() << " for vhost " << (!req->GetIdent().empty() ? req->GetIdent() + "@" : "") << req->GetHost(); @@ -246,7 +276,7 @@ class CommandHSActivate : public Command bool OnHelp(CommandSource &source, const Anope::string &subcommand) override { source.Reply(_("Activate the requested vhost for the given user.")); - if (Config->GetModule(this->owner)->Get("memouser")) + if (Config->GetModule(this->GetOwner())->Get("memouser")) source.Reply(_("A memo informing the user will also be sent.")); return true; @@ -255,6 +285,8 @@ class CommandHSActivate : public Command class CommandHSReject : public Command { + ServiceReference memoserv; + public: CommandHSReject(Module *creator) : Command(creator, "hostserv/reject", 1, 2) { @@ -289,7 +321,7 @@ class CommandHSReject : public Command req->Delete(); - if (Config->GetModule(this->owner)->Get("memouser") && MemoServ::service) + if (Config->GetModule(this->GetOwner())->Get("memouser") && memoserv) { Anope::string message; if (!reason.empty()) @@ -297,7 +329,7 @@ class CommandHSReject : public Command else message = _("[auto memo] Your requested vHost has been rejected."); - MemoServ::service->Send(source.service->nick, nick, Language::Translate(source.GetAccount(), message.c_str()), true); + memoserv->Send(source.service->nick, nick, Language::Translate(source.GetAccount(), message.c_str()), true); } source.Reply(_("Vhost for \002{0}\002 has been rejected."), na->GetNick()); @@ -307,7 +339,7 @@ class CommandHSReject : public Command bool OnHelp(CommandSource &source, const Anope::string &subcommand) override { source.Reply(_("Reject the requested vhost for the given user.")); - if (Config->GetModule(this->owner)->Get("memouser")) + if (Config->GetModule(this->GetOwner())->Get("memouser")) source.Reply(_("A memo informing the user will also be sent, which includes the reason for the rejection if supplied.")); return true; @@ -325,12 +357,12 @@ class CommandHSWaiting : public Command void Execute(CommandSource &source, const std::vector ¶ms) override { unsigned counter = 0; - unsigned display_counter = 0, listmax = Config->GetModule(this->owner)->Get("listmax"); + unsigned display_counter = 0, listmax = Config->GetModule(this->GetOwner())->Get("listmax"); ListFormatter list(source.GetAccount()); list.AddColumn(_("Number")).AddColumn(_("Nick")).AddColumn(_("Vhost")).AddColumn(_("Created")); - for (HostRequest *hr : Serialize::GetObjects(hostrequest)) + for (HostRequest *hr : Serialize::GetObjects()) { if (!listmax || display_counter < listmax) { @@ -388,27 +420,4 @@ class HSRequest : public Module } }; -static void req_send_memos(Module *me, CommandSource &source, const Anope::string &vIdent, const Anope::string &vHost) -{ - Anope::string host; - std::list >::iterator it, it_end; - - if (!vIdent.empty()) - host = vIdent + "@" + vHost; - else - host = vHost; - - if (Config->GetModule(me)->Get("memooper") && MemoServ::service) - for (Oper *o : Serialize::GetObjects(operblock)) - { - NickServ::Nick *na = NickServ::FindNick(o->GetName()); - if (!na) - continue; - - Anope::string message = Anope::printf(_("[auto memo] vHost \002%s\002 has been requested by %s."), host.c_str(), source.GetNick().c_str()); - - MemoServ::service->Send(source.service->nick, na->GetNick(), message, true); - } -} - MODULE_INIT(HSRequest) -- cgit