/soc/2015/jgeboski/facebook: 68220e4ccb15: facebook: upgraded MQ...

James Geboski jgeboski at gmail.com
Sat Aug 8 19:44:09 EDT 2015


Changeset: 68220e4ccb15f8a63f69c006dddfee13ccca22a7
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-08-08 19:43 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/68220e4ccb15

Description:

facebook: upgraded MQTT version to MQTToT

diffstat:

 libpurple/protocols/facebook/api.c    |  102 ++++++++++++++++++++++++++-------
 libpurple/protocols/facebook/mqtt.c   |   26 +++-----
 libpurple/protocols/facebook/mqtt.h   |   13 ++--
 libpurple/protocols/facebook/thrift.c |   22 ++++++-
 libpurple/protocols/facebook/thrift.h |    3 +
 5 files changed, 115 insertions(+), 51 deletions(-)

diffs (272 lines):

diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c
--- a/libpurple/protocols/facebook/api.c
+++ b/libpurple/protocols/facebook/api.c
@@ -577,36 +577,92 @@ fb_api_cb_mqtt_error(FbMqtt *mqtt, GErro
 static void
 fb_api_cb_mqtt_open(FbMqtt *mqtt, gpointer data)
 {
+	const GByteArray *bytes;
 	FbApi *api = data;
 	FbApiPrivate *priv = api->priv;
-	gchar *json;
-	JsonBuilder *bldr;
+	FbThrift *thft;
+	GByteArray *cytes;
 
 	static guint8 flags = FB_MQTT_CONNECT_FLAG_USER |
 	                      FB_MQTT_CONNECT_FLAG_PASS |
 	                      FB_MQTT_CONNECT_FLAG_CLR;
 
-	bldr = fb_json_bldr_new(JSON_NODE_OBJECT);
-	fb_json_bldr_add_bool(bldr, "chat_on", TRUE);
-	fb_json_bldr_add_bool(bldr, "fg", FALSE);
-	fb_json_bldr_add_bool(bldr, "no_auto_fg", TRUE);
-	fb_json_bldr_add_int(bldr, "mqtt_sid", priv->mid);
-	fb_json_bldr_add_int(bldr, "nwt", 1);
-	fb_json_bldr_add_int(bldr, "nwst", 0);
-	fb_json_bldr_add_str(bldr, "a", FB_API_AGENT);
-	fb_json_bldr_add_str(bldr, "d", priv->did);
-	fb_json_bldr_add_str(bldr, "pf", "jz");
-	fb_json_bldr_add_strf(bldr, "u", "%" FB_ID_FORMAT, priv->uid);
-
-	json = fb_json_bldr_close(bldr, JSON_NODE_OBJECT, NULL);
-	fb_mqtt_connect(mqtt,
-		flags,       /* Flags */
-		priv->cid,   /* Client identifier */
-		json,        /* Will message */
-		priv->token, /* Username */
-		NULL);
-
-	g_free(json);
+	thft = fb_thrift_new(NULL, 0, TRUE);
+
+	/* Write the client identifier */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_STRING, 1);
+	fb_thrift_write_str(thft, priv->cid);
+
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_STRUCT, 4);
+
+	/* Write the user identifier */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I64, 5);
+	fb_thrift_write_i64(thft, priv->uid);
+
+	/* Write the information string */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_STRING, 6);
+	fb_thrift_write_str(thft, "");
+
+	/* Write the UNKNOWN ("cp"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I64, 7);
+	fb_thrift_write_i64(thft, 23);
+
+	/* Write the UNKNOWN ("ecp"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I64, 8);
+	fb_thrift_write_i64(thft, 26);
+
+	/* Write the UNKNOWN */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I32, 9);
+	fb_thrift_write_i32(thft, 1);
+
+	/* Write the UNKNOWN ("chat_on"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_BOOL, 10);
+	fb_thrift_write_bool(thft, TRUE);
+
+	/* Write the UNKNOWN ("no_auto_fg"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_BOOL, 11);
+	fb_thrift_write_bool(thft, TRUE);
+
+	/* Write the device identifier */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_STRING, 12);
+	fb_thrift_write_str(thft, priv->did);
+
+	/* Write the UNKNOWN ("fg"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_BOOL, 13);
+	fb_thrift_write_bool(thft, TRUE);
+
+	/* Write the UNKNOWN ("nwt"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I32, 14);
+	fb_thrift_write_i32(thft, 1);
+
+	/* Write the UNKNOWN ("nwst"?) */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I32, 15);
+	fb_thrift_write_i32(thft, 0);
+
+	/* Write the MQTT identifier */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_I64, 16);
+	fb_thrift_write_i64(thft, priv->mid);
+
+	/* Write the UNKNOWN */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_LIST, 18);
+	fb_thrift_write_list(thft, FB_THRIFT_TYPE_I32, 0);
+	fb_thrift_write_stop(thft);
+
+	/* Write the token */
+	fb_thrift_write_field(thft, FB_THRIFT_TYPE_STRING, 19);
+	fb_thrift_write_str(thft, priv->token);
+
+	/* Write the STOP for the struct */
+	fb_thrift_write_stop(thft);
+
+	bytes = fb_thrift_get_bytes(thft);
+	cytes = fb_util_zcompress(bytes);
+
+	fb_util_debug_hexdump(FB_UTIL_DEBUG_INFO, bytes, "Writing connect");
+	fb_mqtt_connect(mqtt, flags, cytes);
+
+	g_byte_array_free(cytes, TRUE);
+	g_object_unref(thft);
 }
 
 static void
diff --git a/libpurple/protocols/facebook/mqtt.c b/libpurple/protocols/facebook/mqtt.c
--- a/libpurple/protocols/facebook/mqtt.c
+++ b/libpurple/protocols/facebook/mqtt.c
@@ -598,35 +598,27 @@ fb_mqtt_open(FbMqtt *mqtt, const gchar *
 }
 
 void
-fb_mqtt_connect(FbMqtt *mqtt, guint8 flags, const gchar *cid, ...)
+fb_mqtt_connect(FbMqtt *mqtt, guint8 flags, const GByteArray *pload)
 {
-	const gchar *str;
 	FbMqttMessage *msg;
-	va_list ap;
 
-	g_return_if_fail(cid != NULL);
 	g_return_if_fail(!fb_mqtt_connected(mqtt, FALSE));
+	g_return_if_fail(pload != NULL);
 
 	/* Facebook always sends a CONNACK, use QoS1 */
 	flags |= FB_MQTT_CONNECT_FLAG_QOS1;
 
 	msg = fb_mqtt_message_new(FB_MQTT_MESSAGE_TYPE_CONNECT, 0);
-	fb_mqtt_message_write_str(msg, FB_MQTT_NAME);  /* Protocol name */
-	fb_mqtt_message_write_byte(msg, FB_MQTT_VERS); /* Protocol version */
-	fb_mqtt_message_write_byte(msg, flags);        /* Flags */
-	fb_mqtt_message_write_u16(msg, FB_MQTT_KA);    /* Keep alive */
-	fb_mqtt_message_write_str(msg, cid);           /* Client identifier */
+	fb_mqtt_message_write_str(msg, FB_MQTT_NAME);   /* Protocol name */
+	fb_mqtt_message_write_byte(msg, FB_MQTT_LEVEL); /* Protocol level */
+	fb_mqtt_message_write_byte(msg, flags);         /* Flags */
+	fb_mqtt_message_write_u16(msg, FB_MQTT_KA);     /* Keep alive */
 
-	va_start(ap, cid);
+	fb_mqtt_message_write(msg, pload->data, pload->len);
+	fb_mqtt_write(mqtt, msg);
 
-	while ((str = va_arg(ap, const gchar*)) != NULL) {
-		fb_mqtt_message_write_str(msg, str);
-	}
-
-	va_end(ap);
-	fb_mqtt_write(mqtt, msg);
+	fb_mqtt_timeout(mqtt);
 	g_object_unref(msg);
-	fb_mqtt_timeout(mqtt);
 }
 
 gboolean
diff --git a/libpurple/protocols/facebook/mqtt.h b/libpurple/protocols/facebook/mqtt.h
--- a/libpurple/protocols/facebook/mqtt.h
+++ b/libpurple/protocols/facebook/mqtt.h
@@ -27,11 +27,11 @@
 
 #include "connection.h"
 
-#define FB_MQTT_NAME "MQIsdp"
-#define FB_MQTT_VERS 3
-#define FB_MQTT_KA   60
-#define FB_MQTT_HOST "mqtt.facebook.com"
-#define FB_MQTT_PORT 443
+#define FB_MQTT_NAME  "MQTToT"
+#define FB_MQTT_LEVEL 3
+#define FB_MQTT_KA    60
+#define FB_MQTT_HOST  "mqtt.facebook.com"
+#define FB_MQTT_PORT  443
 
 #define FB_MQTT_TIMEOUT_CONN (FB_MQTT_KA * 1500)
 #define FB_MQTT_TIMEOUT_PING (FB_MQTT_KA * 1000)
@@ -171,8 +171,7 @@ void
 fb_mqtt_open(FbMqtt *mqtt, const gchar *host, gint port);
 
 void
-fb_mqtt_connect(FbMqtt *mqtt, guint8 flags, const gchar *cid, ...)
-                G_GNUC_NULL_TERMINATED;
+fb_mqtt_connect(FbMqtt *mqtt, guint8 flags, const GByteArray *pload);
 
 gboolean
 fb_mqtt_connected(FbMqtt *mqtt, gboolean error);
diff --git a/libpurple/protocols/facebook/thrift.c b/libpurple/protocols/facebook/thrift.c
--- a/libpurple/protocols/facebook/thrift.c
+++ b/libpurple/protocols/facebook/thrift.c
@@ -77,6 +77,7 @@ fb_thrift_new(GByteArray *bytes, guint o
 		priv->bytes  = bytes;
 		priv->offset = offset;
 	} else {
+		priv->bytes  = g_byte_array_new();
 		priv->flags |= FB_THRIFT_FLAG_INTERNAL;
 	}
 
@@ -88,6 +89,16 @@ fb_thrift_new(GByteArray *bytes, guint o
 	return thft;
 }
 
+const GByteArray *
+fb_thrift_get_bytes(FbThrift *thft)
+{
+	FbThriftPrivate *priv;
+
+	g_return_val_if_fail(FB_IS_THRIFT(thft), NULL);
+	priv = thft->priv;
+	return priv->bytes;
+}
+
 guint
 fb_thrift_get_pos(FbThrift *thft)
 {
@@ -728,7 +739,7 @@ fb_thrift_write_i16(FbThrift *thft, gint
 void
 fb_thrift_write_vi16(FbThrift *thft, guint16 u16)
 {
-	fb_thrift_write_vi32(thft, u16);
+	fb_thrift_write_vi64(thft, u16);
 }
 
 void
@@ -755,7 +766,6 @@ fb_thrift_write_vi32(FbThrift *thft, gui
 	fb_thrift_write_vi64(thft, u32);
 }
 
-
 void
 fb_thrift_write_i64(FbThrift *thft, gint64 i64)
 {
@@ -790,10 +800,14 @@ fb_thrift_write_vi64(FbThrift *thft, gui
 
 	do {
 		last = (u64 & ~0x7F) == 0;
-		byte = !last ? ((u64 & 0x7F) | 0x80) : (u64 & 0x0F);
+		byte = u64 & 0x7F;
+
+		if (!last) {
+			byte |= 0x80;
+			u64 >>= 7;
+		}
 
 		fb_thrift_write_byte(thft, byte);
-		u64 >>= 7;
 	} while (!last);
 }
 
diff --git a/libpurple/protocols/facebook/thrift.h b/libpurple/protocols/facebook/thrift.h
--- a/libpurple/protocols/facebook/thrift.h
+++ b/libpurple/protocols/facebook/thrift.h
@@ -84,6 +84,9 @@ fb_thrift_get_type(void);
 FbThrift *
 fb_thrift_new(GByteArray *bytes, guint offset, gboolean compact);
 
+const GByteArray *
+fb_thrift_get_bytes(FbThrift *thft);
+
 guint
 fb_thrift_get_pos(FbThrift *thft);
 



More information about the Commits mailing list