From 25a7a059a20f51e6df201ba606e38c4aef45e8f0 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 29 Jan 2015 15:34:37 +0000 Subject: - receipts for offline messages - slightly enilghtened login procedure - WHATSAPP_KEY_PUSH_NAME removed git-svn-id: http://svn.miranda-ng.org/main/trunk@11950 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp | 18 +++++++++ protocols/WhatsApp/src/WhatsAPI++/WAConnection.h | 1 + protocols/WhatsApp/src/connection.cpp | 46 ++++++++-------------- protocols/WhatsApp/src/contacts.cpp | 8 ++-- protocols/WhatsApp/src/db.h | 1 - protocols/WhatsApp/src/messages.cpp | 3 +- protocols/WhatsApp/src/proto.h | 2 +- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp index 5566d23145..9aa4450937 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp +++ b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp @@ -210,6 +210,8 @@ bool WAConnection::read() throw(WAException) parseMessage(node); else if (ProtocolTreeNode::tagEquals(node, "ack")) parseAck(node); + else if (ProtocolTreeNode::tagEquals(node, "receipt")) + parseReceipt(node); else if (ProtocolTreeNode::tagEquals(node, "chatstates")) parseChatStates(node); @@ -266,6 +268,22 @@ void WAConnection::parseAck(ProtocolTreeNode *node) throw(WAException) } } +void WAConnection::parseReceipt(ProtocolTreeNode *node) throw(WAException) +{ + const string &from = node->getAttributeValue("from"); + const string &id = node->getAttributeValue("id"); + const string &ts = node->getAttributeValue("t"); + + if (m_pEventHandler != NULL) { + FMessage msg(from, false, id); + msg.status = FMessage::STATUS_RECEIVED_BY_TARGET; + m_pEventHandler->onMessageStatusUpdate(&msg); + } + + out.write(ProtocolTreeNode("ack") + << XATTR("to", from) << XATTR("id", id) << XATTR("type", "read") << XATTRI("t", time(0))); +} + void WAConnection::parseChatStates(ProtocolTreeNode *node) throw (WAException) { const string &from = node->getAttributeValue("from"); diff --git a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h index 6bc53e4c86..6ed955caa7 100644 --- a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h +++ b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h @@ -335,6 +335,7 @@ private: void parseIq(ProtocolTreeNode *node) throw(WAException); void parseMessage(ProtocolTreeNode* node) throw(WAException); void parsePresense(ProtocolTreeNode*) throw(WAException); + void parseReceipt(ProtocolTreeNode *node) throw (WAException); std::map parseCategories(ProtocolTreeNode* node) throw(WAException); void sendMessageWithMedia(FMessage* message) throw(WAException); diff --git a/protocols/WhatsApp/src/connection.cpp b/protocols/WhatsApp/src/connection.cpp index 1ff049ae6d..ca3f77a762 100644 --- a/protocols/WhatsApp/src/connection.cpp +++ b/protocols/WhatsApp/src/connection.cpp @@ -2,49 +2,35 @@ void WhatsAppProto::stayConnectedLoop(void*) { - std::string cc, in, pass; - DBVARIANT dbv = { 0 }; - - if (!getString(WHATSAPP_KEY_CC, &dbv)) { - cc = dbv.pszVal; - db_free(&dbv); - } - if (cc.empty()) { + ptrA cc(getStringA(WHATSAPP_KEY_CC)); + if (mir_strlen(cc) == 0) { NotifyEvent(m_tszUserName, TranslateT("Please enter a country code."), NULL, WHATSAPP_EVENT_CLIENT); return; } - if (!getString(WHATSAPP_KEY_LOGIN, &dbv)) { - in = dbv.pszVal; - db_free(&dbv); - } - if (in.empty()) { + ptrA in(getStringA(WHATSAPP_KEY_LOGIN)); + if (mir_strlen(in) == 0) { NotifyEvent(m_tszUserName, TranslateT("Please enter a phone number without country code."), NULL, WHATSAPP_EVENT_CLIENT); return; } - m_szPhoneNumber = cc + in; + + m_szPhoneNumber = std::string(cc) + std::string(in); m_szJid = m_szPhoneNumber + "@s.whatsapp.net"; - if (!getString(WHATSAPP_KEY_NICK, &dbv)) { - m_szNick = dbv.pszVal; - db_free(&dbv); - } - if (m_szNick.empty()) { + ptrA szNick(getStringA(WHATSAPP_KEY_NICK)); + if (mir_strlen(szNick) == 0) { NotifyEvent(m_tszUserName, TranslateT("Please enter a nickname."), NULL, WHATSAPP_EVENT_CLIENT); return; } + m_szNick = szNick; - if (!getString(WHATSAPP_KEY_PASS, &dbv)) { - pass = dbv.pszVal; - db_free(&dbv); - } - if (pass.empty()) { + ptrA szPassword(getStringA(WHATSAPP_KEY_PASS)); + if (mir_strlen(szPassword) == 0) { NotifyEvent(m_tszUserName, TranslateT("Please enter a password."), NULL, WHATSAPP_EVENT_CLIENT); return; } // ----------------------------- - Mutex writerMutex; bool error = false; @@ -63,7 +49,7 @@ void WhatsAppProto::stayConnectedLoop(void*) if (m_iDesiredStatus == ID_STATUS_OFFLINE || error) { debugLogA("Set status to offline"); SetAllContactStatuses(ID_STATUS_OFFLINE, true); - this->ToggleStatusMenuItems(false); + ToggleStatusMenuItems(false); int prevStatus = m_iStatus; m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, prevStatus); @@ -74,7 +60,7 @@ void WhatsAppProto::stayConnectedLoop(void*) try { unsigned passLen; - ptrA passBin((char*)mir_base64_decode(pass.c_str(), &passLen)); + ptrA passBin((char*)mir_base64_decode(szPassword, &passLen)); std::string password(passBin, passLen), resource = ACCOUNT_RESOURCE; int portNumber; if (getByte(WHATSAPP_KEY_SSL, 0)) @@ -104,7 +90,7 @@ void WhatsAppProto::stayConnectedLoop(void*) // #TODO Move out of try block. Exception is expected on disconnect while (true) { - this->lastPongTime = time(NULL); + m_tLastWriteTime = time(NULL); if (!m_pConnection->read()) break; } @@ -131,11 +117,11 @@ void WhatsAppProto::sentinelLoop(void*) while (WaitForSingleObjectEx(update_loop_lock_, 1000, true) == WAIT_TIMEOUT) { if (m_iStatus != ID_STATUS_OFFLINE && m_pConnection != NULL && m_iDesiredStatus == m_iStatus) { // #TODO Quiet after pong or tree read? - int quietInterval = difftime(time(NULL), this->lastPongTime); + int quietInterval = difftime(time(NULL), m_tLastWriteTime); if (quietInterval >= MAX_SILENT_INTERVAL) { try { debugLogA("send ping"); - this->lastPongTime = time(NULL); + m_tLastWriteTime = time(NULL); m_pConnection->sendPing(); } catch (exception &e) { diff --git a/protocols/WhatsApp/src/contacts.cpp b/protocols/WhatsApp/src/contacts.cpp index 33b289c098..0250420ed2 100644 --- a/protocols/WhatsApp/src/contacts.cpp +++ b/protocols/WhatsApp/src/contacts.cpp @@ -22,13 +22,13 @@ MCONTACT WhatsAppProto::AddToContactList(const std::string& jid, BYTE , bool don if (new_name != NULL) { DBVARIANT dbv; string oldName; - if (db_get_utf(hContact, m_szModuleName, WHATSAPP_KEY_PUSH_NAME, &dbv)) + if (db_get_utf(hContact, m_szModuleName, WHATSAPP_KEY_NICK, &dbv)) oldName = jid.c_str(); else { oldName = dbv.pszVal; db_free(&dbv); } - db_set_utf(hContact, m_szModuleName, WHATSAPP_KEY_PUSH_NAME, new_name); + db_set_utf(hContact, m_szModuleName, WHATSAPP_KEY_NICK, new_name); if (oldName.compare(string(new_name)) != 0) { CMString tmp(FORMAT, TranslateT("is now known as '%s'"), ptrT(mir_utf8decodeT(new_name))); @@ -60,7 +60,7 @@ MCONTACT WhatsAppProto::AddToContactList(const std::string& jid, BYTE , bool don db_set_ts(hContact, "CList", "Group", tszGroup); if (new_name != NULL) - db_set_utf(hContact, m_szModuleName, WHATSAPP_KEY_PUSH_NAME, new_name); + db_set_utf(hContact, m_szModuleName, WHATSAPP_KEY_NICK, new_name); if (isChatRoom) setByte(hContact, "SimpleChatRoom", 1); @@ -497,7 +497,7 @@ INT_PTR __cdecl WhatsAppProto::OnChangeGroupSubject(WPARAM hContact, LPARAM lPar input_box* ib = new input_box; DBVARIANT dbv; - if (getTString(hContact, WHATSAPP_KEY_PUSH_NAME, &dbv)) + if (getTString(hContact, WHATSAPP_KEY_NICK, &dbv)) ib->defaultValue = _T(""); else { ib->defaultValue = dbv.ptszVal; diff --git a/protocols/WhatsApp/src/db.h b/protocols/WhatsApp/src/db.h index 228a25bde2..38f1f16be0 100644 --- a/protocols/WhatsApp/src/db.h +++ b/protocols/WhatsApp/src/db.h @@ -8,7 +8,6 @@ #define WHATSAPP_KEY_MAP_STATUSES "MapStatuses" #define WHATSAPP_KEY_LOGGING_ENABLE "LoggingEnable" #define WHATSAPP_KEY_NAME "RealName" -#define WHATSAPP_KEY_PUSH_NAME "Nick" #define WHATSAPP_KEY_LAST_SEEN "LastSeen" #define WHATSAPP_KEY_AVATAR_ID "AvatarId" #define WHATSAPP_KEY_SYSTRAY_NOTIFY "UseSystrayNotify" diff --git a/protocols/WhatsApp/src/messages.cpp b/protocols/WhatsApp/src/messages.cpp index 3f4210e9a2..d2f170a020 100644 --- a/protocols/WhatsApp/src/messages.cpp +++ b/protocols/WhatsApp/src/messages.cpp @@ -22,9 +22,8 @@ void WhatsAppProto::onMessageForMe(FMessage* paramFMessage, bool paramBoolean) msg = ¶mFMessage->data; } - if (isChatRoom) { + if (isChatRoom) msg->insert(0, std::string("[").append(paramFMessage->notifyname).append("]: ")); - } MCONTACT hContact = this->AddToContactList(paramFMessage->key.remote_jid, 0, false, isChatRoom ? NULL : paramFMessage->notifyname.c_str(), isChatRoom); diff --git a/protocols/WhatsApp/src/proto.h b/protocols/WhatsApp/src/proto.h index 33cc9bcad0..09e98b8a7c 100644 --- a/protocols/WhatsApp/src/proto.h +++ b/protocols/WhatsApp/src/proto.h @@ -167,7 +167,7 @@ private: WASocketConnection *conn; WAConnection *m_pConnection; Mutex connMutex; - int lastPongTime; + time_t m_tLastWriteTime; std::vector m_Challenge; string m_szPhoneNumber; -- cgit v1.2.3