Standardize all protocol header guard macros.
[pidgin-git.git] / libpurple / protocols / jabber / roster.c
blob65db6557fb8db0397588c386498b4379b48c4f2e
1 /*
2 * purple - Jabber Protocol Plugin
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "internal.h"
24 #include "debug.h"
25 #include "server.h"
26 #include "util.h"
28 #include "buddy.h"
29 #include "chat.h"
30 #include "google/google.h"
31 #include "google/google_roster.h"
32 #include "presence.h"
33 #include "roster.h"
34 #include "iq.h"
36 #include <string.h>
38 /* Take a list of strings and join them with a ", " separator */
39 static gchar *roster_groups_join(GSList *list)
41 GString *out = g_string_new(NULL);
42 for ( ; list; list = list->next) {
43 out = g_string_append(out, (const char *)list->data);
44 if (list->next)
45 out = g_string_append(out, ", ");
48 return g_string_free(out, FALSE);
51 static void roster_request_cb(JabberStream *js, const char *from,
52 JabberIqType type, const char *id,
53 PurpleXmlNode *packet, gpointer data)
55 PurpleXmlNode *query;
57 if (type == JABBER_IQ_ERROR) {
59 * This shouldn't happen in any real circumstances and
60 * likely constitutes a server-side misconfiguration (i.e.
61 * explicitly not loading mod_roster...)
63 purple_debug_error("jabber", "Error retrieving roster!?\n");
64 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
65 return;
68 query = purple_xmlnode_get_child(packet, "query");
69 if (query == NULL) {
70 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
71 return;
74 jabber_roster_parse(js, from, type, id, query);
75 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
78 void jabber_roster_request(JabberStream *js)
80 JabberIq *iq;
81 PurpleXmlNode *query;
83 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:roster");
84 query = purple_xmlnode_get_child(iq->node, "query");
86 if (js->server_caps & JABBER_CAP_GOOGLE_ROSTER) {
87 purple_xmlnode_set_attrib(query, "xmlns:gr", NS_GOOGLE_ROSTER);
88 purple_xmlnode_set_attrib(query, "gr:ext", "2");
91 jabber_iq_set_callback(iq, roster_request_cb, NULL);
92 jabber_iq_send(iq);
95 static void remove_purple_buddies(JabberStream *js, const char *jid)
97 GSList *buddies, *l;
99 buddies = purple_blist_find_buddies(purple_connection_get_account(js->gc), jid);
101 for(l = buddies; l; l = l->next)
102 purple_blist_remove_buddy(l->data);
104 g_slist_free(buddies);
107 static void add_purple_buddy_to_groups(JabberStream *js, const char *jid,
108 const char *alias, GSList *groups)
110 GSList *buddies, *l;
111 PurpleAccount *account = purple_connection_get_account(js->gc);
113 buddies = purple_blist_find_buddies(purple_connection_get_account(js->gc), jid);
115 if(!groups) {
116 if(!buddies) {
117 groups = g_slist_append(groups, g_strdup(JABBER_ROSTER_DEFAULT_GROUP));
118 } else {
119 /* TODO: What should we do here? Removing the local buddies
120 * is wrong, but so is letting the group state get out of sync with
121 * the server.
123 g_slist_free(buddies);
124 return;
128 while(buddies) {
129 PurpleBuddy *b = buddies->data;
130 PurpleGroup *g = purple_buddy_get_group(b);
132 buddies = g_slist_delete_link(buddies, buddies);
134 /* XMPP groups are case-sensitive, but libpurple groups are
135 * case-insensitive. We treat a buddy in both "Friends" and "friends"
136 * as only being in one group, but if we push changes about the buddy
137 * to the server, the buddy will be dropped from one of the groups.
138 * Not optimal, but better than the alternative, I think.
140 l = g_slist_find_custom(groups, purple_group_get_name(g),
141 (GCompareFunc)purple_utf8_strcasecmp);
142 if (!l && g == purple_blist_get_default_group()) {
143 l = g_slist_find_custom(groups, JABBER_ROSTER_DEFAULT_GROUP,
144 (GCompareFunc)purple_utf8_strcasecmp);
146 if (!l && g == purple_blist_get_default_group()) {
147 l = g_slist_find_custom(groups,
148 _purple_blist_get_localized_default_group_name(),
149 (GCompareFunc)purple_utf8_strcasecmp);
152 if (l) {
153 /* The buddy is already on the local list. Update info. */
154 const char *servernick, *balias;
156 /* Previously stored serverside / buddy-supplied alias */
157 if((servernick = purple_blist_node_get_string((PurpleBlistNode*)b, "servernick")))
158 purple_serv_got_alias(js->gc, jid, servernick);
160 /* Alias from our roster retrieval */
161 balias = purple_buddy_get_local_alias(b);
162 if(alias && !purple_strequal(alias, balias))
163 purple_serv_got_private_alias(js->gc, jid, alias);
164 g_free(l->data);
165 groups = g_slist_delete_link(groups, l);
166 } else {
167 /* This buddy isn't in the group on the server anymore */
168 purple_debug_info("jabber", "jabber_roster_parse(): "
169 "Removing %s from group '%s' on the local list",
170 purple_buddy_get_name(b),
171 jabber_roster_group_get_global_name(g));
172 purple_blist_remove_buddy(b);
176 if (groups) {
177 char *tmp = roster_groups_join(groups);
178 purple_debug_info("jabber", "jabber_roster_parse(): Adding %s to "
179 "groups: %s\n", jid, tmp);
180 g_free(tmp);
183 while(groups) {
184 PurpleGroup *g = purple_blist_find_group(groups->data);
185 PurpleBuddy *b = purple_buddy_new(account, jid, alias);
187 if(!g) {
188 g = purple_group_new(groups->data);
189 purple_blist_add_group(g, NULL);
192 purple_blist_add_buddy(b, NULL, g, NULL);
193 purple_buddy_set_local_alias(b, alias);
195 g_free(groups->data);
196 groups = g_slist_delete_link(groups, groups);
199 g_slist_free(buddies);
202 void jabber_roster_parse(JabberStream *js, const char *from,
203 JabberIqType type, const char *id, PurpleXmlNode *query)
205 PurpleXmlNode *item, *group;
207 if (!jabber_is_own_account(js, from)) {
208 purple_debug_warning("jabber", "Received bogon roster push from %s\n",
209 from);
210 return;
213 js->currently_parsing_roster_push = TRUE;
215 for(item = purple_xmlnode_get_child(query, "item"); item; item = purple_xmlnode_get_next_twin(item))
217 const char *jid, *name, *subscription, *ask;
218 JabberBuddy *jb;
220 subscription = purple_xmlnode_get_attrib(item, "subscription");
221 jid = purple_xmlnode_get_attrib(item, "jid");
222 name = purple_xmlnode_get_attrib(item, "name");
223 ask = purple_xmlnode_get_attrib(item, "ask");
225 if(!jid)
226 continue;
228 if(!(jb = jabber_buddy_find(js, jid, TRUE)))
229 continue;
231 if(subscription) {
232 if (purple_strequal(subscription, "remove"))
233 jb->subscription = JABBER_SUB_REMOVE;
234 else if (jb == js->user_jb)
235 jb->subscription = JABBER_SUB_BOTH;
236 else if (purple_strequal(subscription, "none"))
237 jb->subscription = JABBER_SUB_NONE;
238 else if (purple_strequal(subscription, "to"))
239 jb->subscription = JABBER_SUB_TO;
240 else if (purple_strequal(subscription, "from"))
241 jb->subscription = JABBER_SUB_FROM;
242 else if (purple_strequal(subscription, "both"))
243 jb->subscription = JABBER_SUB_BOTH;
246 if(purple_strequal(ask, "subscribe"))
247 jb->subscription |= JABBER_SUB_PENDING;
248 else
249 jb->subscription &= ~JABBER_SUB_PENDING;
251 if(jb->subscription & JABBER_SUB_REMOVE) {
252 remove_purple_buddies(js, jid);
253 } else {
254 GSList *groups = NULL;
256 if (js->server_caps & JABBER_CAP_GOOGLE_ROSTER)
257 if (!jabber_google_roster_incoming(js, item))
258 continue;
260 for(group = purple_xmlnode_get_child(item, "group"); group; group = purple_xmlnode_get_next_twin(group)) {
261 char *group_name = purple_xmlnode_get_data(group);
263 if (group_name == NULL || *group_name == '\0')
264 /* Changing this string? Look in add_purple_buddy_to_groups */
265 group_name = g_strdup(JABBER_ROSTER_DEFAULT_GROUP);
268 * See the note in add_purple_buddy_to_groups; the core handles
269 * names case-insensitively and this is required to not
270 * end up with duplicates if a buddy is in, e.g.,
271 * 'XMPP' and 'xmpp'
273 if (g_slist_find_custom(groups, group_name, (GCompareFunc)purple_utf8_strcasecmp))
274 g_free(group_name);
275 else
276 groups = g_slist_prepend(groups, group_name);
279 add_purple_buddy_to_groups(js, jid, name, groups);
280 if (jb == js->user_jb)
281 jabber_presence_fake_to_self(js, NULL);
285 if (type == JABBER_IQ_SET) {
286 JabberIq *ack = jabber_iq_new(js, JABBER_IQ_RESULT);
287 jabber_iq_set_id(ack, id);
288 jabber_iq_send(ack);
291 js->currently_parsing_roster_push = FALSE;
294 /* jabber_roster_update frees the GSList* passed in */
295 static void jabber_roster_update(JabberStream *js, const char *name,
296 GSList *groups)
298 PurpleBuddy *b;
299 PurpleGroup *g;
300 GSList *l;
301 JabberIq *iq;
302 PurpleXmlNode *query, *item, *group;
303 const char *balias;
305 if (js->currently_parsing_roster_push)
306 return;
308 if(!(b = purple_blist_find_buddy(purple_connection_get_account(js->gc), name)))
309 return;
311 if (groups) {
312 char *tmp = roster_groups_join(groups);
314 purple_debug_info("jabber", "jabber_roster_update(%s): [Source: "
315 "groups]: groups: %s\n", name, tmp);
316 g_free(tmp);
317 } else {
318 GSList *buddies = purple_blist_find_buddies(purple_connection_get_account(js->gc), name);
319 char *tmp;
321 if(!buddies)
322 return;
323 while(buddies) {
324 b = buddies->data;
325 g = purple_buddy_get_group(b);
326 groups = g_slist_append(groups,
327 (char *)jabber_roster_group_get_global_name(g));
328 buddies = g_slist_remove(buddies, b);
331 tmp = roster_groups_join(groups);
332 purple_debug_info("jabber", "jabber_roster_update(%s): [Source: local blist]: groups: %s\n",
333 name, tmp);
334 g_free(tmp);
337 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
339 query = purple_xmlnode_get_child(iq->node, "query");
340 item = purple_xmlnode_new_child(query, "item");
342 purple_xmlnode_set_attrib(item, "jid", name);
344 balias = purple_buddy_get_local_alias(b);
345 purple_xmlnode_set_attrib(item, "name", balias ? balias : "");
347 for(l = groups; l; l = l->next) {
348 group = purple_xmlnode_new_child(item, "group");
349 purple_xmlnode_insert_data(group, l->data, -1);
352 g_slist_free(groups);
354 if (js->server_caps & JABBER_CAP_GOOGLE_ROSTER) {
355 jabber_google_roster_outgoing(js, query, item);
356 purple_xmlnode_set_attrib(query, "xmlns:gr", NS_GOOGLE_ROSTER);
357 purple_xmlnode_set_attrib(query, "gr:ext", "2");
359 jabber_iq_send(iq);
362 void jabber_roster_add_buddy(PurpleConnection *gc, PurpleBuddy *buddy,
363 PurpleGroup *group, const char *message)
365 JabberStream *js = purple_connection_get_protocol_data(gc);
366 char *who;
367 JabberID *jid;
368 JabberBuddy *jb;
369 JabberBuddyResource *jbr;
370 const char *name;
372 /* If we haven't received the roster yet, ignore any adds */
373 if (js->state != JABBER_STREAM_CONNECTED)
374 return;
376 name = purple_buddy_get_name(buddy);
377 jid = jabber_id_new(name);
378 if (jid == NULL) {
379 /* TODO: Remove the buddy from the list? */
380 return;
383 /* Adding a chat room or a chat user to the roster is *not* supported. */
384 if (jid->node && jabber_chat_find(js, jid->node, jid->domain) != NULL) {
386 * This is the same thing Bonjour does. If it causes problems, move
387 * it to an idle callback.
389 purple_debug_warning("jabber", "Cowardly refusing to add a MUC user "
390 "to your buddy list and removing the buddy. "
391 "Buddies can only be added by real (non-MUC) "
392 "JID\n");
393 purple_blist_remove_buddy(buddy);
394 jabber_id_free(jid);
395 return;
398 who = jabber_id_get_bare_jid(jid);
399 if (jid->resource != NULL) {
401 * If the buddy name added contains a resource, strip that off and
402 * rename the buddy.
404 purple_buddy_set_name(buddy, who);
407 jb = jabber_buddy_find(js, who, FALSE);
409 purple_debug_info("jabber", "jabber_roster_add_buddy(): Adding %s\n", who);
411 jabber_roster_update(js, who, NULL);
413 if (jb == js->user_jb) {
414 jabber_presence_fake_to_self(js, NULL);
415 } else if(!jb || !(jb->subscription & JABBER_SUB_TO)) {
416 jabber_presence_subscription_set(js, who, "subscribe");
417 } else if((jbr =jabber_buddy_find_resource(jb, NULL))) {
418 purple_protocol_got_user_status(purple_connection_get_account(gc), who,
419 jabber_buddy_state_get_status_id(jbr->state),
420 "priority", jbr->priority, jbr->status ? "message" : NULL, jbr->status, NULL);
423 g_free(who);
426 void jabber_roster_alias_change(PurpleConnection *gc, const char *name, const char *alias)
428 PurpleBuddy *b = purple_blist_find_buddy(purple_connection_get_account(gc), name);
430 if(b != NULL) {
431 purple_buddy_set_local_alias(b, alias);
433 purple_debug_info("jabber", "jabber_roster_alias_change(): Aliased %s to %s\n",
434 name, alias ? alias : "(null)");
436 jabber_roster_update(purple_connection_get_protocol_data(gc), name, NULL);
440 void jabber_roster_group_change(PurpleConnection *gc, const char *name,
441 const char *old_group, const char *new_group)
443 GSList *buddies, *groups = NULL;
444 PurpleBuddy *b;
446 if(!old_group || !new_group || purple_strequal(old_group, new_group))
447 return;
449 buddies = purple_blist_find_buddies(purple_connection_get_account(gc), name);
450 while(buddies) {
451 b = buddies->data;
452 groups = g_slist_append(groups, (char*)new_group);
453 buddies = g_slist_remove(buddies, b);
456 purple_debug_info("jabber", "jabber_roster_group_change(): Moving %s from %s to %s\n",
457 name, old_group, new_group);
459 jabber_roster_update(purple_connection_get_protocol_data(gc), name, groups);
462 void jabber_roster_group_rename(PurpleConnection *gc, const char *old_name,
463 PurpleGroup *group, GList *moved_buddies)
465 GList *l;
466 const char *gname = jabber_roster_group_get_global_name(group);
467 for(l = moved_buddies; l; l = l->next) {
468 PurpleBuddy *buddy = l->data;
469 jabber_roster_group_change(gc, purple_buddy_get_name(buddy), old_name, gname);
473 void jabber_roster_remove_buddy(PurpleConnection *gc, PurpleBuddy *buddy,
474 PurpleGroup *group) {
475 const char *name = purple_buddy_get_name(buddy);
476 GSList *buddies = purple_blist_find_buddies(purple_connection_get_account(gc), name);
478 buddies = g_slist_remove(buddies, buddy);
479 if(buddies != NULL) {
480 PurpleBuddy *tmpbuddy;
481 PurpleGroup *tmpgroup;
482 GSList *groups = NULL;
484 while(buddies) {
485 tmpbuddy = buddies->data;
486 tmpgroup = purple_buddy_get_group(tmpbuddy);
487 groups = g_slist_append(groups,
488 (char *)jabber_roster_group_get_global_name(tmpgroup));
489 buddies = g_slist_remove(buddies, tmpbuddy);
492 purple_debug_info("jabber", "jabber_roster_remove_buddy(): "
493 "Removing %s from %s", purple_buddy_get_name(buddy),
494 jabber_roster_group_get_global_name(group));
496 jabber_roster_update(purple_connection_get_protocol_data(gc), name, groups);
497 } else {
498 JabberIq *iq = jabber_iq_new_query(purple_connection_get_protocol_data(gc), JABBER_IQ_SET,
499 "jabber:iq:roster");
500 PurpleXmlNode *query = purple_xmlnode_get_child(iq->node, "query");
501 PurpleXmlNode *item = purple_xmlnode_new_child(query, "item");
503 purple_xmlnode_set_attrib(item, "jid", name);
504 purple_xmlnode_set_attrib(item, "subscription", "remove");
506 purple_debug_info("jabber", "jabber_roster_remove_buddy(): Removing %s\n",
507 purple_buddy_get_name(buddy));
509 jabber_iq_send(iq);
513 const gchar *
514 jabber_roster_group_get_global_name(PurpleGroup *group)
516 const gchar *name = NULL;
518 if (group)
519 name = purple_group_get_name(group);
521 if (name == NULL)
522 name = JABBER_ROSTER_DEFAULT_GROUP;
523 else if (purple_strequal(name, PURPLE_BLIST_DEFAULT_GROUP_NAME))
524 name = JABBER_ROSTER_DEFAULT_GROUP;
525 else if (purple_strequal(name, _purple_blist_get_localized_default_group_name()))
526 name = JABBER_ROSTER_DEFAULT_GROUP;
528 return name;