clang-format

This commit is contained in:
Daniel Schmitz 2021-06-22 17:21:31 +08:00
parent 6e3b1f8e7b
commit 31a26e99f4

View file

@ -1,5 +1,5 @@
#include <znc/IRCNetwork.h>
#include <znc/Chan.h> #include <znc/Chan.h>
#include <znc/IRCNetwork.h>
using std::map; using std::map;
@ -8,55 +8,47 @@ class CIdleRPGMod;
#define IDLERPG_JOIN_LOGIN_WAIT_TIME 10 #define IDLERPG_JOIN_LOGIN_WAIT_TIME 10
class CIdleRPGChannel { class CIdleRPGChannel {
public: public:
CIdleRPGChannel() {} CIdleRPGChannel() {}
CIdleRPGChannel(const CString& sLine) { CIdleRPGChannel(const CString& sLine) { FromString(sLine); }
FromString(sLine); virtual ~CIdleRPGChannel() {}
}
virtual ~CIdleRPGChannel() {}
const CString& GetChannel() const { const CString& GetChannel() const { return m_Channel; }
return m_Channel; const CString& GetBotnick() const { return m_Botnick; }
} const CString& GetUsername() const { return m_Username; }
const CString& GetBotnick() const { const CString& GetPassword() const { return m_Password; }
return m_Botnick;
}
const CString& GetUsername() const {
return m_Username;
}
const CString& GetPassword() const {
return m_Password;
}
bool FromString(const CString& sLine) { bool FromString(const CString& sLine) {
m_Channel = sLine.Token(0).AsLower(); m_Channel = sLine.Token(0).AsLower();
m_Botnick = sLine.Token(1); m_Botnick = sLine.Token(1);
m_Username = sLine.Token(2); m_Username = sLine.Token(2);
m_Password = sLine.Token(3); m_Password = sLine.Token(3);
return !m_Channel.empty() && !m_Botnick.empty() && !m_Username.empty() && !m_Password.empty(); return !m_Channel.empty() && !m_Botnick.empty() &&
} !m_Username.empty() && !m_Password.empty();
}
CString ToString() const { CString ToString() const {
return GetChannel() + " " + GetBotnick() + " " + GetUsername() + " " + GetPassword(); return GetChannel() + " " + GetBotnick() + " " + GetUsername() + " " +
} GetPassword();
}
private: private:
protected: protected:
CString m_Channel; CString m_Channel;
CString m_Botnick; CString m_Botnick;
CString m_Username; CString m_Username;
CString m_Password; CString m_Password;
}; };
class CIdleRPGModTimer : public CTimer { class CIdleRPGModTimer : public CTimer {
public: public:
CIdleRPGModTimer(CModule* pModule, CIdleRPGChannel* sChannel, unsigned int uInterval, CIdleRPGModTimer(CModule* pModule, CIdleRPGChannel* sChannel,
unsigned int uCycles, const CString& sLabel, unsigned int uInterval, unsigned int uCycles,
const CString& sDescription) const CString& sLabel, const CString& sDescription)
: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) { : CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {
pChannel = sChannel; pChannel = sChannel;
} }
~CIdleRPGModTimer() override {} ~CIdleRPGModTimer() override {}
@ -64,211 +56,236 @@ class CIdleRPGModTimer : public CTimer {
void RunJob() override; void RunJob() override;
private: private:
CIdleRPGChannel* pChannel; CIdleRPGChannel* pChannel;
}; };
class CIdleRPGMod : public CModule { class CIdleRPGMod : public CModule {
public: public:
MODCONSTRUCTOR(CIdleRPGMod) { MODCONSTRUCTOR(CIdleRPGMod) {
AddHelpCommand(); AddHelpCommand();
AddCommand("Add", t_d("<#channel> <botnick> <username> <password>"), t_d("Adds a new channel or updates a channel"), [=](const CString& sLine) { CommandAdd(sLine); }); AddCommand("Add", t_d("<#channel> <botnick> <username> <password>"),
AddCommand("Del", t_d("<#channel>"), t_d("Removes a channel"), [=](const CString& sLine) { CommandDel(sLine); }); t_d("Adds a new channel or updates a channel"),
AddCommand("Clear", "", t_d("Clears all current settings"), [=](const CString& sLine) { CommandClear(); }); [=](const CString& sLine) { CommandAdd(sLine); });
AddCommand("List", "", t_d("Displays current settings"), [=](const CString& sLine) { CommandList(); }); AddCommand("Del", t_d("<#channel>"), t_d("Removes a channel"),
AddCommand("Login", t_d("[<#channel>]"), t_d("Manually triggers login now (either for specific channel or all channels)"), [=](const CString& sLine) { CommandLogin(sLine); }); [=](const CString& sLine) { CommandDel(sLine); });
} AddCommand("Clear", "", t_d("Clears all current settings"),
[=](const CString& sLine) { CommandClear(); });
AddCommand("List", "", t_d("Displays current settings"),
[=](const CString& sLine) { CommandList(); });
AddCommand("Login", t_d("[<#channel>]"),
t_d("Manually triggers login now (either for specific "
"channel or all channels)"),
[=](const CString& sLine) { CommandLogin(sLine); });
}
~CIdleRPGMod() override {} ~CIdleRPGMod() override {}
bool OnLoad(const CString& sArgs, CString& sMessage) override { bool OnLoad(const CString& sArgs, CString& sMessage) override {
// 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 = new CIdleRPGChannel;
if (!pChannel->FromString(sLine) || FindChannel(pChannel->GetChannel())) { if (!pChannel->FromString(sLine) ||
delete pChannel; FindChannel(pChannel->GetChannel())) {
} else { delete pChannel;
m_Channels[pChannel->GetChannel()] = pChannel; } else {
} m_Channels[pChannel->GetChannel()] = pChannel;
} }
}
return true; return true;
} }
CIdleRPGChannel* FindChannel(const CString& sChannel) { CIdleRPGChannel* FindChannel(const CString& sChannel) {
map<CString, CIdleRPGChannel*>::iterator it = map<CString, CIdleRPGChannel*>::iterator it = m_Channels.find(sChannel);
m_Channels.find(sChannel);
return (it != m_Channels.end()) ? it->second : nullptr; 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"));
return; return;
} }
CTable Table; CTable Table;
Table.AddColumn(t_s("Channel")); Table.AddColumn(t_s("Channel"));
Table.AddColumn(t_s("Botnick")); Table.AddColumn(t_s("Botnick"));
Table.AddColumn(t_s("Username")); Table.AddColumn(t_s("Username"));
Table.AddColumn(t_s("Password")); Table.AddColumn(t_s("Password"));
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);
} }
void CommandAdd (const CString& sLine) { void CommandAdd(const CString& sLine) {
if (sLine.Token(1).empty()) { if (sLine.Token(1).empty()) {
PutModule(t_s("Usage: Add <#channel> <botnick> <username> <password>")); PutModule(
return; t_s("Usage: Add <#channel> <botnick> <username> <password>"));
} return;
}
CString sChannel = sLine.Token(1).AsLower(); CString sChannel = sLine.Token(1).AsLower();
CString sBotnick = sLine.Token(2); CString sBotnick = sLine.Token(2);
CString sUsername = sLine.Token(3); CString sUsername = sLine.Token(3);
CString sPassword = sLine.Token(4); CString sPassword = sLine.Token(4);
if (sChannel.empty()) { if (sChannel.empty()) {
PutModule("Channel not supplied"); PutModule("Channel not supplied");
return; return;
} }
if (sBotnick.empty()) { if (sBotnick.empty()) {
PutModule("Botnick not supplied"); PutModule("Botnick not supplied");
return; return;
} }
if (sUsername.empty()) { if (sUsername.empty()) {
PutModule("Username not supplied"); PutModule("Username not supplied");
return; return;
} }
if (sPassword.empty()) { if (sPassword.empty()) {
PutModule("Password not supplied"); PutModule("Password not supplied");
return; return;
} }
CIdleRPGChannel* pChannel = new CIdleRPGChannel(); CIdleRPGChannel* pChannel = new CIdleRPGChannel();
pChannel->FromString(sChannel+" "+sBotnick+" "+sUsername+" "+sPassword); pChannel->FromString(sChannel + " " + sBotnick + " " + sUsername + " " +
m_Channels[pChannel->GetChannel()] = pChannel; sPassword);
SetNV(pChannel->GetChannel(), pChannel->ToString()); m_Channels[pChannel->GetChannel()] = pChannel;
SetNV(pChannel->GetChannel(), pChannel->ToString());
PutModule(t_f("Saved settings for channel {1}")(pChannel->GetChannel())); PutModule(
t_f("Saved settings for channel {1}")(pChannel->GetChannel()));
} }
void CommandDel (const CString& sLine) { void CommandDel(const CString& sLine) {
if (sLine.Token(1).empty()) { if (sLine.Token(1).empty()) {
PutModule(t_s("Usage: Del <#channel>")); PutModule(t_s("Usage: Del <#channel>"));
return; return;
} }
CString sChannel = sLine.Token(1).AsLower(); CString sChannel = sLine.Token(1).AsLower();
map<CString, CIdleRPGChannel*>::iterator it = map<CString, CIdleRPGChannel*>::iterator it = m_Channels.find(sChannel);
m_Channels.find(sChannel);
if (it == m_Channels.end()) { if (it == m_Channels.end()) {
PutModule(t_f("Channel {1} not found")(sChannel)); PutModule(t_f("Channel {1} not found")(sChannel));
return; return;
} }
delete it->second; delete it->second;
m_Channels.erase(it); m_Channels.erase(it);
DelNV(sChannel); DelNV(sChannel);
PutModule(t_f("Channel {1} removed")(sChannel)); PutModule(t_f("Channel {1} removed")(sChannel));
} }
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); CIdleRPGChannel* fChannel = FindChannel(sChannel);
if (!fChannel) { if (!fChannel) {
PutModule(t_f("Invalid channel {1}")(sChannel)); PutModule(t_f("Invalid channel {1}")(sChannel));
return; return;
} }
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); Login(it.second);
} }
} }
void CommandClear () { void CommandClear() {
ClearNV(); ClearNV();
m_Channels.clear(); m_Channels.clear();
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(new CIdleRPGModTimer(this, sChan, IDLERPG_JOIN_LOGIN_WAIT_TIME, 1, "idlerpg_login_timer_"+sChan->GetChannel(), "Tries login to IdleRPG bot")); AddTimer(
new CIdleRPGModTimer(this, sChan, IDLERPG_JOIN_LOGIN_WAIT_TIME, 1,
"idlerpg_login_timer_" + sChan->GetChannel(),
"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}]")(sChan->GetChannel())); PutModule(t_f("Error logging in: Invalid channel [{1}]")(
return; sChan->GetChannel()));
} return;
}
// Botnick on channel? // Botnick on channel?
CNick* pBot = pChan->FindNick(sChan->GetBotnick()); CNick* pBot = pChan->FindNick(sChan->GetBotnick());
if (!pBot) { if (!pBot) {
PutModule(t_f("Error logging in: Bot [{1}] not found in channel [{2}]")(sChan->GetBotnick(), sChan->GetChannel())); PutModule(
return; t_f("Error logging in: Bot [{1}] not found in channel [{2}]")(
} sChan->GetBotnick(), sChan->GetChannel()));
return;
}
// Bot has op? // Bot has op?
if (!pBot->HasPerm(CChan::Op)) { if (!pBot->HasPerm(CChan::Op)) {
PutModule(t_f("Error logging in: Bot [{1}] not operator in in channel [{2}]")(sChan->GetBotnick(), sChan->GetChannel())); PutModule(t_f(
return; "Error logging in: Bot [{1}] not operator in in channel [{2}]")(
} sChan->GetBotnick(), sChan->GetChannel()));
return;
}
PutIRC("PRIVMSG " + sChan->GetBotnick() + " :login " + sChan->GetUsername() + " " + sChan->GetPassword()); PutIRC("PRIVMSG " + sChan->GetBotnick() + " :login " +
PutModule(t_s("Logging you in with "+sChan->GetBotnick()+" on "+sChan->GetChannel())); sChan->GetUsername() + " " + sChan->GetPassword());
PutModule(t_s("Logging you in with " + sChan->GetBotnick() + " on " +
sChan->GetChannel()));
} }
void OnJoin(const CNick& Nick, CChan& Channel) override { void OnJoin(const CNick& Nick, CChan& Channel) override {
// Setup done? // Setup done?
if (m_Channels.empty()) { if (m_Channels.empty()) {
return; return;
} }
// Correct channel? // Correct channel?
CIdleRPGChannel* fChannel = FindChannel(Channel.GetName().AsLower()); CIdleRPGChannel* fChannel = FindChannel(Channel.GetName().AsLower());
if (!fChannel) { if (!fChannel) {
return; return;
} }
// Either Bot or user joins // Either Bot or user joins
if (Nick.GetNick() != fChannel->GetBotnick() && !GetNetwork()->GetCurNick().Equals(Nick.GetNick())) { if (Nick.GetNick() != fChannel->GetBotnick() &&
return; !GetNetwork()->GetCurNick().Equals(Nick.GetNick())) {
} return;
}
QueueLogin(fChannel); QueueLogin(fChannel);
} }
private: private:
map<CString, CIdleRPGChannel*> m_Channels; map<CString, CIdleRPGChannel*> m_Channels;
}; };
void CIdleRPGModTimer::RunJob() { ((CIdleRPGMod*)GetModule())->Login(pChannel); } void CIdleRPGModTimer::RunJob() {
((CIdleRPGMod*)GetModule())->Login(pChannel);
}
template <> template <>
void TModInfo<CIdleRPGMod>(CModInfo& Info) { void TModInfo<CIdleRPGMod>(CModInfo& Info) {
Info.AddType(CModInfo::NetworkModule); Info.AddType(CModInfo::NetworkModule);
Info.SetWikiPage("idlerpg"); Info.SetWikiPage("idlerpg");
} }
NETWORKMODULEDEFS(CIdleRPGMod, t_s("Automatically handles your login to IdleRPG games/channels")) NETWORKMODULEDEFS(
CIdleRPGMod,
t_s("Automatically handles your login to IdleRPG games/channels"))