static void parse_new_messages(PurpleConnection *pc, FacebookAccount *fba, JsonArray *messages) { int i; PurpleBuddy *buddy; purple_debug_info("facebook", "parsing new messages\n"); for (i = 0; i < json_array_get_length(messages); i++) { const gchar *type; gchar *from, *to; JsonObject *object = json_node_get_object(json_array_get_element(messages, i)); type = json_node_get_string(json_object_get_member(object, "type")); from = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(object, "from"))); to = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(object, "to"))); /* Use the in-line buddy name if the buddy list hasn't been downloaded yet */ buddy = purple_find_buddy(pc->account, from); if (buddy == NULL || buddy->server_alias == NULL || buddy->alias == NULL) { if (json_object_has_member(object, "from_name")) { const gchar *from_name = json_node_get_string(json_object_get_member( object, "from_name")); fb_blist_set_alias(fba, from, from_name); } } if (from && to && g_str_equal(type, "msg")) { JsonObject *messageObj = json_node_get_object(json_object_get_member(object, "msg")); parse_message(pc, fba, messageObj, from, to, PURPLE_CONV_TYPE_IM); } else if (from && g_str_equal(type, "typ")) { purple_debug_info("facebook", "handling typing notification\n"); gint typing = json_node_get_int(json_object_get_member(object, "st")); if (typing == 0) { serv_got_typing(pc, from, 10, PURPLE_NOT_TYPING); } else { serv_got_typing(pc, from, 10, PURPLE_TYPING); } } else if (g_str_equal(type, "group_msg")) { PurpleConversation *conv = fb_find_chat(fba, to); if (conv != NULL) { if (json_object_has_member(object, "to_name")) { const gchar *to_name = json_node_get_string(json_object_get_member( object, "to_name")); purple_conversation_set_title(conv, to_name); PurpleChat *chat = purple_blist_find_chat(fba->account, to); purple_blist_alias_chat(chat, to_name); } if (!purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), from)) { purple_conv_chat_add_user(PURPLE_CONV_CHAT(conv), from, NULL, PURPLE_CBFLAGS_NONE, FALSE); } if (!purple_find_buddy(fba->account, from)) { const char *from_name = json_node_get_string(json_object_get_member( object, "from_name")); buddy = purple_buddy_new(fba->account, from, from_name); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, purple_find_group(DEFAULT_GROUP_NAME), NULL); } } JsonObject *messageObj = json_node_get_object(json_object_get_member(object, "msg")); parse_message(pc, fba, messageObj, from, to, PURPLE_CONV_TYPE_CHAT); } /* * we've received something from a buddy, assume they're online * only if it's not from ourselves */ if (from && fba->uid != atoll(from)) { purple_prpl_got_user_status( fba->account, from, purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE), NULL); } g_free(from); g_free(to); fba->message_fetch_sequence++; } }
/** * If the buddy does not yet exist, then create it and add it to * our buddy list. In either case we set the correct status for * the buddy. */ void bonjour_buddy_add_to_purple(BonjourBuddy *bonjour_buddy) { PurpleBuddy *buddy; PurpleGroup *group; PurpleAccount *account = bonjour_buddy->account; const char *status_id, *old_hash, *new_hash; /* Translate between the Bonjour status and the Purple status */ if (bonjour_buddy->status != NULL && g_ascii_strcasecmp("dnd", bonjour_buddy->status) == 0) status_id = BONJOUR_STATUS_ID_AWAY; else status_id = BONJOUR_STATUS_ID_AVAILABLE; /* * TODO: Figure out the idle time by getting the "away" * field from the DNS SD. */ /* Make sure the Bonjour group exists in our buddy list */ group = purple_find_group(BONJOUR_GROUP_NAME); /* Use the buddy's domain, instead? */ if (group == NULL) { group = purple_group_new(BONJOUR_GROUP_NAME); purple_blist_add_group(group, NULL); } /* Make sure the buddy exists in our buddy list */ buddy = purple_find_buddy(account, bonjour_buddy->name); if (buddy == NULL) { buddy = purple_buddy_new(account, bonjour_buddy->name, NULL); buddy->proto_data = bonjour_buddy; purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, group, NULL); } /* Create the alias for the buddy using the first and the last name */ if (bonjour_buddy->nick) serv_got_alias(purple_account_get_connection(account), buddy->name, bonjour_buddy->nick); else { gchar *alias = NULL; const char *first, *last; first = bonjour_buddy->first; last = bonjour_buddy->last; if ((first && *first) || (last && *last)) alias = g_strdup_printf("%s%s%s", (first && *first ? first : ""), (first && *first && last && *last ? " " : ""), (last && *last ? last : "")); serv_got_alias(purple_account_get_connection(account), buddy->name, alias); g_free(alias); } /* Set the user's status */ if (bonjour_buddy->msg != NULL) purple_prpl_got_user_status(account, buddy->name, status_id, "message", bonjour_buddy->msg, NULL); else purple_prpl_got_user_status(account, buddy->name, status_id, NULL); purple_prpl_got_user_idle(account, buddy->name, FALSE, 0); /* TODO: Because we don't save Bonjour buddies in blist.xml, * we will always have to look up the buddy icon at login time. * I think we should figure out a way to do something about this. */ /* Deal with the buddy icon */ old_hash = purple_buddy_icons_get_checksum_for_user(buddy); new_hash = (bonjour_buddy->phsh && *(bonjour_buddy->phsh)) ? bonjour_buddy->phsh : NULL; if (new_hash && (!old_hash || strcmp(old_hash, new_hash) != 0)) { /* Look up the new icon data */ /* TODO: Make sure the hash assigned to the retrieved buddy icon is the same * as what we looked up. */ bonjour_dns_sd_retrieve_buddy_icon(bonjour_buddy); } else if (!new_hash) purple_buddy_icons_set_for_user(account, buddy->name, NULL, 0, NULL); }
void fb_got_facepile(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer user_data) { gchar *group = user_data; JsonParser *parser; JsonObject *object, *payload, *user_obj; JsonArray *facepile; PurpleConversation *conv; PurpleConvChat *chat; gchar *uid; guint i; PurpleGroup *pgroup; purple_debug_info("facebook", "got facepile %s\n", data?data:"(null)"); conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, group, fba->account); chat = PURPLE_CONV_CHAT(conv); parser = fb_get_parser(data, data_len); if (!parser) { purple_debug_warning("facebook", "could not fetch facepile for group %s\n", group); g_free(group); return; } object = fb_get_json_object(parser, NULL); payload = json_node_get_object( json_object_get_member(object, "payload")); facepile = json_node_get_array( json_object_get_member(payload, "facepile_click_info")); pgroup = purple_find_group(DEFAULT_GROUP_NAME); if (!pgroup) { pgroup = purple_group_new(DEFAULT_GROUP_NAME); purple_blist_add_group(pgroup, NULL); } purple_conv_chat_clear_users(chat); uid = g_strdup_printf("%" G_GINT64_FORMAT, fba->uid); purple_conv_chat_add_user(chat, uid, NULL, PURPLE_CBFLAGS_NONE, FALSE); if (!purple_find_buddy(fba->account, uid)) { PurpleBuddy *buddy = purple_buddy_new(fba->account, uid, "You"); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, pgroup, NULL); } g_free(uid); for (i = 0; i < json_array_get_length(facepile); i++) { user_obj = json_node_get_object( json_array_get_element(facepile, i)); uid = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(user_obj, "uid"))); purple_conv_chat_add_user(PURPLE_CONV_CHAT(conv), uid, NULL, PURPLE_CBFLAGS_NONE, FALSE); if (!purple_find_buddy(fba->account, uid)) { const char *alias = json_node_get_string(json_object_get_member(user_obj, "name")); PurpleBuddy *buddy = purple_buddy_new(fba->account, uid, alias); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, pgroup, NULL); } g_free(uid); } g_free(group); }