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 bool reading_db; // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
29 // DO REMEMBER TO SET IT, otherwise it's annoying :P
32 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
33 ServerInstance->Modules->Attach(eventlist, this, 3);
40 virtual ~ModuleXLineDB()
44 /** Called whenever an xline is added by a local user.
45 * This method is triggered after the line is added.
46 * @param source The sender of the line or NULL for local server
47 * @param line The xline being added
49 void OnAddLine(User* source, XLine* line)
51 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
59 /** Called whenever an xline is deleted.
60 * This method is triggered after the line is deleted.
61 * @param source The user removing the line or NULL for local server
62 * @param line the line being deleted
64 void OnDelLine(User* source, XLine* line)
69 void OnExpireLine(XLine *line)
74 void RemoveLine(XLine *line)
76 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
85 * We need to perform an atomic write so as not to fuck things up.
86 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
87 * Technically, that means that this can block, but I have *never* seen that.
90 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
91 f = fopen("data/xline.db.new", "w");
94 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
95 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
99 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
102 * Now, much as I hate writing semi-unportable formats, additional
103 * xline types may not have a conf tag, so let's just write them.
104 * In addition, let's use a file version, so we can maintain some
105 * semblance of backwards compatibility for reading on startup..
108 fprintf(f, "VERSION 1\n");
111 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
112 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
114 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
118 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
120 XLine *line = i->second;
121 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
122 ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
126 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
129 write_error = ferror(f);
130 write_error |= fclose(f);
133 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
134 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
138 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
139 if (rename("data/xline.db.new", "data/xline.db") < 0)
141 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
142 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
152 char linebuf[MAXBUF];
153 unsigned int lineno = 0;
155 f = fopen("data/xline.db", "r");
160 /* xline.db doesn't exist, fake good return value (we don't care about this) */
165 /* this might be slightly more problematic. */
166 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
167 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
172 while (fgets(linebuf, MAXBUF, f))
185 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
188 // Inspired by the command parser. :)
189 irc::tokenstream tokens(linebuf);
191 std::string command_p[MAXPARAMETERS];
194 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
196 command_p[items] = tmp;
200 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
202 if (command_p[0] == "VERSION")
204 if (command_p[1] == "1")
206 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
211 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
212 ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
216 else if (command_p[0] == "LINE")
218 // Mercilessly stolen from spanningtree
219 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
223 ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
227 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
228 xl->SetCreateTime(atoi(command_p[4].c_str()));
230 if (ServerInstance->XLines->AddLine(xl, NULL))
232 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
245 virtual Version GetVersion()
247 return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
251 MODULE_INIT(ModuleXLineDB)