summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2015-02-10 21:27:38 +0000
committerGeorge Hazan <george.hazan@gmail.com>2015-02-10 21:27:38 +0000
commite3506fe66886912cee16a91793d17010a87eecef (patch)
treeb06ba97cf876baa40ca4ed1bda85f9e932e435f9
parent327745289de431e7e7dad6e4ce0eb8cef43eaeb5 (diff)
various rudiments & atavisms exterminated
git-svn-id: http://svn.miranda-ng.org/main/trunk@12085 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r--protocols/WhatsApp/src/WASocketConnection.cpp12
-rw-r--r--protocols/WhatsApp/src/WASocketConnection.h2
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp80
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h16
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/ISocketConnection.h1
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp21
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/WAConnection.h5
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp4
-rw-r--r--protocols/WhatsApp/src/WhatsAPI++/utilities.cpp2
-rw-r--r--protocols/WhatsApp/src/connection.cpp1
-rw-r--r--protocols/WhatsApp/src/contacts.cpp16
11 files changed, 64 insertions, 96 deletions
diff --git a/protocols/WhatsApp/src/WASocketConnection.cpp b/protocols/WhatsApp/src/WASocketConnection.cpp
index 3d8744a91f..188bc023be 100644
--- a/protocols/WhatsApp/src/WASocketConnection.cpp
+++ b/protocols/WhatsApp/src/WASocketConnection.cpp
@@ -72,26 +72,20 @@ int WASocketConnection::waitForRead()
void WASocketConnection::flush() {}
-void WASocketConnection::write(const std::vector<unsigned char>& bytes, int offset, int length)
+void WASocketConnection::write(const std::vector<unsigned char> &bytes, int length)
{
NETLIBBUFFER nlb;
std::string tmpBuf = std::string(bytes.begin(), bytes.end());
- nlb.buf = (char*)&(tmpBuf.c_str()[offset]);
+ nlb.buf = (char*)&(tmpBuf.c_str()[0]);
nlb.len = length;
nlb.flags = MSG_NODUMP;
- int result = CallService(MS_NETLIB_SEND, reinterpret_cast<WPARAM>(this->hConn),
- reinterpret_cast<LPARAM>(&nlb));
+ int result = CallService(MS_NETLIB_SEND, WPARAM(hConn), LPARAM(&nlb));
if (result < length) {
throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
}
}
-void WASocketConnection::write(const std::vector<unsigned char>& bytes, int length)
-{
- this->write(bytes, 0, length);
-}
-
unsigned char WASocketConnection::read()
{
SetLastError(0);
diff --git a/protocols/WhatsApp/src/WASocketConnection.h b/protocols/WhatsApp/src/WASocketConnection.h
index 72775b9cf1..6f9d5f2426 100644
--- a/protocols/WhatsApp/src/WASocketConnection.h
+++ b/protocols/WhatsApp/src/WASocketConnection.h
@@ -26,7 +26,7 @@ public:
virtual int read(unsigned char*, int length);
virtual void flush();
virtual void write(const std::vector<unsigned char>& b, int length);
- virtual void write(const std::vector<unsigned char>& bytes, int offset, int length);
+ // virtual void write(const std::vector<unsigned char>& bytes, int offset, int length);
virtual void makeNonBlock();
virtual int waitForRead();
virtual void forceShutdown();
diff --git a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp
index 4d55e536a0..a0137e5180 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp
+++ b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.cpp
@@ -10,51 +10,51 @@
#include "BinTreeNodeWriter.h"
#include "utilities.h"
-BinTreeNodeWriter::BinTreeNodeWriter(WAConnection* conn, ISocketConnection* connection, IMutex* mutex)
+BinTreeNodeWriter::BinTreeNodeWriter(WAConnection *_conn, ISocketConnection *_connection, IMutex *_mutex)
{
- this->bSecure = false;
- this->mutex = mutex;
- this->conn = conn;
- this->out = new ByteArrayOutputStream(2048);
- this->realOut = connection;
- this->dataBegin = 0;
+ bSecure = false;
+ bFlush = true;
+ mutex = _mutex;
+ conn = _conn;
+ out = new ByteArrayOutputStream(4096);
+ realOut = _connection;
+ dataBegin = 0;
}
BinTreeNodeWriter::~BinTreeNodeWriter()
{
- delete this->out;
+ delete out;
}
void BinTreeNodeWriter::writeDummyHeader()
{
- int num = 3;
- this->dataBegin = (int)out->getPosition();
- int num2 = this->dataBegin + num;
- out->setLength(num2);
- out->setPosition(num2);
+ dataBegin = (int)out->getPosition(); // save node start offset
+
+ unsigned char zero3[3] = { 0, 0, 0 };
+ out->write(zero3, 3);
}
void BinTreeNodeWriter::processBuffer()
{
unsigned char num = 0;
if (bSecure) {
- long num2 = (long)out->getLength() + 4L;
- out->setLength(num2);
- out->setPosition(num2);
- num = 8;
+ unsigned char zero4[4] = { 0, 0, 0, 0 };
+ out->write(zero4, 4); // reserve place for hmac
+ num = 0x80;
}
- long num3 = (long)out->getLength() - 3L - (long) this->dataBegin;
+ int num3 = (int)out->getLength() - 3 - dataBegin;
if (num3 >= 1048576L)
throw WAException("Buffer too large: " + num3, WAException::CORRUPT_STREAM_EX, 0);
- std::vector<unsigned char>& buffer = out->getBuffer();
+ std::vector<unsigned char> &buffer = out->getBuffer();
if (bSecure) {
- int num4 = (int)num3 - 4;
- this->conn->outputKey.encodeMessage(buffer.data(), this->dataBegin + 3 + num4, this->dataBegin + 3, num4);
+ int num4 = num3 - 4;
+ conn->outputKey.encodeMessage(buffer.data(), dataBegin + 3 + num4, dataBegin + 3, num4);
}
- buffer[dataBegin] = (unsigned char)((unsigned long)((unsigned long)num << 4) | (unsigned long)((num3 & 16711680L) >> 16));
- buffer[dataBegin + 1] = (unsigned char)((num3 & 65280L) >> 8);
- buffer[dataBegin + 2] = (unsigned char)(num3 & 255L);
+
+ buffer[dataBegin + 2] = (unsigned char)(num3 & 0xFF); num3 >>= 8;
+ buffer[dataBegin + 1] = (unsigned char)(num3 & 0xFF); num3 >>= 8;
+ buffer[dataBegin] = (unsigned char)(num3 & 0xFF) | num;
}
void BinTreeNodeWriter::streamStart(std::string domain, std::string resource)
@@ -76,7 +76,7 @@ void BinTreeNodeWriter::streamStart(std::string domain, std::string resource)
out->write(1);
this->writeAttributes(&attributes);
this->processBuffer();
- this->flushBuffer(true, 0);
+ this->flushBuffer(true);
}
catch (exception& ex) {
this->mutex->unlock();
@@ -211,11 +211,6 @@ void BinTreeNodeWriter::writeInternal(const ProtocolTreeNode &node)
void BinTreeNodeWriter::flushBuffer(bool flushNetwork)
{
- this->flushBuffer(flushNetwork, this->dataBegin);
-}
-
-void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset)
-{
try {
this->processBuffer();
}
@@ -225,18 +220,20 @@ void BinTreeNodeWriter::flushBuffer(bool flushNetwork, int startingOffset)
throw ex;
}
- std::vector<unsigned char> buffer(out->getBuffer().begin(), out->getBuffer().end());
- int num = (int)(out->getLength() - (long)startingOffset);
- if (flushNetwork && ((long)out->getCapacity() - out->getLength() < 3L || out->getLength() > 4096L)) {
- delete this->out;
- this->out = new ByteArrayOutputStream(4096);
- }
+ if (!flushNetwork)
+ return;
- if (flushNetwork) {
- this->realOut->write(buffer, startingOffset, num);
+ std::vector<unsigned char> &buffer = out->getBuffer();
+ this->realOut->write(buffer, (int)buffer.size());
+ if (out->getCapacity() - out->getLength() < 3L || out->getLength() > 4096L) {
+ delete out;
+ out = new ByteArrayOutputStream(4096);
+ }
+ else {
out->setPosition(0);
out->setLength(0);
}
+ dataBegin = 0;
}
void BinTreeNodeWriter::streamEnd()
@@ -256,11 +253,6 @@ void BinTreeNodeWriter::streamEnd()
void BinTreeNodeWriter::write(const ProtocolTreeNode& node)
{
- write(node, true);
-}
-
-void BinTreeNodeWriter::write(const ProtocolTreeNode &node, bool needsFlush)
-{
this->mutex->lock();
try {
this->writeDummyHeader();
@@ -274,7 +266,7 @@ void BinTreeNodeWriter::write(const ProtocolTreeNode &node, bool needsFlush)
out->write(0);
else
writeInternal(node);
- flushBuffer(needsFlush);
+ flushBuffer(bFlush);
}
catch (exception& ex) {
this->mutex->unlock();
diff --git a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h
index 9b59f26f03..f7ed6a6bc8 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h
+++ b/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h
@@ -30,16 +30,16 @@ using namespace std;
#include <map>
-class WAConnection;
+class BinTreeNodeWriter
+{
+ friend class WAConnection;
-class BinTreeNodeWriter {
-private:
- WAConnection* conn;
+ WAConnection *conn;
ISocketConnection *realOut;
ByteArrayOutputStream *out;
- IMutex* mutex;
- int dataBegin;
- bool bSecure;
+ IMutex *mutex;
+ int dataBegin;
+ bool bSecure, bFlush;
void writeListStart(int i);
void writeInt8(int v);
@@ -62,10 +62,8 @@ public:
void streamStart(std::string domain, std::string resource);
void flushBuffer(bool flushNetwork);
- void flushBuffer(bool flushNetwork, int startingOffset);
void streamEnd();
void write(const ProtocolTreeNode &node);
- void write(const ProtocolTreeNode &node, bool needsFlush);
void setSecure() { bSecure = true; }
};
diff --git a/protocols/WhatsApp/src/WhatsAPI++/ISocketConnection.h b/protocols/WhatsApp/src/WhatsAPI++/ISocketConnection.h
index 1aa9d92a49..3ae7932e95 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/ISocketConnection.h
+++ b/protocols/WhatsApp/src/WhatsAPI++/ISocketConnection.h
@@ -13,7 +13,6 @@ public:
virtual unsigned char read() = 0;
virtual void flush() = 0;
virtual void write(const std::vector<unsigned char>& b, int length) = 0;
- virtual void write(const std::vector<unsigned char>& bytes, int offset, int length) = 0;
virtual int read(unsigned char*, int length) = 0;
virtual int read(std::vector<unsigned char>& b, int off, int length) = 0;
virtual void makeNonBlock() = 0;
diff --git a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp
index 1d5e59b00e..6052c768bc 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp
+++ b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.cpp
@@ -115,7 +115,6 @@ WAConnection::WAConnection(const std::string &user, const std::string &resource,
this->supports_receipt_acks = false;
this->iqid = 0;
- this->verbose = true;
this->lastTreeRead = 0;
this->expire_date = 0L;
this->account_kind = -1;
@@ -135,14 +134,7 @@ std::string WAConnection::gidToGjid(const std::string &gid)
std::string WAConnection::makeId(const std::string &prefix)
{
- this->iqid++;
- std::string id;
- if (this->verbose)
- id = prefix + Utilities::intToStr(this->iqid);
- else
- id = Utilities::itoa(this->iqid, 16);
-
- return id;
+ return prefix + Utilities::itoa(++this->iqid, 16);
}
ProtocolTreeNode* WAConnection::getReceiptAck(const std::string &to, const std::string &id, const std::string &receiptType) throw(WAException)
@@ -176,11 +168,6 @@ void WAConnection::setLogin(WALogin* login)
this->account_kind = login->m_iAccountKind;
}
-void WAConnection::setVerboseId(bool b)
-{
- this->verbose = b;
-}
-
bool WAConnection::read() throw(WAException)
{
ProtocolTreeNode *node;
@@ -843,11 +830,13 @@ void WAConnection::sendPresenceSubscriptionRequest(const std::string &to) throw(
void WAConnection::sendQueryLastOnline(const std::string &jid) throw (WAException)
{
+ if (jid == "Server@s.whatsapp.net") // to avoid error 405
+ return;
+
std::string id = makeId("iq_");
this->pending_server_requests[id] = new IqResultQueryLastOnlineHandler(this);
- ProtocolTreeNode *queryNode = new ProtocolTreeNode("query");
- out.write(ProtocolTreeNode("iq", queryNode)
+ out.write(ProtocolTreeNode("iq", new ProtocolTreeNode("query"))
<< XATTR("id", id) << XATTR("type", "get") << XATTR("to", jid) << XATTR("xmlns", "jabber:iq:last"));
}
diff --git a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h
index 957fcf7dfc..03637b8c3a 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h
+++ b/protocols/WhatsApp/src/WhatsAPI++/WAConnection.h
@@ -322,7 +322,6 @@ private:
BinTreeNodeWriter out;
WAListener *m_pEventHandler;
WAGroupListener *m_pGroupEventHandler;
- bool verbose;
int iqid;
std::map<string, IqResultHandler*> pending_server_requests;
IMutex *m_pMutex;
@@ -370,11 +369,13 @@ public:
static int tokenLookup(const std::string&);
void logData(const char *format, ...);
+ void flush() { out.flushBuffer(true); }
static std::string removeResourceFromJid(const std::string &jid);
+ void setFlush(bool _flush) { if (out.bFlush = _flush) out.flushBuffer(true); }
+
void setLogin(WALogin *login);
- void setVerboseId(bool b);
void sendMessage(FMessage* message) throw(WAException);
void sendAvailableForChat() throw(WAException);
diff --git a/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp b/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp
index 80be5402ed..fbca542320 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp
+++ b/protocols/WhatsApp/src/WhatsAPI++/WALogin.cpp
@@ -61,7 +61,7 @@ void WALogin::sendFeatures()
ProtocolTreeNode* pictureChild = new ProtocolTreeNode("w:profile:picture") << XATTR("type", "all");
children->push_back(pictureChild);
- m_pConnection->out.write(ProtocolTreeNode("stream:features", NULL, children), true);
+ m_pConnection->out.write(ProtocolTreeNode("stream:features", NULL, children));
}
void WALogin::sendAuth(const std::vector<unsigned char>& existingChallenge)
@@ -71,7 +71,7 @@ void WALogin::sendAuth(const std::vector<unsigned char>& existingChallenge)
data = getAuthBlob(existingChallenge);
m_pConnection->out.write(ProtocolTreeNode("auth", data) <<
- XATTR("mechanism", "WAUTH-2") << XATTR("user", m_pConnection->user), true);
+ XATTR("mechanism", "WAUTH-2") << XATTR("user", m_pConnection->user));
}
std::vector<unsigned char>* WALogin::getAuthBlob(const std::vector<unsigned char>& nonce)
diff --git a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp
index 1c6f28e009..bbffd4712a 100644
--- a/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp
+++ b/protocols/WhatsApp/src/WhatsAPI++/utilities.cpp
@@ -355,7 +355,7 @@ std::string string_format(const char* fmt, va_list ap)
{
int size = 100;
std::string str;
- while (1) {
+ while (true) {
str.resize(size);
//va_start(ap, fmt);
int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
diff --git a/protocols/WhatsApp/src/connection.cpp b/protocols/WhatsApp/src/connection.cpp
index 33152e4098..be64ed1b41 100644
--- a/protocols/WhatsApp/src/connection.cpp
+++ b/protocols/WhatsApp/src/connection.cpp
@@ -77,7 +77,6 @@ void WhatsAppProto::stayConnectedLoop(void*)
m_pConnection->setLogin(&login);
}
m_pConnection->nick = m_szNick;
- m_pConnection->setVerboseId(true);
if (m_iDesiredStatus != ID_STATUS_INVISIBLE)
m_pConnection->sendAvailableForChat();
diff --git a/protocols/WhatsApp/src/contacts.cpp b/protocols/WhatsApp/src/contacts.cpp
index 7c58d64707..c6153e09e7 100644
--- a/protocols/WhatsApp/src/contacts.cpp
+++ b/protocols/WhatsApp/src/contacts.cpp
@@ -88,20 +88,16 @@ void WhatsAppProto::SetAllContactStatuses(int status, bool reset_client)
void WhatsAppProto::ProcessBuddyList(void*)
{
- std::vector<std::string> jids;
- for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
- if (isChatRoom(hContact))
- continue;
+ // m_pConnection->setFlush(false);
+ for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
ptrA jid(getStringA(hContact, WHATSAPP_KEY_ID));
- if (jid) {
- try {
- m_pConnection->sendQueryLastOnline(std::string(jid));
- }
- CODE_BLOCK_CATCH_ALL
- }
+ if (jid)
+ m_pConnection->sendQueryLastOnline((char*)jid);
}
+ // m_pConnection->setFlush(true);
+
try {
m_pConnection->sendGetGroups();
}