aboutsummaryrefslogtreecommitdiffstats
path: root/facebook/facebook.c
diff options
context:
space:
mode:
Diffstat (limited to 'facebook/facebook.c')
-rw-r--r--facebook/facebook.c67
1 files changed, 64 insertions, 3 deletions
diff --git a/facebook/facebook.c b/facebook/facebook.c
index 2561674..f96b05b 100644
--- a/facebook/facebook.c
+++ b/facebook/facebook.c
@@ -16,6 +16,41 @@
*/
#include "facebook.h"
+#include "facebook-util.h"
+
+/**
+ * Implemented #fb_api_funcs->error().
+ *
+ * @param api The #fb_api.
+ * @param err The #GError.
+ * @param data The user defined data, which is #fb_data.
+ **/
+static void fb_cb_api_error(fb_api_t *api, GError *err, gpointer data)
+{
+ fb_data_t *fata = data;
+
+ FB_UTIL_DEBUGLN("Error: %s", err->message);
+ imcb_error(fata->ic, "%s", err->message);
+ imc_logout(fata->ic, TRUE);
+}
+
+/**
+ * Implemented #fb_api_funcs->auth().
+ *
+ * @param api The #fb_api.
+ * @param data The user defined data, which is #fb_data.
+ **/
+static void fb_cb_api_auth(fb_api_t *api, gpointer data)
+{
+ fb_data_t *fata = data;
+ account_t *acc = fata->ic->acc;
+
+ set_setstr(&acc->set, "token", api->token);
+ imcb_log(fata->ic, "Authentication finished");
+
+ account_off(acc->bee, acc);
+ account_on(acc->bee, acc);
+}
/**
* Creates a new #fb_data with an #account. The returned #fb_data
@@ -23,19 +58,27 @@
*
* @param acc The #account.
*
- * @return The #fb_data_t or NULL on error.
+ * @return The #fb_data or NULL on error.
**/
fb_data_t *fb_data_new(account_t *acc)
{
fb_data_t *fata;
+ static const fb_api_funcs_t funcs = {
+ .error = fb_cb_api_error,
+ .auth = fb_cb_api_auth
+ };
+
g_return_val_if_fail(acc != NULL, NULL);
fata = g_new0(fb_data_t, 1);
+ fata->api = fb_api_new(&funcs, fata);
fata->ic = imcb_new(acc);
fata->ic->proto_data = fata;
+ fata->api->token = g_strdup(set_getstr(&acc->set, "token"));
+
return fata;
}
@@ -49,9 +92,11 @@ void fb_data_free(fb_data_t *fata)
if (G_UNLIKELY(fata == NULL))
return;
+ fb_api_free(fata->api);
g_free(fata);
}
+
/**
* Implements #prpl->init(). This initializes an account.
*
@@ -59,7 +104,10 @@ void fb_data_free(fb_data_t *fata)
**/
static void fb_init(account_t *acc)
{
+ set_t *s;
+ s = set_add(&acc->set, "token", NULL, NULL, acc);
+ s->flags = SET_NULL_OK | SET_HIDDEN | SET_PASSWORD;
}
/**
@@ -69,7 +117,18 @@ static void fb_init(account_t *acc)
**/
static void fb_login(account_t *acc)
{
+ fb_data_t *fata;
+
+ fata = fb_data_new(acc);
+ imcb_log(fata->ic, "Connecting");
+
+ if (fata->api->token == NULL) {
+ imcb_log(fata->ic, "Requesting authentication token");
+ fb_api_auth(fata->api, acc->user, acc->pass);
+ return;
+ }
+ imcb_connected(fata->ic);
}
/**
@@ -79,7 +138,9 @@ static void fb_login(account_t *acc)
**/
static void fb_logout(struct im_connection *ic)
{
+ fb_data_t *fata = ic->proto_data;
+ fb_data_free(fata);
}
/**
@@ -216,7 +277,7 @@ static void fb_auth_deny(struct im_connection *ic, const char *who)
/**
* Implements #prpl->buddy_data_add(). This adds data to the buddy.
*
- * @param bu The #bee_user_t.
+ * @param bu The #bee_user.
**/
static void fb_buddy_data_add(struct bee_user *bu)
{
@@ -226,7 +287,7 @@ static void fb_buddy_data_add(struct bee_user *bu)
/**
* Implements #prpl->buddy_data_free(). This frees the buddy data.
*
- * @param bu The #bee_user_t.
+ * @param bu The #bee_user.
**/
static void fb_buddy_data_free(struct bee_user *bu)
{