From a3702d87bdfaecff251b7be56d880eac95ad176e Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 19 Apr 2021 12:13:43 +0300 Subject: WhatsApp: contact list processing --- protocols/WhatsAppWeb/src/proto.h | 3 +++ protocols/WhatsAppWeb/src/server.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'protocols/WhatsAppWeb') diff --git a/protocols/WhatsAppWeb/src/proto.h b/protocols/WhatsAppWeb/src/proto.h index 93d2c075fb..896c99bab4 100644 --- a/protocols/WhatsAppWeb/src/proto.h +++ b/protocols/WhatsAppWeb/src/proto.h @@ -101,8 +101,11 @@ class WhatsAppProto : public PROTO void OnRestoreSession2(const JSONNode &node); void OnStartSession(const JSONNode &node); + // binary packets void ProcessBinaryPacket(const MBinBuffer &buf); + void ProcessContacts(const JSONNode &node); + // text packets void ProcessPacket(const JSONNode &node); void ProcessBlocked(const JSONNode &node); void ProcessCmd(const JSONNode &node); diff --git a/protocols/WhatsAppWeb/src/server.cpp b/protocols/WhatsAppWeb/src/server.cpp index ad3b94a64f..5231b80fe5 100644 --- a/protocols/WhatsAppWeb/src/server.cpp +++ b/protocols/WhatsAppWeb/src/server.cpp @@ -356,7 +356,7 @@ bool WhatsAppProto::ServerThreadWorker() break; } - Netlib_Dump(m_hServerConn, dest.data(), dest.length(), false, 0); + // Netlib_Dump(m_hServerConn, dest.data(), dest.length(), false, 0); ProcessBinaryPacket(dest); } else { @@ -431,6 +431,39 @@ void WhatsAppProto::ProcessBinaryPacket(const MBinBuffer &buf) return; debugLogA("packed JSON: %s", root.write().c_str()); + + CMStringA szType = root["type"].as_mstring(); + if (szType == "contacts") + ProcessContacts(root["$list$"]); +} + +void WhatsAppProto::ProcessContacts(const JSONNode &list) +{ + for (auto &it : list) { + CMStringW jid(it["jid"].as_mstring()); + auto *pUser = AddUser(T2Utf(jid), false); + + CMStringW wszNick(it["notify"].as_mstring()); + if (wszNick.IsEmpty()) { + int idx = jid.Find('@'); + wszNick = (idx == -1) ? jid : jid.Left(idx); + } + setWString(pUser->hContact, "Nick", wszNick); + + CMStringW wszFullName(it["name"].as_mstring()); + wszFullName.TrimRight(); + if (!wszFullName.IsEmpty()) { + setWString(pUser->hContact, "FullName", wszFullName); + + CMStringW wszShort(it["short"].as_mstring()); + wszShort.TrimRight(); + if (!wszShort.IsEmpty()) { + setWString(pUser->hContact, "FirstName", wszShort); + if (wszShort.GetLength()+1 < wszFullName.GetLength()) + setWString(pUser->hContact, "LastName", wszFullName.c_str() + 1 + wszShort.GetLength()); + } + } + } } ///////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3