This commit is contained in:
Daniel Schmitz 2021-06-22 19:27:05 +08:00
parent 31a26e99f4
commit 46345aaf15

View file

@ -10,7 +10,6 @@ class CIdleRPGMod;
class CIdleRPGChannel { class CIdleRPGChannel {
public: public:
CIdleRPGChannel() {} CIdleRPGChannel() {}
CIdleRPGChannel(const CString& sLine) { FromString(sLine); }
virtual ~CIdleRPGChannel() {} virtual ~CIdleRPGChannel() {}
const CString& GetChannel() const { return m_Channel; } const CString& GetChannel() const { return m_Channel; }
@ -43,7 +42,7 @@ class CIdleRPGChannel {
class CIdleRPGModTimer : public CTimer { class CIdleRPGModTimer : public CTimer {
public: public:
CIdleRPGModTimer(CModule* pModule, CIdleRPGChannel* sChannel, CIdleRPGModTimer(CModule* pModule, CIdleRPGChannel& sChannel,
unsigned int uInterval, unsigned int uCycles, unsigned int uInterval, unsigned int uCycles,
const CString& sLabel, const CString& sDescription) const CString& sLabel, const CString& sDescription)
: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) { : CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {
@ -56,7 +55,7 @@ class CIdleRPGModTimer : public CTimer {
void RunJob() override; void RunJob() override;
private: private:
CIdleRPGChannel* pChannel; CIdleRPGChannel pChannel;
}; };
class CIdleRPGMod : public CModule { class CIdleRPGMod : public CModule {
@ -82,28 +81,22 @@ class CIdleRPGMod : public CModule {
~CIdleRPGMod() override {} ~CIdleRPGMod() override {}
bool OnLoad(const CString& sArgs, CString& sMessage) override { bool OnLoad(const CString& sArgs, CString& sMessage) override {
m_Channels.empty();
// Load current channels // Load current channels
for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) { for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
const CString& sLine = it->second; const CString& sLine = it->second;
CIdleRPGChannel* pChannel = new CIdleRPGChannel; CIdleRPGChannel pChannel;
auto search = m_Channels.find(pChannel.GetChannel());
if (!pChannel->FromString(sLine) || if (pChannel.FromString(sLine) && search == m_Channels.end()) {
FindChannel(pChannel->GetChannel())) { m_Channels[pChannel.GetChannel()] = pChannel;
delete pChannel;
} else {
m_Channels[pChannel->GetChannel()] = pChannel;
} }
} }
return true; return true;
} }
CIdleRPGChannel* FindChannel(const CString& sChannel) {
map<CString, CIdleRPGChannel*>::iterator it = m_Channels.find(sChannel);
return (it != m_Channels.end()) ? it->second : nullptr;
}
void CommandList() { void CommandList() {
if (m_Channels.empty()) { if (m_Channels.empty()) {
PutModule(t_s("No channels setup yet. Try: help add")); PutModule(t_s("No channels setup yet. Try: help add"));
@ -118,10 +111,10 @@ class CIdleRPGMod : public CModule {
for (const auto& it : m_Channels) { for (const auto& it : m_Channels) {
Table.AddRow(); Table.AddRow();
Table.SetCell(t_s("Channel"), it.second->GetChannel()); Table.SetCell(t_s("Channel"), it.second.GetChannel());
Table.SetCell(t_s("Botnick"), it.second->GetBotnick()); Table.SetCell(t_s("Botnick"), it.second.GetBotnick());
Table.SetCell(t_s("Username"), it.second->GetUsername()); Table.SetCell(t_s("Username"), it.second.GetUsername());
Table.SetCell(t_s("Password"), it.second->GetPassword()); Table.SetCell(t_s("Password"), it.second.GetPassword());
} }
PutModule(Table); PutModule(Table);
@ -156,14 +149,13 @@ class CIdleRPGMod : public CModule {
return; return;
} }
CIdleRPGChannel* pChannel = new CIdleRPGChannel(); CIdleRPGChannel pChannel;
pChannel->FromString(sChannel + " " + sBotnick + " " + sUsername + " " + pChannel.FromString(sChannel + " " + sBotnick + " " + sUsername + " " +
sPassword); sPassword);
m_Channels[pChannel->GetChannel()] = pChannel; m_Channels[pChannel.GetChannel()] = pChannel;
SetNV(pChannel->GetChannel(), pChannel->ToString()); SetNV(pChannel.GetChannel(), pChannel.ToString());
PutModule( PutModule(t_f("Saved settings for channel {1}")(pChannel.GetChannel()));
t_f("Saved settings for channel {1}")(pChannel->GetChannel()));
} }
void CommandDel(const CString& sLine) { void CommandDel(const CString& sLine) {
@ -173,15 +165,14 @@ class CIdleRPGMod : public CModule {
} }
CString sChannel = sLine.Token(1).AsLower(); CString sChannel = sLine.Token(1).AsLower();
map<CString, CIdleRPGChannel*>::iterator it = m_Channels.find(sChannel); auto search = m_Channels.find(sChannel);
if (it == m_Channels.end()) { if (search == m_Channels.end()) {
PutModule(t_f("Channel {1} not found")(sChannel)); PutModule(t_f("Channel {1} not found")(sChannel));
return; return;
} }
delete it->second; m_Channels.erase(search);
m_Channels.erase(it);
DelNV(sChannel); DelNV(sChannel);
PutModule(t_f("Channel {1} removed")(sChannel)); PutModule(t_f("Channel {1} removed")(sChannel));
} }
@ -189,19 +180,22 @@ class CIdleRPGMod : public CModule {
void CommandLogin(const CString& sLine) { void CommandLogin(const CString& sLine) {
CString sChannel = sLine.Token(1).AsLower(); CString sChannel = sLine.Token(1).AsLower();
if (!sChannel.empty()) { if (!sChannel.empty()) {
CIdleRPGChannel* fChannel = FindChannel(sChannel); auto search = m_Channels.find(sChannel);
if (!fChannel) {
if (search == m_Channels.end()) {
PutModule(t_f("Invalid channel {1}")(sChannel)); PutModule(t_f("Invalid channel {1}")(sChannel));
return; return;
} }
CIdleRPGChannel fChannel = search->second;
Login(fChannel); Login(fChannel);
return; return;
} }
// Go through all channels and login // Go through all channels and login
for (const auto& it : m_Channels) { for (const auto& it : m_Channels) {
Login(it.second); CIdleRPGChannel fChannel = it.second;
Login(fChannel);
} }
} }
@ -211,29 +205,29 @@ class CIdleRPGMod : public CModule {
PutModule(t_s("All settings cleared!")); PutModule(t_s("All settings cleared!"));
} }
void QueueLogin(CIdleRPGChannel* sChan) { void QueueLogin(CIdleRPGChannel& sChan) {
RemTimer("idlerpg_login_timer_" + sChan->GetChannel()); RemTimer("idlerpg_login_timer_" + sChan.GetChannel());
AddTimer( AddTimer(
new CIdleRPGModTimer(this, sChan, IDLERPG_JOIN_LOGIN_WAIT_TIME, 1, new CIdleRPGModTimer(this, sChan, IDLERPG_JOIN_LOGIN_WAIT_TIME, 1,
"idlerpg_login_timer_" + sChan->GetChannel(), "idlerpg_login_timer_" + sChan.GetChannel(),
"Tries login to IdleRPG bot")); "Tries login to IdleRPG bot"));
} }
void Login(CIdleRPGChannel* sChan) { void Login(CIdleRPGChannel& sChan) {
// Valid channel? // Valid channel?
CChan* pChan = this->GetNetwork()->FindChan(sChan->GetChannel()); CChan* pChan = this->GetNetwork()->FindChan(sChan.GetChannel());
if (!pChan) { if (!pChan) {
PutModule(t_f("Error logging in: Invalid channel [{1}]")( PutModule(t_f("Error logging in: Invalid channel [{1}]")(
sChan->GetChannel())); sChan.GetChannel()));
return; return;
} }
// Botnick on channel? // Botnick on channel?
CNick* pBot = pChan->FindNick(sChan->GetBotnick()); CNick* pBot = pChan->FindNick(sChan.GetBotnick());
if (!pBot) { if (!pBot) {
PutModule( PutModule(
t_f("Error logging in: Bot [{1}] not found in channel [{2}]")( t_f("Error logging in: Bot [{1}] not found in channel [{2}]")(
sChan->GetBotnick(), sChan->GetChannel())); sChan.GetBotnick(), sChan.GetChannel()));
return; return;
} }
@ -241,14 +235,14 @@ class CIdleRPGMod : public CModule {
if (!pBot->HasPerm(CChan::Op)) { if (!pBot->HasPerm(CChan::Op)) {
PutModule(t_f( PutModule(t_f(
"Error logging in: Bot [{1}] not operator in in channel [{2}]")( "Error logging in: Bot [{1}] not operator in in channel [{2}]")(
sChan->GetBotnick(), sChan->GetChannel())); sChan.GetBotnick(), sChan.GetChannel()));
return; return;
} }
PutIRC("PRIVMSG " + sChan->GetBotnick() + " :login " + PutIRC("PRIVMSG " + sChan.GetBotnick() + " :login " +
sChan->GetUsername() + " " + sChan->GetPassword()); sChan.GetUsername() + " " + sChan.GetPassword());
PutModule(t_s("Logging you in with " + sChan->GetBotnick() + " on " + PutModule(t_s("Logging you in with " + sChan.GetBotnick() + " on " +
sChan->GetChannel())); sChan.GetChannel()));
} }
void OnJoin(const CNick& Nick, CChan& Channel) override { void OnJoin(const CNick& Nick, CChan& Channel) override {
@ -258,13 +252,15 @@ class CIdleRPGMod : public CModule {
} }
// Correct channel? // Correct channel?
CIdleRPGChannel* fChannel = FindChannel(Channel.GetName().AsLower()); auto search = m_Channels.find(Channel.GetName().AsLower());
if (!fChannel) { if (search == m_Channels.end()) {
return; return;
} }
CIdleRPGChannel fChannel = search->second;
// Either Bot or user joins // Either Bot or user joins
if (Nick.GetNick() != fChannel->GetBotnick() && if (Nick.GetNick() != fChannel.GetBotnick() &&
!GetNetwork()->GetCurNick().Equals(Nick.GetNick())) { !GetNetwork()->GetCurNick().Equals(Nick.GetNick())) {
return; return;
} }
@ -273,7 +269,7 @@ class CIdleRPGMod : public CModule {
} }
private: private:
map<CString, CIdleRPGChannel*> m_Channels; map<CString, CIdleRPGChannel> m_Channels;
}; };
void CIdleRPGModTimer::RunJob() { void CIdleRPGModTimer::RunJob() {