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
Added avatar support.
Also fixed a small bug with user status.
davidgfnet committed Sep 14, 2013
commit b406df20bd029553a2af7814439d5a29e2c1418b
13 changes: 13 additions & 0 deletions src/net/davidgf/android/MiscUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

package net.davidgf.android;

import java.security.MessageDigest;
import java.math.BigInteger;

public class MiscUtil {

private static byte [] base64_chars = new byte []{'A','B','C','D','E','F','G','H','I','J','K','L',
@@ -129,6 +132,16 @@ public static String getUser(String user) {
return user.split("@")[0];
}

public static String getEncodedSha1Sum( byte [] data ) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(data);
return new BigInteger(1, md.digest()).toString(16);
}
catch (Exception e) {
return new String("");
}
}
}


69 changes: 44 additions & 25 deletions src/net/davidgf/android/WAConnection.java
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.IQ;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.vcard.BinaryPhoto;
import com.xabber.android.data.connection.ConnectionThread;

import net.davidgf.android.WhatsappConnection;
@@ -367,6 +369,16 @@ public void sendPacket(Packet packet) {
Registration r = (Registration)packet;
System.out.println(r.getChildElementXML());
}
if (packet instanceof VCard) {
// The request for a VCard has been queued, now we should provide the Avatar!
packet.setFrom(packet.getTo());
VCard vc = (VCard)packet;

BinaryPhoto bp = new BinaryPhoto();
bp.setData(waconnection.getUserAvatar(packet.getFrom()));
vc.getPhotos().add(bp);
submitPacket(packet);
}
if (packet instanceof RosterPacket) {
RosterPacket r = (RosterPacket)packet;
// Check Add/Remove Contact/Group:
@@ -412,6 +424,9 @@ public void sendPacket(Packet packet) {

// Notify others
this.firePacketSendingListeners(packet);

// DO stuff again
processLoop();
}

private void popWriteData() {
@@ -618,31 +633,7 @@ private void readPackets(Thread thisThread, InputStream istream) {
}
}

// Proceed to push data to underlying connection class
synchronized ( waconnection ) {
synchronized (inbuffer) {
int used = waconnection.pushIncomingData(inbuffer);
inbuffer = Arrays.copyOfRange(inbuffer, used, inbuffer.length);
}
}

// Process stuff
this.popWriteData(); // Ready data might be waiting ...

if (waconnection.isConnected() && !waconnected) {
connectionOK(); // Notify the connection status
waconnected = true;
}

if (listenerExecutor == null) {
System.out.println("Null!!! Shit\n");
}

Packet p = waconnection.getNextPacket();
while (p != null) {
submitPacket(p);
p = waconnection.getNextPacket();
}
processLoop();
} while (r >= 0);
}catch (IOException e) {
System.out.println("Error!\n" + e.toString());
@@ -653,6 +644,34 @@ private void readPackets(Thread thisThread, InputStream istream) {
// Signal the writer thread so it can also end
writewait.release();
}

private void processLoop() {
// Proceed to push data to underlying connection class
synchronized ( waconnection ) {
synchronized (inbuffer) {
int used = waconnection.pushIncomingData(inbuffer);
inbuffer = Arrays.copyOfRange(inbuffer, used, inbuffer.length);
}
}

// Process stuff
this.popWriteData(); // Ready data might be waiting ...

if (waconnection.isConnected() && !waconnected) {
connectionOK(); // Notify the connection status
waconnected = true;
}

if (listenerExecutor == null) {
System.out.println("Null!!! Shit\n");
}

Packet p = waconnection.getNextPacket();
while (p != null) {
submitPacket(p);
p = waconnection.getNextPacket();
}
}

/**
* Establishes a connection to the XMPP server and performs an automatic login
69 changes: 66 additions & 3 deletions src/net/davidgf/android/WhatsappConnection.java
Original file line number Diff line number Diff line change
@@ -13,7 +13,10 @@
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smackx.packet.MessageEvent;
import com.xabber.xmpp.vcard.VCard;
import com.xabber.xmpp.avatar.VCardUpdate;

import java.util.*;

@@ -35,6 +38,8 @@ private enum SessionStatus { SessionNone, SessionConnecting, SessionWaitingChall

private Vector <Packet> received_packets;
private Vector <Contact> contacts;

private int iqid;

public WhatsappConnection(String phone, String pass, String nick) {
session_key = new byte[20];
@@ -47,6 +52,7 @@ public WhatsappConnection(String phone, String pass, String nick) {
outbuffer = new DataBuffer();
received_packets = new Vector <Packet>();
contacts = new Vector <Contact> ();
iqid = 0;
}

public Tree read_tree(DataBuffer data) {
@@ -168,8 +174,11 @@ else if (t.getTag().equals("success")) {
//this->updateGroups();

// Resend contact status query (for already added contacts)
for (int i = 0; i < contacts.size(); i++)
System.out.println("SUbscribe delayed\n");
for (int i = 0; i < contacts.size(); i++) {
subscribePresence(contacts.get(i).phone);
queryPreview(contacts.get(i).phone);
}

//std::cout << "Logged in!!!" << std::endl;
//std::cout << "Account " << phone << " status: " << account_status << " kind: " << account_type <<
@@ -193,6 +202,14 @@ else if (t.getTag().equals("iq")) {
if (t.hasAttribute("from") && t.hasAttribute("id") && t.hasChild("ping")) {
this.doPong(t.getAttribute("id"),t.getAttribute("from"));
}

if (t.hasAttributeValue("type","result") && t.hasAttribute("from")) {
Tree tb = t.getChild("picture");
if (tb != null) {
if (tb.hasAttributeValue("type","preview"))
this.addPreviewPicture(t.getAttribute("from"),tb.getData());
}
}
}
else if (t.getTag().equals("message")) {
if (t.hasAttributeValue("type","chat") && t.hasAttribute("from")) {
@@ -257,6 +274,51 @@ private DataBuffer generateResponse(final String from, final String type, final
return serialize_tree(mes,true);
}

private void queryPreview(final String user) {
final String fuser = user+"@"+whatsappserver;
final String reqid = String.valueOf(++iqid);
Tree pic = new Tree ("picture",
new HashMap < String,String >() {{ put("xmlns","w:profile:picture"); put("type","preview"); }} );
Tree req = new Tree("iq",
new HashMap < String,String >() {{ put("id",reqid); put("type","get"); put("to",fuser); }} );

req.addChild(pic);

outbuffer = outbuffer.addBuf(new DataBuffer(serialize_tree(req,true)));
}

public byte[] getUserAvatar(String user) {
// Look for preview
user = MiscUtil.getUser(user);
for (int i = 0; i < contacts.size(); i++) {
if (contacts.get(i).phone.equals(user)) {
return contacts.get(i).ppprev;
}
}
return new byte[0];
}

private void addPreviewPicture(String user, byte [] picture) {
System.out.println("Received preview...\n");
user = MiscUtil.getUser(user);

// Save preview
for (int i = 0; i < contacts.size(); i++) {
if (contacts.get(i).phone.equals(user)) {
contacts.get(i).ppprev = picture;
break;
}
}

VCardUpdate vc = new VCardUpdate();
vc.setPhotoHash(MiscUtil.getEncodedSha1Sum(picture));
Presence p = new Presence(Presence.Type.subscribed);
p.setTo(phone);
p.setFrom(user);
p.addExtension(vc);
received_packets.add(p);
}

private void receiveMessage(AbstractMessage msg) {
received_packets.add(msg.serializePacket());
}
@@ -407,11 +469,12 @@ public void addContact(String user, boolean user_request) {

if (conn_status == SessionStatus.SessionConnected) {
subscribePresence(user);
queryPreview(user);
}
}

public void subscribePresence(String user) {
final String username = MiscUtil.getUser(user);
final String username = MiscUtil.getUser(user)+"@"+whatsappserver;
Tree request = new Tree("presence",
new HashMap < String,String >() {{ put("type","subscribe"); put("to",username); }} );

@@ -520,7 +583,7 @@ public class Contact {
String status;
long last_seen, last_status;
boolean mycontact;
String ppprev, pppicture;
byte[] ppprev, pppicture;
boolean subscribed;

Contact(String phone, boolean myc) {