#include "module.h"
#include "../extra/sql.h"
#include "../commands/os_session.h"
static Anope::string ToString(const std::vector<Anope::string> &strings)
{
Anope::string ret;
for (unsigned i = 0; i < strings.size(); ++i)
ret += " " + strings[i];
if (!ret.empty())
ret.erase(ret.begin());
return ret;
}
static std::vector<Anope::string> MakeVector(const Anope::string &buf)
{
Anope::string s;
spacesepstream sep(buf);
std::vector<Anope::string> params;
while (sep.GetToken(s))
{
if (s[0] == ':')
{
s.erase(s.begin());
if (!s.empty() && !sep.StreamEnd())
params.push_back(s + " " + sep.GetRemaining());
else if (!s.empty())
params.push_back(s);
}
else
params.push_back(s);
}
return params;
}
static NickAlias *CurNick = NULL;
static NickCore *CurCore = NULL;
static ChannelInfo *CurChannel = NULL;
static BotInfo *CurBot = NULL;
static void Write(const Anope::string &data);
static void WriteNickMetadata(const Anope::string &key, const Anope::string &data);
static void WriteCoreMetadata(const Anope::string &key, const Anope::string &data);
static void WriteChannelMetadata(const Anope::string &key, const Anope::string &data);
static void WriteBotMetadata(const Anope::string &key, const Anope::string &data);
class CommandSQLSync : public Command
{
public:
CommandSQLSync(Module *creator) : Command(creator, "operserv/sqlsync", 0, 0)
{
this->SetDesc(_("Import your databases to SQL"));
this->SetSyntax("");
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms);
bool OnHelp(CommandSource &source, const Anope::string &subcommand)
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("This command syncs your databases with SQL. You should\n"
"only have to execute this command once, when you initially\n"
"import your databases into SQL."));
return true;
}
};
class MySQLInterface : public SQLInterface
{
public:
MySQLInterface(Module *o) : SQLInterface(o) { }
void OnResult(const SQLResult &r);
void OnError(const SQLResult &r);
};
class DBMySQL;
static DBMySQL *me;
class DBMySQL : public Module
{
private:
CommandSQLSync commandsqlsync;
MySQLInterface sqlinterface;
service_reference<SQLProvider> SQL;
public:
service_reference<SessionService> SessionInterface;
time_t lastwarn;
bool ro;
void RunQuery(const SQLQuery &query)
{
if (SQL)
{
if (readonly && this->ro)
{
readonly = this->ro = false;
BotInfo *bi = findbot(Config->OperServ);
if (bi)
ircdproto->SendGlobops(bi, "Found SQL again, going out of readonly mode...");
}
SQL->Run(&sqlinterface, query);
}
else
{
if (Anope::CurTime - Config->UpdateTimeout > lastwarn)
{
BotInfo *bi = findbot(Config->OperServ);
if (bi)
ircdproto->SendGlobops(bi, "Unable to locate SQL reference, is m_mysql loaded? Going to readonly...");
readonly = this->ro = true;
this->lastwarn = Anope::CurTime;
}
}
}
DBMySQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE), commandsqlsync(this), sqlinterface(this), SQL("mysql/main"), SessionInterface("session")
{
me = this;
this->lastwarn = 0;
this->ro = false;
Implementation i[] = {
I_OnLoadDatabase, I_OnServerConnect
};
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
ModuleManager::RegisterService(&commandsqlsync);
if (CurrentUplink)
OnServerConnect();
}
void OnServerConnect()
{
Implementation i[] = {
/* Misc */
I_OnSaveDatabase, I_OnPostCommand,
/* NickServ */
I_OnNickAddAccess, I_OnNickEraseAccess, I_OnNickClearAccess,
I_OnDelCore, I_OnNickForbidden, I_OnNickGroup,
I_OnNickRegister, I_OnChangeCoreDisplay,
I_OnNickSuspended, I_OnDelNick,
/* ChanServ */
I_OnAccessAdd, I_OnAccessDel, I_OnAccessClear, I_OnLevelChange,
I_OnChanForbidden, I_OnDelChan, I_OnChanRegistered, I_OnChanSuspend,
I_OnAkickAdd, I_OnAkickDel, I_OnMLock, I_OnUnMLock,
/* BotServ */
I_OnBotCreate, I_OnBotChange, I_OnBotDelete,
I_OnBotAssign, I_OnBotUnAssign,
I_OnBadWordAdd, I_OnBadWordDel,
/* MemoServ */
I_OnMemoSend, I_OnMemoDel,
/* OperServ */
I_OnExceptionAdd, I_OnExceptionDel,
I_OnAddXLine, I_OnDelXLine,
/* HostServ */
I_OnSetVhost, I_OnDeleteVhost
};
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
}
EventReturn OnLoadDatabase()
{
if (!SQL)
{
Log() << "Error, unable to find service reference for SQL, is m_mysql loaded and configured properly?";
return EVENT_CONTINUE;
}
SQLQuery query;
query = "SELECT * FROM `anope_ns_core`";
SQLResult r = SQL->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
NickCore *nc = new NickCore(r.Get(i, "display"));
nc->pass = r.Get(i, "pass");
nc->email = r.Get(i, "email");
nc->greet = r.Get(i, "greet");
spacesepstream sep(r.Get(i, "flags"));
Anope::string buf;
std::vector<Anope::string> flags;
while (sep.GetToken(buf))
flags.push_back(buf);
nc->FromString(flags);
nc->language = r.Get(i, "language");
nc->memos.memomax = r.Get(i, "memomax").is_number_only() ? convertTo<int16>(r.Get(i, "memomax")) : 20;
}
query = "SELECT * FROM `anope_ns_access`";
r = SQL->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
NickCore *nc = findcore(r.Get(i, "display"));
if (!nc)
{
Log() << "MySQL: Got NickCore access entry for nonexistant core " << r.Get(i, "display");
continue;
}
nc->AddAccess(r.Get(i, "access"));
}
query = "SELECT * FROM `anope_ns_core_metadata`";
r = SQL->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
NickCore *nc = findcore(r.Get(i, "display"));
if (!nc)
{
Log() << "MySQL: Got NickCore access entry for nonexistant core " << r.Get(i, "display");
continue;
}
EventReturn MOD_RESULT;;
std::vector<Anope::string> Params = MakeVector(r.Get(i, "value"));
FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(nc, r.Get(i, "name"), Params));
}
query = "SELECT * FROM `anope_ns_alias`";
r = SQL->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
NickCore *nc = findcore(r.Get(i, "display"));
if (!nc)
{
Log() << "MySQL: Got NickAlias for nick " << r.Get(i, "nick") << " with nonexistant core " << r.Get(i, "display");
continue;
}
NickAlias *na = new NickAlias(r.Get(i, "nick"), nc);
na->last_quit = r.Get(i, "last_quit");
na->last_realname = r.Get(i, "last_realname");
na->last_usermask = r.Get(i, "last_usermask");
na->last_realhost = r.Get(i, "last_realhost");
na->time_registered = r.Get(i, "time_registered").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "time_registered")) : Anope::CurTime;
na->last_seen = r.Get(i, "last_seen").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "last_seen")) : Anope::CurTime;
spacesepstream sep(r.Get(i, "flags"));
Anope::string buf;
std::vector<Anope::string> flag
|