#include "inspircd.h"
#include "xline.h"
+/* $ModConfig: <xlinedb filename="data/xline.db">
+ * Specify the filename for the xline database here*/
/* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
class ModuleXLineDB : public Module
std::vector<XLine *> xlines;
bool reading_db; // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
// DO REMEMBER TO SET IT, otherwise it's annoying :P
+ std::string xlinedbpath;
public:
ModuleXLineDB() {
Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
ServerInstance->Modules->Attach(eventlist, this, 3);
+ /* Load the configuration
+ * Note:
+ * this is on purpose not in the OnRehash() method. It would be non-trivial to change the database on-the-fly.
+ * Imagine a scenario where the new file already exists. Merging the current XLines with the existing database is likely a bad idea
+ * ...and so is discarding all current in-memory XLines for the ones in the database.
+ */
+ ConfigTag* Conf = ServerInstance->Config->ConfValue("xlinedb");
+ xlinedbpath = Conf->getString("filename", DATA_PATH "/xline.db");
+
reading_db = true;
ReadDatabase();
reading_db = false;
* -- w00t
*/
ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
- f = fopen("data/xline.db.new", "w");
+ std::string xlinenewdbpath = xlinedbpath + ".new";
+ f = fopen(xlinenewdbpath.c_str(), "w");
if (!f)
{
ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
return false;
}
+#ifdef _WIN32
+ if (remove(xlinedbpath.c_str()))
+ {
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot remove old database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
+ return false;
+ }
+#endif
// Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
- if (rename("data/xline.db.new", "data/xline.db") < 0)
+ if (rename(xlinenewdbpath.c_str(), xlinedbpath.c_str()) < 0)
{
ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
char linebuf[MAXBUF];
unsigned int lineno = 0;
- f = fopen("data/xline.db", "r");
+ f = fopen(xlinedbpath.c_str(), "r");
if (!f)
{
if (errno == ENOENT)
{
ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
}
+ else
+ delete xl;
}
}
virtual Version GetVersion()
{
- return Version("Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db).", VF_VENDOR);
+ return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
}
};