Merge pull request #957 from jpnurmi/cap-notify

Add cap-notify & away-notify support
This commit is contained in:
J-P Nurmi
2015-07-12 19:34:31 +02:00
5 changed files with 110 additions and 20 deletions
+53 -18
View File
@@ -874,27 +874,20 @@ void CClient::HandleCap(const CString& sLine)
//TODO support ~ and = modifiers
CString sSubCmd = sLine.Token(1);
std::map<CString, std::function<void(bool bVal)>> mCoreCaps = {
{"multi-prefix", [this](bool bVal) { m_bNamesx = bVal; }},
{"userhost-in-names", [this](bool bVal) { m_bUHNames = bVal; }},
{"echo-message", [this](bool bVal) { m_bEchoMessage = bVal; }},
{"server-time", [this](bool bVal) { m_bServerTime = bVal; }},
{"batch", [this](bool bVal) { m_bBatch = bVal; }},
};
// For compatibility with older clients
mCoreCaps["znc.in/server-time-iso"] = mCoreCaps["server-time"];
mCoreCaps["znc.in/batch"] = mCoreCaps["batch"];
mCoreCaps["znc.in/self-message"] = [this](bool bVal) { m_bSelfMessage = bVal; };
if (sSubCmd.Equals("LS")) {
SCString ssOfferCaps;
for (const auto& it : mCoreCaps) {
ssOfferCaps.insert(it.first);
for (const auto& it : m_mCoreCaps) {
bool bServerDependent = std::get<0>(it.second);
if (!bServerDependent || m_ssServerDependentCaps.count(it.first) > 0)
ssOfferCaps.insert(it.first);
}
GLOBALMODULECALL(OnClientCapLs(this, ssOfferCaps), NOTHING);
CString sRes = CString(" ").Join(ssOfferCaps.begin(), ssOfferCaps.end());
RespondCap("LS :" + sRes);
m_bInCap = true;
if (sLine.Token(2).ToInt() >= 302) {
m_bCapNotify = true;
}
} else if (sSubCmd.Equals("END")) {
m_bInCap = false;
if (!IsAttached()) {
@@ -914,7 +907,12 @@ void CClient::HandleCap(const CString& sLine)
if (sCap.TrimPrefix("-"))
bVal = false;
bool bAccepted = mCoreCaps.count(sCap) > 0;
bool bAccepted = false;
const auto& it = m_mCoreCaps.find(sCap);
if (m_mCoreCaps.end() != it) {
bool bServerDependent = std::get<0>(it->second);
bAccepted = !bServerDependent || m_ssServerDependentCaps.count(sCap) > 0;
}
GLOBALMODULECALL(IsClientCapSupported(this, sCap, bVal), &bAccepted);
if (!bAccepted) {
@@ -931,9 +929,10 @@ void CClient::HandleCap(const CString& sLine)
if (sCap.TrimPrefix("-"))
bVal = false;
auto handler_it = mCoreCaps.find(sCap);
if (mCoreCaps.end() != handler_it) {
handler_it->second(bVal);
auto handler_it = m_mCoreCaps.find(sCap);
if (m_mCoreCaps.end() != handler_it) {
const auto& handler = std::get<1>(handler_it->second);
handler(bVal);
}
GLOBALMODULECALL(OnClientCapRequest(this, sCap, bVal), NOTHING);
@@ -996,3 +995,39 @@ void CClient::ParseIdentifier(const CString& sAuthLine) {
m_sUser = sAuthLine;
}
}
void CClient::NotifyServerDependentCaps(const SCString& ssCaps)
{
for (const CString& sCap : ssCaps) {
const auto& it = m_mCoreCaps.find(sCap);
if (m_mCoreCaps.end() != it) {
bool bServerDependent = std::get<0>(it->second);
if (bServerDependent) {
m_ssServerDependentCaps.insert(sCap);
}
}
}
if (HasCapNotify() && !m_ssServerDependentCaps.empty()) {
CString sCaps = CString(" ").Join(m_ssServerDependentCaps.begin(), m_ssServerDependentCaps.end());
PutClient(":irc.znc.in CAP " + GetNick() + " NEW :" + sCaps);
}
}
void CClient::ClearServerDependentCaps()
{
if (HasCapNotify() && !m_ssServerDependentCaps.empty()) {
CString sCaps = CString(" ").Join(m_ssServerDependentCaps.begin(), m_ssServerDependentCaps.end());
PutClient(":irc.znc.in CAP " + GetNick() + " DEL :" + sCaps);
for (const CString& sCap : m_ssServerDependentCaps) {
const auto& it = m_mCoreCaps.find(sCap);
if (m_mCoreCaps.end() != it) {
const auto& handler = std::get<1>(it->second);
handler(false);
}
}
}
m_ssServerDependentCaps.clear();
}
+12
View File
@@ -567,6 +567,10 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
size_t uIdx, uSize;
if (m_pIRCSock) {
pClient->NotifyServerDependentCaps(m_pIRCSock->GetAcceptedCaps());
}
pClient->SetPlaybackActive(true);
if (m_RawBuffer.IsEmpty()) {
@@ -659,6 +663,7 @@ void CIRCNetwork::ClientDisconnected(CClient *pClient) {
if (it != m_vClients.end()) {
m_vClients.erase(it);
}
pClient->ClearServerDependentCaps();
}
CUser* CIRCNetwork::GetUser() const {
@@ -1223,6 +1228,10 @@ void CIRCNetwork::SetIRCSocket(CIRCSock* pIRCSock) {
}
void CIRCNetwork::IRCConnected() {
const SCString& ssCaps = m_pIRCSock->GetAcceptedCaps();
for (CClient* pClient : m_vClients) {
pClient->NotifyServerDependentCaps(ssCaps);
}
if (m_uJoinDelay > 0) {
m_pJoinTimer->Delay(m_uJoinDelay);
} else {
@@ -1231,6 +1240,9 @@ void CIRCNetwork::IRCConnected() {
}
void CIRCNetwork::IRCDisconnected() {
for (CClient* pClient : m_vClients) {
pClient->ClearServerDependentCaps();
}
m_pIRCSock = nullptr;
SetIRCServer("");
+12 -1
View File
@@ -60,6 +60,7 @@ CIRCSock::CIRCSock(CIRCNetwork* pNetwork)
m_bAuthed(false),
m_bNamesx(false),
m_bUHNames(false),
m_bAwayNotify(false),
m_sPerms("*!@%+"),
m_sPermModes("qaohv"),
m_scUserModes(),
@@ -809,7 +810,7 @@ void CIRCSock::ReadLine(const CString& sData) {
sArgs.Split(" ", vsTokens, false);
for (const CString& sCap : vsTokens) {
if (OnServerCapAvailable(sCap) || sCap == "multi-prefix" || sCap == "userhost-in-names") {
if (OnServerCapAvailable(sCap) || sCap == "multi-prefix" || sCap == "userhost-in-names" || sCap == "away-notify") {
m_ssPendingCaps.insert(sCap);
}
}
@@ -820,6 +821,8 @@ void CIRCSock::ReadLine(const CString& sData) {
m_bNamesx = true;
} else if ("userhost-in-names" == sArgs) {
m_bUHNames = true;
} else if ("away-notify" == sArgs) {
m_bAwayNotify = true;
}
m_ssAcceptedCaps.insert(sArgs);
} else if (sSubCmd == "NAK") {
@@ -836,6 +839,14 @@ void CIRCSock::ReadLine(const CString& sData) {
} else if (sCmd.Equals("INVITE")) {
IRCSOCKMODULECALL(OnInvite(Nick, sLine.Token(3).TrimPrefix_n(":")), &bReturn);
if (bReturn) return;
} else if (sCmd.Equals("AWAY")) {
const vector<CClient*>& vClients = m_pNetwork->GetClients();
for (CClient* pClient : vClients) {
if (pClient->HasAwayNotify()) {
m_pNetwork->PutUser(sLine, pClient);
}
}
return;
}
}