1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd: (C) 2002-2011 InspIRCd Development Team
* See: http://wiki.inspircd.org/Credits
*
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
#include "inspircd.h"
#include "protocol.h"
/******************************************************************************
* Flat-file database read/write
******************************************************************************/
/* structure for single entry */
struct EntryDescriptor
{
std::string name, ts, topicset, topicsetby, topic, modes;
};
/** reads the channel database and returns structure with it */
class DatabaseReader
{
/* the entry descriptor */
EntryDescriptor entry;
/* file */
FILE *fd;
public:
/* constructor, opens the file */
DatabaseReader (std::string filename)
{
/* initialize */
fd = NULL;
/* open the file */
fd = fopen (filename.c_str ( ), "r");
/* if we can't open the file, return. */
if (!fd) return;
}
/* destructor will close the file */
~DatabaseReader ( )
{
/* if fd is not null, close it */
if (fd) fclose (fd);
}
/* get next entry */
EntryDescriptor *next ( )
{
/* if fd is NULL, fake eof */
if (!fd) return 0;
/* clear data */
entry.name = "";
entry.ts = "";
entry.topicset = "";
entry.topicsetby = "";
entry.modes = "";
entry.topic = "";
std::string str;
/* read single characters from the file and add them to the string, end at EOF or \n, ignore \r */
while (1)
{
int c = fgetc (fd);
if ((c == EOF) || ((unsigned char)c == '\n')) break;
if ((unsigned char)c == '\r') continue;
str.push_back ((unsigned char)c);
}
/* ready to parse the line */
if (str == "") return 0;
std::string token;
irc::spacesepstream sep(str, false);
/* get first one */
/* malformed if it is not chaninfo */
if (!sep.GetToken (token) || token != "chaninfo")
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed channel database");
return 0;
}
/* okay, read channel name */
/* name was get, but if it's the last token, database is malformed */
if (!sep.GetToken (token))
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed channel database");
return 0;
}
/* save name */
entry.name = token;
/* set channel timestamp */
sep.GetToken (entry.ts);
/* initial entry read, read next lines in a loop until end, eof means malformed database again */
while (1)
{
str.clear ( );
/* read the line */
while (1)
{
int c = fgetc (fd);
if (c == EOF)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed channel database");
return 0;
}
unsigned char c2 = (unsigned char)c;
if (c2 == '\n') break;
if (c2 == '\r') continue;
str.push_back (c2);
}
irc::spacesepstream sep2(str, false);
/* get the token */
if (str == "")
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed channel database");
return 0;
}
sep2.GetToken (token);
/* break the loop if token is "end" */
if (token == "end") break;
/* it is not, so the large if statement there */
else if (token == "topic")
{
/* topic info, if end then it is malformed */
if (sep2.StreamEnd ( ))
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed topic declaration in channel database for channel %s", entry.name.c_str ( ));
}
/* if not, then read topic set time */
/* if end then malformed */
if (!sep2.GetToken (token))
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed topic declaration in channel database for channel %s", entry.name.c_str ( ));
}
/* save that */
entry.topicset = token;
/* get next token */
/* if last then malformed */
if (!sep2.GetToken (token))
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed topic declaration in channel database for channel %s", entry.name.c_str ( ));
}
entry.topicsetby = token;
/* get rest of the declaration into the topic string, this is a topic */
entry.topic = sep2.GetRemaining ( );
} else if (token == "modes")
{
/* modes token, used for representing modes, it's the easier one, just load remaining data */
if (sep2.StreamEnd ( ))
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "malformed modes declaration in channel database for channel %s", entry.name.c_str ( ));
continue;
}
entry.modes = sep2.GetRemaining ( );
}
}
/* return entry address */
return &entry;
}
};
/* class being a database writer, gets a database file name on construct */
class DatabaseWriter
{
/* file stream */
FILE *fd;
std::string dbname, tmpname;
/* public */
public:
/* constructor */
DatabaseWriter (std::string filename)
{
fd = NULL;
dbname = filename;
tmpname = filename + ".tmp";
/* ready, open temporary database */
fd = fopen (tmpname.c_str ( ), "w");
if (!fd)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "cannot save to the channel database");
}
}
/* destructor */
~DatabaseWriter ( )
{
if (fd)
{
/* saving has been ended, close the database and flush buffers */
fclose (fd);
/* rename the database file */
if (rename (tmpname.c_str ( ), dbname.c_str ( )) == -1)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "can't rename the database file");
}
}
}
/* save the single channel */
void next (Channel *chan)
{
if (!fd) return;
/* first, construct the chaninfo line */
std::string line;
line.append ("chaninfo ").append (chan->name).append (" ").append (ConvToStr(chan->age)).append ("\n");
if (fputs (line.c_str ( ), fd) == EOF)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "unable to write the channel entry");
fclose (fd);
fd = NULL;
return;
}
/* now, write the topic if present */
if (!chan->topic.empty ( ))
{
line.clear ( );
line.append ("topic ").append (ConvToStr (chan->topicset)).append (" ").append (chan->setby).append (" ").append (chan->topic).append ("\n");
if (fputs (line.c_str ( ), fd) == EOF)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "unable to write channel's topic declaration");
fclose (fd);
fd = NULL;
return;
}
}
/* get all channel modes */
line.clear ( );
irc::modestacker ms;
chan->ChanModes (ms, MODELIST_FULL);
line.append ("modes ").append (ms.popModeLine (FORMAT_PERSIST, INT_MAX, INT_MAX)).append ("\nend\n");
if (fputs (line.c_str ( ), fd) == EOF)
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "can't write mode declaration");
fclose (fd);
fd = NULL;
return;
}
}
};
class FlatFileChannelDB : public Module
{
private:
std::string filedb;
bool dirty; // filedb needs to be flushed to disk
bool storeregistered, storepermanent;
/** Whether or not to store a channel to the database */
bool ShouldStoreChannel(Channel* c)
{
return (storeregistered && c->IsModeSet("registered")) || (storepermanent && c->IsModeSet("permanent"));
}
void WriteFileDatabase ( )
{
// Dump entire database; open/close in constructor/destructor
DatabaseWriter db (filedb);
for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
{
if (ShouldStoreChannel(i->second))
db.next(i->second);
}
}
/** Read flat-file database */
void ReadFileDatabase ( )
{
/* create the reader object and open the database */
DatabaseReader db (filedb);
/* start the database read loop */
EntryDescriptor *entry;
while ((entry = db.next ( )))
{
/* we get a database entry */
/* so if channel name doesn't start with #, give an error and continue */
if (entry->name[0] != '#')
{
ServerInstance->Logs->Log ("MODULE", DEFAULT, "invalid channel name in channel database");
continue;
}
/* entry is valid */
/* try to find the channel */
Channel *chan = ServerInstance->FindChan (entry->name);
time_t ourTS = atol (entry->ts.c_str ( ));
/* now, things that will be done depends on if channel was found or not */
if (!chan)
{
/* channel does not exist, allocate it, it will be constructed and added to the network as an empty channel */
chan = new Channel (entry->name, ourTS);
/* empty modeless channel added and allocated */
}
else if(chan->age > ourTS)
{
chan = Channel::Nuke(chan, entry->name, ourTS);
}
else if(chan->age < ourTS)
{
continue; // there's a channel older than the one we want to load, so we don't load it
}
/* so, channel exists in the network, then if our topic is newer than the current, set data
* if no topic was ever set, topicset is 0 */
time_t topicTS = atol (entry->topicset.c_str ( ));
if (topicTS && topicTS >= chan->topicset)
{
chan->topic = entry->topic;
chan->topicset = atol (entry->topicset.c_str ( ));
chan->setby = entry->topicsetby;
chan->WriteChannelWithServ(chan->setby, "TOPIC %s :%s", chan->name.c_str(), chan->topic.c_str());
}
/* if modestring is not empty, then set all modes in it */
if (!entry->modes.empty ( ))
{
/* create sepstream */
irc::spacesepstream sep(entry->modes);
/* create modestacker for stacking all mode changes */
irc::modestacker ms;
/* spacesepstream is used to iterate through all modes that were saved for a channel */
/* modestacker ms is used to stack them */
/* iterate through the mode list */
while (!sep.StreamEnd ( ))
{
/* get the mode to be applied */
std::string token, name, value;
sep.GetToken (token);
/* the mode looks like name=value unless it doesn't have any value then it's just name */
/* find the position of = sign */
size_t namepos = token.find ('=');
/* if we didn't find anything, set name to the token because mode is... valueless */
if (namepos == std::string::npos) name = token;
/* if it found that = sign, we have both name and value */
else
{
/* it found it, name is string before the character, value is the string after it */
name = token.substr (0, namepos);
value = token.substr (namepos + 1);
}
/* mode name and value found, we can now add it to the modestacker */
/* hmm, or we can't, reason is that letters are separate in user and channel modes, but mode names are not, so what if this thing we're setting is
an user mode? */
/* to check that, we must find the mode */
ModeHandler *mc = ServerInstance->Modes->FindMode (name);
/* those actions will be taken only if mode was found and if it's the channel mode */
if (mc && mc->GetModeType ( ) == MODETYPE_CHANNEL)
{
/* mode was found and is not an user mode but a channel mode, push it to the modestacker, we use id instead of name for quicker push because we
have the mode handler now */
ms.push (irc::modechange (mc->id, value));
}
}
/* okay, all modes were pushed, modestacker can now produce the mode line */
/* so, process all mode changes and apply them on a channel as a merge */
ServerInstance->Modes->Process (ServerInstance->FakeClient, chan, ms, true);
ServerInstance->Modes->Send (ServerInstance->FakeClient, chan, ms);
ServerInstance->PI->SendMode (ServerInstance->FakeClient, chan, ms, true);
}
}
}
public:
/* module constructor, for initializing stuff */
FlatFileChannelDB() : dirty(true)
{
}
/* get module version and flags. */
Version GetVersion()
{
return Version("Provides channel database storage in a flat-file format", VF_VENDOR);
}
void init ( )
{
Implementation eventlist[] = {
I_OnBackgroundTimer, I_OnMode, I_OnPostTopicChange
};
ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
ReadFileDatabase();
}
/* rehash event */
void ReadConfig(ConfigReadStatus& status)
{
ConfigTag *tag = ServerInstance->Config->GetTag ("chandb");
filedb = tag->getString ("dbfile");
storeregistered = tag->getBool("storeregistered", true);
storepermanent = tag->getBool("storepermanent", false);
}
/* called on background timer to write all channels to disk if they were changed */
void OnBackgroundTimer (time_t cur)
{
/* if not dirty then don't do anything */
if (!dirty)
return;
/* dirty, one of registered channels was changed, save it */
WriteFileDatabase();
/* clear dirty to prevent next savings */
dirty = false;
}
/* after the topic has been changed, call this to check if channel was registered */
void OnPostTopicChange (User *user, Channel *chan, const std::string &topic)
{
if (ShouldStoreChannel(chan)) dirty = true;
}
/* this event is called after each modechange */
void OnMode (User *user, Extensible *target, const irc::modestacker &modes)
{
Channel* chan = IS_CHANNEL(target);
if (chan && ShouldStoreChannel(chan)) dirty = true;
}
void Prioritize()
{
// database reading may depend on channel modes being loaded
ServerInstance->Modules->SetPriority(this, I_ModuleInit, PRIORITY_LAST);
}
};
/* register the module */
MODULE_INIT(FlatFileChannelDB)
|