From f7ff0ee3f311bf0ab95f53207cd89a121b4608f8 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sat, 13 Jan 2024 23:34:03 +0000 Subject: [PATCH] Modulefy core server-dependent caps --- include/znc/Client.h | 1 + include/znc/IRCSock.h | 1 + modules/corecaps.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++ src/Client.cpp | 11 ------- src/IRCSock.cpp | 4 --- src/Modules.cpp | 7 ++++ 6 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 modules/corecaps.cpp diff --git a/include/znc/Client.h b/include/znc/Client.h index 7f109988..f048cc28 100644 --- a/include/znc/Client.h +++ b/include/znc/Client.h @@ -335,6 +335,7 @@ class CClient : public CIRCSocket { SCString m_ssServerDependentCaps; friend class ClientTest; + friend class CCoreCaps; }; #endif // !ZNC_CLIENT_H diff --git a/include/znc/IRCSock.h b/include/znc/IRCSock.h index a2ef1961..506c6527 100644 --- a/include/znc/IRCSock.h +++ b/include/znc/IRCSock.h @@ -240,6 +240,7 @@ class CIRCSock : public CIRCSocket { VCString m_vsSSLError; friend class CIRCFloodTimer; + friend class CCoreCaps; }; #endif // !ZNC_IRCSOCK_H diff --git a/modules/corecaps.cpp b/modules/corecaps.cpp new file mode 100644 index 00000000..283526e2 --- /dev/null +++ b/modules/corecaps.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2004-2024 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 +#include +#include +#include + +#include + +class CCoreCaps : public CModule { + class AwayNotify : public CCapability { + void OnServerChangedSupport(CIRCNetwork* pNetwork, + bool bState) override { + pNetwork->GetIRCSock()->m_bAwayNotify = bState; + } + void OnClientChangedSupport(CClient* pClient, bool bState) override { + pClient->m_bAwayNotify = bState; + } + }; + + class AccountNotify : public CCapability { + void OnServerChangedSupport(CIRCNetwork* pNetwork, + bool bState) override { + pNetwork->GetIRCSock()->m_bAccountNotify = bState; + } + void OnClientChangedSupport(CClient* pClient, bool bState) override { + pClient->m_bAccountNotify = bState; + } + }; + + class AccountTag : public CCapability { + void OnServerChangedSupport(CIRCNetwork* pNetwork, + bool bState) override { + pNetwork->GetIRCSock()->m_bAccountTag = bState; + } + void OnClientChangedSupport(CClient* pClient, bool bState) override { + pClient->m_bAccountTag = bState; + pClient->SetTagSupport("account", bState); + } + }; + + class ExtendedJoin : public CCapability { + void OnServerChangedSupport(CIRCNetwork* pNetwork, + bool bState) override { + pNetwork->GetIRCSock()->m_bExtendedJoin = bState; + } + void OnClientChangedSupport(CClient* pClient, bool bState) override { + pClient->m_bExtendedJoin = bState; + } + }; + + public: + MODCONSTRUCTOR(CCoreCaps) { + AddCapability("away-notify", std::make_unique()); + AddCapability("account-notify", std::make_unique()); + AddCapability("account-tag", std::make_unique()); + AddCapability("extended-join", std::make_unique()); + } +}; + +GLOBALMODULEDEFS( + CCoreCaps, + t_s("Adds support for several IRC capabilities, extracted from ZNC core.")) diff --git a/src/Client.cpp b/src/Client.cpp index 05d0c92d..e418216f 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -764,17 +764,6 @@ CClient::CoreCaps() { }}}, {"cap-notify", {false, [](CClient* pClient, bool bVal) { pClient->m_bCapNotify = bVal; }}}, - {"away-notify", - {true, [](CClient* pClient, bool bVal) { pClient->m_bAwayNotify = bVal; }}}, - {"account-notify", - {true, [](CClient* pClient, bool bVal) { pClient->m_bAccountNotify = bVal; }}}, - {"account-tag", - {true, [](CClient* pClient, bool bVal) { - pClient->m_bAccountTag = bVal; - pClient->SetTagSupport("account", bVal); - }}}, - {"extended-join", - {true, [](CClient* pClient, bool bVal) { pClient->m_bExtendedJoin = bVal; }}}, }; // For compatibility with older clients diff --git a/src/IRCSock.cpp b/src/IRCSock.cpp index 356ec53d..28973e12 100644 --- a/src/IRCSock.cpp +++ b/src/IRCSock.cpp @@ -379,11 +379,7 @@ bool CIRCSock::OnCapabilityMessage(CMessage& Message) { std::map> mSupportedCaps = { {"multi-prefix", [this](bool bVal) { m_bNamesx = bVal; }}, {"userhost-in-names", [this](bool bVal) { m_bUHNames = bVal; }}, - {"away-notify", [this](bool bVal) { m_bAwayNotify = bVal; }}, - {"account-notify", [this](bool bVal) { m_bAccountNotify = bVal; }}, - {"account-tag", [this](bool bVal) { m_bAccountTag = bVal; }}, {"cap-notify", [](bool bVal) {}}, - {"extended-join", [this](bool bVal) { m_bExtendedJoin = bVal; }}, {"server-time", [this](bool bVal) { m_bServerTime = bVal; }}, {"znc.in/server-time-iso", [this](bool bVal) { m_bServerTime = bVal; }}, diff --git a/src/Modules.cpp b/src/Modules.cpp index f1b568c5..85c033d8 100644 --- a/src/Modules.cpp +++ b/src/Modules.cpp @@ -1621,9 +1621,12 @@ bool CModules::OnServerCapAvailable(const CString& sCap, const CString& sValue) pMod->SetClient(m_pClient); if (m_pUser) { CUser* pOldUser = pMod->GetUser(); + CIRCNetwork* pOldNetwork = pMod->GetNetwork(); pMod->SetUser(m_pUser); + pMod->SetNetwork(m_pNetwork); bResult |= pMod->OnServerCap302Available(sCap, sValue); pMod->SetUser(pOldUser); + pMod->SetNetwork(pOldNetwork); } else { // WTF? Is that possible? bResult |= pMod->OnServerCap302Available(sCap, sValue); @@ -1691,9 +1694,12 @@ bool CModules::IsClientCapSupported(CClient* pClient, const CString& sCap, pMod->SetClient(m_pClient); if (m_pUser) { CUser* pOldUser = pMod->GetUser(); + CIRCNetwork* pOldNetwork = pMod->GetNetwork(); pMod->SetUser(m_pUser); + pMod->SetNetwork(m_pNetwork); bResult |= pMod->IsClientCapSupported(pClient, sCap, bState); pMod->SetUser(pOldUser); + pMod->SetNetwork(pOldNetwork); } else { // WTF? Is that possible? bResult |= pMod->IsClientCapSupported(pClient, sCap, bState); @@ -1998,6 +2004,7 @@ void CModules::GetDefaultMods(set& ssMods, const map ns = { {"chansaver", CModInfo::UserModule}, {"controlpanel", CModInfo::UserModule}, + {"corecaps", CModInfo::GlobalModule}, {"simple_away", CModInfo::NetworkModule}, {"webadmin", CModInfo::GlobalModule}};