1
0
mirror of https://github.com/anope/anope.git synced 2026-06-25 17:46:37 +02:00
Files
anope/modules/extra/db_mysql_live.cpp
T

252 lines
6.8 KiB
C++

#include "module.h"
#include "async_commands.h"
#include "sql.h"
class MySQLLiveModule : public Module
{
service_reference<SQLProvider> SQL;
service_reference<AsynchCommandsService> ACS;
SQLResult RunQuery(const Anope::string &query)
{
if (!this->SQL)
throw SQLException("Unable to locate SQL reference, is m_mysql loaded and configured correctly?");
return SQL->RunQuery(query);
}
const Anope::string Escape(const Anope::string &query)
{
return SQL ? SQL->Escape(query) : query;
}
CommandMutex *CurrentCommand()
{
if (this->ACS)
return this->ACS->CurrentCommand();
return NULL;
}
public:
MySQLLiveModule(const Anope::string &modname, const Anope::string &creator) :
Module(modname, creator), SQL("mysql/main"), ACS("asynch_commands")
{
Implementation i[] = { I_OnFindBot, I_OnFindChan, I_OnFindNick, I_OnFindCore };
ModuleManager::Attach(i, this, 4);
}
void OnFindBot(const Anope::string &nick)
{
static bool lookup = true;
if (lookup == false)
{
lookup = true;
return;
}
try
{
CommandMutex *current_command = this->CurrentCommand();
if (current_command)
current_command->Unlock();
SQLResult res = this->RunQuery("SELECT * FROM `anope_bs_core` WHERE `nick` = '" + this->Escape(nick) + "'");
if (current_command)
current_command->Lock();
lookup = false;
BotInfo *bi = findbot(res.Get(0, "nick"));
if (!bi)
bi = new BotInfo(res.Get(0, "nick"), res.Get(0, "user"), res.Get(0, "host"), res.Get(0, "rname"));
else
{
bi->SetIdent(res.Get(0, "user"));
bi->host = res.Get(0, "host");
bi->realname = res.Get(0, "rname");
}
if (res.Get(0, "flags").equals_cs("PRIVATE"))
bi->SetFlag(BI_PRIVATE);
bi->created = convertTo<time_t>(res.Get(0, "created"));
bi->chancount = convertTo<uint32>(res.Get(0, "chancount"));
}
catch (const SQLException &) { }
catch (const ConvertException &) { }
}
void OnFindChan(const Anope::string &chname)
{
static bool lookup = true;
if (lookup == false)
{
lookup = true;
return;
}
try
{
CommandMutex *current_command = this->CurrentCommand();
if (current_command)
current_command->Unlock();
SQLResult res = this->RunQuery("SELECT * FROM `anope_cs_info` WHERE `name` = '" + this->Escape(chname) + "'");
if (current_command)
current_command->Lock();
lookup = false;
ChannelInfo *ci = cs_findchan(res.Get(0, "name"));
if (!ci)
ci = new ChannelInfo(res.Get(0, "name"));
ci->founder = findcore(res.Get(0, "founder"));
ci->successor = findcore(res.Get(0, "successor"));
ci->desc = res.Get(0, "descr");
ci->time_registered = convertTo<time_t>(res.Get(0, "time_registered"));
ci->ClearFlags();
ci->FromString(BuildStringVector(res.Get(0, "flags")));
ci->forbidby = res.Get(0, "forbidby");
ci->forbidreason = res.Get(0, "forbidreason");
ci->bantype = convertTo<int>(res.Get(0, "bantype"));
ci->memos.memomax = convertTo<unsigned>(res.Get(0, "memomax"));
Anope::string mlock_on = res.Get(0, "mlock_on"),
mlock_off = res.Get(0, "mlock_off"),
mlock_params = res.Get(0, "mlock_params"),
mlock_params_off = res.Get(0, "mlock_params_off");
Anope::string mode;
std::vector<Anope::string> modes;
spacesepstream sep(mlock_on);
while (sep.GetToken(mode))
modes.push_back(mode);
ci->Extend("db_mlock_modes_on", new ExtensibleItemRegular<std::vector<Anope::string> >(modes));
modes.clear();
sep = mlock_off;
while (sep.GetToken(mode))
modes.push_back(mode);
ci->Extend("db_mlock_modes_off", new ExtensibleItemRegular<std::vector<Anope::string> >(modes));
modes.clear();
sep = mlock_params;
while (sep.GetToken(mode))
modes.push_back(mode);
ci->Extend("mlock_params", new ExtensibleItemRegular<std::vector<Anope::string> >(modes));
modes.clear();
sep = mlock_params_off;
while (sep.GetToken(mode))
modes.push_back(mode);
ci->Extend("mlock_params_off", new ExtensibleItemRegular<std::vector<Anope::string> >(modes));
ci->LoadMLock();
if (res.Get(0, "botnick").equals_cs(ci->bi ? ci->bi->nick : "") == false)
{
if (ci->bi)
ci->bi->UnAssign(NULL, ci);
BotInfo *bi = findbot(res.Get(0, "botnick"));
if (bi)
bi->Assign(NULL, ci);
}
ci->capsmin = convertTo<int16>(res.Get(0, "capsmin"));
ci->capspercent = convertTo<int16>(res.Get(0, "capspercent"));
ci->floodlines = convertTo<int16>(res.Get(0, "floodlines"));
ci->floodsecs = convertTo<int16>(res.Get(0, "floodsecs"));
ci->repeattimes = convertTo<int16>(res.Get(0, "repeattimes"));
if (ci->c)
check_modes(ci->c);
}
catch (const SQLException &) { }
catch (const ConvertException &) { }
}
void OnFindNick(const Anope::string &nick)
{
static bool lookup = true;
if (lookup == false)
{
lookup = true;
return;
}
try
{
CommandMutex *current_command = this->CurrentCommand();
if (current_command)
current_command->Unlock();
SQLResult res = this->RunQuery("SELECT * FROM `anope_ns_alias` WHERE `nick` = '" + this->Escape(nick) + "'");
if (current_command)
current_command->Lock();
NickCore *nc = findcore(res.Get(0, "display"));
if (!nc)
return;
lookup = false;
NickAlias *na = findnick(res.Get(0, "nick"));
if (!na)
na = new NickAlias(res.Get(0, "nick"), nc);
na->last_quit = res.Get(0, "last_quit");
na->last_realname = res.Get(0, "last_realname");
na->last_usermask = res.Get(0, "last_usermask");
na->time_registered = convertTo<time_t>(res.Get(0, "time_registered"));
na->last_seen = convertTo<time_t>(res.Get(0, "last_seen"));
na->ClearFlags();
na->FromString(BuildStringVector(res.Get(0, "flags")));
if (na->nc != nc)
{
std::list<NickAlias *>::iterator it = std::find(na->nc->aliases.begin(), na->nc->aliases.end(), na);
if (it != na->nc->aliases.end())
na->nc->aliases.erase(it);
na->nc = nc;
na->nc->aliases.push_back(na);
}
}
catch (const SQLException &) { }
catch (const ConvertException &) { }
}
void OnFindCore(const Anope::string &nick)
{
static bool lookup = true;
if (lookup == false)
{
lookup = true;
return;
}
try
{
CommandMutex *current_command = this->CurrentCommand();
if (current_command)
current_command->Unlock();
SQLResult res = this->RunQuery("SELECT * FROM `anope_ns_core` WHERE `name` = '" + this->Escape(nick) + "'");
if (current_command)
current_command->Lock();
lookup = false;
NickCore *nc = findcore(res.Get(0, "display"));
if (!nc)
nc = new NickCore(res.Get(0, "display"));
nc->pass = res.Get(0, "pass");
nc->email = res.Get(0, "email");
nc->greet = res.Get(0, "greet");
nc->ClearFlags();
nc->FromString(BuildStringVector(res.Get(0, "flags")));
nc->language = res.Get(0, "language");
}
catch (const SQLException &) { }
catch (const ConvertException &) { }
}
};
MODULE_INIT(MySQLLiveModule)