]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xline_db.cpp
Add method for writing server notices.
[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 /* $ModConfig: <xlinedb filename="data/xline.db">
25  *  Specify the filename for the xline database here*/
26 /* $ModDesc: Keeps a dynamic log of all XLines created, and stores them in a seperate conf file (xline.db). */
27
28 class ModuleXLineDB : public Module
29 {
30         bool dirty;
31         std::string xlinedbpath;
32  public:
33         void init()
34         {
35                 /* Load the configuration
36                  * Note:
37                  *              this is on purpose not in the OnRehash() method. It would be non-trivial to change the database on-the-fly.
38                  *              Imagine a scenario where the new file already exists. Merging the current XLines with the existing database is likely a bad idea
39                  *              ...and so is discarding all current in-memory XLines for the ones in the database.
40                  */
41                 ConfigTag* Conf = ServerInstance->Config->ConfValue("xlinedb");
42                 xlinedbpath = Conf->getString("filename", DATA_PATH "/xline.db");
43
44                 // Read xlines before attaching to events
45                 ReadDatabase();
46
47                 Implementation eventlist[] = { I_OnAddLine, I_OnDelLine, I_OnExpireLine, I_OnBackgroundTimer };
48                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
49                 dirty = false;
50         }
51
52         /** Called whenever an xline is added by a local user.
53          * This method is triggered after the line is added.
54          * @param source The sender of the line or NULL for local server
55          * @param line The xline being added
56          */
57         void OnAddLine(User* source, XLine* line)
58         {
59                 dirty = true;
60         }
61
62         /** Called whenever an xline is deleted.
63          * This method is triggered after the line is deleted.
64          * @param source The user removing the line or NULL for local server
65          * @param line the line being deleted
66          */
67         void OnDelLine(User* source, XLine* line)
68         {
69                 dirty = true;
70         }
71
72         void OnExpireLine(XLine *line)
73         {
74                 dirty = true;
75         }
76
77         void OnBackgroundTimer(time_t now)
78         {
79                 if (dirty)
80                 {
81                         if (WriteDatabase())
82                                 dirty = false;
83                 }
84         }
85
86         bool WriteDatabase()
87         {
88                 FILE *f;
89
90                 /*
91                  * We need to perform an atomic write so as not to fuck things up.
92                  * So, let's write to a temporary file, flush and sync the FD, then rename the file..
93                  * Technically, that means that this can block, but I have *never* seen that.
94                  *              -- w00t
95                  */
96                 ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Opening temporary database");
97                 std::string xlinenewdbpath = xlinedbpath + ".new";
98                 f = fopen(xlinenewdbpath.c_str(), "w");
99                 if (!f)
100                 {
101                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
102                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
103                         return false;
104                 }
105
106                 ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Opened. Writing..");
107
108                 /*
109                  * Now, much as I hate writing semi-unportable formats, additional
110                  * xline types may not have a conf tag, so let's just write them.
111                  * In addition, let's use a file version, so we can maintain some
112                  * semblance of backwards compatibility for reading on startup..
113                  *              -- w00t
114                  */
115                 fprintf(f, "VERSION 1\n");
116
117                 // Now, let's write.
118                 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
119                 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
120                 {
121                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
122                         if (!lookup)
123                                 continue; // Not possible as we just obtained the list from XLineManager
124
125                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
126                         {
127                                 XLine* line = i->second;
128                                 fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
129                                         ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
130                         }
131                 }
132
133                 ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
134
135                 int write_error = 0;
136                 write_error = ferror(f);
137                 write_error |= fclose(f);
138                 if (write_error)
139                 {
140                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
141                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
142                         return false;
143                 }
144
145 #ifdef _WIN32
146                 if (remove(xlinedbpath.c_str()))
147                 {
148                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Cannot remove old database! %s (%d)", strerror(errno), errno);
149                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
150                         return false;
151                 }
152 #endif
153                 // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
154                 if (rename(xlinenewdbpath.c_str(), xlinedbpath.c_str()) < 0)
155                 {
156                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
157                         ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
158                         return false;
159                 }
160
161                 return true;
162         }
163
164         bool ReadDatabase()
165         {
166                 FILE *f;
167                 char linebuf[MAXBUF];
168
169                 f = fopen(xlinedbpath.c_str(), "r");
170                 if (!f)
171                 {
172                         if (errno == ENOENT)
173                         {
174                                 /* xline.db doesn't exist, fake good return value (we don't care about this) */
175                                 return true;
176                         }
177                         else
178                         {
179                                 /* this might be slightly more problematic. */
180                                 ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Cannot read database! %s (%d)", strerror(errno), errno);
181                                 ServerInstance->SNO->WriteToSnoMask('a', "database: cannot read db: %s (%d)", strerror(errno), errno);
182                                 return false;
183                         }
184                 }
185
186                 while (fgets(linebuf, MAXBUF, f))
187                 {
188                         char *c = linebuf;
189
190                         while (c && *c)
191                         {
192                                 if (*c == '\n')
193                                 {
194                                         *c = '\0';
195                                 }
196
197                                 c++;
198                         }
199
200                         // Inspired by the command parser. :)
201                         irc::tokenstream tokens(linebuf);
202                         int items = 0;
203                         std::string command_p[7];
204                         std::string tmp;
205
206                         while (tokens.GetToken(tmp) && (items < 7))
207                         {
208                                 command_p[items] = tmp;
209                                 items++;
210                         }
211
212                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Processing %s", linebuf);
213
214                         if (command_p[0] == "VERSION")
215                         {
216                                 if (command_p[1] == "1")
217                                 {
218                                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: Reading db version %s", command_p[1].c_str());
219                                 }
220                                 else
221                                 {
222                                         fclose(f);
223                                         ServerInstance->Logs->Log("m_xline_db",LOG_DEBUG, "xlinedb: I got database version %s - I don't understand it", command_p[1].c_str());
224                                         ServerInstance->SNO->WriteToSnoMask('a', "database: I got a database version (%s) I don't understand", command_p[1].c_str());
225                                         return false;
226                                 }
227                         }
228                         else if (command_p[0] == "LINE")
229                         {
230                                 // Mercilessly stolen from spanningtree
231                                 XLineFactory* xlf = ServerInstance->XLines->GetFactory(command_p[1]);
232
233                                 if (!xlf)
234                                 {
235                                         ServerInstance->SNO->WriteToSnoMask('a', "database: Unknown line type (%s).", command_p[1].c_str());
236                                         continue;
237                                 }
238
239                                 XLine* xl = xlf->Generate(ServerInstance->Time(), atoi(command_p[5].c_str()), command_p[3], command_p[6], command_p[2]);
240                                 xl->SetCreateTime(atoi(command_p[4].c_str()));
241
242                                 if (ServerInstance->XLines->AddLine(xl, NULL))
243                                 {
244                                         ServerInstance->SNO->WriteToSnoMask('x', "database: Added a line of type %s", command_p[1].c_str());
245                                 }
246                                 else
247                                         delete xl;
248                         }
249                 }
250
251                 fclose(f);
252                 return true;
253         }
254
255         virtual Version GetVersion()
256         {
257                 return Version("Keeps a dynamic log of all XLines created, and stores them in a separate conf file (xline.db).", VF_VENDOR);
258         }
259 };
260
261 MODULE_INIT(ModuleXLineDB)