Files
znc/modules/block_motd.cpp
Kyle Fuller 45a6329312 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.
2017-03-30 13:34:25 +02:00

101 lines
2.9 KiB
C++

/*
* Copyright (C) 2004-2017 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/Modules.h>
#include <znc/IRCNetwork.h>
using std::set;
class CBlockMotd : public CModule {
public:
MODCONSTRUCTOR(CBlockMotd) {
AddHelpCommand();
AddCommand("GetMotd", static_cast<CModCommand::ModCmdFunc>(
&CBlockMotd::OverrideCommand),
"[<server>]",
"Override the block with this command. Can optionally "
"specify which server to query.");
}
~CBlockMotd() override {}
void OverrideCommand(const CString& sLine) {
if (!GetNetwork() || !GetNetwork()->GetIRCSock()) {
PutModule("You are not connected to an IRC Server.");
return;
}
TemporarilyAcceptMotd();
const CString sServer = sLine.Token(1);
if (sServer.empty()) {
PutIRC("motd");
} else {
PutIRC("motd " + sServer);
}
}
EModRet OnRaw(CString& sLine) override {
const CString sCmd = sLine.Token(1);
if ((sCmd == "375" /* begin of MOTD */ || sCmd == "372" /* MOTD */) &&
!ShouldTemporarilyAcceptMotd())
return HALT;
if (sCmd == "376" /* End of MOTD */) {
if (!ShouldTemporarilyAcceptMotd()) {
sLine = sLine.Token(0) + " 422 " + sLine.Token(2) +
" :MOTD blocked by ZNC";
}
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:
set<CIRCSock *> m_sTemporaryAcceptedMotdSocks;
};
template <>
void TModInfo<CBlockMotd>(CModInfo& Info) {
Info.AddType(CModInfo::NetworkModule);
Info.AddType(CModInfo::GlobalModule);
Info.SetWikiPage("block_motd");
}
USERMODULEDEFS(CBlockMotd,
"Block the MOTD from IRC so it's not sent to your client(s).")