Skip to content

Commit d407d94

Browse files
authored
Refactor metadata DB handling and clean up code
Removed unused variable 'seenaccounts' and related comments. Simplified database replacement logic and added error handling.
1 parent 896fa81 commit d407d94

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

src/modules/m_ircv3_metadata_db.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,12 @@ class ModuleIRCv3MetadataDB final
241241
{
242242
if (persistusers && usermeta && accountapi)
243243
{
244-
std::unordered_set<std::string> seenaccounts;
245244
for (const auto& [_, user] : ServerInstance->Users.GetUsers())
246245
{
247246
const std::string* accountname = accountapi->GetAccountName(user);
248247
if (!accountname || accountname->empty())
249248
continue;
250249

251-
seenaccounts.insert(*accountname);
252-
253250
const std::string internal = GetInternal(user, usermeta);
254251
auto it = userdb.find(*accountname);
255252

@@ -278,9 +275,6 @@ class ModuleIRCv3MetadataDB final
278275
}
279276
}
280277

281-
// Remove entries for accounts that are no longer online if they have no persisted data.
282-
// We keep offline accounts in the DB so metadata persists across reconnects.
283-
(void)seenaccounts;
284278
}
285279

286280
if (persistchans != ChanPolicy::NONE && chanmeta)
@@ -589,18 +583,18 @@ class ModuleIRCv3MetadataDB final
589583
}
590584

591585
#ifdef _WIN32
592-
remove(dbpath.c_str());
586+
remove(dbpath.c_str());
593587
#endif
594-
if (rename(newpath.c_str(), dbpath.c_str()) < 0)
595-
{
596-
ServerInstance->Logs.Critical(MODNAME, "Cannot replace old database \"{}\" with new database \"{}\"! {} ({})", dbpath, newpath, strerror(errno), errno);
597-
ServerInstance->SNO.WriteToSnoMask('a', "database: cannot replace old ircv3 metadata db \"{}\" with new db \"{}\": {} ({})", dbpath, newpath, strerror(errno), errno);
588+
if (rename(newpath.c_str(), dbpath.c_str()) < 0)
589+
{
590+
ServerInstance->Logs.Critical(MODNAME, "Cannot replace old database \"{}\" with new database \"{}\"! {} ({})", dbpath, newpath, strerror(errno), errno);
591+
ServerInstance->SNO.WriteToSnoMask('a', "database: cannot replace old ircv3 metadata db \"{}\" with new db \"{}\": {} ({})", dbpath, newpath, strerror(errno), errno);
598592
std::error_code ec;
599593
std::filesystem::remove(newpath, ec);
600-
return false;
601-
}
594+
return false;
595+
}
602596

603-
return true;
597+
return true;
604598
}
605599

606600
public:
@@ -698,6 +692,28 @@ class ModuleIRCv3MetadataDB final
698692
ApplyUserIfPresent(user, account);
699693
}
700694

695+
void OnMode(User* user, User* usertarget, Channel* chantarget, const Modes::ChangeList& changelist, ModeParser::ModeProcessFlag processflags) override
696+
{
697+
// When +P is set on a channel, apply any persisted metadata from the DB.
698+
if (!chantarget || persistchans != ChanPolicy::PERMANENT)
699+
return;
700+
701+
ModeHandler* pm = ServerInstance->Modes.FindMode('P', MODETYPE_CHANNEL);
702+
if (!pm)
703+
return;
704+
705+
for (const auto& change : changelist.getlist())
706+
{
707+
if (change.mh == pm && change.adding)
708+
{
709+
auto it = chandb.find(chantarget->name);
710+
if (it != chandb.end())
711+
ApplyChannelIfPresent(chantarget->name, it->second);
712+
return;
713+
}
714+
}
715+
}
716+
701717
void OnLoadModule(Module* mod) override
702718
{
703719
// When m_ircv3_metadata is (re)loaded it recreates its extension items,
@@ -805,3 +821,4 @@ class ModuleIRCv3MetadataDB final
805821
};
806822

807823
MODULE_INIT(ModuleIRCv3MetadataDB)
824+

0 commit comments

Comments
 (0)