1
0
mirror of https://github.com/anope/anope.git synced 2026-06-25 12:36:38 +02:00
Files
anope/src/nickcore.cpp
T
Adam d33a0f75a5 Pretty large coding style cleanup, in source doc
cleanup, and allow protocol mods to depend on each
other
2012-11-22 00:50:33 -05:00

272 lines
6.7 KiB
C++

/*
*
* (C) 2003-2012 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*/
#include "services.h"
#include "modules.h"
#include "account.h"
#include "config.h"
Serialize::Checker<nickcore_map> NickCoreList("NickCore");
static const Anope::string NickNameFlagStrings[] = {
"BEGIN", "NO_EXPIRE", "HELD", "COLLIDED", ""
};
template<> const Anope::string* Flags<NickNameFlag>::flags_strings = NickNameFlagStrings;
static const Anope::string NickCoreFlagStrings[] = {
"BEGIN", "KILLPROTECT", "SECURE", "MSG", "MEMO_HARDMAX", "MEMO_SIGNON", "MEMO_RECEIVE",
"PRIVATE", "HIDE_EMAIL", "HIDE_MASK", "HIDE_QUIT", "KILL_QUICK", "KILL_IMMED",
"MEMO_MAIL", "HIDE_STATUS", "SUSPENDED", "AUTOOP", "UNCONFIRMED", "STATS", ""
};
template<> const Anope::string* Flags<NickCoreFlag>::flags_strings = NickCoreFlagStrings;
NickCore::NickCore(const Anope::string &coredisplay) : Serializable("NickCore")
{
if (coredisplay.empty())
throw CoreException("Empty display passed to NickCore constructor");
this->o = NULL;
this->channelcount = 0;
this->lastmail = 0;
this->memos.memomax = Config->MSMaxMemos;
this->language = Config->NSDefLanguage;
this->display = coredisplay;
/* Set default nick core flags */
for (size_t t = NI_BEGIN + 1; t != NI_END; ++t)
if (Config->NSDefFlags.HasFlag(static_cast<NickCoreFlag>(t)))
this->SetFlag(static_cast<NickCoreFlag>(t));
size_t old = NickCoreList->size();
(*NickCoreList)[this->display] = this;
if (old == NickCoreList->size())
Log(LOG_DEBUG) << "Duplicate account " << coredisplay << " in nickcore table?";
}
NickCore::~NickCore()
{
FOREACH_MOD(I_OnDelCore, OnDelCore(this));
for (std::list<User *>::iterator it = this->users.begin(); it != this->users.end();)
{
User *user = *it++;
user->Logout();
}
this->users.clear();
/* Remove the core from the list */
NickCoreList->erase(this->display);
/* Clear access before deleting display name, we want to be able to use the display name in the clear access event */
this->ClearAccess();
if (!this->memos.memos->empty())
{
for (unsigned i = 0, end = this->memos.memos->size(); i < end; ++i)
this->memos.GetMemo(i)->Destroy();
this->memos.memos->clear();
}
}
Serialize::Data NickCore::Serialize() const
{
Serialize::Data data;
data["display"].SetMax(Config->NickLen) << this->display;
data["pass"] << this->pass;
data["email"] << this->email;
data["greet"] << this->greet;
data["language"] << this->language;
data["flags"] << this->ToString();
for (unsigned i = 0; i < this->access.size(); ++i)
data["access"] << this->access[i] << " ";
for (unsigned i = 0; i < this->cert.size(); ++i)
data["cert"] << this->cert[i] << " ";
data["memomax"] << this->memos.memomax;
for (unsigned i = 0; i < this->memos.ignores.size(); ++i)
data["memoignores"] << this->memos.ignores[i] << " ";
return data;
}
Serializable* NickCore::Unserialize(Serializable *obj, Serialize::Data &data)
{
NickCore *nc;
if (obj)
nc = anope_dynamic_static_cast<NickCore *>(obj);
else
nc = new NickCore(data["display"].astr());
data["pass"] >> nc->pass;
data["email"] >> nc->email;
data["greet"] >> nc->greet;
data["language"] >> nc->language;
nc->FromString(data["flags"].astr());
{
Anope::string buf;
data["access"] >> buf;
spacesepstream sep(buf);
nc->access.clear();
while (sep.GetToken(buf))
nc->access.push_back(buf);
}
{
Anope::string buf;
data["cert"] >> buf;
spacesepstream sep(buf);
nc->cert.clear();
while (sep.GetToken(buf))
nc->cert.push_back(buf);
}
data["memomax"] >> nc->memos.memomax;
{
Anope::string buf;
data["memoignores"] >> buf;
spacesepstream sep(buf);
nc->memos.ignores.clear();
while (sep.GetToken(buf))
nc->memos.ignores.push_back(buf);
}
return nc;
}
void NickCore::SetDisplay(const NickAlias *na)
{
if (na->nc != this || na->nick == this->display)
return;
FOREACH_MOD(I_OnChangeCoreDisplay, OnChangeCoreDisplay(this, na->nick));
/* Remove the core from the list */
NickCoreList->erase(this->display);
this->display = na->nick;
(*NickCoreList)[this->display] = this;
}
bool NickCore::IsServicesOper() const
{
return this->o != NULL;
}
void NickCore::AddAccess(const Anope::string &entry)
{
this->access.push_back(entry);
FOREACH_MOD(I_OnNickAddAccess, OnNickAddAccess(this, entry));
}
Anope::string NickCore::GetAccess(unsigned entry) const
{
if (this->access.empty() || entry >= this->access.size())
return "";
return this->access[entry];
}
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(I_OnNickEraseAccess, OnNickEraseAccess(this, entry));
this->access.erase(this->access.begin() + i);
break;
}
}
void NickCore::ClearAccess()
{
FOREACH_MOD(I_OnNickClearAccess, 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::AddCert(const Anope::string &entry)
{
this->cert.push_back(entry);
FOREACH_MOD(I_OnNickAddCert, OnNickAddCert(this, entry));
}
Anope::string NickCore::GetCert(unsigned entry) const
{
if (this->cert.empty() || entry >= this->cert.size())
return "";
return this->cert[entry];
}
bool NickCore::FindCert(const Anope::string &entry) const
{
for (unsigned i = 0, end = this->cert.size(); i < end; ++i)
if (this->cert[i] == entry)
return true;
return false;
}
void NickCore::EraseCert(const Anope::string &entry)
{
for (unsigned i = 0, end = this->cert.size(); i < end; ++i)
if (this->cert[i] == entry)
{
FOREACH_MOD(I_OnNickEraseCert, OnNickEraseCert(this, entry));
this->cert.erase(this->cert.begin() + i);
break;
}
}
void NickCore::ClearCert()
{
FOREACH_MOD(I_OnNickClearCert, OnNickClearCert(this));
this->cert.clear();
}
NickCore* NickCore::Find(const Anope::string &nick)
{
nickcore_map::const_iterator it = NickCoreList->find(nick);
if (it != NickCoreList->end())
{
it->second->QueueUpdate();
return it->second;
}
return NULL;
}