diff options
Diffstat (limited to 'src/nickcore.cpp')
-rw-r--r-- | src/nickcore.cpp | 156 |
1 files changed, 39 insertions, 117 deletions
diff --git a/src/nickcore.cpp b/src/nickcore.cpp index 1d70ad522..190a026bd 100644 --- a/src/nickcore.cpp +++ b/src/nickcore.cpp @@ -18,25 +18,22 @@ Serialize::Checker<nickcore_map> NickCoreList("NickCore"); nickcoreid_map NickCoreIdList; -NickCore::NickCore(const Anope::string &coredisplay, uint64_t coreid) : Serializable("NickCore"), chanaccess("ChannelInfo"), aliases("NickAlias") +NickCore::NickCore(const Anope::string &coredisplay, uint64_t coreid) + : Serializable("NickCore") + , chanaccess("ChannelInfo") + , id(coreid) + , display(coredisplay) + , aliases("NickAlias") { if (coredisplay.empty()) throw CoreException("Empty display passed to NickCore constructor"); - this->o = NULL; - this->channelcount = 0; - this->lastmail = 0; + if (!NickCoreList->insert_or_assign(this->display, this).second) + Log(LOG_DEBUG) << "Duplicate account " << this->display << " in NickCore table"; - this->display = coredisplay; - this->id = coreid; - - size_t old = NickCoreList->size(); - (*NickCoreList)[this->display] = this; - if (old == NickCoreList->size()) - Log(LOG_DEBUG) << "Duplicate account " << coredisplay << " in nickcore table?"; - - if (this->id) - NickCoreIdList[this->id] = this; + // Upgrading users may not have an account identifier. + if (this->id && !NickCoreIdList.insert_or_assign(this->id, this).second) + Log(LOG_DEBUG) << "Duplicate account id " << this->id << " in NickCore table"; FOREACH_MOD(OnNickCoreCreate, (this)); } @@ -61,8 +58,6 @@ NickCore::~NickCore() if (this->id) NickCoreIdList.erase(this->id); - this->ClearAccess(); - if (!this->memos.memos->empty()) { for (unsigned i = 0, end = this->memos.memos->size(); i < end; ++i) @@ -73,20 +68,24 @@ NickCore::~NickCore() void NickCore::Serialize(Serialize::Data &data) const { - data["display"] << this->display; - data["uniqueid"] << this->id; - data["pass"] << this->pass; - data["email"] << this->email; - data["language"] << this->language; - for (unsigned i = 0; i < this->access.size(); ++i) - data["access"] << this->access[i] << " "; - data["memomax"] << this->memos.memomax; - for (unsigned i = 0; i < this->memos.ignores.size(); ++i) - data["memoignores"] << this->memos.ignores[i] << " "; + data.Store("display", this->display); + data.Store("uniqueid", this->id); + data.Store("pass", this->pass); + data.Store("email", this->email); + data.Store("language", this->language); + data.Store("lastmail", this->lastmail); + data.Store("time_registered", this->time_registered); + data.Store("memomax", this->memos.memomax); + + std::ostringstream oss; + for (const auto &ignore : this->memos.ignores) + oss << ignore << " "; + data.Store("memoignores", oss.str()); + Extensible::ExtensibleSerialize(this, this, data); } -Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data) +Serializable *NickCore::Unserialize(Serializable *obj, Serialize::Data &data) { NickCore *nc; @@ -104,14 +103,8 @@ Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data) data["pass"] >> nc->pass; data["email"] >> nc->email; data["language"] >> nc->language; - { - Anope::string buf; - data["access"] >> buf; - spacesepstream sep(buf); - nc->access.clear(); - while (sep.GetToken(buf)) - nc->access.push_back(buf); - } + data["lastmail"] >> nc->lastmail; + data["time_registered"] >> nc->time_registered; data["memomax"] >> nc->memos.memomax; { Anope::string buf; @@ -127,10 +120,6 @@ Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data) /* compat */ bool b; b = false; - data["extensible:SECURE"] >> b; - if (b) - nc->Extend<bool>("NS_SECURE"); - b = false; data["extensible:PRIVATE"] >> b; if (b) nc->Extend<bool>("NS_PRIVATE"); @@ -163,7 +152,7 @@ Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data) return nc; } -void NickCore::SetDisplay(const NickAlias *na) +void NickCore::SetDisplay(NickAlias *na) { if (na->nc != this || na->nick == this->display) return; @@ -171,13 +160,14 @@ void NickCore::SetDisplay(const NickAlias *na) FOREACH_MOD(OnChangeCoreDisplay, (this, na->nick)); /* this affects the serialized aliases */ - for (unsigned i = 0; i < aliases->size(); ++i) - aliases->at(i)->QueueUpdate(); + for (auto *alias : *aliases) + alias->QueueUpdate(); /* Remove the core from the list */ NickCoreList->erase(this->display); this->display = na->nick; + this->na = na; (*NickCoreList)[this->display] = this; } @@ -187,67 +177,6 @@ bool NickCore::IsServicesOper() const return this->o != NULL; } -void NickCore::AddAccess(const Anope::string &entry) -{ - this->access.push_back(entry); - FOREACH_MOD(OnNickAddAccess, (this, entry)); -} - -Anope::string NickCore::GetAccess(unsigned entry) const -{ - if (this->access.empty() || entry >= this->access.size()) - return ""; - return this->access[entry]; -} - -unsigned NickCore::GetAccessCount() const -{ - return this->access.size(); -} - -bool NickCore::FindAccess(const Anope::string &entry) -{ - for (unsigned i = 0, end = this->access.size(); i < end; ++i) - if (this->access[i] == entry) - return true; - - return false; -} - -void NickCore::EraseAccess(const Anope::string &entry) -{ - for (unsigned i = 0, end = this->access.size(); i < end; ++i) - if (this->access[i] == entry) - { - FOREACH_MOD(OnNickEraseAccess, (this, entry)); - this->access.erase(this->access.begin() + i); - break; - } -} - -void NickCore::ClearAccess() -{ - FOREACH_MOD(OnNickClearAccess, (this)); - this->access.clear(); -} - -bool NickCore::IsOnAccess(const User *u) const -{ - Anope::string buf = u->GetIdent() + "@" + u->host, buf2, buf3; - if (!u->vhost.empty()) - buf2 = u->GetIdent() + "@" + u->vhost; - if (!u->GetCloakedHost().empty()) - buf3 = u->GetIdent() + "@" + u->GetCloakedHost(); - - for (unsigned i = 0, end = this->access.size(); i < end; ++i) - { - Anope::string a = this->GetAccess(i); - if (Anope::Match(buf, a) || (!buf2.empty() && Anope::Match(buf2, a)) || (!buf3.empty() && Anope::Match(buf3, a))) - return true; - } - return false; -} - void NickCore::AddChannelReference(ChannelInfo *ci) { ++(*this->chanaccess)[ci]; @@ -255,7 +184,7 @@ void NickCore::AddChannelReference(ChannelInfo *ci) void NickCore::RemoveChannelReference(ChannelInfo *ci) { - int& i = (*this->chanaccess)[ci]; + int &i = (*this->chanaccess)[ci]; if (--i <= 0) this->chanaccess->erase(ci); } @@ -263,11 +192,11 @@ void NickCore::RemoveChannelReference(ChannelInfo *ci) void NickCore::GetChannelReferences(std::deque<ChannelInfo *> &queue) { queue.clear(); - for (std::map<ChannelInfo *, int>::iterator it = this->chanaccess->begin(), it_end = this->chanaccess->end(); it != it_end; ++it) - queue.push_back(it->first); + for (const auto &[ci, _] : *this->chanaccess) + queue.push_back(ci); } -NickCore* NickCore::Find(const Anope::string &nick) +NickCore *NickCore::Find(const Anope::string &nick) { nickcore_map::const_iterator it = NickCoreList->find(nick); if (it != NickCoreList->end()) @@ -284,14 +213,7 @@ uint64_t NickCore::GetId() if (this->id) return this->id; - NickAlias *na = NickAlias::Find(this->display); - if (!na) - { - Log(LOG_DEBUG) << "Unable to find the display NickAlias for NickCore: " << this->display; - return 0; - } - - Anope::string secretid = this->display + "\0" + stringify(na->time_registered); + Anope::string secretid = this->display + "\0" + Anope::ToString(this->time_registered); // Generate the account id. This should almost always only have one // iteration but in the rare case that we generate a duplicate id we try @@ -300,8 +222,8 @@ uint64_t NickCore::GetId() { // Generate a random key for SipHash. char key[16]; - for (size_t i = 0; i < sizeof(key); ++i) - key[i] = rand() % CHAR_MAX; + for (auto &chr : key) + chr = Anope::RandomNumber() % CHAR_MAX; uint64_t newid = Anope::SipHash24(secretid.c_str(), secretid.length(), key); nickcoreid_map::const_iterator it = NickCoreIdList.find(newid); |