summaryrefslogtreecommitdiff
path: root/modules/database
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2011-09-25 04:19:15 -0400
committerAdam <Adam@anope.org>2011-09-25 04:19:15 -0400
commit1f2399de364c09adcce4193895cd362d80ffdfc5 (patch)
tree5f40fc531f22c174b6e10bb7bc12842a4a21d30b /modules/database
parent43201ead9575a74e350710bc191f4ac67366aca7 (diff)
Added a new database format and sqlite support. Also moved db-convert to a module.
Diffstat (limited to 'modules/database')
-rw-r--r--modules/database/db_flatfile.cpp191
-rw-r--r--modules/database/db_mysql.cpp1568
-rw-r--r--modules/database/db_old.cpp1021
-rw-r--r--modules/database/db_plain.cpp706
-rw-r--r--modules/database/db_sql.cpp169
-rw-r--r--modules/database/db_sql_live_read.cpp434
-rw-r--r--modules/database/db_sql_live_write.cpp (renamed from modules/database/db_mysql_live.cpp)73
7 files changed, 2197 insertions, 1965 deletions
diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp
new file mode 100644
index 000000000..f17ed6fd1
--- /dev/null
+++ b/modules/database/db_flatfile.cpp
@@ -0,0 +1,191 @@
+/*
+ * (C) 2003-2011 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 "module.h"
+
+Anope::string DatabaseFile;
+
+class DBFlatFile : public Module
+{
+ /* Day the last backup was on */
+ int LastDay;
+ /* Backup file names */
+ std::list<Anope::string> Backups;
+
+ SerializableBase *find(const Anope::string &sname)
+ {
+ for (unsigned i = 0; i < serialized_types.size(); ++i)
+ if (serialized_types[i]->serialize_name() == sname)
+ return serialized_types[i];
+ return NULL;
+ }
+
+ public:
+ DBFlatFile(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE)
+ {
+ this->SetAuthor("Anope");
+
+ Implementation i[] = { I_OnReload, I_OnLoadDatabase, I_OnSaveDatabase };
+ ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
+
+ OnReload();
+
+ LastDay = 0;
+ }
+
+ void BackupDatabase()
+ {
+ /* Do not backup a database that doesn't exist */
+ if (!IsFile(DatabaseFile))
+ return;
+
+ time_t now = Anope::CurTime;
+ tm *tm = localtime(&now);
+
+ if (tm->tm_mday != LastDay)
+ {
+ LastDay = tm->tm_mday;
+ Anope::string newname = "backups/" + DatabaseFile + "." + stringify(tm->tm_year) + stringify(tm->tm_mon) + stringify(tm->tm_mday);
+
+ /* Backup already exists */
+ if (IsFile(newname))
+ return;
+
+ Log(LOG_DEBUG) << "db_flatfile: Attemping to rename " << DatabaseFile << " to " << newname;
+ if (rename(DatabaseFile.c_str(), newname.c_str()))
+ {
+ Log() << "Unable to back up database!";
+
+ if (!Config->NoBackupOkay)
+ quitting = true;
+
+ return;
+ }
+
+ Backups.push_back(newname);
+
+ if (Config->KeepBackups > 0 && Backups.size() > static_cast<unsigned>(Config->KeepBackups))
+ {
+ DeleteFile(Backups.front().c_str());
+ Backups.pop_front();
+ }
+ }
+ }
+
+ void OnReload()
+ {
+ ConfigReader config;
+ DatabaseFile = config.ReadValue("flatfile", "database", "anope.db", 0);
+ }
+
+ EventReturn OnLoadDatabase()
+ {
+ std::fstream db;
+ db.open(DatabaseFile.c_str(), std::ios_base::in);
+
+ if (!db.is_open())
+ {
+ Log() << "Unable to open " << DatabaseFile << " for reading!";
+ return EVENT_CONTINUE;
+ }
+
+ SerializableBase *sb = NULL;
+ SerializableBase::serialized_data data;
+ std::multimap<SerializableBase *, SerializableBase::serialized_data> objects;
+ for (Anope::string buf, token; std::getline(db, buf.str());)
+ {
+ spacesepstream sep(buf);
+
+ if (!sep.GetToken(token))
+ continue;
+
+ if (token == "OBJECT" && sep.GetToken(token))
+ {
+ sb = this->find(token);
+ data.clear();
+ }
+ else if (token == "DATA" && sb != NULL && sep.GetToken(token))
+ data[token] << sep.GetRemaining();
+ else if (token == "END" && sb != NULL)
+ {
+ objects.insert(std::make_pair(sb, data));
+
+ sb = NULL;
+ data.clear();
+ }
+ }
+
+ for (unsigned i = 0; i < serialized_types.size(); ++i)
+ {
+ SerializableBase *stype = serialized_types[i];
+
+ std::multimap<SerializableBase *, SerializableBase::serialized_data>::iterator it = objects.find(stype), it_end = objects.upper_bound(stype);
+ if (it == objects.end())
+ continue;
+ for (; it != it_end; ++it)
+ it->first->alloc(it->second);
+ }
+
+ db.close();
+
+ return EVENT_STOP;
+ }
+
+
+ EventReturn OnSaveDatabase()
+ {
+ BackupDatabase();
+
+ Anope::string tmp_db = DatabaseFile + ".tmp";
+ if (IsFile(DatabaseFile))
+ rename(DatabaseFile.c_str(), tmp_db.c_str());
+
+ std::fstream db(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc);
+ if (!db.is_open())
+ {
+ Log() << "Unable to open " << DatabaseFile << " for writing";
+ if (IsFile(tmp_db))
+ rename(tmp_db.c_str(), DatabaseFile.c_str());
+ return EVENT_CONTINUE;
+ }
+
+ for (std::list<SerializableBase *>::iterator it = serialized_items.begin(), it_end = serialized_items.end(); it != it_end; ++it)
+ {
+ SerializableBase *base = *it;
+ SerializableBase::serialized_data data = base->serialize();
+
+ db << "OBJECT " << base->serialize_name() << "\n";
+ for (SerializableBase::serialized_data::iterator dit = data.begin(), dit_end = data.end(); dit != dit_end; ++dit)
+ db << "DATA " << dit->first << " " << dit->second.astr() << "\n";
+ db << "END\n";
+ }
+
+ db.close();
+
+ if (db.good() == false)
+ {
+ Log() << "Unable to write database";
+ if (!Config->NoBackupOkay)
+ quitting = true;
+ if (IsFile(tmp_db))
+ rename(tmp_db.c_str(), DatabaseFile.c_str());
+ }
+ else
+ DeleteFile(tmp_db.c_str());
+
+ return EVENT_CONTINUE;
+ }
+};
+
+MODULE_INIT(DBFlatFile)
+
+
diff --git a/modules/database/db_mysql.cpp b/modules/database/db_mysql.cpp
deleted file mode 100644
index e03c89d16..000000000
--- a/modules/database/db_mysql.cpp
+++ /dev/null
@@ -1,1568 +0,0 @@
-#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> &params);
-
- 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, Base> SQL;
-
- public:
- service_reference<SessionService, Base> 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));
-
- 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> flags;
- while (sep.GetToken(buf))
- flags.push_back(buf);
-
- na->FromString(flags);
- }
-
- query = "SELECT * FROM `anope_ns_alias_metadata`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- NickAlias *na = findnick(r.Get(i, "nick"));
- if (!na)
- {
- Log() << "MySQL: Got metadata for nonexistant nick " << r.Get(i, "nick");
- continue;
- }
-
- EventReturn MOD_RESULT;
- std::vector<Anope::string> Params = MakeVector(r.Get(i, "value"));
- FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(na, r.Get(i, "name"), Params));
- }
-
- query = "SELECT * FROM `anope_hs_core`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- NickAlias *na = findnick(r.Get(i, "nick"));
- if (!na)
- {
- Log() << "MySQL: Got vhost entry for nonexistant nick " << r.Get(i, "nick");
- continue;
- }
-
- time_t creation = r.Get(i, "time").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "time")) : Anope::CurTime;
- na->hostinfo.SetVhost(r.Get(i, "vident"), r.Get(i, "vhost"), r.Get(i, "creator"), creation);
- }
-
- query = "SELECT * FROM `anope_bs_core`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- BotInfo *bi = findbot(r.Get(i, "nick"));
- if (!bi)
- bi = new BotInfo(r.Get(i, "nick"), r.Get(i, "user"), r.Get(i, "host"));
- bi->realname = r.Get(i, "rname");
- bi->created = r.Get(i, "created").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "created")) : Anope::CurTime;
-
- spacesepstream sep(r.Get(i, "flags"));
- Anope::string buf;
- std::vector<Anope::string> flags;
- while (sep.GetToken(buf))
- flags.push_back(buf);
-
- bi->FromString(flags);
- }
-
- query = "SELECT * FROM `anope_bs_info_metadata`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- BotInfo *bi = findbot(r.Get(i, "botname"));
- if (!bi)
- {
- Log() << "MySQL: BotInfo metadata for nonexistant bot " << r.Get(i, "botname");
- continue;
- }
-
- EventReturn MOD_RESULT;
- std::vector<Anope::string> Params = MakeVector(r.Get(i, "value"));
- FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(bi, r.Get(i, "name"), Params));
- }
-
- query = "SELECT * FROM `anope_cs_info`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- NickCore *nc = NULL;
-
- if (!r.Get(i, "founder").empty())
- {
- nc = findcore(r.Get(i, "founder"));
- if (!nc)
- {
- Log() << "MySQL: Channel " << r.Get(i, "name") << " with nonexistant founder " << r.Get(i, "founder");
- continue;
- }
-
- ChannelInfo *ci = new ChannelInfo(r.Get(i, "name"));
- ci->SetFounder(nc);
- if (!r.Get(i, "successor").empty())
- ci->successor = findcore(r.Get(i, "successor"));
- ci->desc = r.Get(i, "descr");
- ci->time_registered = r.Get(i, "time_registered").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "time_registered")) : Anope::CurTime;
- ci->last_used = r.Get(i, "last_used").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "last_used")) : Anope::CurTime;
- ci->last_topic = r.Get(i, "last_topic");
- ci->last_topic_setter = r.Get(i, "last_topic_setter");
- ci->last_topic_time = r.Get(i, "last_topic_time").is_number_only() ? convertTo<int>(r.Get(i, "last_topic_time")) : Anope::CurTime;
- ci->bantype = r.Get(i, "bantype").is_number_only() ? convertTo<int>(r.Get(i, "bantype")) : 2;
- ci->memos.memomax = r.Get(i, "memomax").is_number_only() ? convertTo<int16>(r.Get(i, "memomax")) : 20;
- ci->capsmin = r.Get(i, "capsmin").is_number_only() ? convertTo<int>(r.Get(i, "capsmin")) : 0;
- ci->capspercent = r.Get(i, "capspercent").is_number_only() ? convertTo<int>(r.Get(i, "capspercent")) : 0;
- ci->floodlines = r.Get(i, "floodlines").is_number_only() ? convertTo<int>(r.Get(i, "floodlines")) : 0;
- ci->floodsecs = r.Get(i, "floodsecs").is_number_only() ? convertTo<int>(r.Get(i, "floodsecs")) : 0;
- ci->repeattimes = r.Get(i, "repeattimes").is_number_only() ? convertTo<int>(r.Get(i, "repeattimes")) : 0;
- ci->bi = findbot(r.Get(i, "botnick"));
- if (ci->bi && !r.Get(i, "botflags").empty())
- {
- spacesepstream sep(r.Get(i, "botflags"));
- Anope::string buf;
- std::vector<Anope::string> flags;
- while (sep.GetToken(buf))
- flags.push_back(buf);
-
- ci->botflags.FromString(flags);
- }
-
- if (!r.Get(i, "flags").empty())
- {
- spacesepstream sep(r.Get(i, "flags"));
- Anope::string buf;
- std::vector<Anope::string> flags;
- while (sep.GetToken(buf))
- flags.push_back(buf);
-
- ci->FromString(flags);
- }
- }
- }
-
- query = "SELECT * FROM `anope_cs_ttb`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel ttb for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- ci->ttb[atoi(r.Get(i, "ttb_id").c_str())] = atoi(r.Get(i, "value").c_str());
- }
-
- query = "SELECT * FROM `anope_bs_badwords`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel badwords entry for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- BadWordType BWTYPE = BW_ANY;
- if (r.Get(i, "type").equals_cs("SINGLE"))
- BWTYPE = BW_SINGLE;
- else if (r.Get(i, "type").equals_cs("START"))
- BWTYPE = BW_START;
- else if (r.Get(i, "type").equals_cs("END"))
- BWTYPE = BW_END;
- ci->AddBadWord(r.Get(i, "word"), BWTYPE);
- }
-
- query = "SELECT * FROM `anope_cs_access`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel access entry for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- const Anope::string &provider = r.Get(i, "provider"), &data = r.Get(i, "data");
- service_reference<AccessProvider> ap(provider);
- if (!ap)
- {
- Log() << "MySQL: Access entry for " << ci->name << " using nonexistant provider " << provider;
- continue;
- }
-
- ChanAccess *access = ap->Create();
- access->ci = ci;
- access->mask = r.Get(i, "mask");
- access->creator = r.Get(i, "creator");
- access->last_seen = r.Get(i, "last_seen").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "last_seen")) : Anope::CurTime;
- access->created = r.Get(i, "created").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "created")) : Anope::CurTime;
- access->Unserialize(data);
- ci->AddAccess(access);
- }
-
- query = "SELECT * FROM `anope_cs_akick`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel access entry for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- NickCore *nc = NULL;
- spacesepstream sep(r.Get(i, "flags"));
- Anope::string flag, mask;
- while (sep.GetToken(flag))
- {
- if (flag.equals_cs("ISNICK"))
- nc = findcore(r.Get(i, "mask"));
-
- AutoKick *ak;
- if (nc)
- ak = ci->AddAkick(r.Get(i, "creator"), nc, r.Get(i, "reason"), atol(r.Get(i, "created").c_str()), atol(r.Get(i, "last_used").c_str()));
- else
- ak = ci->AddAkick(r.Get(i, "creator"), r.Get(i, "mask"), r.Get(i, "reason"), atol(r.Get(i, "created").c_str()), atol(r.Get(i, "last_used").c_str()));
- if (nc)
- ak->SetFlag(AK_ISNICK);
- }
- }
-
- query = "SELECT * FROM `anope_cs_levels`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel level entry for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- ci->SetLevel(r.Get(i, "name"), atoi(r.Get(i, "level").c_str()));
- }
-
- query = "SELECT * FROM `anope_cs_info_metadata`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel metadata for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- EventReturn MOD_RESULT;
- std::vector<Anope::string> Params = MakeVector(r.Get(i, "value"));
- FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(ci, r.Get(i, "name"), Params));
- }
-
- query = "SELECT * FROM `anope_cs_mlock`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- {
- Log() << "MySQL: Channel mlock for nonexistant channel " << r.Get(i, "channel");
- continue;
- }
-
- Anope::string mode_name = r.Get(i, "mode");
- bool set = r.Get(i, "status") == "1" ? true : false;
- Anope::string setter = r.Get(i, "setter");
- time_t mcreated = r.Get(i, "created").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "created")) : Anope::CurTime;
- Anope::string param = r.Get(i, "param");
-
- for (size_t j = CMODE_BEGIN + 1; j < CMODE_END; ++j)
- if (ChannelModeNameStrings[j] == mode_name)
- {
- ChannelModeName n = static_cast<ChannelModeName>(j);
- ci->mode_locks.insert(std::make_pair(n, ModeLock(set, n, param, setter, mcreated)));
- break;
- }
- }
-
- query = "SELECT * FROM `anope_ms_info`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- MemoInfo *mi = NULL;
- if (r.Get(i, "serv").equals_cs("NICK"))
- {
- NickCore *nc = findcore(r.Get(i, "receiver"));
- if (nc)
- mi = &nc->memos;
- }
- else if (r.Get(i, "serv").equals_cs("CHAN"))
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "receiver"));
- if (ci)
- mi = &ci->memos;
- }
- if (mi)
- {
- Memo *m = new Memo();
- mi->memos.push_back(m);
- m->sender = r.Get(i, "sender");
- m->time = r.Get(i, "time").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "time")) : Anope::CurTime;
- m->text = r.Get(i, "text");
-
- if (!r.Get(i, "flags").empty())
- {
- spacesepstream sep(r.Get(i, "flags"));
- Anope::string buf;
- std::vector<Anope::string> flags;
- while (sep.GetToken(buf))
- flags.push_back(buf);
-
- m->FromString(flags);
- }
- }
- }
-
- for (std::list<XLineManager *>::iterator it = XLineManager::XLineManagers.begin(), it_end = XLineManager::XLineManagers.end(); it != it_end; ++it)
- {
- XLineManager *xm = *it;
-
- query = "SELECT * FROM `anope_os_xlines` WHERE `type` = @type";
- query.setValue("type", Anope::string(xm->Type()));
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- Anope::string user = r.Get(i, "user");
- Anope::string host = r.Get(i, "host");
- Anope::string by = r.Get(i, "xby");
- Anope::string reason = r.Get(i, "reason");
- time_t seton = r.Get(i, "seton").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "seton")) : Anope::CurTime;
- time_t expires = r.Get(i, "expires").is_pos_number_only() ? convertTo<time_t>(r.Get(i, "expires")) : Anope::CurTime;
-
- XLine *x = xm->Add(user + "@" + host, by, expires, reason);
- if (x)
- x->Created = seton;
- }
- }
-
- query = "SELECT * FROM `anope_os_exceptions`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- Anope::string mask = r.Get(i, "mask");
- unsigned limit = convertTo<unsigned>(r.Get(i, "slimit"));
- Anope::string creator = r.Get(i, "who");
- Anope::string reason = r.Get(i, "reason");
- time_t expires = convertTo<time_t>(r.Get(i, "expires"));
-
- if (SessionInterface)
- {
- Exception *e = new Exception();
- e->mask = mask;
- e->limit = limit;
- e->who = creator;
- e->reason = reason;
- e->time = expires;
- SessionInterface->AddException(e);
- }
- }
-
- query = "SELECT * FROM `anope_extra`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- std::vector<Anope::string> params = MakeVector(r.Get(i, "data"));
- EventReturn MOD_RESULT;
- FOREACH_RESULT(I_OnDatabaseRead, OnDatabaseRead(params));
- }
-
- 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, "nick"));
- if (!nc)
- continue;
- if (r.Get(i, "name") == "MEMO_IGNORE")
- nc->memos.ignores.push_back(r.Get(i, "value").ci_str());
- }
-
- query = "SELECT * FROM `anope_cs_info_metadata`";
- r = SQL->RunQuery(query);
- for (int i = 0; i < r.Rows(); ++i)
- {
- ChannelInfo *ci = cs_findchan(r.Get(i, "channel"));
- if (!ci)
- continue;
- if (r.Get(i, "name") == "MEMO_IGNORE")
- ci->memos.ignores.push_back(r.Get(i, "value").ci_str());
- }
-
- return EVENT_STOP;
- }
-
- EventReturn OnSaveDatabase()
- {
- SQLQuery query;
-
- query = "TRUNCATE TABLE `anope_os_core`";
- this->RunQuery(query);
-
- query = "INSERT INTO `anope_os_core` (maxusercnt, maxusertime) VALUES(@maxusercnt, @maxusertime)";
- query.setValue("maxusercnt", maxusercnt);
- query.setValue("maxusertime", maxusertime);
- this->RunQuery(query);
-
- for (nickcore_map::const_iterator it = NickCoreList.begin(), it_end = NickCoreList.end(); it != it_end; ++it)
- {
- CurCore = it->second;
- FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteCoreMetadata, CurCore));
- }
-
- for (nickalias_map::const_iterator it = NickAliasList.begin(), it_end = NickAliasList.end(); it != it_end; ++it)
- {
- CurNick = it->second;
- FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteNickMetadata, CurNick));
- }
-
- for (registered_channel_map::const_iterator it = RegisteredChannelList.begin(), it_end = RegisteredChannelList.end(); it != it_end; ++it)
- {
- CurChannel = it->second;
- FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteChannelMetadata, CurChannel));
- }
-
- for (Anope::insensitive_map<BotInfo *>::const_iterator it = BotListByNick.begin(), it_end = BotListByNick.end(); it != it_end; ++it)
- {
- if (it->second->HasFlag(BI_CONF))
- continue;
-
- CurBot = it->second;
- FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteBotMetadata, CurBot));
-
- query = "INSERT INTO `anope_bs_core` (nick, user, host, rname, flags, created, chancount) VALUES(@nick, @user, @host, @rname, @flags, @created, @chancount) ON DUPLICATE KEY UPDATE nick=VALUES(nick), user=VALUES(user), host=VALUES(host), rname=VALUES(rname), flags=VALUES(flags), created=VALUES(created), chancount=VALUES(chancount)";
- query.setValue("nick", CurBot->nick);
- query.setValue("user", CurBot->GetIdent());
- query.setValue("host", CurBot->host);
- query.setValue("rname", CurBot->realname);
- query.setValue("flags", ToString(CurBot->ToString()));
- query.setValue("created", CurBot->created);
- query.setValue("chancount", CurBot->chancount);
- this->RunQuery(query);
- }
-
- query = "TRUNCATE TABLE `anope_extra`";
- this->RunQuery(query);
- FOREACH_MOD(I_OnDatabaseWrite, OnDatabaseWrite(Write));
-
- return EVENT_CONTINUE;
- }
-
- void OnPostCommand(CommandSource &source, Command *command, const std::vector<Anope::string> &params)
- {
- User *u = source.u;
-
- if (command->name.find("nickserv/set/") == 0 || command->name.find("nickserv/saset/") == 0)
- {
- NickAlias *na = findnick(command->name.find("nickserv/set/") == 0 ? source.u->nick : params[1]);
- if (!na)
- return;
-
- if (command->name == "nickserv/set/password" || command->name == "nickserv/saset/password")
- {
- SQLQuery query("UPDATE `anope_ns_core` SET `pass` = @pass WHERE `display` = @display");
- query.setValue("pass", na->nc->pass);
- query.setValue("display", na->nc->display);
- this->RunQuery(query);
- }
- else if (command->name == "nickserv/set/language" || command->name == "nickserv/saset/language")
- {
- SQLQuery query("UPDATE `anope_ns_core` SET `language` = @language WHERE `display` = @display");
- query.setValue("language", na->nc->language);
- query.setValue("display", na->nc->display);
- this->RunQuery(query);
- }
- else if (command->name == "nickserv/set/email" || command->name == "nickserv/saset/email")
- {
- SQLQuery query("UPDATE `anope_ns_core` SET `email` = @email WHERE `display` = @display");
- query.setValue("email", na->nc->email);
- query.setValue("display", na->nc->display);
- this->RunQuery(query);
- }
- else if (command->name == "nickserv/set/greet" || command->name == "nickserv/saset/greet")
- {
- SQLQuery query("UPDATE `anope_ns_core` SET `greet` = @greet WHERE `display` = @display");
- query.setValue("greet", na->nc->greet);
- query.setValue("display", na->nc->display);
- this->RunQuery(query);
- }
- else
- {
- SQLQuery query("UPDATE `anope_ns_core` SET `flags` = @flags WHERE `display` = @display");
- query.setValue("flags", ToString(na->nc->ToString()));
- query.setValue("display", na->nc->display);
- this->RunQuery(query);
- }
- }
- else if (command->name.find("chanserv/set") == 0 || command->name.find("chanserv/saset") == 0)
- {
- ChannelInfo *ci = params.size() > 0 ? cs_findchan(params[0]) : NULL;
- if (!ci)
- return;
-
- if (command->name == "chanserv/set/founder" || command->name == "chanserv/saset/founder")
- {
- SQLQuery query("UPDATE `anope_cs_info` SET `founder` = @founder WHERE `name` = @name");
- query.setValue("founder", ci->GetFounder() ? ci->GetFounder()->display : "");
- query.setValue("name", ci->name);
- this->RunQuery(query);
- }
- else if (command->name == "chanserv/set/successor" || command->name == "chanserv/saset/successor")
- {
- SQLQuery query("UPDATE `anope_cs_info` SET `successor` = @successor WHERE `name` = @name");
- query.setValue("successor", ci->successor ? ci->successor->display : "");
- query.setValue("name", ci->name);
- this->RunQuery(query);
- }
- else if (command->name == "chanserv/set/desc" || command->name == "chanserv/saset/desc")
- {
- SQLQuery query("UPDATE `anope_cs_info` SET `descr` = @descr WHERE `name` = @name");
- query.setValue("descr", ci->desc);
- query.setValue("name", ci->name);
- this->RunQuery(query);
- }
- else if (command->name == "chanserv/set/bantype" || command->name == "chanserv/saset/bantype")
- {
- SQLQuery query("UPDATE `anope_cs_info` SET `bantype` = @bantype WHERE `name` = @name");
- query.setValue("bantype", ci->bantype);
- query.setValue("name", ci->name);
- this->RunQuery(query);
- }
- else
- {
- SQLQuery query("UPDATE `anope_cs_info` SET `flags` = @flags WHERE `name` = @name");
- query.setValue("flags", ToString(ci->ToString()));
- query.setValue("name", ci->name);
- this->RunQuery(query);
- }
- }
- else if (command->name == "botserv/kick" && params.size() > 2)
- {
- ChannelInfo *ci = cs_findchan(params[0]);
- if (!ci)
- return;
- if (!ci->AccessFor(u).HasPriv("SET") && !u->HasPriv("botserv/administration"))
- return;
- if (params[1].equals_ci("BADWORDS") || params[1].equals_ci("BOLDS") || params[1].equals_ci("CAPS") || params[1].equals_ci("COLORS") || params[1].equals_ci("FLOOD") || params[1].equals_ci("REPEAT") || params[1].equals_ci("REVERSES") || params[1].equals_ci("UNDERLINES"))
- {
- if (params[2].equals_ci("ON") || params[2].equals_ci("OFF"))
- {
- for (int i = 0; i < TTB_SIZE; ++i)
- {
- SQLQuery query("INSERT INTO `anope_cs_ttb` (channel, ttb_id, value) VALUES(@channel, @ttb_id, @value) ON DUPLICATE KEY UPDATE channel=VALUES(channel), ttb_id=VALUES(ttb_id), value=VALUES(value)");