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 xlines.push_back(line);
48 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
51 ServerInstance->WriteOpers("%s %s %s %lu %lu :%s", line->type.c_str(), line->Displayable(),
52 ServerInstance->Config->ServerName, line->set_time, line->duration, line->reason);
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)
68 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
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->Log(DEBUG, "xlinedb: Opening temporary database");
91 f = fopen("xline.db.new", "w");
94 ServerInstance->Log(DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
95 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot create new db: %s (%d)", strerror(errno), errno);
99 ServerInstance->Log(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");
112 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
115 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
116 ServerInstance->Config->ServerName, line->set_time, line->duration, line->reason);
119 ServerInstance->Log(DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
122 write_error = ferror(f);
123 write_error |= fclose(f);
126 ServerInstance->Log(DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
127 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
131 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
132 if (rename("xline.db.new", "xline.db") < 0)
134 ServerInstance->Log(DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
135 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
145 char linebuf[MAXBUF];
146 unsigned int lineno = 0;
148 f = fopen("xline.db", "r");
153 /* xline.db doesn't exist, fake good return value (we don't care about this) */
158 /* this might be slightly more problematic. */
159 ServerInstance->Log(DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
160 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot read db: %s (%d)", strerror(errno), errno);
165 while (fgets(linebuf, MAXBUF, f))
178 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
181 // Inspired by the command parser. :)
182 irc::tokenstream tokens(linebuf);
184 std::string command_p[MAXPARAMETERS];
187 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
189 command_p[items] = tmp.c_str();
193 if (command_p[0] == "VERSION")
195 if (command_p[1] == "1")
197 ServerInstance->Log(DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
202 ServerInstance->Log(DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
203 ServerInstance->SNO->WriteToSnoMask('x', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
207 else if (command_p[0] == "LINE")
209 //mercilessly stolen from spanningtree
210 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[0]);
214 ServerInstance->SNO->WriteToSnoMask('x', "database: Unknown line type (%s).", command_p[1].c_str());
218 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());
219 xl->SetCreateTime(atoi(command_p[4].c_str()));
221 if (ServerInstance->XLines->AddLine(xl, NULL))
223 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
233 virtual Version GetVersion()
235 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
239 MODULE_INIT(ModuleXLineDB)