]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xline_db.cpp
m_xline_db Get the xlines from the core, don't maintain a seperate list
[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         bool reading_db;                        // If this is true, addlines are as a result of db reading, so don't bother flushing the db to disk.
29                                                 // DO REMEMBER TO SET IT, otherwise it's annoying :P
30  public:
31         ModuleXLineDB()         {
32                 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine };
33                 ServerInstance->Modules->Attach(eventlist, this, 3);
34
35                 reading_db = true;
36                 ReadDatabase();
37                 reading_db = false;
38         }
39
40         virtual ~ModuleXLineDB()
41         {
42         }
43
44         /** Called whenever an xline is added by a local user.
45          * This method is triggered after the line is added.
46          * @param source The sender of the line or NULL for local server
47          * @param line The xline being added
48          */
49         void OnAddLine(User* source, XLine* line)
50         {
51                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Adding a line");
52
53                 if (!reading_db)
54                 {
55                         WriteDatabase();
56                 }
57         }
58
59         /** Called whenever an xline is deleted.
60          * This method is triggered after the line is deleted.
61          * @param source The user removing the line or NULL for local server
62          * @param line the line being deleted
63          */
64         void OnDelLine(User* source, XLine* line)
65         {
66                 RemoveLine(line);
67         }
68
69         void OnExpireLine(XLine *line)
70         {
71                 RemoveLine(line);
72         }
73
74         void RemoveLine(XLine *line)
75         {
76                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Removing a line");
77                 WriteDatabase();
78         }
79
80         bool WriteDatabase()
81         {
82                 FILE *f;
83
84                 /*
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.
88                  *              -- w00t
89                  */
90                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
91                 f = fopen("data/xline.db.new", "w");
92                 if (!f)
93                 {
94                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
95                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
96                         return false;
97                 }
98
99                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
100
101                 /*
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..
106                  *              -- w00t
107                  */
108                 fprintf(f, "VERSION 1\n");
109
110                 // Now, let's write.
111                 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
112                 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
113                 {
114                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
115                         if (!lookup)
116                                 continue;
117
118                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
119                         {
120                                 XLine *line = i->second;
121                                 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
122                                         ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
123                         }
124                 }
125
126                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
127
128                 int write_error = 0;
129                 write_error = ferror(f);
130                 write_error |= fclose(f);
131                 if (write_error)
132                 {
133                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
134                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
135                         return false;
136                 }
137
138                 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
139                 if (rename("data/xline.db.new", "data/xline.db") < 0)
140                 {
141                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
142                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
143                         return false;
144                 }
145
146                 return true;
147         }
148
149         bool ReadDatabase()
150         {
151                 FILE *f;
152                 char linebuf[MAXBUF];
153                 unsigned int lineno = 0;
154
155                 f = fopen("data/xline.db", "r");
156                 if (!f)
157                 {
158                         if (errno == ENOENT)
159                         {
160                                 /* xline.db doesn't exist, fake good return value (we don't care about this) */
161                                 return true;
162                         }
163                         else
164                         {
165                                 /* this might be slightly more problematic. */
166                                 ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
167                                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
168                                 return false;
169                         }
170                 }
171
172                 while (fgets(linebuf, MAXBUF, f))
173                 {
174                         char *c = linebuf;
175
176                         while (c && *c)
177                         {
178                                 if (*c == '\n')
179                                 {
180                                         *c = '\0';
181                                 }
182
183                                 c++;
184                         }
185                         // Smart man might think of initing to 1, and moving this to the bottom. Don't. We use continue in this loop.
186                         lineno++;
187
188                         // Inspired by the command parser. :)
189                         irc::tokenstream tokens(linebuf);
190                         int items = 0;
191                         std::string command_p[MAXPARAMETERS];
192                         std::string tmp;
193
194                         while (tokens.GetToken(tmp) && (items < MAXPARAMETERS))
195                         {
196                                 command_p[items] = tmp;
197                                 items++;
198                         }
199
200                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Processing %s", linebuf);
201
202                         if (command_p[0] == "VERSION")
203                         {
204                                 if (command_p[1] == "1")
205                                 {
206                                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
207                                 }
208                                 else
209                                 {
210                                         fclose(f);
211                                         ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
212                                         ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
213                                         return false;
214                                 }
215                         }
216                         else if (command_p[0] == "LINE")
217                         {
218                                 // Mercilessly stolen from spanningtree
219                                 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
220
221                                 if (!xlf)
222                                 {
223                                         ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
224                                         continue;
225                                 }
226
227                                 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
228                                 xl->SetCreateTime(atoi(command_p[4].c_str()));
229
230                                 if (ServerInstance->XLines->AddLine(xl, NULL))
231                                 {
232                                         ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
233                                 }
234                         }
235                 }
236
237                 fclose(f);
238                 return true;
239         }
240
241
242
243         virtual Version GetVersion()
244         {
245                 return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
246         }
247 };
248
249 MODULE_INIT(ModuleXLineDB)
250