File: facebook_roster.c

package info (click to toggle)
pidgin 2.10.10-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 59,132 kB
  • sloc: ansic: 323,190; sh: 11,689; makefile: 3,549; python: 1,292; perl: 519; cs: 209; tcl: 96; xml: 10
file content (135 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
 */

#include "facebook_roster.h"

#include "roster.h"

#include "internal.h"

void
jabber_facebook_roster_cleanup(JabberStream *js, xmlnode *query)
{
	PurpleAccount *account = purple_connection_get_account(js->gc);
	xmlnode *item;
	GSList *local_buddies;
	GHashTable *remove_buddies;
	GHashTableIter it;
	PurpleBuddy *buddy;
	const gchar *jid;

	if (js->facebook_roster_cleanup_performed)
		return;
	js->facebook_roster_cleanup_performed = TRUE;

	/* mark all local buddies as "to be removed" */
	remove_buddies = g_hash_table_new_full(g_str_hash, g_str_equal,
		g_free, NULL);
	local_buddies = purple_find_buddies(account, NULL);
	for (; local_buddies; local_buddies = g_slist_delete_link(
		local_buddies, local_buddies))
	{
		buddy = local_buddies->data;

		g_hash_table_insert(remove_buddies, g_strdup(jabber_normalize(
			account, purple_buddy_get_name(buddy))), buddy);
	}

	/* un-mark all remote buddies */
	for (item = xmlnode_get_child(query, "item"); item;
		item = xmlnode_get_next_twin(item))
	{
		jid = xmlnode_get_attrib(item, "jid");

		g_hash_table_remove(remove_buddies,
			jabber_normalize(account, jid));
	}

	/* remove all not-remote buddies */
	g_hash_table_iter_init(&it, remove_buddies);
	while (g_hash_table_iter_next(&it, (gpointer*)&jid, (gpointer*)&buddy)) {
		const gchar *alias = purple_buddy_get_local_buddy_alias(buddy);
		item = xmlnode_new_child(query, "item");
		xmlnode_set_namespace(item, xmlnode_get_namespace(query));
		xmlnode_set_attrib(item, "jid", jid);
		xmlnode_set_attrib(item, "subscription", "remove");
		if (alias)
			xmlnode_set_attrib(item, "name", alias);
	}

	g_hash_table_destroy(remove_buddies);
}

gboolean
jabber_facebook_roster_incoming(JabberStream *js, xmlnode *item)
{
	PurpleAccount *account = purple_connection_get_account(js->gc);
	const gchar *jid, *subscription;
	gchar *jid_norm;
	PurpleBuddy *buddy;
	PurpleGroup *buddy_group;
	xmlnode *group;
	const gchar *alias;

	subscription = xmlnode_get_attrib(item, "subscription");

	/* Skip entries added with jabber_facebook_roster_cleanup */
	if (g_strcmp0(subscription, "remove") == 0)
		return TRUE;

	jid = xmlnode_get_attrib(item, "jid");
	jid_norm = g_strdup(jabber_normalize(account, jid));
	buddy = purple_find_buddy(account, jid);
	g_free(jid_norm);

	/* Facebook forces "Facebook Friends" group */
	while ((group = xmlnode_get_child(item, "group")) != NULL)
		xmlnode_free(group);
	group = xmlnode_new_child(item, "group");
	xmlnode_set_namespace(group, xmlnode_get_namespace(item));

	/* We don't have that buddy on the list, add him to the default group */
	if (!buddy) {
		xmlnode_insert_data(group,
			JABBER_ROSTER_DEFAULT_GROUP, -1);
		return TRUE;
	}

	/* Facebook forces buddy real name as alias */
	alias = purple_buddy_get_local_buddy_alias(buddy);
	if (alias)
		xmlnode_set_attrib(item, "name", alias);

	/* Add buddy to his group */
	buddy_group = purple_buddy_get_group(buddy);
	if (g_strcmp0(purple_group_get_name(buddy_group), _("Buddies")) == 0)
		buddy_group = NULL;
	if (buddy_group) {
		xmlnode_insert_data(group,
			purple_group_get_name(buddy_group), -1);
	} else {
		xmlnode_insert_data(group,
			JABBER_ROSTER_DEFAULT_GROUP, -1);
	}

	return TRUE;
}