]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xline_db.cpp
Code to write xline.db.. reading is not yet handled.
[user/henk/code/inspircd.git] / src / modules / m_xline_db.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 /* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
18
19 class ModuleXLineDB : public Module
20 {
21         std::vector<XLine *> xlines;
22  public:
23         ModuleXLineDB(InspIRCd* Me) : Module(Me)
24         {
25                 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine };
26                 ServerInstance->Modules->Attach(eventlist, this, 2);
27         }
28
29         virtual ~ModuleXLineDB()
30         {
31         }
32
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
37          */
38         void OnAddLine(User* source, XLine* line)
39         {
40                 xlines.push_back(line);
41
42                 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
43                 {
44                         line = (*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);
47                 }
48
49                 WriteDatabase();
50         }
51
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
56          */
57         void OnDelLine(User* source, XLine* line)
58         {
59                 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
60                 {
61                         if ((*i) == line)
62                         {
63                                 xlines.erase(i);
64                                 break;
65                         }
66                 }
67
68                 WriteDatabase();
69         }
70
71         bool WriteDatabase()
72         {
73                 FILE *f;
74
75                 /*
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.
79                  *              -- w00t
80                  */
81                 ServerInstance->Log(DEBUG, "xlinedb: Opening temporary database");
82                 f = fopen("xline.db.new", "w");
83                 if (!f)
84                 {
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);
87                         return false;
88                 }
89
90                 ServerInstance->Log(DEBUG, "xlinedb: Opened. Writing..");
91
92                 /*
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..
97                  *              -- w00t
98                  */
99                 fprintf(f, "VERSION 1\n");
100
101                 // Now, let's write.
102                 XLine *line;
103                 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
104                 {
105                         line = (*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);
108                 }
109
110                 ServerInstance->Log(DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
111
112                 int write_error = 0;
113                 write_error = ferror(f);
114                 write_error |= fclose(f);
115                 if (write_error)
116                 {
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);
119                         return false;
120                 }
121
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)
124                 {
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);
127                         return false;
128                 }
129
130                 return true;
131         }
132
133         virtual Version GetVersion()
134         {
135                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
136         }
137 };
138
139 MODULE_INIT(ModuleXLineDB)
140