1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
19 class ModuleXLineDB : public Module
21 std::vector<XLine *> xlines;
22 bool reading_db; // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
23 // DO REMEMBER TO SET IT, otherwise it's annoying :P
25 ModuleXLineDB(InspIRCd* Me) : Module(Me)
27 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
28 ServerInstance->Modules->Attach(eventlist, this, 3);
35 virtual ~ModuleXLineDB()
39 /** Called whenever an xline is added by a local user.
40 * This method is triggered after the line is added.
41 * @param source The sender of the line or NULL for local server
42 * @param line The xline being added
44 void OnAddLine(User* source, XLine* line)
46 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
47 xlines.push_back(line);
55 /** Called whenever an xline is deleted.
56 * This method is triggered after the line is deleted.
57 * @param source The user removing the line or NULL for local server
58 * @param line the line being deleted
60 void OnDelLine(User* source, XLine* line)
65 void OnExpireLine(XLine *line)
70 void RemoveLine(XLine *line)
72 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
73 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
90 * We need to perform an atomic write so as not to fuck things up.
91 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
92 * Technically, that means that this can block, but I have *never* seen that.
95 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
96 f = fopen("xline.db.new", "w");
99 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
100 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot create new db: %s (%d)", strerror(errno), errno);
104 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
107 * Now, much as I hate writing semi-unportable formats, additional
108 * xline types may not have a conf tag, so let's just write them.
109 * In addition, let's use a file version, so we can maintain some
110 * semblance of backwards compatibility for reading on startup..
113 fprintf(f, "VERSION 1\n");
117 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
120 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
121 ServerInstance->Config->ServerName, (unsigned long)line->set_time, (unsigned long)line->duration, line->reason);
124 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
127 write_error = ferror(f);
128 write_error |= fclose(f);
131 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
132 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
136 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
137 if (rename("xline.db.new", "xline.db") < 0)
139 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
140 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
150 char linebuf[MAXBUF];
151 unsigned int lineno = 0;
153 f = fopen("xline.db", "r");
158 /* xline.db doesn't exist, fake good return value (we don't care about this) */
163 /* this might be slightly more problematic. */
164 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
165 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot read db: %s (%d)", strerror(errno), errno);
170 while (fgets(linebuf, MAXBUF, f))
183 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
186 // Inspired by the command parser. :)
187 irc::tokenstream tokens(linebuf);
189 std::string command_p[MAXPARAMETERS];
192 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
194 command_p[items] = tmp.c_str();
198 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
200 if (command_p[0] == "VERSION")
202 if (command_p[1] == "1")
204 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
209 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
210 ServerInstance->SNO->WriteToSnoMask('x', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
214 else if (command_p[0] == "LINE")
216 // Mercilessly stolen from spanningtree
217 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
221 ServerInstance->SNO->WriteToSnoMask('x', "database: Unknown line type (%s).", command_p[1].c_str());
225 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3].c_str(), command_p[6].c_str(), command_p[2].c_str());
226 xl->SetCreateTime(atoi(command_p[4].c_str()));
228 if (ServerInstance->XLines->AddLine(xl, NULL))
230 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
240 virtual Version GetVersion()
242 return Version("$Id$", VF_VENDOR, API_VERSION);
246 MODULE_INIT(ModuleXLineDB)