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/>.
24 /* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
26 class ModuleXLineDB : public Module
28 std::vector<XLine *> xlines;
29 bool reading_db; // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
30 // DO REMEMBER TO SET IT, otherwise it's annoying :P
33 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
34 ServerInstance->Modules->Attach(eventlist, this, 3);
41 virtual ~ModuleXLineDB()
45 /** Called whenever an xline is added by a local user.
46 * This method is triggered after the line is added.
47 * @param source The sender of the line or NULL for local server
48 * @param line The xline being added
50 void OnAddLine(User* source, XLine* line)
52 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
53 xlines.push_back(line);
61 /** Called whenever an xline is deleted.
62 * This method is triggered after the line is deleted.
63 * @param source The user removing the line or NULL for local server
64 * @param line the line being deleted
66 void OnDelLine(User* source, XLine* line)
71 void OnExpireLine(XLine *line)
76 void RemoveLine(XLine *line)
78 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
79 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
96 * We need to perform an atomic write so as not to fuck things up.
97 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
98 * Technically, that means that this can block, but I have *never* seen that.
101 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
102 f = fopen("data/xline.db.new", "w");
105 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
106 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
110 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
113 * Now, much as I hate writing semi-unportable formats, additional
114 * xline types may not have a conf tag, so let's just write them.
115 * In addition, let's use a file version, so we can maintain some
116 * semblance of backwards compatibility for reading on startup..
119 fprintf(f, "VERSION 1\n");
123 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
126 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
127 ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
130 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
133 write_error = ferror(f);
134 write_error |= fclose(f);
137 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
138 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
142 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
143 if (rename("data/xline.db.new", "data/xline.db") < 0)
145 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
146 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
156 char linebuf[MAXBUF];
157 unsigned int lineno = 0;
159 f = fopen("data/xline.db", "r");
164 /* xline.db doesn't exist, fake good return value (we don't care about this) */
169 /* this might be slightly more problematic. */
170 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
171 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
176 while (fgets(linebuf, MAXBUF, f))
189 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
192 // Inspired by the command parser. :)
193 irc::tokenstream tokens(linebuf);
195 std::string command_p[MAXPARAMETERS];
198 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
200 command_p[items] = tmp;
204 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
206 if (command_p[0] == "VERSION")
208 if (command_p[1] == "1")
210 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
215 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
216 ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
220 else if (command_p[0] == "LINE")
222 // Mercilessly stolen from spanningtree
223 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
227 ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
231 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
232 xl->SetCreateTime(atoi(command_p[4].c_str()));
234 if (ServerInstance->XLines->AddLine(xl, NULL))
236 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
247 virtual Version GetVersion()
249 return Version("Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db).", VF_VENDOR);
253 MODULE_INIT(ModuleXLineDB)