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;
23 ModuleXLineDB(InspIRCd* Me) : Module(Me)
25 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine };
26 ServerInstance->Modules->Attach(eventlist, this, 2);
29 virtual ~ModuleXLineDB()
33 /** Called whenever an xline is added by a local user.
34 * This method is triggered after the line is added.
35 * @param source The sender of the line or NULL for local server
36 * @param line The xline being added
38 void OnAddLine(User* source, XLine* line)
40 xlines.push_back(line);
42 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
45 ServerInstance->WriteOpers("%s %s %s %lu %lu :%s", line->type.c_str(), line->Displayable(),
46 ServerInstance->Config->ServerName, line->set_time, line->duration, line->reason);
52 /** Called whenever an xline is deleted.
53 * This method is triggered after the line is deleted.
54 * @param source The user removing the line or NULL for local server
55 * @param line the line being deleted
57 void OnDelLine(User* source, XLine* line)
59 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
76 * We need to perform an atomic write so as not to fuck things up.
77 * So, let's write to a temporary file, flush and sync the FD, then rename the file..
78 * Technically, that means that this can block, but I have *never* seen that.
81 ServerInstance->Log(DEBUG, "xlinedb: Opening temporary database");
82 f = fopen("xline.db.new", "w");
85 ServerInstance->Log(DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
86 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot create new db: %s (%d)", strerror(errno), errno);
90 ServerInstance->Log(DEBUG, "xlinedb: Opened. Writing..");
93 * Now, much as I hate writing semi-unportable formats, additional
94 * xline types may not have a conf tag, so let's just write them.
95 * In addition, let's use a file version, so we can maintain some
96 * semblance of backwards compatibility for reading on startup..
99 fprintf(f, "VERSION 1\n");
103 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
106 fprintf(f, "%s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
107 ServerInstance->Config->ServerName, line->set_time, line->duration, line->reason);
110 ServerInstance->Log(DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
113 write_error = ferror(f);
114 write_error |= fclose(f);
117 ServerInstance->Log(DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
118 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
122 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
123 if (rename("xline.db.new", "xline.db") < 0)
125 ServerInstance->Log(DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
126 ServerInstance->SNO->WriteToSnoMask('x', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
133 virtual Version GetVersion()
135 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
139 MODULE_INIT(ModuleXLineDB)