1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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
26 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
27 ServerInstance->Modules->Attach(eventlist, this, 3);
34 virtual ~ModuleXLineDB()
38 /** Called whenever an xline is added by a local user.
39 * This method is triggered after the line is added.
40 * @param source The sender of the line or NULL for local server
41 * @param line The xline being added
43 void OnAddLine(User* source, XLine* line)
45 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
46 xlines.push_back(line);
54 /** Called whenever an xline is deleted.
55 * This method is triggered after the line is deleted.
56 * @param source The user removing the line or NULL for local server
57 * @param line the line being deleted
59 void OnDelLine(User* source, XLine* line)
64 void OnExpireLine(XLine *line)
69 void RemoveLine(XLine *line)
71 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
72 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
89 * We need to perform an atomic write so as not to fuck things up.
90 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
91 * Technically, that means that this can block, but I have *never* seen that.
94 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
95 f = fopen("data/xline.db.new", "w");
98 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
99 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
103 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
106 * Now, much as I hate writing semi-unportable formats, additional
107 * xline types may not have a conf tag, so let's just write them.
108 * In addition, let's use a file version, so we can maintain some
109 * semblance of backwards compatibility for reading on startup..
112 fprintf(f, "VERSION 1\n");
116 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
119 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
120 ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
123 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
126 write_error = ferror(f);
127 write_error |= fclose(f);
130 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
131 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
135 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
136 if (rename("data/xline.db.new", "data/xline.db") < 0)
138 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
139 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
149 char linebuf[MAXBUF];
150 unsigned int lineno = 0;
152 f = fopen("data/xline.db", "r");
157 /* xline.db doesn't exist, fake good return value (we don't care about this) */
162 /* this might be slightly more problematic. */
163 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
164 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
169 while (fgets(linebuf, MAXBUF, f))
182 // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
185 // Inspired by the command parser. :)
186 irc::tokenstream tokens(linebuf);
188 std::string command_p[MAXPARAMETERS];
191 while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
193 command_p[items] = tmp;
197 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
199 if (command_p[0] == "VERSION")
201 if (command_p[1] == "1")
203 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
208 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
209 ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
213 else if (command_p[0] == "LINE")
215 // Mercilessly stolen from spanningtree
216 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
220 ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
224 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
225 xl->SetCreateTime(atoi(command_p[4].c_str()));
227 if (ServerInstance->XLines->AddLine(xl, NULL))
229 ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
240 virtual Version GetVersion()
242 return Version("Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db).", VF_VENDOR);
246 MODULE_INIT(ModuleXLineDB)