summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-10-03 21:00:21 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-10-03 21:00:21 +0300
commit1394d503041125fd2d75fae80af404e2cd476eeb (patch)
tree4462875820b1094822b6fac281168c8862fc1493
parentcc0cde8d87358738bcad304e8bdc9c944d55cdaa (diff)
code cleaning
-rw-r--r--protocols/WhatsAppWeb/src/iq.cpp15
-rw-r--r--protocols/WhatsAppWeb/src/server.cpp19
-rw-r--r--protocols/WhatsAppWeb/src/stdafx.h2
-rw-r--r--protocols/WhatsAppWeb/src/utils.cpp13
-rw-r--r--protocols/WhatsAppWeb/src/utils.h35
-rw-r--r--protocols/WhatsAppWeb/src/wanode.cpp18
6 files changed, 72 insertions, 30 deletions
diff --git a/protocols/WhatsAppWeb/src/iq.cpp b/protocols/WhatsAppWeb/src/iq.cpp
index 8fe5b5ddf8..e20befe5a5 100644
--- a/protocols/WhatsAppWeb/src/iq.cpp
+++ b/protocols/WhatsAppWeb/src/iq.cpp
@@ -26,8 +26,7 @@ void WhatsAppProto::OnIqCountPrekeys(const WANode &node)
return;
}
- WANode iq("iq");
- iq << CHAR_PARAM("xmlns", "encrypt") << CHAR_PARAM("type", "set") << CHAR_PARAM("to", S_WHATSAPP_NET) << CHAR_PARAM("id", generateMessageId());
+ WANodeIq iq(IQ::SET, "encrypt"); iq << CHAR_PARAM("id", generateMessageId());
auto regId = encodeBigEndian(getDword(DBKEY_REG_ID));
iq.addChild("registration")->content.append(regId.c_str(), regId.size());
@@ -96,10 +95,7 @@ void WhatsAppProto::OnSuccess(const WANode &)
{
OnLoggedIn();
- WANode iq("iq");
- iq << CHAR_PARAM("to", S_WHATSAPP_NET) << CHAR_PARAM("xmlns", "passive") << CHAR_PARAM("type", "set");
- iq.addChild("active");
- WSSendNode(iq);
+ WSSendNode(WANodeIq(IQ::SET, "passive") << XCHILD("active"));
}
/////////////////////////////////////////////////////////////////////////////////////////
@@ -116,9 +112,7 @@ void WhatsAppProto::OnIqResult(const WANode &node)
void WhatsAppProto::OnIqPairDevice(const WANode &node)
{
- WANode reply("iq");
- reply << CHAR_PARAM("to", S_WHATSAPP_NET) << CHAR_PARAM("type", "result") << CHAR_PARAM("id", node.getAttr("id"));
- WSSendNode(reply);
+ WSSendNode(WANodeIq(IQ::RESULT) << CHAR_PARAM("id", node.getAttr("id")));
if (auto *pRef = node.getChild("pair-device")->getChild("ref")) {
ShowQrCode(pRef->getBody());
@@ -224,8 +218,7 @@ void WhatsAppProto::OnIqPairSuccess(const WANode &node)
proto::ADVDeviceIdentity deviceIdentity;
deviceIdentity.ParseFromString(deviceDetails);
- WANode reply("iq");
- reply << CHAR_PARAM("to", S_WHATSAPP_NET) << CHAR_PARAM("type", "result") << CHAR_PARAM("id", node.getAttr("id"));
+ WANodeIq reply(IQ::RESULT); reply << CHAR_PARAM("id", node.getAttr("id"));
WANode *nodePair = reply.addChild("pair-device-sign");
diff --git a/protocols/WhatsAppWeb/src/server.cpp b/protocols/WhatsAppWeb/src/server.cpp
index 9b534af176..bd87da1533 100644
--- a/protocols/WhatsAppWeb/src/server.cpp
+++ b/protocols/WhatsAppWeb/src/server.cpp
@@ -239,10 +239,9 @@ void WhatsAppProto::OnLoggedIn()
m_impl.m_keepAlive.Start(1000);
// retrieve loaded prekeys
- WANode iq("iq");
- iq << CHAR_PARAM("id", generateMessageId()) << CHAR_PARAM("xmlns", "encrypt") << CHAR_PARAM("type", "get") << CHAR_PARAM("to", S_WHATSAPP_NET);
- iq.addChild("count");
- WSSendNode(iq, &WhatsAppProto::OnIqCountPrekeys);
+ WSSendNode(
+ WANodeIq(IQ::GET, "encrypt") << XCHILD("count"),
+ &WhatsAppProto::OnIqCountPrekeys);
}
void WhatsAppProto::OnLoggedOut(void)
@@ -262,10 +261,7 @@ void WhatsAppProto::SendKeepAlive()
{
time_t now = time(0);
if (now - m_lastRecvTime > 20) {
- WANode iq("iq");
- iq << CHAR_PARAM("id", generateMessageId()) << CHAR_PARAM("to", S_WHATSAPP_NET) << CHAR_PARAM("type", "get") << CHAR_PARAM("xmlns", "w:p");
- iq.addChild("ping");
- WSSendNode(iq);
+ WSSendNode(WANodeIq(IQ::GET, "w:p") << CHAR_PARAM("id", generateMessageId()) << XCHILD("ping"));
m_lastRecvTime = now;
}
@@ -273,11 +269,8 @@ void WhatsAppProto::SendKeepAlive()
void WhatsAppProto::SetServerStatus(int iStatus)
{
- if (mir_wstrlen(m_wszNick)) {
- WANode iq("presence");
- iq << CHAR_PARAM("name", T2Utf(m_wszNick)) << CHAR_PARAM("type", (iStatus == ID_STATUS_ONLINE) ? "available" : "unavailable");
- WSSendNode(iq);
- }
+ if (mir_wstrlen(m_wszNick))
+ WSSendNode(WANode("presence") << CHAR_PARAM("name", T2Utf(m_wszNick)) << CHAR_PARAM("type", (iStatus == ID_STATUS_ONLINE) ? "available" : "unavailable"));
}
/////////////////////////////////////////////////////////////////////////////////////////
diff --git a/protocols/WhatsAppWeb/src/stdafx.h b/protocols/WhatsAppWeb/src/stdafx.h
index c0e160cc2d..17ab137be9 100644
--- a/protocols/WhatsAppWeb/src/stdafx.h
+++ b/protocols/WhatsAppWeb/src/stdafx.h
@@ -6,7 +6,7 @@ Copyright © 2019-22 George Hazan
*/
#pragma once
-#pragma warning(disable:4996 4290 4200 4324)
+#pragma warning(disable:4996 4290 4200 4239 4324)
#include <malloc.h>
#include <time.h>
diff --git a/protocols/WhatsAppWeb/src/utils.cpp b/protocols/WhatsAppWeb/src/utils.cpp
index 127ec33a45..4c8fb5d8ca 100644
--- a/protocols/WhatsAppWeb/src/utils.cpp
+++ b/protocols/WhatsAppWeb/src/utils.cpp
@@ -155,6 +155,14 @@ int WhatsAppProto::WSSendNode(WANode &node, WA_PKT_HANDLER pHandler)
if (m_hServerConn == nullptr)
return 0;
+ if (pHandler != nullptr) {
+ CMStringA id(generateMessageId());
+ node.addAttr("id", id);
+
+ mir_cslock lck(m_csPacketQueue);
+ m_arPacketQueue.insert(new WARequest(id, pHandler));
+ }
+
CMStringA szText;
node.print(szText);
debugLogA("Sending binary node:\n%s", szText.c_str());
@@ -162,11 +170,6 @@ int WhatsAppProto::WSSendNode(WANode &node, WA_PKT_HANDLER pHandler)
WAWriter writer;
writer.writeNode(&node);
- if (pHandler != nullptr) {
- mir_cslock lck(m_csPacketQueue);
- m_arPacketQueue.insert(new WARequest(node.getAttr("id"), pHandler));
- }
-
MBinBuffer encData = m_noise->encrypt(writer.body.data(), writer.body.length());
MBinBuffer payload = m_noise->encodeFrame(encData.data(), encData.length());
WebSocket_SendBinary(m_hServerConn, payload.data(), payload.length());
diff --git a/protocols/WhatsAppWeb/src/utils.h b/protocols/WhatsAppWeb/src/utils.h
index 292a8e7011..cd98fe8749 100644
--- a/protocols/WhatsAppWeb/src/utils.h
+++ b/protocols/WhatsAppWeb/src/utils.h
@@ -60,6 +60,38 @@ __forceinline WANode& operator<<(WANode &node, const CHAR_PARAM &param)
return node;
}
+/////////////////////////////////////////////////////////////////////////////////////////
+
+namespace IQ
+{
+ enum Type { GET, SET, RESULT };
+};
+
+struct WANodeIq : public WANode
+{
+ WANodeIq(IQ::Type type, const char *pszXmlns = nullptr, const char *pszTo = nullptr);
+};
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+struct XCHILD
+{
+ const char *name, *value;
+
+ __forceinline XCHILD(const char *_name) :
+ name(_name)
+ {}
+};
+
+__forceinline WANode& operator<<(WANode &node, const XCHILD &child)
+{
+ node.addChild(child.name);
+ return node;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// WAReader
+
class WAReader
{
const BYTE *m_buf, *m_limit;
@@ -87,6 +119,9 @@ public:
__forceinline uint32_t readInt32() { return readIntN(4); }
};
+/////////////////////////////////////////////////////////////////////////////////////////
+// WAWriter
+
class WAWriter
{
__forceinline void writeInt8(int value) { writeIntN(value, 1); }
diff --git a/protocols/WhatsAppWeb/src/wanode.cpp b/protocols/WhatsAppWeb/src/wanode.cpp
index 518c29d9dd..5bc78d32cc 100644
--- a/protocols/WhatsAppWeb/src/wanode.cpp
+++ b/protocols/WhatsAppWeb/src/wanode.cpp
@@ -24,6 +24,24 @@ struct Attr
};
/////////////////////////////////////////////////////////////////////////////////////////
+// WANodeIq members
+
+WANodeIq::WANodeIq(IQ::Type type, const char *pszXmlns, const char *pszTo) :
+ WANode("iq")
+{
+ switch (type) {
+ case IQ::GET: addAttr("type", "get"); break;
+ case IQ::SET: addAttr("type", "set"); break;
+ case IQ::RESULT: addAttr("type", "result"); break;
+ }
+
+ if (pszXmlns)
+ addAttr("xmlns", pszXmlns);
+
+ addAttr("to", pszTo ? pszTo : S_WHATSAPP_NET);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
// WANode members
WANode::WANode() :