aboutsummaryrefslogtreecommitdiffstats
path: root/facebook/facebook-api.c
diff options
context:
space:
mode:
authorjgeboski <jgeboski@gmail.com>2015-01-14 22:53:46 -0500
committerjgeboski <jgeboski@gmail.com>2015-01-14 22:53:46 -0500
commit68c46dd797ad24642b69d7dd8914a521cd20e87f (patch)
treee27cc52960aff8c52c932db762505a483eab8747 /facebook/facebook-api.c
parent53e9a0fe52b47f7d05039d0430c4e4b9928cecf6 (diff)
downloadbitlbee-facebook-68c46dd797ad24642b69d7dd8914a521cd20e87f.tar.gz
bitlbee-facebook-68c46dd797ad24642b69d7dd8914a521cd20e87f.tar.bz2
bitlbee-facebook-68c46dd797ad24642b69d7dd8914a521cd20e87f.tar.xz
Implemented message sending queue
Diffstat (limited to 'facebook/facebook-api.c')
-rw-r--r--facebook/facebook-api.c50
1 files changed, 47 insertions, 3 deletions
diff --git a/facebook/facebook-api.c b/facebook/facebook-api.c
index ed94853..5187c81 100644
--- a/facebook/facebook-api.c
+++ b/facebook/facebook-api.c
@@ -343,6 +343,40 @@ static void fb_api_cb_mqtt_connack(fb_mqtt_t *mqtt, gpointer data)
}
/**
+ * Handles message responses which publish the next message queued.
+ *
+ * @param api The #fb_api.
+ * @param pload The message payload.
+ **/
+static void fb_api_cb_publish_mr(fb_api_t *api, const GByteArray *pload)
+{
+ json_value *json;
+ gchar *msg;
+ gboolean res;
+
+ json = fb_api_json_new(api, (gchar*) pload->data, pload->len);
+
+ if (json == NULL)
+ return;
+
+ if (!fb_json_bool_chk(json, "succeeded", &res) || !res) {
+ fb_api_error(api, FB_API_ERROR, "Failed to send message");
+ goto finish;
+ }
+
+ msg = g_queue_pop_head(api->msgs);
+ g_free(msg);
+
+ if (!g_queue_is_empty(api->msgs)) {
+ msg = g_queue_peek_head(api->msgs);
+ fb_api_publish(api, "/send_message2", msg, NULL);
+ }
+
+finish:
+ json_value_free(json);
+}
+
+/**
* Handles messages which are to be published to the user.
*
* @param api The #fb_api.
@@ -529,7 +563,9 @@ static void fb_api_cb_mqtt_publish(fb_mqtt_t *mqtt, const gchar *topic,
fb_util_hexdump(bytes, 2, "Reading message:");
- if (g_ascii_strcasecmp(topic, "/t_ms") == 0)
+ if (g_ascii_strcasecmp(topic, "/send_message_response") == 0)
+ fb_api_cb_publish_mr(api, bytes);
+ else if (g_ascii_strcasecmp(topic, "/t_ms") == 0)
fb_api_cb_publish_ms(api, bytes);
else if (g_ascii_strcasecmp(topic, "/t_p") == 0)
fb_api_cb_publish_p(api, bytes);
@@ -568,6 +604,7 @@ fb_api_t *fb_api_new(const fb_api_funcs_t *funcs, gpointer data)
api->data = data;
api->http = fb_http_new(FB_API_AGENT);
api->mqtt = fb_mqtt_new(&muncs, api);
+ api->msgs = g_queue_new();
return api;
}
@@ -616,6 +653,7 @@ void fb_api_free(fb_api_t *api)
if (api->err != NULL)
g_error_free(api->err);
+ g_queue_free_full(api->msgs, g_free);
fb_mqtt_free(api->mqtt);
fb_http_free(api->http);
@@ -855,7 +893,8 @@ void fb_api_disconnect(fb_api_t *api)
**/
void fb_api_message(fb_api_t *api, const gchar *uid, const gchar *msg)
{
- guint64 msgid;
+ guint64 msgid;
+ gchar *rmsg;
g_return_if_fail(api != NULL);
g_return_if_fail(uid != NULL);
@@ -863,12 +902,17 @@ void fb_api_message(fb_api_t *api, const gchar *uid, const gchar *msg)
msgid = FB_API_MSGID(g_get_real_time() / 1000, g_random_int());
- fb_api_publish(api, "/send_message2", "{"
+ rmsg = g_strdup_printf("{"
"\"body\":\"%s\","
"\"to\":\"%s\","
"\"sender_fbid\":\"%s\","
"\"msgid\":%" G_GUINT64_FORMAT
"}", msg, uid, api->uid, msgid);
+
+ if (g_queue_is_empty(api->msgs))
+ fb_api_publish(api, "/send_message2", rmsg, NULL);
+
+ g_queue_push_tail(api->msgs, rmsg);
}
/**