From 733a94902d663adab56f4ac10d5f04a0d69554b6 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 3 Dec 2017 14:41:10 -0500 Subject: nickserv/cert: use serialize find and regiser type --- modules/nickserv/cert.cpp | 81 ++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/modules/nickserv/cert.cpp b/modules/nickserv/cert.cpp index cb089e26e..eb6f5f3d0 100644 --- a/modules/nickserv/cert.cpp +++ b/modules/nickserv/cert.cpp @@ -24,35 +24,6 @@ static Anope::hash_map certmap; -class CertServiceImpl : public CertService -{ - public: - CertServiceImpl(Module *o) : CertService(o) { } - - NickServ::Account* FindAccountFromCert(const Anope::string &cert) override - { -#warning "use serialize find" - Anope::hash_map::iterator it = certmap.find(cert); - if (it != certmap.end()) - return it->second; - return NULL; - } - - bool Matches(User *u, NickServ::Account *nc) override - { - std::vector cl = nc->GetRefs(); - return !u->fingerprint.empty() && FindCert(cl, u->fingerprint); - } - - NSCertEntry *FindCert(const std::vector &cl, const Anope::string &certfp) override - { - for (NSCertEntry *e : cl) - if (e->GetCert() == certfp) - return e; - return nullptr; - } -}; - class NSCertEntryImpl : public NSCertEntry { friend class NSCertEntryType; @@ -78,7 +49,7 @@ class NSCertEntryType : public Serialize::Type { using Serialize::ObjectField::ObjectField; - void OnSet(NSCertEntryImpl *s, NickServ::Account *acc) override + void OnSet(NSCertEntryImpl *s, NickServ::Account *old, NickServ::Account *acc) override { const Anope::string &cert = s->GetCert(); @@ -91,9 +62,8 @@ class NSCertEntryType : public Serialize::Type { using Serialize::Field::Field; - void OnSet(NSCertEntryImpl *s, const Anope::string &m) override + void OnSet(NSCertEntryImpl *s, Anope::string *old, const Anope::string &m) override { - Anope::string *old = this->Get_(s); if (old != nullptr) certmap.erase(*old); @@ -107,6 +77,49 @@ class NSCertEntryType : public Serialize::Type , mask(this, "mask", &NSCertEntryImpl::cert) { } + + NickServ::Account *FindAccount(const Anope::string &cert) + { + Serialize::ID id; + EventReturn result = EventManager::Get()->Dispatch(&Event::SerializeEvents::OnSerializeFind, this, &this->mask, cert, id); + if (result == EVENT_ALLOW) + return RequireID(id)->GetAccount(); + + Anope::hash_map::iterator it = certmap.find(cert); + if (it != certmap.end()) + return it->second; + return nullptr; + } +}; + +class CertServiceImpl : public CertService +{ + NSCertEntryType &cert_type; + + public: + CertServiceImpl(Module *o, NSCertEntryType &ct) : CertService(o) + , cert_type(ct) + { + } + + NickServ::Account* FindAccountFromCert(const Anope::string &cert) override + { + return cert_type.FindAccount(cert); + } + + bool Matches(User *u, NickServ::Account *nc) override + { + std::vector cl = nc->GetRefs(); + return !u->fingerprint.empty() && FindCert(cl, u->fingerprint); + } + + NSCertEntry *FindCert(const std::vector &cl, const Anope::string &certfp) override + { + for (NSCertEntry *e : cl) + if (e->GetCert() == certfp) + return e; + return nullptr; + } }; NSCertEntryImpl::~NSCertEntryImpl() @@ -317,6 +330,7 @@ class NSCert : public Module , public EventHook , public EventHook { + NSCertEntryType certtype; CommandNSCert commandnscert; CertServiceImpl cs; @@ -324,8 +338,9 @@ class NSCert : public Module NSCert(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR) , EventHook(this) , EventHook(this) + , certtype(this) , commandnscert(this) - , cs(this) + , cs(this, certtype) { if (!IRCD || !IRCD->CanCertFP) throw ModuleException("Your IRCd does not support ssl client certificates"); -- cgit