From: Kyle Fuller Date: Sat, 2 Jul 2016 10:24:06 +0000 (+0100) Subject: Remove devices when API returns 401 X-Git-Tag: 1.1.0~8^2 X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=6b6b4f79875fd50ff227305d636923f3a1b30b5c;p=znc-palaver.git Remove devices when API returns 401 --- diff --git a/palaver.cpp b/palaver.cpp index 67326d6..372f0c2 100644 --- a/palaver.cpp +++ b/palaver.cpp @@ -126,6 +126,10 @@ public: } } + virtual void HandleStatusCode(unsigned int status) { + + } + void ReadLine(const CString& sData) { CString sLine = sData; sLine.TrimRight("\r\n"); @@ -141,6 +145,8 @@ public: DEBUG("Palaver: Successfully send notification ('" << uStatus << "')"); } + HandleStatusCode(uStatus); + m_eState = Headers; break; } @@ -190,6 +196,18 @@ private: CString m_sHostname; }; +class PLVHTTPNotificationSocket : public PLVHTTPSocket { +public: + PLVHTTPNotificationSocket(CModule *pModule, const CString &sToken, const CString &sMethod, const CString &sURL, MCString &mcsHeaders, const CString &sContent) : PLVHTTPSocket(pModule, sMethod, sURL, mcsHeaders, sContent) { + m_sToken = sToken; + } + + virtual void HandleStatusCode(unsigned int status); + +private: + CString m_sToken; +}; + class CDevice { public: CDevice(const CString &sToken) { @@ -673,7 +691,7 @@ public: } sJSON += "}"; - PLVHTTPSocket *pSocket = new PLVHTTPSocket(&module, "POST", GetPushEndpoint(), mcsHeaders, sJSON); + PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, GetToken(), "POST", GetPushEndpoint(), mcsHeaders, sJSON); module.AddSocket(pSocket); } @@ -686,7 +704,7 @@ public: CString sJSON = "{\"badge\": 0}"; - PLVHTTPSocket *pSocket = new PLVHTTPSocket(&module, "POST", GetPushEndpoint(), mcsHeaders, sJSON); + PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, GetToken(), "POST", GetPushEndpoint(), mcsHeaders, sJSON); module.AddSocket(pSocket); m_uiBadge = 0; @@ -899,6 +917,21 @@ public: return pDevice; } + bool RemoveDeviceWithToken(const CString& sToken) { + for (std::vector::iterator it = m_vDevices.begin(); + it != m_vDevices.end(); ++it) { + CDevice& device = **it; + + if (device.GetToken().Equals(sToken)) { + m_vDevices.erase(it); + Save(); + return true; + } + } + + return false; + } + #pragma mark - Serialization CString GetConfigPath() const { @@ -1151,6 +1184,15 @@ private: std::vector m_vDevices; }; +void PLVHTTPNotificationSocket::HandleStatusCode(unsigned int status) { + if (status == 401) { + if (CPalaverMod *pModule = dynamic_cast(m_pModule)) { + DEBUG("palaver: Removing device"); + pModule->RemoveDeviceWithToken(m_sToken); + } + } +} + template<> void TModInfo(CModInfo& Info) { Info.SetWikiPage("palaver"); }