From 45a6329312609eb4bc3d7328b0a64ac0ae866115 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Wed, 11 Jan 2017 03:08:33 +0000 Subject: [PATCH 1/3] block_motd: Fix tracking of accepting motd from server When the module is loaded globally or per-user the `m_bTemporaryAcceptMotd` would be used to store state on connecting to an MOTD which could lead to timing issues of a full or partial MOTD being incorrectly sent to another server while another user requested motd via the module. This commit will now track this state per IRCSock so if another connected user requests an MOTD and another server sends an MOTD it will not lead to MOTDs being incorrectly unblocked. --- modules/block_motd.cpp | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/modules/block_motd.cpp b/modules/block_motd.cpp index bbefce42..881fde5b 100644 --- a/modules/block_motd.cpp +++ b/modules/block_motd.cpp @@ -15,6 +15,9 @@ */ #include +#include + +using std::set; class CBlockMotd : public CModule { public: @@ -30,7 +33,12 @@ class CBlockMotd : public CModule { ~CBlockMotd() override {} void OverrideCommand(const CString& sLine) { - m_bTemporaryAcceptMotd = true; + if (!GetNetwork() || !GetNetwork()->GetIRCSock()) { + PutModule("You are not connected to an IRC Server."); + return; + } + + TemporarilyAcceptMotd(); const CString sServer = sLine.Token(1); if (sServer.empty()) { @@ -44,21 +52,41 @@ class CBlockMotd : public CModule { const CString sCmd = sLine.Token(1); if ((sCmd == "375" /* begin of MOTD */ || sCmd == "372" /* MOTD */) && - !m_bTemporaryAcceptMotd) + !ShouldTemporarilyAcceptMotd()) return HALT; if (sCmd == "376" /* End of MOTD */) { - if (!m_bTemporaryAcceptMotd) { + if (!ShouldTemporarilyAcceptMotd()) { sLine = sLine.Token(0) + " 422 " + sLine.Token(2) + " :MOTD blocked by ZNC"; } - m_bTemporaryAcceptMotd = false; + StopTemporarilyAcceptingMotd(); } return CONTINUE; } + void OnIRCDisconnected() override { + StopTemporarilyAcceptingMotd(); + } + + bool ShouldTemporarilyAcceptMotd() const { + return m_sTemporaryAcceptedMotdSocks.count(GetNetwork()->GetIRCSock()) > 0; + } + + void TemporarilyAcceptMotd() { + if (ShouldTemporarilyAcceptMotd()) { + return; + } + + m_sTemporaryAcceptedMotdSocks.insert(GetNetwork()->GetIRCSock()); + } + + void StopTemporarilyAcceptingMotd() { + m_sTemporaryAcceptedMotdSocks.erase(GetNetwork()->GetIRCSock()); + } + private: - bool m_bTemporaryAcceptMotd = false; + set m_sTemporaryAcceptedMotdSocks; }; template <> From 2d2672693aff4861d808bd074c454cc0d88e0291 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Wed, 11 Jan 2017 03:12:47 +0000 Subject: [PATCH 2/3] block_motd: Update internal state when server has no MOTD Otherwise if we request MOTD and the server doesn't have one the module would have incorrect state. --- modules/block_motd.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/block_motd.cpp b/modules/block_motd.cpp index 881fde5b..483ab687 100644 --- a/modules/block_motd.cpp +++ b/modules/block_motd.cpp @@ -62,6 +62,12 @@ class CBlockMotd : public CModule { } StopTemporarilyAcceptingMotd(); } + + if (sCmd == "422") { + // Server has no MOTD + StopTemporarilyAcceptingMotd(); + } + return CONTINUE; } From 1449b021a8a075a3e9cb76215bc309850ef9ca7f Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Wed, 11 Jan 2017 03:40:19 +0000 Subject: [PATCH 3/3] block_motd: Command names should be uppercase --- modules/block_motd.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/block_motd.cpp b/modules/block_motd.cpp index 483ab687..d2602167 100644 --- a/modules/block_motd.cpp +++ b/modules/block_motd.cpp @@ -42,9 +42,9 @@ class CBlockMotd : public CModule { const CString sServer = sLine.Token(1); if (sServer.empty()) { - PutIRC("motd"); + PutIRC("MOTD"); } else { - PutIRC("motd " + sServer); + PutIRC("MOTD " + sServer); } }