Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WhatsApp protocol support in Xabber #248

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
71102c0
Starting to add WhatsApp protocol stuff.
davidgfnet Aug 15, 2013
5b52c61
Adding whatsapp protocol internals. Translated from libpurple impleme…
davidgfnet Aug 15, 2013
cf5e0fe
Putting aside some files for some time. Seems to build ok and the Wha…
davidgfnet Aug 16, 2013
a759375
More WA protocol classes.
davidgfnet Aug 16, 2013
c58e5d0
Fixing some bugs and adding more functions for WhatsApp protocol func…
davidgfnet Aug 17, 2013
037bd31
Adding WA connection class wrapper.
davidgfnet Aug 17, 2013
288f9f3
Adding message serialization and socket read/write stuff.
davidgfnet Aug 17, 2013
16ef9e1
Fixed many bugs and added integration in the system.
davidgfnet Aug 21, 2013
b4a2ea9
Fixed many bugs. Now we are able to communicate with the servers and …
davidgfnet Aug 21, 2013
ad1aa7c
Fixed many bugs with auth. Now auth works!
davidgfnet Aug 22, 2013
518e6b9
Bit of cleanup.
davidgfnet Aug 22, 2013
db86318
Connect & login working!
davidgfnet Aug 22, 2013
2785d07
Message sending works.
davidgfnet Aug 22, 2013
5749171
Fixed many java related bugs.
davidgfnet Aug 23, 2013
28fdc14
Chat working more or less.
davidgfnet Aug 23, 2013
4cc20d6
Add makefile to remember build commands
davidgfnet Aug 23, 2013
9cd5e65
Added rather simple support for image chats.
davidgfnet Aug 24, 2013
f57034e
Adding permanent contacts using a SQLite table.
davidgfnet Aug 24, 2013
0c8304f
Fixed small issue with contact addition startup :)
davidgfnet Aug 24, 2013
b406df2
Added avatar support.
davidgfnet Aug 25, 2013
e3a3d70
Disabling debug output
davidgfnet Aug 25, 2013
6bdd1e7
Support for timestamp in incoming messages.
davidgfnet Aug 27, 2013
e41b76a
Adding presence sending.
davidgfnet Aug 27, 2013
c57f39f
Adding feature: sending status message along with status.
davidgfnet Aug 27, 2013
015a730
Fix small NullPointerException bug due to presence setting when disco…
davidgfnet Aug 31, 2013
4b7f81c
Adding support for group chats.
davidgfnet Sep 1, 2013
44cf312
Adding "RESOURCE" as nickname for WA accounts.
davidgfnet Sep 1, 2013
fbdb370
Correct small bug which prevented build.
davidgfnet Sep 2, 2013
8c1b0bc
Cleanup of dead code
davidgfnet Sep 15, 2013
2f4f857
More cleanup
davidgfnet Sep 15, 2013
48f113b
Add last seen at the VCard notes field.
davidgfnet Sep 15, 2013
604c9ec
Start adding HTTPS interface for user statuses
davidgfnet Sep 15, 2013
9be0f3c
Update WA protocol to V1.4.
davidgfnet Feb 1, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More WA protocol classes.
Modified XMPP connection wrapping so we can implement a WhatsappConnection class under the Connection object.
davidgfnet committed Sep 14, 2013
commit a7593759836de97ba5f8247b9ec265ce45c22780
8 changes: 4 additions & 4 deletions src/com/xabber/android/data/connection/ConnectionItem.java
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
*/
package com.xabber.android.data.connection;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Connection;

import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
@@ -99,7 +99,7 @@ public String getRealJid() {
ConnectionThread connectionThread = getConnectionThread();
if (connectionThread == null)
return null;
XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
Connection xmppConnection = connectionThread.getXMPPConnection();
if (xmppConnection == null)
return null;
String user = xmppConnection.getUser();
@@ -188,7 +188,7 @@ protected void disconnect(final ConnectionThread connectionThread) {
Thread thread = new Thread("Disconnection thread for " + this) {
@Override
public void run() {
XMPPConnection xmppConnection = connectionThread
Connection xmppConnection = connectionThread
.getXMPPConnection();
if (xmppConnection != null)
try {
@@ -261,7 +261,7 @@ protected void onAuthorized(ConnectionThread connectionThread) {
* @return <code>true</code> if connection thread was managed.
*/
private boolean onDisconnect(ConnectionThread connectionThread) {
XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
Connection xmppConnection = connectionThread.getXMPPConnection();
boolean acceptable = isManaged(connectionThread);
if (xmppConnection == null)
LogManager.i(this, "onClose " + acceptable);
4 changes: 2 additions & 2 deletions src/com/xabber/android/data/connection/ConnectionManager.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
@@ -181,7 +181,7 @@ public void sendPacket(String account, Packet packet)
|| !connectionThread.getConnectionItem().getState()
.isConnected())
throw new NetworkException(R.string.NOT_CONNECTED);
XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
Connection xmppConnection = connectionThread.getXMPPConnection();
try {
xmppConnection.sendPacket(packet);
} catch (IllegalStateException e) {
5 changes: 3 additions & 2 deletions src/com/xabber/android/data/connection/ConnectionThread.java
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import javax.net.ssl.SSLException;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
@@ -67,7 +68,7 @@ public class ConnectionThread implements
/**
* SMACK connection.
*/
private XMPPConnection xmppConnection;
private Connection xmppConnection;

/**
* Thread holder for this connection.
@@ -144,7 +145,7 @@ public Thread newThread(Runnable runnable) {
started = false;
}

public XMPPConnection getXMPPConnection() {
public Connection getXMPPConnection() {
return xmppConnection;
}

Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
@@ -112,7 +112,7 @@ public void onSettingsChanged() {
.getConnectionThread();
if (connectionThread == null)
continue;
XMPPConnection xmppConnection = connectionThread
Connection xmppConnection = connectionThread
.getXMPPConnection();
if (xmppConnection == null)
continue;
3 changes: 2 additions & 1 deletion src/com/xabber/android/data/extension/muc/MUCManager.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import java.util.Collections;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
@@ -279,7 +280,7 @@ public boolean inUse(final String account, final String room) {
*/
public void joinRoom(final String account, final String room,
boolean requested) {
final XMPPConnection xmppConnection;
final Connection xmppConnection;
final RoomChat roomChat;
final String nickname;
final String password;
Original file line number Diff line number Diff line change
@@ -40,22 +40,28 @@ DataBuffer addBuf(DataBuffer other) {
return ret;
}
DataBuffer decodedBuffer(RC4Decoder decoder, int clength, boolean dout) {
DataBuffer deco = new DataBuffer(copyOfRange(this.buffer,0,clength));
if (dout) decoder.cipher(&deco->buffer[0],clength-4);
else decoder.cipher(&deco->buffer[4],clength-4);
DataBuffer deco = new DataBuffer(Arrays.copyOfRange(this.buffer,0,clength));
if (dout) decoder.cipher(Arrays.copyOfRange(deco.buffer,0,clength-4));
else decoder.cipher(Arrays.copyOfRange(deco.buffer,4,clength-4));
return deco;
}
DataBuffer encodedBuffer(RC4Decoder decoder, byte [] key, boolean dout) {
DataBuffer deco = *this;
decoder->cipher(&deco.buffer[0],blen);
unsigned char hmacint[4]; DataBuffer hmac;
KeyGenerator::calc_hmac(deco.buffer,blen,key,(unsigned char*)&hmacint);
hmac.addData(hmacint,4);
DataBuffer deco = new DataBuffer(Arrays.copyOfRange(this.buffer,0,this.buffer.length));

if (dout) deco = deco + hmac;
else deco = hmac + deco;
decoder.cipher(deco.buffer);
byte [] hmacint = KeyGenerator.calc_hmac(deco.buffer,key);
DataBuffer hmac = new DataBuffer(hmacint);

return deco;
DataBuffer res = new DataBuffer();
if (dout) {
res.addBuf(deco);
res.addBuf(hmac);
}else{
res.addBuf(hmac);
res.addBuf(deco);
}

return res;
}
byte [] getPtr() {
return buffer;
@@ -93,7 +99,7 @@ int getInt(int nbytes, int offset) {
int ret = 0;
for (int i = 0; i < nbytes; i++) {
ret <<= 8;
ret |= (int)(buffer[i+offset]0xFF);
ret |= (int)(buffer[i+offset] & 0xFF);
}
return ret;
}
@@ -102,7 +108,7 @@ void putInt(int value, int nbytes) {

byte [] out = new byte[nbytes];
for (int i = 0; i < nbytes; i++) {
out[nbytes-i-1] = (byte)(value>>(i<<3))&0xFF;
out[nbytes-i-1] = (byte)((value>>(i<<3)) & 0xFF);
}
this.addData(out);
}
@@ -157,7 +163,7 @@ String readString() {
// throw 0;
int type = readInt(1);
if (type > 4 && type < 0xf5) {
return getDecoded(type);
return MiscUtil.getDecoded(type);
}
else if (type == 0) {
return "";
@@ -171,7 +177,7 @@ else if (type == 0xfd) {
return readRawString(slen);
}
else if (type == 0xfe) {
return getDecoded(readInt(1)+0xf5);
return MiscUtil.getDecoded(readInt(1)+0xf5);
}
else if (type == 0xfa) {
String u = readString();
@@ -198,7 +204,7 @@ void putRawString(byte [] s) {
}
}
void putString(String s) {
int lu = lookupDecoded(s);
int lu = MiscUtil.lookupDecoded(s);
if (lu > 4 && lu < 0xf5) {
putInt(lu,1);
}
@@ -226,7 +232,7 @@ boolean isList() {
return (buffer[0] == 248 || buffer[0] == 0 || buffer[0] == 249);
}
Vector <Tree> readList(WhatsappConnection c) {
Vector <Tree> l;
Vector <Tree> l = new Vector<Tree>();
int size = readListSize();
while (size-- > 0) {
l.add(c.read_tree(this));
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@

import java.security.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class KeyGenerator {

public static byte [] PKCS5_PBKDF2_HMAC_SHA1(byte [] pass, byte [] salt) throws Exception {
char [] password = new char[pass.length];
for (int i = 0; i < pass.length; i++)
password[i] = (char)(pass[i]&0xFF);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec ks = new PBEKeySpec(password,salt,16,20);
SecretKey s = f.generateSecret(ks);
return s.getEncoded();
}

private static byte [] hexmap = new byte[]{ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };

public static byte [] generateKeyImei(String imei, byte [] salt) {
@@ -18,7 +30,7 @@ public class KeyGenerator {
hashhex[2*i+1] = hexmap[hash[i]&0xF];
}

//PKCS5_PBKDF2_HMAC_SHA1 (hashhex,32,(unsigned char*)salt,saltlen,16,20,(unsigned char*)out);
return PKCS5_PBKDF2_HMAC_SHA1 (hashhex,salt);
}
catch (Exception e) {
return new byte[0];
@@ -28,7 +40,7 @@ public static byte[] generateKeyV2(String pw, byte [] salt) {
try {
byte [] decpass = MiscUtil.base64_decode(pw.getBytes());

//PKCS5_PBKDF2_HMAC_SHA1 (dec.c_str(),20,(unsigned char*)salt,saltlen,16,20,(unsigned char*)out);
return PKCS5_PBKDF2_HMAC_SHA1 (decpass,salt);
}
catch (Exception e) {
return new byte[0];
@@ -46,7 +58,7 @@ public static byte[] generateKeyV2(String pw, byte [] salt) {
hashhex[2*i+1] = hexmap[hash[i]&0xF];
}

//PKCS5_PBKDF2_HMAC_SHA1 (hashhex,32,(unsigned char*)salt,saltlen,16,20,(unsigned char*)out);
return PKCS5_PBKDF2_HMAC_SHA1 (hashhex,salt);
}
catch (Exception e) {
return new byte[0];
2 changes: 1 addition & 1 deletion src/net/davidgf/android/MiscUtil.java
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ private static byte findarray(byte what) {
public static String getDecoded(int n) {
return new String(dictionary[n & 255]);
}
int lookupDecoded(String value) {
public static int lookupDecoded(String value) {
for (int i = 0; i < 256; i++) {
if (dictionary[i].equals(value))
return i;
File renamed without changes.
41 changes: 41 additions & 0 deletions src/net/davidgf/android/WhatsappConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


public class WhatsappConnection {

public Tree read_tree(DataBuffer data) {
Tree t = new Tree("");
return t;
}

/*int lsize = data->readListSize();
int type = data->getInt(1);
if (type == 1) {
data->popData(1);
Tree t;
t.readAttributes(data,lsize);
t.setTag("start");
return t;
}else if (type == 2) {
data->popData(1);
return Tree("treeerr"); // No data in this tree...
}

Tree t;
t.setTag(data->readString());
t.readAttributes(data,lsize);

if ((lsize & 1) == 1) {
return t;
}

if (data->isList()) {
t.setChildren(data->readList(this));
}else{
t.setData(data->readString());
}

return t;*/

}


4 changes: 4 additions & 0 deletions src/org/jivesoftware/smack/Connection.java
Original file line number Diff line number Diff line change
@@ -226,6 +226,10 @@ protected ConnectionConfiguration getConfiguration() {
public String getServiceName() {
return config.getServiceName();
}

public boolean isAlive() {
return true;
}

/**
* Returns the host name of the server where the XMPP server is running. This would be the
1 change: 1 addition & 0 deletions src/org/jivesoftware/smack/XMPPConnection.java
Original file line number Diff line number Diff line change
@@ -1095,6 +1095,7 @@ public void setRosterStorage(RosterStorage storage)
*
* @return <code>false</code> if timeout occur.
*/
@Override
public boolean isAlive() {
PacketWriter packetWriter = this.packetWriter;
return packetWriter == null || packetWriter.isAlive();