summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2012-10-01 18:50:29 -0400
committerAdam <Adam@anope.org>2012-10-01 18:50:29 -0400
commita434baed9154d90ad0dfd31c71a463fb8300bfd8 (patch)
tree587b81c85c8abd676bb6fe1897415017a6d3f85a /modules
parentf14a3dfb8a4cc9da7b5066ac8320265d54dba177 (diff)
Allow modules to store data in their own databases.
Diffstat (limited to 'modules')
-rw-r--r--modules/commands/cs_entrymsg.cpp7
-rw-r--r--modules/commands/cs_seen.cpp7
-rw-r--r--modules/commands/cs_set_misc.cpp7
-rw-r--r--modules/commands/cs_suspend.cpp7
-rw-r--r--modules/commands/hs_request.cpp5
-rw-r--r--modules/commands/ns_ajoin.cpp5
-rw-r--r--modules/commands/ns_set_misc.cpp7
-rw-r--r--modules/commands/ns_suspend.cpp7
-rw-r--r--modules/commands/os_forbid.h2
-rw-r--r--modules/commands/os_ignore.h2
-rw-r--r--modules/commands/os_news.h2
-rw-r--r--modules/commands/os_oper.cpp7
-rw-r--r--modules/commands/os_session.h2
-rw-r--r--modules/commands/os_stats.cpp5
-rw-r--r--modules/database/db_flatfile.cpp103
-rw-r--r--modules/database/db_sql.cpp12
-rw-r--r--modules/database/db_sql_live.cpp25
-rw-r--r--modules/protocol/inspircd11.cpp2
18 files changed, 109 insertions, 105 deletions
diff --git a/modules/commands/cs_entrymsg.cpp b/modules/commands/cs_entrymsg.cpp
index e5b8d2221..1fce5fac0 100644
--- a/modules/commands/cs_entrymsg.cpp
+++ b/modules/commands/cs_entrymsg.cpp
@@ -20,7 +20,7 @@ struct EntryMsg : Serializable
Anope::string message;
time_t when;
- EntryMsg(ChannelInfo *c, const Anope::string &cname, const Anope::string &cmessage, time_t ct = Anope::CurTime)
+ EntryMsg(ChannelInfo *c, const Anope::string &cname, const Anope::string &cmessage, time_t ct = Anope::CurTime) : Serializable("EntryMsg")
{
this->ci = c;
this->creator = cname;
@@ -28,11 +28,6 @@ struct EntryMsg : Serializable
this->when = ct;
}
- const Anope::string serialize_name() const anope_override
- {
- return "EntryMsg";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data data;
diff --git a/modules/commands/cs_seen.cpp b/modules/commands/cs_seen.cpp
index ce88efa44..8ae12ba7e 100644
--- a/modules/commands/cs_seen.cpp
+++ b/modules/commands/cs_seen.cpp
@@ -33,15 +33,10 @@ struct SeenInfo : Serializable
Anope::string message; // for part/kick/quit
time_t last; // the time when the user was last seen
- SeenInfo()
+ SeenInfo() : Serializable("SeenInfo")
{
}
- const Anope::string serialize_name() const anope_override
- {
- return "SeenInfo";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data data;
diff --git a/modules/commands/cs_set_misc.cpp b/modules/commands/cs_set_misc.cpp
index 9b8879bbd..251a2b480 100644
--- a/modules/commands/cs_set_misc.cpp
+++ b/modules/commands/cs_set_misc.cpp
@@ -18,15 +18,10 @@ struct CSMiscData : ExtensibleItem, Serializable
Anope::string name;
Anope::string data;
- CSMiscData(ChannelInfo *c, const Anope::string &n, const Anope::string &d) : ci(c), name(n), data(d)
+ CSMiscData(ChannelInfo *c, const Anope::string &n, const Anope::string &d) : Serializable("CSMiscData"), ci(c), name(n), data(d)
{
}
- const Anope::string serialize_name() const anope_override
- {
- return "CSMiscData";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data sdata;
diff --git a/modules/commands/cs_suspend.cpp b/modules/commands/cs_suspend.cpp
index e7969c926..9cb829ad1 100644
--- a/modules/commands/cs_suspend.cpp
+++ b/modules/commands/cs_suspend.cpp
@@ -18,15 +18,10 @@ struct ChanSuspend : ExtensibleItem, Serializable
Anope::string chan;
time_t when;
- ChanSuspend()
+ ChanSuspend() : Serializable("ChanSuspend")
{
}
- const Anope::string serialize_name() const
- {
- return "ChanSuspend";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data sd;
diff --git a/modules/commands/hs_request.cpp b/modules/commands/hs_request.cpp
index 94b181f51..8a9de386b 100644
--- a/modules/commands/hs_request.cpp
+++ b/modules/commands/hs_request.cpp
@@ -30,10 +30,7 @@ struct HostRequest : ExtensibleItem, Serializable
Anope::string host;
time_t time;
- const Anope::string serialize_name() const anope_override
- {
- return "HostRequest";
- }
+ HostRequest() : Serializable("HostRequest") { }
Serialize::Data serialize() const anope_override
{
diff --git a/modules/commands/ns_ajoin.cpp b/modules/commands/ns_ajoin.cpp
index 5a33fb7db..d3b2cadb8 100644
--- a/modules/commands/ns_ajoin.cpp
+++ b/modules/commands/ns_ajoin.cpp
@@ -26,10 +26,7 @@ struct AJoinEntry : Serializable
Anope::string channel;
Anope::string key;
- const Anope::string serialize_name() const anope_override
- {
- return "AJoinEntry";
- }
+ AJoinEntry() : Serializable("AJoinEntry") { }
Serialize::Data serialize() const anope_override
{
diff --git a/modules/commands/ns_set_misc.cpp b/modules/commands/ns_set_misc.cpp
index c4c7b4ae3..0b7011892 100644
--- a/modules/commands/ns_set_misc.cpp
+++ b/modules/commands/ns_set_misc.cpp
@@ -19,15 +19,10 @@ struct NSMiscData : ExtensibleItem, Serializable
Anope::string name;
Anope::string data;
- NSMiscData(NickCore *ncore, const Anope::string &n, const Anope::string &d) : nc(ncore), name(n), data(d)
+ NSMiscData(NickCore *ncore, const Anope::string &n, const Anope::string &d) : Serializable("NSMiscData"), nc(ncore), name(n), data(d)
{
}
- const Anope::string serialize_name() const anope_override
- {
- return "NSMiscData";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data sdata;
diff --git a/modules/commands/ns_suspend.cpp b/modules/commands/ns_suspend.cpp
index dd3da66f6..6b316379e 100644
--- a/modules/commands/ns_suspend.cpp
+++ b/modules/commands/ns_suspend.cpp
@@ -18,15 +18,10 @@ struct NickSuspend : ExtensibleItem, Serializable
Anope::string nick;
time_t when;
- NickSuspend()
+ NickSuspend() : Serializable("NickSuspend")
{
}
- const Anope::string serialize_name() const
- {
- return "NickSuspend";
- }
-
Serialize::Data serialize() const anope_override
{
Serialize::Data sd;
diff --git a/modules/commands/os_forbid.h b/modules/commands/os_forbid.h
index dcc5a9d6e..f79b63616 100644
--- a/modules/commands/os_forbid.h
+++ b/modules/commands/os_forbid.h
@@ -18,7 +18,7 @@ struct ForbidData : Serializable
time_t expires;
ForbidType type;
- const Anope::string serialize_name() const anope_override { return "ForbidData"; }
+ ForbidData() : Serializable("ForbidData") { }
Serialize::Data serialize() const anope_override;
static Serializable* unserialize(Serializable *obj, Serialize::Data &data);
};
diff --git a/modules/commands/os_ignore.h b/modules/commands/os_ignore.h
index 7c9bf3db5..38042a72d 100644
--- a/modules/commands/os_ignore.h
+++ b/modules/commands/os_ignore.h
@@ -17,7 +17,7 @@ struct IgnoreData : Serializable
Anope::string reason;
time_t time; /* When do we stop ignoring them? */
- const Anope::string serialize_name() const anope_override { return "IgnoreData"; }
+ IgnoreData() : Serializable("IgnoreData") { }
Serialize::Data serialize() const anope_override;
static Serializable* unserialize(Serializable *obj, Serialize::Data &data);
};
diff --git a/modules/commands/os_news.h b/modules/commands/os_news.h
index e2ad1fc4f..426121fb7 100644
--- a/modules/commands/os_news.h
+++ b/modules/commands/os_news.h
@@ -22,7 +22,7 @@ struct NewsItem : Serializable
Anope::string who;
time_t time;
- const Anope::string serialize_name() const anope_override { return "NewsItem"; }
+ NewsItem() : Serializable("NewsItem") { }
Serialize::Data serialize() const anope_override;
static Serializable* unserialize(Serializable *obj, Serialize::Data &data);
};
diff --git a/modules/commands/os_oper.cpp b/modules/commands/os_oper.cpp
index 528ff7382..e2e7b4ab3 100644
--- a/modules/commands/os_oper.cpp
+++ b/modules/commands/os_oper.cpp
@@ -15,12 +15,7 @@
struct MyOper : Oper, Serializable
{
- MyOper(const Anope::string &n, OperType *o) : Oper(n, o) { }
-
- const Anope::string serialize_name() const anope_override
- {
- return "Oper";
- }
+ MyOper(const Anope::string &n, OperType *o) : Oper(n, o), Serializable("Oper") { }
Serialize::Data serialize() const anope_override
{
diff --git a/modules/commands/os_session.h b/modules/commands/os_session.h
index aa32d9d58..ee37a7594 100644
--- a/modules/commands/os_session.h
+++ b/modules/commands/os_session.h
@@ -19,7 +19,7 @@ struct Exception : Serializable
time_t time; /* When this exception was added */
time_t expires; /* Time when it expires. 0 == no expiry */
- const Anope::string serialize_name() const anope_override { return "Exception"; }
+ Exception() : Serializable("Exception") { }
Serialize::Data serialize() const anope_override;
static Serializable* unserialize(Serializable *obj, Serialize::Data &data);
};
diff --git a/modules/commands/os_stats.cpp b/modules/commands/os_stats.cpp
index dd5bf9ee0..a418396bd 100644
--- a/modules/commands/os_stats.cpp
+++ b/modules/commands/os_stats.cpp
@@ -15,10 +15,7 @@
struct Stats : Serializable
{
- const Anope::string serialize_name() const
- {
- return "Stats";
- }
+ Stats() : Serializable("Stats") { }
Serialize::Data serialize() const anope_override
{
diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp
index a4ab5ce9a..4649a3666 100644
--- a/modules/database/db_flatfile.cpp
+++ b/modules/database/db_flatfile.cpp
@@ -82,42 +82,58 @@ class DBFlatFile : public Module
EventReturn OnLoadDatabase() anope_override
{
- std::fstream db;
- db.open(DatabaseFile.c_str(), std::ios_base::in);
+ std::map<Module *, std::fstream *> databases;
+ databases[NULL] = new std::fstream(DatabaseFile.c_str(), std::ios_base::in);
- if (!db.is_open())
+ if (!databases[NULL]->is_open())
{
+ delete databases[NULL];
Log() << "Unable to open " << DatabaseFile << " for reading!";
return EVENT_CONTINUE;
}
- SerializeType *st = NULL;
- Serialize::Data data;
- std::multimap<SerializeType *, Serialize::Data> objects;
- for (Anope::string buf, token; std::getline(db, buf.str());)
- {
- spacesepstream sep(buf);
-
- if (!sep.GetToken(token))
- continue;
+ const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder();
- if (token == "OBJECT" && sep.GetToken(token))
+ for (unsigned i = 0; i < type_order.size(); ++i)
+ {
+ SerializeType *stype = SerializeType::Find(type_order[i]);
+ if (stype && !databases.count(stype->GetOwner()))
{
- st = SerializeType::Find(token);
- data.clear();
+ Anope::string db_name = db_dir + "/module_" + stype->GetOwner()->name + ".db";
+ databases[stype->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::in);
}
- else if (token == "DATA" && st != NULL && sep.GetToken(token))
- data[token] << sep.GetRemaining();
- else if (token == "END" && st != NULL)
- {
- objects.insert(std::make_pair(st, data));
+ }
- st = NULL;
- data.clear();
+ std::multimap<SerializeType *, Serialize::Data> objects;
+ for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it)
+ {
+ std::fstream *db = it->second;
+ SerializeType *st = NULL;
+ Serialize::Data data;
+ for (Anope::string buf, token; std::getline(*db, buf.str());)
+ {
+ spacesepstream sep(buf);
+
+ if (!sep.GetToken(token))
+ continue;
+
+ if (token == "OBJECT" && sep.GetToken(token))
+ {
+ st = SerializeType::Find(token);
+ data.clear();
+ }
+ else if (token == "DATA" && st != NULL && sep.GetToken(token))
+ data[token] << sep.GetRemaining();
+ else if (token == "END" && st != NULL)
+ {
+ objects.insert(std::make_pair(st, data));
+
+ st = NULL;
+ data.clear();
+ }
}
}
- const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder();
for (unsigned i = 0; i < type_order.size(); ++i)
{
SerializeType *stype = SerializeType::Find(type_order[i]);
@@ -129,7 +145,11 @@ class DBFlatFile : public Module
it->first->Unserialize(NULL, it->second);
}
- db.close();
+ for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it)
+ {
+ it->second->close();
+ delete it->second;
+ }
return EVENT_STOP;
}
@@ -143,9 +163,11 @@ class DBFlatFile : public Module
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())
+ std::map<Module *, std::fstream *> databases;
+ databases[NULL] = new std::fstream(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc);
+ if (!databases[NULL]->is_open())
{
+ delete databases[NULL];
Log() << "Unable to open " << DatabaseFile << " for writing";
if (IsFile(tmp_db))
rename(tmp_db.c_str(), DatabaseFile.c_str());
@@ -156,19 +178,30 @@ class DBFlatFile : public Module
for (std::list<Serializable *>::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it)
{
Serializable *base = *it;
+ SerializeType *s_type = base->GetSerializableType();
+
+ if (!s_type)
+ continue;
+
Serialize::Data data = base->serialize();
+
+ if (!databases.count(s_type->GetOwner()))
+ {
+ Anope::string db_name = db_dir + "/module_" + s_type->GetOwner()->name + ".db";
+ databases[s_type->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::out | std::ios_base::trunc);
+ }
+ std::fstream *fd = databases[s_type->GetOwner()];
- db << "OBJECT " << base->serialize_name() << "\n";
+ *fd << "OBJECT " << s_type->GetName() << "\n";
for (Serialize::Data::iterator dit = data.begin(), dit_end = data.end(); dit != dit_end; ++dit)
- db << "DATA " << dit->first << " " << dit->second.astr() << "\n";
- db << "END\n";
+ *fd << "DATA " << dit->first << " " << dit->second.astr() << "\n";
+ *fd << "END\n";
}
- db.close();
-
- if (db.good() == false)
+ if (databases[NULL]->good() == false)
{
Log() << "Unable to write database";
+ databases[NULL]->close();
if (!Config->NoBackupOkay)
quitting = true;
if (IsFile(tmp_db))
@@ -177,6 +210,12 @@ class DBFlatFile : public Module
else
unlink(tmp_db.c_str());
+ for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it)
+ {
+ it->second->close();
+ delete it->second;
+ }
+
return EVENT_CONTINUE;
}
};
diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp
index c07bdaa59..acc59b668 100644
--- a/modules/database/db_sql.cpp
+++ b/modules/database/db_sql.cpp
@@ -105,13 +105,17 @@ class DBSQL : public Module, public Pipe
continue;
obj->UpdateCache();
+ SerializeType *s_type = obj->GetSerializableType();
+ if (!s_type)
+ continue;
+
Serialize::Data data = obj->serialize();
- std::vector<SQLQuery> create = this->sql->CreateTable(this->prefix + obj->serialize_name(), data);
+ std::vector<SQLQuery> create = this->sql->CreateTable(this->prefix + s_type->GetName(), data);
for (unsigned i = 0; i < create.size(); ++i)
this->RunBackground(create[i]);
- SQLQuery insert = this->sql->BuildInsert(this->prefix + obj->serialize_name(), obj->id, data);
+ SQLQuery insert = this->sql->BuildInsert(this->prefix + s_type->GetName(), obj->id, data);
this->RunBackground(insert, new ResultSQLSQLInterface(this, obj));
}
}
@@ -188,7 +192,9 @@ class DBSQL : public Module, public Pipe
void OnSerializableDestruct(Serializable *obj) anope_override
{
- this->RunBackground("DELETE FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id));
+ SerializeType *s_type = obj->GetSerializableType();
+ if (s_type)
+ this->RunBackground("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id));
}
void OnSerializableUpdate(Serializable *obj) anope_override
diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp
index 4dca16d26..2aa3b73e6 100644
--- a/modules/database/db_sql_live.cpp
+++ b/modules/database/db_sql_live.cpp
@@ -95,18 +95,20 @@ class DBMySQL : public Module, public Pipe
continue;
obj->UpdateCache();
+ SerializeType *s_type = obj->GetSerializableType();
+ if (!s_type)
+ continue;
+
Serialize::Data data = obj->serialize();
- std::vector<SQLQuery> create = this->SQL->CreateTable(this->prefix + obj->serialize_name(), data);
+ std::vector<SQLQuery> create = this->SQL->CreateTable(this->prefix + s_type->GetName(), data);
for (unsigned i = 0; i < create.size(); ++i)
this->RunQueryResult(create[i]);
- SQLResult res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + obj->serialize_name(), obj->id, data));
+ SQLResult res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data));
if (res.GetID() > 0)
obj->id = res.GetID();
- SerializeType *stype = SerializeType::Find(obj->serialize_name());
- if (stype)
- stype->objects.erase(obj->id);
+ s_type->objects.erase(obj->id);
}
}
@@ -144,15 +146,16 @@ class DBMySQL : public Module, public Pipe
{
if (!this->CheckInit())
return;
- this->RunQuery("DELETE FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id));
- SerializeType *stype = SerializeType::Find(obj->serialize_name());
- if (stype)
- stype->objects.erase(obj->id);
+ SerializeType *s_type = obj->GetSerializableType();
+ if (!s_type)
+ return;
+ this->RunQuery("DELETE FROM `" + this->prefix + s_type->GetName() + "` WHERE `id` = " + stringify(obj->id));
+ s_type->objects.erase(obj->id);
}
void OnSerializePtrAssign(Serializable *obj) anope_override
{
- SerializeType *stype = SerializeType::Find(obj->serialize_name());
+ SerializeType *stype = obj->GetSerializableType();
if (stype == NULL || !this->CheckInit() || stype->GetTimestamp() == Anope::CurTime)
return;
@@ -160,7 +163,7 @@ class DBMySQL : public Module, public Pipe
return;
obj->UpdateCache();
- SQLResult res = this->RunQueryResult("SELECT * FROM `" + this->prefix + obj->serialize_name() + "` WHERE `id` = " + stringify(obj->id));
+ SQLResult res = this->RunQueryResult("SELECT * FROM `" + this->prefix + stype->GetName() + "` WHERE `id` = " + stringify(obj->id));
if (res.Rows() == 0)
obj->destroy();
diff --git a/modules/protocol/inspircd11.cpp b/modules/protocol/inspircd11.cpp
index 1eba8321f..e3c121f9c 100644
--- a/modules/protocol/inspircd11.cpp
+++ b/modules/protocol/inspircd11.cpp
@@ -875,7 +875,7 @@ struct IRCDMessageRSQuit : IRCDMessage
else
s = Server::Find(params[0]);
- source.GetServer()->Delete(source.GetServer()->GetName() + " " + source.GetServer()->GetUplink()->GetName());
+ source.GetServer()->Delete(source.GetServer()->GetName() + " " + (s ? s->GetName() : "<unknown>"));
return true;
}