Use GSList functions instead of manual iterations
[pidgin-git.git] / libpurple / protocols / jabber / google / google_roster.c
blob7ebdc30c1617b7ed51081ac505ca094f44dbbe87
1 /**
2 * Purple is the legal property of its developers, whose names are too numerous
3 * to list here. Please refer to the COPYRIGHT file distributed with this
4 * source distribution.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "google_roster.h"
23 #include "jabber.h"
24 #include "presence.h"
25 #include "debug.h"
26 #include "xmlnode.h"
27 #include "roster.h"
29 void jabber_google_roster_outgoing(JabberStream *js, PurpleXmlNode *query, PurpleXmlNode *item)
31 PurpleAccount *account = purple_connection_get_account(js->gc);
32 GSList *list = purple_account_privacy_get_denied(account);
33 const char *jid = purple_xmlnode_get_attrib(item, "jid");
34 char *jid_norm = (char *)jabber_normalize(account, jid);
36 if (g_slist_find_custom(list, jid_norm, (GCompareFunc)g_strcmp0)) {
37 purple_xmlnode_set_attrib(query, "xmlns:gr", NS_GOOGLE_ROSTER);
38 purple_xmlnode_set_attrib(query, "gr:ext", "2");
39 purple_xmlnode_set_attrib(item, "gr:t", "B");
40 return;
44 gboolean jabber_google_roster_incoming(JabberStream *js, PurpleXmlNode *item)
46 PurpleAccount *account = purple_connection_get_account(js->gc);
47 const char *jid = purple_xmlnode_get_attrib(item, "jid");
48 gboolean on_block_list = FALSE;
50 char *jid_norm;
52 const char *grt = purple_xmlnode_get_attrib_with_namespace(item, "t", NS_GOOGLE_ROSTER);
53 const char *subscription = purple_xmlnode_get_attrib(item, "subscription");
54 const char *ask = purple_xmlnode_get_attrib(item, "ask");
56 if ((!subscription || purple_strequal(subscription, "none")) && !ask) {
57 /* The Google Talk servers will automatically add people from your Gmail address book
58 * with subscription=none. If we see someone with subscription=none, ignore them.
60 return FALSE;
63 jid_norm = g_strdup(jabber_normalize(account, jid));
65 on_block_list = NULL != g_slist_find_custom(purple_account_privacy_get_denied(account),
66 jid_norm, (GCompareFunc)strcmp);
68 if (grt && (*grt == 'H' || *grt == 'h')) {
69 /* Hidden; don't show this buddy. */
70 GSList *buddies = purple_blist_find_buddies(account, jid_norm);
71 if (buddies) {
72 purple_debug_info("jabber", "Removing %s from local buddy list\n",
73 jid_norm);
75 g_slist_free_full(buddies, (GDestroyNotify)purple_blist_remove_buddy);
78 g_free(jid_norm);
79 return FALSE;
82 if (!on_block_list && (grt && (*grt == 'B' || *grt == 'b'))) {
83 purple_debug_info("jabber", "Blocking %s\n", jid_norm);
84 purple_account_privacy_deny_add(account, jid_norm, TRUE);
85 } else if (on_block_list && (!grt || (*grt != 'B' && *grt != 'b' ))){
86 purple_debug_info("jabber", "Unblocking %s\n", jid_norm);
87 purple_account_privacy_deny_remove(account, jid_norm, TRUE);
90 g_free(jid_norm);
91 return TRUE;
94 void jabber_google_roster_add_deny(JabberStream *js, const char *who)
96 PurpleAccount *account;
97 GSList *buddies;
98 JabberIq *iq;
99 PurpleXmlNode *query;
100 PurpleXmlNode *item;
101 PurpleXmlNode *group;
102 PurpleBuddy *b;
103 JabberBuddy *jb;
104 const char *balias;
106 jb = jabber_buddy_find(js, who, TRUE);
108 account = purple_connection_get_account(js->gc);
109 buddies = purple_blist_find_buddies(account, who);
110 if(!buddies)
111 return;
113 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
115 query = purple_xmlnode_get_child(iq->node, "query");
116 item = purple_xmlnode_new_child(query, "item");
118 do {
119 PurpleGroup *g;
121 b = buddies->data;
122 g = purple_buddy_get_group(b);
124 group = purple_xmlnode_new_child(item, "group");
125 purple_xmlnode_insert_data(group,
126 jabber_roster_group_get_global_name(g), -1);
128 buddies = g_slist_delete_link(buddies, buddies);
129 } while (buddies);
131 balias = purple_buddy_get_local_alias(b);
132 purple_xmlnode_set_attrib(item, "jid", who);
133 purple_xmlnode_set_attrib(item, "name", balias ? balias : "");
134 purple_xmlnode_set_attrib(item, "gr:t", "B");
135 purple_xmlnode_set_attrib(query, "xmlns:gr", NS_GOOGLE_ROSTER);
136 purple_xmlnode_set_attrib(query, "gr:ext", "2");
138 jabber_iq_send(iq);
140 /* Synthesize a sign-off */
141 if (jb) {
142 JabberBuddyResource *jbr;
143 GList *l = jb->resources;
144 while (l) {
145 jbr = l->data;
146 if (jbr && jbr->name)
148 purple_debug_misc("jabber", "Removing resource %s\n", jbr->name);
149 jabber_buddy_remove_resource(jb, jbr->name);
151 l = l->next;
155 purple_protocol_got_user_status(account, who, "offline", NULL);
158 void jabber_google_roster_rem_deny(JabberStream *js, const char *who)
160 GSList *buddies;
161 JabberIq *iq;
162 PurpleXmlNode *query;
163 PurpleXmlNode *item;
164 PurpleXmlNode *group;
165 PurpleBuddy *b;
166 const char *balias;
168 buddies = purple_blist_find_buddies(purple_connection_get_account(js->gc), who);
169 if(!buddies)
170 return;
172 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
174 query = purple_xmlnode_get_child(iq->node, "query");
175 item = purple_xmlnode_new_child(query, "item");
177 do {
178 PurpleGroup *g;
180 b = buddies->data;
181 g = purple_buddy_get_group(b);
183 group = purple_xmlnode_new_child(item, "group");
184 purple_xmlnode_insert_data(group,
185 jabber_roster_group_get_global_name(g), -1);
187 buddies = g_slist_delete_link(buddies, buddies);
188 } while (buddies);
190 balias = purple_buddy_get_local_alias(b);
191 purple_xmlnode_set_attrib(item, "jid", who);
192 purple_xmlnode_set_attrib(item, "name", balias ? balias : "");
193 purple_xmlnode_set_attrib(query, "xmlns:gr", NS_GOOGLE_ROSTER);
194 purple_xmlnode_set_attrib(query, "gr:ext", "2");
196 jabber_iq_send(iq);
198 /* See if he's online */
199 jabber_presence_subscription_set(js, who, "probe");