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