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
Message sending works.
Contacts are not retained across sessions and connection drops frequently.
Receiving messages does not work yet :(
davidgfnet committed Sep 14, 2013
commit 2785d076071c2299355b758a2d457397c257e4e6
9 changes: 9 additions & 0 deletions src/net/davidgf/android/MiscUtil.java
Original file line number Diff line number Diff line change
@@ -115,6 +115,15 @@ public static int lookupDecoded(String value) {
}
return 0;
}

public static String bytesToUTF8(byte [] ba) {
try {
return new String(ba, "UTF-8");
}
catch (Exception e) {
return new String();
}
}

}

67 changes: 65 additions & 2 deletions src/net/davidgf/android/WAConnection.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.IQ;
import com.xabber.android.data.connection.ConnectionThread;

import net.davidgf.android.WhatsappConnection;
@@ -36,7 +37,7 @@
import java.security.SecureRandom;
import java.security.Security;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.concurrent.*;

/**
* Creates a socket connection to a WA server.
@@ -70,9 +71,10 @@ public class WAConnection extends Connection {
byte [] outbuffer;
byte [] outbuffer_mutex;
WhatsappConnection waconnection;

Semaphore readwait,writewait;

private ExecutorService listenerExecutor;

int msgid;

private static final String waUA = "WhatsApp/2.10.750 Android/4.2.1 Device/GalaxyS3";
@@ -106,6 +108,16 @@ public WAConnection(ConnectionThread ct, String serviceName, CallbackHandler cal
readwait = new Semaphore(0);
writewait = new Semaphore(0);
this.cthread = ct;

listenerExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable,
"WA Listener Processor");
thread.setDaemon(true);
return thread;
}
});

}

/**
@@ -360,6 +372,21 @@ public void sendPacket(Packet packet) {
}
if (packet instanceof RosterPacket) {
RosterPacket r = (RosterPacket)packet;
// Check Add/Remove Contact/Group:
if (r.getType() == IQ.Type.SET) {
Collection<RosterPacket.Item> items = r.getRosterItems();
if (items.size() == 1) {
RosterPacket.Item it = ((RosterPacket.Item)(items.toArray()[0]));
if (it.getItemType() == RosterPacket.ItemType.remove) {
System.out.println("Remove contact!\n");
}else{
// Adding contact, notify underlying connection for status query
//waconnection.addContact(it.getUser());
System.out.println("Add contact!\n");
}
}
}

System.out.println(r.toXML());
}

@@ -571,11 +598,22 @@ private void readPackets(Thread thisThread, InputStream istream) {
}
}

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

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

Packet p = waconnection.getNextPacket();
while (p != null) {
for (PacketCollector collector: getPacketCollectors()) {
collector.processPacket(p);
}
listenerExecutor.submit(new ListenerNotification(p));
p = waconnection.getNextPacket();
}
} while (r >= 0);
}catch (IOException e) {
System.out.println("Error!\n" + e.toString());
@@ -711,5 +749,30 @@ public void reset() throws IOException {
wrappedReader.reset();
}
}



/**
* A runnable to notify all listeners of a packet.
*/
private class ListenerNotification implements Runnable {

private Packet packet;

public ListenerNotification(Packet packet) {
this.packet = packet;
}

public void run() {
for (ListenerWrapper listenerWrapper : recvListeners.values()) {
try {
listenerWrapper.notifyListener(packet);
} catch (RuntimeException e) {
e.printStackTrace();
}
}
}
}

}

84 changes: 84 additions & 0 deletions src/net/davidgf/android/WhatsappConnection.java
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
*/

package net.davidgf.android;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import java.util.*;

@@ -28,6 +30,8 @@ private enum SessionStatus { SessionNone, SessionConnecting, SessionWaitingChall

private String account_type, account_status, account_expiration, account_creation;
private String mypresence;

private Vector <Packet> received_packets;

public WhatsappConnection(String phone, String pass, String nick) {
session_key = new byte[20];
@@ -38,6 +42,7 @@ public WhatsappConnection(String phone, String pass, String nick) {
this.nickname = nick;
this.mypresence = "available";
outbuffer = new DataBuffer();
received_packets = new Vector <Packet>();
}

public Tree read_tree(DataBuffer data) {
@@ -169,6 +174,31 @@ else if (t.getTag().equals("iq")) {
this.doPong(t.getAttribute("id"),t.getAttribute("from"));
}
}
else if (t.getTag().equals("message")) {
if (t.hasAttributeValue("type","chat") && t.hasAttribute("from")) {
long time = 0;
if (t.hasAttribute("t"))
time = Integer.parseInt(t.getAttribute("t"));
String from = t.getAttribute("from");
String id = t.getAttribute("id");
String author = t.getAttribute("author");

Tree tb = t.getChild("body");
if (tb != null) {
this.receiveMessage(
new ChatMessage(from,time,id,MiscUtil.bytesToUTF8(tb.getData()),author));
}
}

// Received ACK
if (t.hasAttribute("type") && t.hasAttribute("from") && !t.hasChild("received")) {
DataBuffer reply = generateResponse(t.getAttribute("from"),
t.getAttribute("type"),
t.getAttribute("id"));
outbuffer = outbuffer.addBuf(reply);

}
}
/*else if (treelist[i].getTag() == "failure") {
if (conn_status == SessionWaitingAuthOK)
this->notifyError(errorAuth);
@@ -177,6 +207,28 @@ else if (t.getTag().equals("iq")) {
}*/
}

private DataBuffer generateResponse(final String from, final String type, final String id) {
Tree received = new Tree("received",new HashMap < String,String >() {{ put("xmlns","urn:xmpp:receipts"); }} );
Tree mes = new Tree("message",new HashMap < String,String >() {{
put("to",from); put("type",type); put("id",id); }} );
mes.addChild(received);
return serialize_tree(mes,true);
}


private void receiveMessage(AbstractMessage msg) {
received_packets.add(msg.serializePacket());
}

public Packet getNextPacket() {
if (received_packets.size() == 0)
return null;
Packet r = received_packets.get(0);
received_packets.remove(0);

return r;
}

private void notifyMyPresence() {
// Send the nickname and the current status
Tree pres = new Tree("presence", new HashMap < String,String >() {{ put("name",nickname); put("type",mypresence); }} );
@@ -319,6 +371,38 @@ void sendAuthResponse() {
return new byte[0];
}
}

public abstract class AbstractMessage {
protected String from, id, author;
protected long time;

public AbstractMessage(String from, long time, String id, String author) {
this.from = from;
this.id = id;
this.author = author;
this.time = time;
}

public abstract Packet serializePacket();
}

public class ChatMessage extends AbstractMessage {
private String message;
public ChatMessage(String from, long time, String id, String message, String author) {
super(from, time, id, author);
this.message = message;
}

public Packet serializePacket() {
Message message = new Message();
message.setTo(this.from);
message.setFrom("");
message.setType(Message.Type.chat);
message.setBody(this.message);

return message;
}
}
}