1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 };
28 ServerInstance->Modules->Attach(eventlist, this, 2);
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->Log(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)
62 ServerInstance->Log(DEBUG, "xlinedb: Removing a line");
63 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
80 * We need to perform an atomic write so as not to fuck things up.
81 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
82 * Technically, that means that this can block, but I have *never* seen that.
85 ServerInstance->Log(DEBUG, "xlinedb: Opening temporary database");
86 f = fopen("xline.db.new", "w");
89 ServerInstance->Log(DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
90 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot create new db: %s (%d)", strerror(errno), errno);
94 ServerInstance->Log(DEBUG, "xlinedb: 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 fprintf(f, "VERSION 1\n");
107 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
110 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
111 ServerInstance->Config->ServerName, line->set_time, line->duration, line->reason);
114 ServerInstance->Log(DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
117 write_error = ferror(f);
118 write_error |= fclose(f);
121 ServerInstance->Log(DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
122 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
126 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
127 if (rename("xline.db.new", "xline.db") < 0)
129 ServerInstance->Log(DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
130 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
140 char linebuf[MAXBUF];
141 unsigned int lineno = 0;
143 f = fopen("xline.db", "r");
148 /* xline.db doesn't exist, fake good return value (we don't care about this) */
153 /* this might be slightly more problematic. */
154 ServerInstance->Log(DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
155 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot read db: %s (%d)", strerror(errno), errno);
160 while (fgets(linebuf, MAXBUF, f))
173 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
176 // Inspired by the command parser. :)
177 irc::tokenstream tokens(linebuf);
179 std::string command_p[MAXPARAMETERS];
182 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
184 command_p[items] = tmp.c_str();
188 ServerInstance->Log(DEBUG, "xlinedb: Processing %s", linebuf);
190 if (command_p[0] == "VERSION")
192 if (command_p[1] == "1")
194 ServerInstance->Log(DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
199 ServerInstance->Log(DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
200 ServerInstance->SNO->WriteToSnoMask('x', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
204 else if (command_p[0] == "LINE")
206 // Mercilessly stolen from spanningtree
207 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
211 ServerInstance->SNO->WriteToSnoMask('x', "database: Unknown line type (%s).", command_p[1].c_str());
215 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());
216 xl->SetCreateTime(atoi(command_p[4].c_str()));
218 if (ServerInstance->XLines->AddLine(xl, NULL))
220 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
230 virtual Version GetVersion()
232 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
236 MODULE_INIT(ModuleXLineDB)