Skip to content

Commit cfcf200

Browse files
committed
chanfix: more updates & persist db
1 parent 32ae692 commit cfcf200

File tree

3 files changed

+407
-188
lines changed

3 files changed

+407
-188
lines changed

modules/third/ChanFix/chanfix.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ class ChanFixTimer final
226226
Gather,
227227
Expire,
228228
Autofix,
229-
Save,
230229
};
231230

232231
ChanFixTimer(Module* owner, ChanFixCore& core, time_t seconds, Kind kind)
@@ -249,9 +248,6 @@ class ChanFixTimer final
249248
case Kind::Autofix:
250249
cf.AutoFixTick();
251250
break;
252-
case Kind::Save:
253-
cf.SaveDB();
254-
break;
255251
}
256252
}
257253

@@ -260,9 +256,39 @@ class ChanFixTimer final
260256
Kind k;
261257
};
262258

259+
class ChanFixDeferredSaveTimer final
260+
: public Timer
261+
{
262+
ChanFixCore& cf;
263+
264+
public:
265+
ChanFixDeferredSaveTimer(Module* creator, time_t timeout, bool dorepeat, ChanFixCore& core)
266+
: Timer(creator, timeout, dorepeat)
267+
, cf(core)
268+
{
269+
}
270+
271+
void Tick() override
272+
{
273+
if (!this->cf.LegacyImportNeedsSave())
274+
{
275+
delete this;
276+
return;
277+
}
278+
279+
if (!Me || !Me->IsSynced())
280+
return;
281+
282+
this->cf.ClearLegacyImportNeedsSave();
283+
Anope::SaveDatabases();
284+
delete this;
285+
}
286+
};
287+
263288
class ChanFix final
264289
: public Module
265290
{
291+
ChanFixChannelDataType chanfixdata_type;
266292
ChanFixCore core;
267293

268294
CommandChanFix cmd_chanfix;
@@ -276,28 +302,25 @@ class ChanFix final
276302
std::unique_ptr<ChanFixTimer> gather;
277303
std::unique_ptr<ChanFixTimer> expire;
278304
std::unique_ptr<ChanFixTimer> autofix;
279-
std::unique_ptr<ChanFixTimer> save;
280305

281306
void RecreateTimers()
282307
{
283308
this->gather.reset();
284309
this->expire.reset();
285310
this->autofix.reset();
286-
this->save.reset();
287311

288312
if (this->core.GetGatherInterval() > 0)
289313
this->gather = std::make_unique<ChanFixTimer>(this, this->core, this->core.GetGatherInterval(), ChanFixTimer::Kind::Gather);
290314
if (this->core.GetExpireInterval() > 0)
291315
this->expire = std::make_unique<ChanFixTimer>(this, this->core, this->core.GetExpireInterval(), ChanFixTimer::Kind::Expire);
292316
if (this->core.GetAutofixInterval() > 0)
293317
this->autofix = std::make_unique<ChanFixTimer>(this, this->core, this->core.GetAutofixInterval(), ChanFixTimer::Kind::Autofix);
294-
if (this->core.GetSaveInterval() > 0)
295-
this->save = std::make_unique<ChanFixTimer>(this, this->core, this->core.GetSaveInterval(), ChanFixTimer::Kind::Save);
296318
}
297319

298320
public:
299321
ChanFix(const Anope::string& modname, const Anope::string& creator)
300322
: Module(modname, creator, VENDOR)
323+
, chanfixdata_type(this)
301324
, core(this)
302325
, cmd_chanfix(this, core)
303326
, cmd_cs_chanfix(this, core)
@@ -314,6 +337,24 @@ class ChanFix final
314337
this->core.OnReload(conf);
315338
this->RecreateTimers();
316339
}
340+
341+
void OnModuleLoad(User*, Module* m) override
342+
{
343+
if (m == this)
344+
{
345+
this->core.LegacyImportIfNeeded();
346+
if (this->core.LegacyImportNeedsSave())
347+
{
348+
if (Me && Me->IsSynced())
349+
{
350+
this->core.ClearLegacyImportNeedsSave();
351+
Anope::SaveDatabases();
352+
}
353+
else
354+
new ChanFixDeferredSaveTimer(this, 1, true, this->core);
355+
}
356+
}
357+
}
317358
};
318359

319360
MODULE_INIT(ChanFix)

modules/third/ChanFix/chanfix.h

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <unordered_map>
1414
#include <vector>
1515

16+
#define CHANFIX_CHANNEL_DATA_TYPE "ChanFixChannel"
17+
1618
struct CFOpRecord final
1719
{
1820
Anope::string account; // empty or "*" means no account
@@ -24,6 +26,13 @@ struct CFOpRecord final
2426
};
2527

2628
struct CFChannelRecord final
29+
{
30+
// Deprecated placeholder. The ChanFix DB is now stored via Anope's
31+
// serialization system using CFChannelData.
32+
};
33+
34+
struct CFChannelData final
35+
: Serializable
2736
{
2837
Anope::string name;
2938
time_t ts = 0;
@@ -42,6 +51,19 @@ struct CFChannelRecord final
4251
time_t nofix_time = 0;
4352

4453
Anope::unordered_map<CFOpRecord> oprecords;
54+
55+
explicit CFChannelData(const Anope::string& chname);
56+
~CFChannelData() override;
57+
};
58+
59+
class ChanFixChannelDataType final
60+
: public Serialize::Type
61+
{
62+
public:
63+
explicit ChanFixChannelDataType(Module* owner);
64+
65+
void Serialize(Serializable* obj, Serialize::Data& data) const override;
66+
Serializable* Unserialize(Serializable* obj, Serialize::Data& data) const override;
4567
};
4668

4769
class ChanFixCore final
@@ -55,7 +77,10 @@ class ChanFixCore final
5577
void GatherTick();
5678
void ExpireTick();
5779
void AutoFixTick();
58-
void SaveDB() const;
80+
81+
void LegacyImportIfNeeded();
82+
bool LegacyImportNeedsSave() const { return this->legacy_import_needs_save; }
83+
void ClearLegacyImportNeedsSave() { this->legacy_import_needs_save = false; }
5984

6085
bool IsAdmin(CommandSource& source) const;
6186
bool IsAuspex(CommandSource& source) const;
@@ -72,14 +97,17 @@ class ChanFixCore final
7297
time_t GetGatherInterval() const { return this->gather_interval; }
7398
time_t GetExpireInterval() const { return this->expire_interval; }
7499
time_t GetAutofixInterval() const { return this->autofix_interval; }
75-
time_t GetSaveInterval() const { return this->save_interval; }
76100

77101
private:
78102
Module* module;
79103
BotInfo* chanfix = nullptr;
104+
bool legacy_import_needs_save = false;
80105

81106
bool do_autofix = false;
82107
bool join_to_fix = false;
108+
bool clear_modes_on_fix = false;
109+
bool clear_bans_on_fix = false;
110+
bool clear_moderated_on_fix = false;
83111

84112
unsigned int op_threshold = 3;
85113
unsigned int min_fix_score = 12;
@@ -92,42 +120,30 @@ class ChanFixCore final
92120
time_t gather_interval = 5 * 60;
93121
time_t expire_interval = 60 * 60;
94122
time_t autofix_interval = 60;
95-
time_t save_interval = 10 * 60;
96123
unsigned int expire_divisor = 672;
97124

98125
char op_status_char = 'o';
99126

100127
Anope::string admin_priv = "chanfix/admin";
101128
Anope::string auspex_priv = "chanfix/auspex";
102129

103-
Anope::unordered_map<CFChannelRecord> channels;
104-
105-
static constexpr const char* DB_MAGIC = "chanfix";
106-
static constexpr unsigned DB_VERSION = 1;
107-
108-
void LoadDB();
109-
Anope::string GetDBPath() const;
110-
111-
static Anope::string EscapeValue(const Anope::string& in);
112-
static Anope::string UnescapeValue(const Anope::string& in);
113-
114130
bool IsRegistered(Channel* c) const;
115131
static bool IsValidChannelName(const Anope::string& name);
116132

117-
CFChannelRecord* GetRecord(const Anope::string& chname);
118-
CFChannelRecord& GetOrCreateRecord(Channel* c);
133+
CFChannelData* GetRecord(const Anope::string& chname);
134+
CFChannelData& GetOrCreateRecord(Channel* c);
119135

120136
unsigned int CountOps(Channel* c) const;
121137
Anope::string KeyForUser(User* u) const;
122-
CFOpRecord* FindRecord(CFChannelRecord& rec, User* u);
123-
void UpdateOpRecord(CFChannelRecord& rec, User* u);
138+
CFOpRecord* FindRecord(CFChannelData& rec, User* u);
139+
bool UpdateOpRecord(CFChannelData& rec, User* u);
124140

125141
unsigned int CalculateScore(const CFOpRecord& orec) const;
126-
unsigned int GetHighScore(const CFChannelRecord& rec) const;
127-
unsigned int GetThreshold(const CFChannelRecord& rec, time_t now) const;
142+
unsigned int GetHighScore(const CFChannelData& rec) const;
143+
unsigned int GetThreshold(const CFChannelData& rec, time_t now) const;
128144

129-
bool ShouldHandle(CFChannelRecord& rec, Channel* c) const;
130-
bool CanStartFix(const CFChannelRecord& rec, Channel* c) const;
131-
bool FixChannel(CFChannelRecord& rec, Channel* c);
145+
bool ShouldHandle(CFChannelData& rec, Channel* c) const;
146+
bool CanStartFix(const CFChannelData& rec, Channel* c) const;
147+
bool FixChannel(CFChannelData& rec, Channel* c);
132148
void ClearBans(Channel* c);
133149
};

0 commit comments

Comments
 (0)