+ void OnDelLine(User* source, XLine* line) CXX11_OVERRIDE
+ {
+ dirty = true;
+ }
+
+ void OnExpireLine(XLine *line) CXX11_OVERRIDE
+ {
+ dirty = true;
+ }
+
+ void OnBackgroundTimer(time_t now) CXX11_OVERRIDE
+ {
+ if (dirty)
+ {
+ if (WriteDatabase())
+ dirty = false;
+ }
+ }
+
+ bool WriteDatabase()
+ {
+ /*
+ * We need to perform an atomic write so as not to fuck things up.
+ * So, let's write to a temporary file, flush it, then rename the file..
+ * Technically, that means that this can block, but I have *never* seen that.
+ * -- w00t
+ */
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Opening temporary database");
+ std::string xlinenewdbpath = xlinedbpath + ".new";
+ std::ofstream stream(xlinenewdbpath.c_str());
+ if (!stream.is_open())
+ {
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot create database \"%s\"! %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new xline db \"%s\": %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
+ return false;
+ }
+
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Opened. Writing..");
+
+ /*
+ * Now, much as I hate writing semi-unportable formats, additional
+ * xline types may not have a conf tag, so let's just write them.
+ * In addition, let's use a file version, so we can maintain some
+ * semblance of backwards compatibility for reading on startup..
+ * -- w00t
+ */
+ stream << "VERSION 1" << std::endl;
+
+ // Now, let's write.
+ std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
+ for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
+ {
+ XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
+ if (!lookup)
+ continue; // Not possible as we just obtained the list from XLineManager
+
+ for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
+ {
+ XLine* line = i->second;
+ stream << "LINE " << line->type << " " << line->Displayable() << " "
+ << ServerInstance->Config->ServerName << " " << line->set_time << " "
+ << line->duration << " " << line->reason << std::endl;
+ }
+ }
+
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Finished writing XLines. Checking for error..");
+
+ if (stream.fail())
+ {
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot write to new database \"%s\"! %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new xline db \"%s\": %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
+ return false;
+ }
+ stream.close();
+
+#ifdef _WIN32
+ remove(xlinedbpath.c_str());
+#endif
+ // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
+ if (rename(xlinenewdbpath.c_str(), xlinedbpath.c_str()) < 0)
+ {
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot replace old database \"%s\" with new database \"%s\"! %s (%d)", xlinedbpath.c_str(), xlinenewdbpath.c_str(), strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old xline db \"%s\" with new db \"%s\": %s (%d)", xlinedbpath.c_str(), xlinenewdbpath.c_str(), strerror(errno), errno);
+ return false;
+ }
+
+ return true;
+ }
+
+ bool ReadDatabase()