]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xline_db.cpp
365602b7ebe937518777eb853fcf8dfa43ce2991
[user/henk/code/inspircd.git] / src / modules / m_xline_db.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "xline.h"
23
24 /* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
25
26 class ModuleXLineDB : public Module
27 {
28         std::vector<XLine *> xlines;
29         bool reading_db;                        // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
30                                                 // DO REMEMBER TO SET IT, otherwise it's annoying :P
31  public:
32         ModuleXLineDB()         {
33                 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
34                 ServerInstance->Modules->Attach(eventlist, this, 3);
35
36                 reading_db = true;
37                 ReadDatabase();
38                 reading_db = false;
39         }
40
41         virtual ~ModuleXLineDB()
42         {
43         }
44
45         /** Called whenever an xline is added by a local user.
46          * This method is triggered after the line is added.
47          * @param source The sender of the line or NULL for local server
48          * @param line The xline being added
49          */
50         void OnAddLine(User* source, XLine* line)
51         {
52                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
53                 xlines.push_back(line);
54
55                 if (!reading_db)
56                 {
57                         WriteDatabase();
58                 }
59         }
60
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
65          */
66         void OnDelLine(User* source, XLine* line)
67         {
68                 RemoveLine(line);
69         }
70
71         void OnExpireLine(XLine *line)
72         {
73                 RemoveLine(line);
74         }
75
76         void RemoveLine(XLine *line)
77         {
78                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
79                 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
80                 {
81                         if ((*i) == line)
82                         {
83                                 xlines.erase(i);
84                                 break;
85                         }
86                 }
87
88                 WriteDatabase();
89         }
90
91         bool WriteDatabase()
92         {
93                 FILE *f;
94
95                 /*
96                  * We need to perform an atomic write so as not to fuck things up.
97                  * So, let's write to a temporary file, flush and sync the FD, then rename the file..
98                  * Technically, that means that this can block, but I have *never* seen that.
99                  *              -- w00t
100                  */
101                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
102                 f = fopen("data/xline.db.new", "w");
103                 if (!f)
104                 {
105                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
106                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
107                         return false;
108                 }
109
110                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
111
112                 /*
113                  * Now, much as I hate writing semi-unportable formats, additional
114                  * xline types may not have a conf tag, so let's just write them.
115                  * In addition, let's use a file version, so we can maintain some
116                  * semblance of backwards compatibility for reading on startup..
117                  *              -- w00t
118                  */
119                 fprintf(f, "VERSION 1\n");
120
121                 // Now, let's write.
122                 XLine *line;
123                 for (std::vector<XLine *>::iterator i = xlines.begin(); i != xlines.end(); i++)
124                 {
125                         line = (*i);
126                         fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
127                                 ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
128                 }
129
130                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
131
132                 int write_error = 0;
133                 write_error = ferror(f);
134                 write_error |= fclose(f);
135                 if (write_error)
136                 {
137                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
138                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
139                         return false;
140                 }
141
142                 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
143                 if (rename("data/xline.db.new", "data/xline.db") < 0)
144                 {
145                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
146                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
147                         return false;
148                 }
149
150                 return true;
151         }
152
153         bool ReadDatabase()
154         {
155                 FILE *f;
156                 char linebuf[MAXBUF];
157                 unsigned int lineno = 0;
158
159                 f = fopen("data/xline.db", "r");
160                 if (!f)
161                 {
162                         if (errno == ENOENT)
163                         {
164                                 /* xline.db doesn't exist, fake good return value (we don't care about this) */
165                                 return true;
166                         }
167                         else
168                         {
169                                 /* this might be slightly more problematic. */
170                                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
171                                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
172                                 return false;
173                         }
174                 }
175
176                 while (fgets(linebuf, MAXBUF, f))
177                 {
178                         char *c = linebuf;
179
180                         while (c && *c)
181                         {
182                                 if (*c == '\n')
183                                 {
184                                         *c = '\0';
185                                 }
186
187                                 c++;
188                         }
189                         // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
190                         lineno++;
191
192                         // Inspired by the command parser. :)
193                         irc::tokenstream tokens(linebuf);
194                         int items = 0;
195                         std::string command_p[MAXPARAMETERS];
196                         std::string tmp;
197
198                         while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
199                         {
200                                 command_p[items] = tmp;
201                                 items++;
202                         }
203
204                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
205
206                         if (command_p[0] == "VERSION")
207                         {
208                                 if (command_p[1] == "1")
209                                 {
210                                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
211                                 }
212                                 else
213                                 {
214                                         fclose(f);
215                                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
216                                         ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
217                                         return false;
218                                 }
219                         }
220                         else if (command_p[0] == "LINE")
221                         {
222                                 // Mercilessly stolen from spanningtree
223                                 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
224
225                                 if (!xlf)
226                                 {
227                                         ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
228                                         continue;
229                                 }
230
231                                 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
232                                 xl->SetCreateTime(atoi(command_p[4].c_str()));
233
234                                 if (ServerInstance->XLines->AddLine(xl, NULL))
235                                 {
236                                         ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
237                                 }
238                         }
239                 }
240
241                 fclose(f);
242                 return true;
243         }
244
245
246
247         virtual Version GetVersion()
248         {
249                 return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
250         }
251 };
252
253 MODULE_INIT(ModuleXLineDB)
254