/* telepathy-gruschler - A Telepathy connection manager for social networks. * Copyright (C) 2009 Mathias Hasselmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include #include #include #include #include static RestXmlNode * get_xml (RestProxyCall *call) { static RestXmlParser *parser = NULL; RestXmlNode *root, *node; if (parser == NULL) parser = rest_xml_parser_new (); root = rest_xml_parser_parse_from_data (parser, rest_proxy_call_get_payload (call), rest_proxy_call_get_payload_length (call)); if (!g_strcmp0 (root->name, "error_response")) { node = rest_xml_node_find (root, "error_msg"); g_error ("Error from facebook: %s", node->content); } g_object_unref (call); return root; } static void facebook_proxy_login (RestProxy *proxy, const char *token, const char *email, const char *pass) { char *url; url = facebook_proxy_build_login_url (FACEBOOK_PROXY (proxy), token); g_print ("login URL: %s\n", url); SoupSession *session; SoupMessage *msg; session = soup_session_sync_new_with_options (SOUP_SESSION_USER_AGENT, "Firefox/3.0 ", SOUP_SESSION_ADD_FEATURE, soup_cookie_jar_new (), NULL); msg = soup_message_new (SOUP_METHOD_GET, url); soup_session_send_message (session, msg); GMatchInfo *info, *info_params; GRegex *regex, *re_parms; const char *s; int start, end; g_free (url); url = NULL; regex = g_regex_new ("(.*?)", G_REGEX_CASELESS | G_REGEX_DOTALL, 0, NULL); if (g_regex_match_full (regex, msg->response_body->data, msg->response_body->length, 0, 0, &info, NULL)) { url = g_match_info_fetch (info, 1); g_match_info_fetch_pos (info, 2, &start, &end); } g_match_info_free (info); g_regex_unref (regex); regex = g_regex_new ("", G_REGEX_CASELESS | G_REGEX_DOTALL, 0, NULL); re_parms = g_regex_new ("\\b(\\S+?)=\"(.*?)\"", G_REGEX_CASELESS | G_REGEX_DOTALL, 0, NULL); GHashTable *params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); if (g_regex_match_full (regex, s = msg->response_body->data + start, end - start, 0, 0, &info, NULL)) { do { char *name = NULL, *value = NULL; g_match_info_fetch_pos (info, 1, &start, &end); if (g_regex_match_full (re_parms, s + start, end - start, 0, 0, &info_params, NULL)) { do { char *param = g_match_info_fetch (info_params, 1); if (!g_strcmp0 (param, "name")) name = g_match_info_fetch (info_params, 2); else if (!g_strcmp0 (param, "value")) value = g_match_info_fetch (info_params, 2); g_free (param); } while (g_match_info_next (info_params, NULL)); } if (name && value) { if (!g_strcmp0 (name, "charset_test")) { g_free (value); g_free (name); continue; } if (!g_strcmp0 (name, "email")) { g_free (value); value = g_strdup (email); } else if (!g_strcmp0 (name, "pass")) { g_free (value); value = g_strdup (pass); } g_hash_table_insert (params, name, value); } } while (g_match_info_next (info, NULL)); } g_match_info_free (info); g_regex_unref (regex); g_object_unref (msg); char *form = soup_form_encode_hash (params); msg = soup_message_new (SOUP_METHOD_POST, url); soup_message_set_request (msg, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, form, strlen (form)); soup_session_send_message (session, msg); g_file_set_contents ("blub.html", msg->response_body->data, msg->response_body->length, NULL); g_object_unref (msg); g_object_unref (session); } static char ** facebook_proxy_friends_get (RestProxy *proxy) { GError *error = NULL; RestXmlNode *root, *node; RestProxyCall *call; GPtrArray *uids; call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "friends.get"); if (!rest_proxy_call_run (call, NULL, &error)) g_error ("Cannot get friends: %s", error->message); root = get_xml (call); node = rest_xml_node_find (root, "uid"); uids = g_ptr_array_new (); while (node) { if (!g_strcmp0 ("uid", node->name)) g_ptr_array_add (uids, g_strdup (node->content)); node = node->next; } rest_xml_node_unref (root); g_ptr_array_add (uids, NULL); return (char **) g_ptr_array_free (uids, FALSE); } int main (int argc, char **argv) { GError *error = NULL; RestProxyCall *call; RestProxy *proxy; RestXmlNode *root; char *token, *session_key, *secret, *uid; setlocale (LC_ALL, ""); g_thread_init (NULL); g_type_init (); proxy = facebook_proxy_new (GRUSCHLER_FACEBOOK_APIKEY, GRUSCHLER_FACEBOOK_SECRET); call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "auth.createToken"); if (!rest_proxy_call_run (call, NULL, &error)) g_error ("cannot get token: %s", error->message); root = get_xml (call); if (g_strcmp0 (root->name, "auth_createToken_response")) g_error ("Unexpected response to auto.createToken"); token = g_strdup (root->content); rest_xml_node_unref (root); g_print ("token: %s\n", token); facebook_proxy_login (proxy, token, GRUSCHLER_FACEBOOK_DEFAULT_EMAIL, GRUSCHLER_FACEBOOK_DEFAULT_PASSWORD); call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "auth.getSession"); rest_proxy_call_add_param (call, "auth_token", token); if (!rest_proxy_call_run (call, NULL, &error)) g_error ("Cannot get session: %s", error->message); root = get_xml (call); session_key = rest_xml_node_find (root, "session_key")->content; secret = rest_xml_node_find (root, "secret")->content; uid = rest_xml_node_find (root, "uid")->content; g_print ("Got new secret %s and session key %s for %s\n", secret, session_key, uid); facebook_proxy_set_session_key (FACEBOOK_PROXY (proxy), session_key); facebook_proxy_set_app_secret (FACEBOOK_PROXY (proxy), secret); rest_xml_node_unref (root); char **uids = facebook_proxy_friends_get (proxy); call = rest_proxy_new_call (proxy); rest_proxy_call_set_function (call, "users.getInfo"); rest_proxy_call_add_param (call, "uids", g_strjoinv (",", uids)); rest_proxy_call_add_param (call, "fields", "uid,about_me,birthday_date,current_location,first_name,hometown_location,last_name,name,pic,profile_update_time,profile_url,sex,status,username,website"); rest_proxy_call_add_param (call, "locale", "en_US"); if (!rest_proxy_call_run (call, NULL, &error)) g_error ("Cannot get user info: %s", error->message); root = get_xml (call); RestXmlNode *user, *detail; for (user = rest_xml_node_find (root, "user"); user; user = user->next) { if (g_strcmp0 ("user", user->name)) continue; GHashTableIter iter; g_print ("---\n"); g_hash_table_iter_init (&iter, user->children); while (g_hash_table_iter_next (&iter, NULL, (gpointer) &detail)) { g_print (" %s: %s\n", detail->name, detail->content); } } g_strfreev (uids); g_object_unref (proxy); /* fql.multiquery queries= { "friends": "SELECT uid2 FROM friend WHERE uid1=667897485", "profiles": "SELECT uid, name, profile_update_time FROM user WHERE profile_update_time > 0 AND uid IN (SELECT uid2 FROM friend WHERE uid1=667897485)" } */ return 0; }