2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class ModuleXLineDB : public Module
28 std::string xlinedbpath;
30 void init() CXX11_OVERRIDE
32 /* Load the configuration
34 * This is on purpose not changed on a rehash. It would be non-trivial to change the database on-the-fly.
35 * Imagine a scenario where the new file already exists. Merging the current XLines with the existing database is likely a bad idea
36 * ...and so is discarding all current in-memory XLines for the ones in the database.
38 ConfigTag* Conf = ServerInstance->Config->ConfValue("xlinedb");
39 xlinedbpath = ServerInstance->Config->Paths.PrependData(Conf->getString("filename", "xline.db"));
41 // Read xlines before attaching to events
47 /** Called whenever an xline is added by a local user.
48 * This method is triggered after the line is added.
49 * @param source The sender of the line or NULL for local server
50 * @param line The xline being added
52 void OnAddLine(User* source, XLine* line) CXX11_OVERRIDE
57 /** Called whenever an xline is deleted.
58 * This method is triggered after the line is deleted.
59 * @param source The user removing the line or NULL for local server
60 * @param line the line being deleted
62 void OnDelLine(User* source, XLine* line) CXX11_OVERRIDE
67 void OnBackgroundTimer(time_t now) CXX11_OVERRIDE
79 * We need to perform an atomic write so as not to fuck things up.
80 * So, let's write to a temporary file, flush it, then rename the file..
81 * Technically, that means that this can block, but I have *never* seen that.
84 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Opening temporary database");
85 std::string xlinenewdbpath = xlinedbpath + ".new";
86 std::ofstream stream(xlinenewdbpath.c_str());
87 if (!stream.is_open())
89 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot create database \"%s\"! %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
90 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot create new xline db \"%s\": %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
94 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Opened. Writing..");
97 * Now, much as I hate writing semi-unportable formats, additional
98 * xline types may not have a conf tag, so let's just write them.
99 * In addition, let's use a file version, so we can maintain some
100 * semblance of backwards compatibility for reading on startup..
103 stream << "VERSION 1" << std::endl;
106 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
107 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
109 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
111 continue; // Not possible as we just obtained the list from XLineManager
113 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
115 XLine* line = i->second;
116 stream << "LINE " << line->type << " " << line->Displayable() << " "
117 << line->source << " " << line->set_time << " "
118 << line->duration << " :" << line->reason << std::endl;
122 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Finished writing XLines. Checking for error..");
126 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot write to new database \"%s\"! %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
127 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot write to new xline db \"%s\": %s (%d)", xlinenewdbpath.c_str(), strerror(errno), errno);
133 remove(xlinedbpath.c_str());
135 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
136 if (rename(xlinenewdbpath.c_str(), xlinedbpath.c_str()) < 0)
138 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);
139 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot replace old xline db \"%s\" with new db \"%s\": %s (%d)", xlinedbpath.c_str(), xlinenewdbpath.c_str(), strerror(errno), errno);
148 // If the xline database doesn't exist then we don't need to load it.
149 if (!FileSystem::FileExists(xlinedbpath))
152 std::ifstream stream(xlinedbpath.c_str());
153 if (!stream.is_open())
155 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cannot read database \"%s\"! %s (%d)", xlinedbpath.c_str(), strerror(errno), errno);
156 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot read xline db \"%s\": %s (%d)", xlinedbpath.c_str(), strerror(errno), errno);
161 while (std::getline(stream, line))
163 // Inspired by the command parser. :)
164 irc::tokenstream tokens(line);
166 std::string command_p[7];
169 while (tokens.GetTrailing(tmp) && (items < 7))
171 command_p[items] = tmp;
175 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Processing %s", line.c_str());
177 if (command_p[0] == "VERSION")
179 if (command_p[1] != "1")
182 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "I got database version %s - I don't understand it", command_p[1].c_str());
183 ServerInstance->SNO->WriteToSnoMask('x', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
187 else if (command_p[0] == "LINE")
189 // Mercilessly stolen from spanningtree
190 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
194 ServerInstance->SNO->WriteToSnoMask('x', "database: Unknown line type (%s).", command_p[1].c_str());
198 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
199 xl->SetCreateTime(atoi(command_p[4].c_str()));
201 if (ServerInstance->XLines->AddLine(xl, NULL))
203 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
213 Version GetVersion() CXX11_OVERRIDE
215 return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
219 MODULE_INIT(ModuleXLineDB)