/soc/2015/nakulgulati/main: 1a1aec65df40: hangouts: channel, lon...

Nakul at rock.pidgin.im Nakul at rock.pidgin.im
Fri Aug 21 23:09:22 EDT 2015


Changeset: 1a1aec65df40e375e2de68e58808d9250a73a2fc
Author:	 Nakul Gulati
Date:	 2015-08-22 04:27 +0530
Branch:	 hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/1a1aec65df40

Description:

hangouts: channel, long poll

diffstat:

 libpurple/protocols/hangouts/api.c      |  125 ++++++++++++++++++++-----------
 libpurple/protocols/hangouts/api.h      |    5 +-
 libpurple/protocols/hangouts/channel.c  |   17 +++-
 libpurple/protocols/hangouts/hangouts.c |    6 +-
 libpurple/protocols/hangouts/json.c     |  120 ++++++++++++++++++++++++++++++
 libpurple/protocols/hangouts/json.h     |   29 +++++++
 6 files changed, 252 insertions(+), 50 deletions(-)

diffs (truncated from 500 to 300 lines):

diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -78,13 +78,12 @@ struct _HangoutsApiHttpInfo
 };
 
 const gchar *chat_init_path[] =
-  {
-  "$.ds:7[0][2]",
-  "$.ds:34[0][2]",
-  "$.ds:2[0][4]",
-  "$.ds:2[0][6]",
-  "$.ds:4[0][7]",
-  "$.ds:21[0][1][4]"
+  { "$.ds:7[0][2]", /*api-key*/
+  "$.ds:34[0][2]", /*email*/
+  "$.ds:2[0][4]", /*header-date*/
+  "$.ds:4[0][7]", /*header-id*/
+  "$.ds:2[0][6]", /*header-version*/
+  "$.ds:21[0][1][4]" /*timestamp*/
   };
 
 static void
@@ -119,19 +118,15 @@ hangouts_api_class_init (HangoutsApiClas
   gobject_class->dispose = hangouts_api_dispose;
   g_type_class_add_private (klass, sizeof(HangoutsApiPrivate));
 
-  g_signal_new ("auth",
-    		G_TYPE_FROM_CLASS(klass),
-    		G_SIGNAL_ACTION, 0,
-    		NULL, NULL,
-    		g_cclosure_marshal_VOID__VOID,
-      		G_TYPE_NONE,
-      		NULL);
-  
-  g_signal_new ("get-chat-init",
-		G_TYPE_FROM_CLASS(klass),
-		G_SIGNAL_ACTION, 0,
-		NULL, NULL,
-		g_cclosure_marshal_VOID__VOID,
+  g_signal_new ("auth", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0,
+  NULL,
+		NULL, g_cclosure_marshal_VOID__VOID,
+		G_TYPE_NONE,
+		NULL);
+
+  g_signal_new ("get-chat-init", G_TYPE_FROM_CLASS(klass), G_SIGNAL_ACTION, 0,
+  NULL,
+		NULL, g_cclosure_marshal_VOID__VOID,
 		G_TYPE_NONE,
 		NULL);
 }
@@ -149,16 +144,23 @@ hangouts_api_init (HangoutsApi *api)
 static void
 hangouts_api_cb_connect (HangoutsChannel *channel, gpointer *data)
 {
+  /*HangoutsApi *api = data;
+  gchar *str = hangouts_api_get_request_header (api);
+  purple_debug_misc ("hangouts-api", "Results: %s\n", str);*/
   if (!hangouts_channel_is_subscribed (channel))
     {
-      hangouts_channel_subscribe (channel);
+      //hangouts_channel_subscribe (channel);
     }
 }
 
 static void
 hangouts_api_cb_subscribe (HangoutsChannel *channel, gpointer *data)
 {
+  HangoutsApi *api;
+
+  api = data;
   purple_debug_misc ("hangouts-api", "channel subscribed");
+  //hangouts_api_syncallevents (api);
 }
 
 HangoutsApi *
@@ -173,14 +175,34 @@ hangouts_api_new (PurpleConnection *gc)
   priv->gc = gc;
 
   g_signal_connect(priv->channel, "connect",
-		   G_CALLBACK(hangouts_api_cb_connect), NULL);
+		   G_CALLBACK(hangouts_api_cb_connect), api);
   g_signal_connect(priv->channel, "subscribe",
-		   G_CALLBACK(hangouts_api_cb_subscribe), NULL);
+		   G_CALLBACK(hangouts_api_cb_subscribe), api);
 
   return api;
 }
 
 void
+hangouts_api_set_client_id (PurpleConnection *gc, const gchar *chunk)
+{
+  purple_debug_misc ("hangouts-api", "chunk with cid: %s\n", chunk);
+  HangoutsApi *api;
+  JsonNode *node;
+  gchar *string;
+
+  api = purple_connection_get_protocol_data (gc);
+  node = hangouts_json_node_new (chunk, -1, FALSE);
+
+  string = hangouts_json_node_get_str (node, "$[0][1][0]['p']", NULL);
+  node = hangouts_json_node_new (string, -1, FALSE);
+
+  /*string = hangouts_json_node_get_str (node, "$['3']['2']", NULL);*/
+
+  /*gchar *str = hangouts_api_get_request_header (api);*/
+  purple_debug_misc ("hangouts-api", "Results: %s\n", string);
+}
+
+void
 hangouts_api_set_refresh_token (HangoutsApi *api, const gchar *refresh_token)
 {
   HangoutsApiPrivate *priv;
@@ -200,9 +222,9 @@ hangouts_api_get_connection (HangoutsApi
 }
 
 static void
-hangouts_api_auth_with_code_cb (PurpleHttpConnection *http_conn,
-				PurpleHttpResponse *response,
-				gpointer user_data)
+hangouts_api_oauth_with_code_cb (PurpleHttpConnection *http_conn,
+				 PurpleHttpResponse *response,
+				 gpointer user_data)
 {
   HangoutsApi *api;
   HangoutsApiPrivate *priv;
@@ -241,7 +263,7 @@ hangouts_api_auth_with_code_cb (PurpleHt
 }
 
 void
-hangouts_api_auth_with_code (HangoutsApi *api, gchar *auth_code)
+hangouts_api_oauth_with_code (HangoutsApi *api, const gchar *auth_code)
 {
   PurpleHttpRequest *request;
   PurpleConnection *gc;
@@ -266,7 +288,7 @@ hangouts_api_auth_with_code (HangoutsApi
 				  "application/x-www-form-urlencoded");
   purple_http_request_set_contents (request, request_data, -1);
 
-  purple_http_request (gc, request, hangouts_api_auth_with_code_cb, api);
+  purple_http_request (gc, request, hangouts_api_oauth_with_code_cb, api);
 }
 
 static void
@@ -449,15 +471,25 @@ hangouts_api_get_request_header (Hangout
 {
 
   HangoutsApiPrivate *priv = api->priv;
+  JsonBuilder *builder;
+  gchar *header;
 
-  gchar *header = g_strdup_printf ("["
-				   "[6, 3, %s, %s],"
-				   "[%s, %s],"
-				   "null,"
-				   "\"en\""
-				   "]",
-				   priv->header_version, priv->header_date,
-				   priv->client_id, priv->header_id);
+  builder = hangouts_json_builder_new (JSON_NODE_ARRAY);
+
+  hangouts_json_builder_array_begin (builder, NULL);
+  hangouts_json_builder_add_int (builder, NULL, 13);
+  hangouts_json_builder_add_int (builder, NULL, 1);
+  hangouts_json_builder_add_str (builder, NULL, priv->header_version);
+  hangouts_json_builder_add_str (builder, NULL, priv->header_date);
+  hangouts_json_builder_array_end (builder);
+  hangouts_json_builder_array_begin (builder, NULL);
+  hangouts_json_builder_add_str (builder, NULL, priv->client_id);
+  hangouts_json_builder_add_str (builder, NULL, priv->header_id);
+  hangouts_json_builder_array_end (builder);
+  hangouts_json_builder_add_null (builder, NULL);
+  hangouts_json_builder_add_str (builder, NULL, "en");
+
+  header = hangouts_json_builder_close (builder, JSON_NODE_ARRAY);
 
   return header;
 }
@@ -516,7 +548,10 @@ static void
 hangouts_api_syncallevents_cb (PurpleHttpConnection *http_conn,
 			       PurpleHttpResponse *response, gpointer user_data)
 {
+  const gchar *raw;
 
+  raw = purple_http_response_get_data (response, NULL);
+  purple_debug_misc ("hangouts-api-syncall", "%s", raw);
 }
 
 void
@@ -526,21 +561,23 @@ hangouts_api_syncallevents (HangoutsApi 
     { hangouts_api_syncallevents_cb, "conversations/syncallnewevents", "POST" };
 
   gchar *header, *body;
-  gchar *time_stamp;
+  guint64 timestamp;
 
-  time_stamp = api->priv->sync_timestamp;
+  timestamp = api->priv->sync_timestamp;
   header = hangouts_api_get_request_header (api);
-  body = g_strdup_printf ("["
+  body = g_strdup_printf ("[["
 			  "%s, "
-			  "%s,"
+			  "%u,"
 			  "[], "
 			  "null, "
 			  "[], "
 			  "False, "
 			  "[], "
 			  "1048576"
-			  "]",
-			  header, g_time_val_to_iso8601 (time_stamp));
+			  "]]",
+			  header, timestamp);
+
+  purple_debug_misc ("hangouts-api-sync", "body: %s", body);
 
   hangouts_api_request (api, &info, FALSE, body);
 }
@@ -620,11 +657,11 @@ hangouts_api_setactiveclient (HangoutsAp
       is_active_flag = "False";
     }
 
-  body = g_strdup_printf ("["
+  body = g_strdup_printf ("[["
 			  "%s, "
 			  "%s, "
 			  "%s\/%s, "
-			  "%s]",
+			  "%s]]",
 			  header, is_active_flag, priv->email, priv->client_id,
 			  timeout_sec);
 
diff --git a/libpurple/protocols/hangouts/api.h b/libpurple/protocols/hangouts/api.h
--- a/libpurple/protocols/hangouts/api.h
+++ b/libpurple/protocols/hangouts/api.h
@@ -90,13 +90,16 @@ HangoutsApi *
 hangouts_api_new (PurpleConnection *gc);
 
 void
+hangouts_api_set_client_id (PurpleConnection *gc, const gchar *chunk);
+
+void
 hangouts_api_set_refresh_token (HangoutsApi *api, const gchar *refresh_token);
 
 PurpleConnection *
 hangouts_api_get_connection(HangoutsApi *api);
 
 void
-hangouts_api_auth_with_code (HangoutsApi *api, gchar *auth_code);
+hangouts_api_oauth_with_code (HangoutsApi *api, const gchar *auth_code);
 
 void
 hangouts_api_oauth_refresh_token (HangoutsApi *api);
diff --git a/libpurple/protocols/hangouts/channel.c b/libpurple/protocols/hangouts/channel.c
--- a/libpurple/protocols/hangouts/channel.c
+++ b/libpurple/protocols/hangouts/channel.c
@@ -83,6 +83,8 @@ hangouts_channel_new (PurpleConnection *
   priv = channel->priv;
   priv->cj = purple_http_cookie_jar_new ();
   priv->gc = gc;
+  priv->connected = FALSE;
+  priv->subscribed = FALSE;
 
   return channel;
 }
@@ -191,7 +193,8 @@ hangouts_channel_long_poll_cb (PurpleHtt
 			       PurpleHttpResponse *response, gpointer user_data)
 {
   HangoutsChannel *channel;
-  const gchar *raw;
+  const gchar *raw = purple_http_response_get_data(response,NULL);
+  purple_debug_misc("hangouts-channel", "long poll res: %s", raw);
 
   channel = user_data;
 
@@ -215,13 +218,21 @@ hangouts_channel_long_poll_watcher (Purp
   channel = user_data;
   priv = channel->priv;
 
+  chunk = g_strdup (http_conn->response_buffer->str);
+
   if (processed > 0 && !priv->connected)
     {
       priv->connected = TRUE;
-      g_signal_emit_by_name (channel, "connect");
+      //g_signal_emit_by_name (channel, "connect");
+      hangouts_channel_subscribe(channel);
     }
   //TODO: parse chunk
-  chunk = g_strdup (http_conn->response_buffer->str);
+
+  if(strstr(chunk, "lcsw")){
+      hangouts_api_set_client_id(priv->gc, chunk);
+  }
+
+  purple_debug_misc("hangouts-longpoll", "chunk: %s", chunk);
 }
 



More information about the Commits mailing list