7014
|
1 /*
|
|
2 * gaim - Jabber Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
|
|
5 *
|
|
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.
|
|
10 *
|
|
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.
|
|
15 *
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
|
21 #include "internal.h"
|
|
22 #include "debug.h"
|
|
23 #include "server.h"
|
|
24
|
|
25 #include "buddy.h"
|
|
26 #include "presence.h"
|
|
27 #include "roster.h"
|
|
28 #include "iq.h"
|
|
29
|
|
30 #include <string.h>
|
|
31
|
|
32
|
|
33 void jabber_roster_request(JabberStream *js)
|
|
34 {
|
|
35 JabberIq *iq;
|
|
36
|
|
37 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:roster");
|
|
38
|
|
39 jabber_iq_send(iq);
|
|
40 }
|
|
41
|
|
42 static void remove_gaim_buddies(JabberStream *js, const char *jid)
|
|
43 {
|
|
44 GSList *buddies, *l;
|
|
45
|
|
46 buddies = gaim_find_buddies(js->gc->account, jid);
|
|
47
|
|
48 for(l = buddies; l; l = l->next)
|
|
49 gaim_blist_remove_buddy(l->data);
|
|
50
|
|
51 g_slist_free(buddies);
|
|
52 }
|
|
53
|
|
54 static void add_gaim_buddies_in_groups(JabberStream *js, const char *jid,
|
|
55 const char *alias, GSList *groups)
|
|
56 {
|
|
57 GSList *buddies, *g2, *l;
|
|
58 int present =0, idle=0, signon=0, state=0;
|
|
59
|
|
60 buddies = gaim_find_buddies(js->gc->account, jid);
|
|
61
|
|
62 g2 = groups;
|
|
63
|
|
64 if(!groups) {
|
|
65 if(!buddies)
|
|
66 g2 = g_slist_append(g2, g_strdup(_("Buddies")));
|
|
67 else
|
|
68 return;
|
|
69 }
|
|
70
|
|
71 if(buddies) {
|
|
72 present = ((GaimBuddy*)buddies->data)->present;
|
|
73 signon = ((GaimBuddy*)buddies->data)->signon;
|
|
74 idle = ((GaimBuddy*)buddies->data)->idle;
|
|
75 state = ((GaimBuddy*)buddies->data)->uc;
|
|
76 }
|
|
77
|
|
78 while(buddies) {
|
|
79 GaimBuddy *b = buddies->data;
|
|
80 GaimGroup *g = gaim_find_buddys_group(b);
|
|
81
|
|
82 buddies = g_slist_remove(buddies, b);
|
|
83
|
|
84 if((l = g_slist_find_custom(g2, g->name, (GCompareFunc)strcmp))) {
|
|
85 if(alias && (!b->alias || strcmp(b->alias, alias)))
|
|
86 gaim_blist_alias_buddy(b, alias);
|
|
87 g_free(l->data);
|
|
88 g2 = g_slist_delete_link(g2, l);
|
|
89 } else {
|
|
90 gaim_blist_remove_buddy(b);
|
|
91 }
|
|
92 }
|
|
93
|
|
94 while(g2) {
|
|
95 GaimBuddy *b = gaim_buddy_new(js->gc->account, jid, alias);
|
|
96 GaimGroup *g = gaim_find_group(g2->data);
|
|
97
|
|
98 if(!g) {
|
|
99 g = gaim_group_new(g2->data);
|
|
100 gaim_blist_add_group(g, NULL);
|
|
101 }
|
|
102
|
|
103 b->present = present;
|
|
104 b->signon = signon;
|
|
105 b->idle = idle;
|
|
106 b->uc = state;
|
|
107
|
|
108 gaim_blist_add_buddy(b, NULL, g, NULL);
|
|
109 g_free(g2->data);
|
|
110 g2 = g_slist_delete_link(g2, g2);
|
|
111 }
|
|
112
|
|
113 g_slist_free(buddies);
|
|
114 }
|
|
115
|
|
116 void jabber_roster_parse(JabberStream *js, xmlnode *packet)
|
|
117 {
|
|
118 xmlnode *query, *item, *group;
|
|
119 const char *from = xmlnode_get_attrib(packet, "from");
|
|
120 if(from && strcmp(gaim_account_get_username(js->gc->account), from))
|
|
121 return;
|
|
122
|
|
123 query = xmlnode_get_child(packet, "query");
|
|
124 if(!query)
|
|
125 return;
|
|
126
|
|
127 js->roster_parsed = TRUE;
|
|
128
|
|
129 for(item = query->child; item; item = item->next)
|
|
130 {
|
|
131 const char *jid, *name, *subscription, *ask;
|
|
132 JabberBuddy *jb;
|
|
133
|
|
134 if(item->type != NODE_TYPE_TAG || strcmp(item->name, "item"))
|
|
135 continue;
|
|
136
|
|
137 subscription = xmlnode_get_attrib(item, "subscription");
|
|
138 jid = xmlnode_get_attrib(item, "jid");
|
|
139 name = xmlnode_get_attrib(item, "name");
|
|
140 ask = xmlnode_get_attrib(item, "ask");
|
|
141
|
|
142 jb = jabber_buddy_find(js, jid, TRUE);
|
|
143
|
|
144 if(!strcmp(subscription, "to"))
|
|
145 jb->subscription = JABBER_SUB_TO;
|
|
146 else if(!strcmp(subscription, "from"))
|
|
147 jb->subscription = JABBER_SUB_FROM;
|
|
148 else if(!strcmp(subscription, "both"))
|
|
149 jb->subscription = JABBER_SUB_BOTH;
|
|
150 else
|
|
151 jb->subscription = JABBER_SUB_NONE;
|
|
152
|
|
153 if(ask && !strcmp(ask, "subscribe"))
|
|
154 jb->subscription |= JABBER_SUB_PENDING;
|
|
155 else
|
|
156 jb->subscription &= ~JABBER_SUB_PENDING;
|
|
157
|
|
158 if(jb->subscription == JABBER_SUB_NONE) {
|
|
159 jb = jabber_buddy_find(js, jid, FALSE);
|
|
160 if(jb)
|
|
161 jb->subscription = JABBER_SUB_NONE;
|
|
162 remove_gaim_buddies(js, jid);
|
|
163 } else {
|
|
164 GSList *groups = NULL;
|
|
165
|
|
166 for(group = item->child; group; group = group->next) {
|
|
167 if(group->type != NODE_TYPE_TAG || strcmp(group->name, "group"))
|
|
168 continue;
|
|
169 groups = g_slist_append(groups,
|
|
170 xmlnode_get_data(group));
|
|
171 }
|
|
172 add_gaim_buddies_in_groups(js, jid, name, groups);
|
|
173 }
|
|
174 }
|
|
175
|
|
176 gaim_blist_save();
|
|
177 }
|
|
178
|
|
179 static void jabber_roster_update(JabberStream *js, const char *name,
|
|
180 GSList *grps)
|
|
181 {
|
|
182 GaimBuddy *b;
|
|
183 GaimGroup *g;
|
|
184 GSList *groups = NULL, *l;
|
|
185 JabberIq *iq;
|
|
186 xmlnode *query, *item, *group;
|
|
187
|
|
188 if(grps) {
|
|
189 groups = grps;
|
|
190 } else {
|
|
191 GSList *buddies = gaim_find_buddies(js->gc->account, name);
|
|
192 if(!buddies)
|
|
193 return;
|
|
194 while(buddies) {
|
|
195 b = buddies->data;
|
|
196 g = gaim_find_buddys_group(b);
|
|
197 groups = g_slist_append(groups, g->name);
|
|
198 buddies = g_slist_remove(buddies, b);
|
|
199 }
|
|
200 }
|
|
201
|
|
202 b = gaim_find_buddy(js->gc->account, name);
|
|
203
|
|
204 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
|
|
205
|
|
206 query = xmlnode_get_child(iq->node, "query");
|
|
207 item = xmlnode_new_child(query, "item");
|
|
208
|
|
209 xmlnode_set_attrib(item, "jid", name);
|
|
210
|
|
211 if(b->alias)
|
|
212 xmlnode_set_attrib(item, "name", b->alias);
|
|
213
|
|
214 for(l = groups; l; l = l->next) {
|
|
215 group = xmlnode_new_child(item, "group");
|
|
216 xmlnode_insert_data(group, l->data, -1);
|
|
217 }
|
|
218
|
|
219 if(!grps)
|
|
220 g_slist_free(groups);
|
|
221
|
|
222 jabber_iq_send(iq);
|
|
223 }
|
|
224
|
|
225 void jabber_roster_add_buddy(GaimConnection *gc, const char *name,
|
|
226 GaimGroup *grp)
|
|
227 {
|
|
228 JabberStream *js = gc->proto_data;
|
|
229 char *who;
|
|
230 GSList *buddies;
|
|
231 JabberBuddy *jb;
|
|
232
|
|
233 if(!js->roster_parsed)
|
|
234 return;
|
|
235
|
|
236 who = jabber_get_bare_jid(name);
|
|
237
|
|
238 buddies = gaim_find_buddies(gc->account, who);
|
|
239
|
|
240 jabber_roster_update(js, who, NULL);
|
|
241
|
|
242 jb = jabber_buddy_find(js, name, FALSE);
|
|
243 if(!jb || !(jb->subscription & JABBER_SUB_TO))
|
|
244 jabber_presence_subscription_set(js, who, "subscribe");
|
|
245 g_free(who);
|
|
246 }
|
|
247
|
|
248 void jabber_roster_alias_change(GaimConnection *gc, const char *name, const char *alias)
|
|
249 {
|
|
250 jabber_roster_update(gc->proto_data, name, NULL);
|
|
251 }
|
|
252
|
|
253 void jabber_roster_group_change(GaimConnection *gc, const char *name,
|
|
254 const char *old_group, const char *new_group)
|
|
255 {
|
|
256 GSList *buddies, *groups = NULL;
|
|
257 GaimBuddy *b;
|
|
258 GaimGroup *g;
|
|
259
|
|
260 if(!old_group || !new_group || !strcmp(old_group, new_group))
|
|
261 return;
|
|
262
|
|
263 buddies = gaim_find_buddies(gc->account, name);
|
|
264 while(buddies) {
|
|
265 b = buddies->data;
|
|
266 g = gaim_find_buddys_group(b);
|
|
267 if(!strcmp(g->name, old_group))
|
|
268 groups = g_slist_append(groups, (char*)new_group); /* ick */
|
|
269 else
|
|
270 groups = g_slist_append(groups, g->name);
|
|
271 buddies = g_slist_remove(buddies, b);
|
|
272 }
|
|
273 jabber_roster_update(gc->proto_data, name, groups);
|
|
274 g_slist_free(groups);
|
|
275 }
|
|
276
|
|
277 void jabber_roster_group_rename(GaimConnection *gc, const char *old_group,
|
|
278 const char *new_group, GList *members)
|
|
279 {
|
|
280 GList *l;
|
|
281 if(old_group && new_group && strcmp(old_group, new_group)) {
|
|
282 for(l = members; l; l = l->next) {
|
|
283 jabber_roster_group_change(gc, l->data, old_group, new_group);
|
|
284 }
|
|
285 }
|
|
286 }
|
|
287
|
|
288 void jabber_roster_remove_buddy(GaimConnection *gc, const char *name, const char *group) {
|
|
289 GSList *buddies = gaim_find_buddies(gc->account, name);
|
|
290 GSList *groups = NULL;
|
|
291 GaimGroup *g = gaim_find_group(group);
|
|
292 GaimBuddy *b = gaim_find_buddy_in_group(gc->account, name, g);
|
|
293
|
|
294 buddies = g_slist_remove(buddies, b);
|
|
295 if(g_slist_length(buddies)) {
|
|
296 while(buddies) {
|
|
297 b = buddies->data;
|
|
298 g = gaim_find_buddys_group(b);
|
|
299 groups = g_slist_append(groups, g->name);
|
|
300 buddies = g_slist_remove(buddies, b);
|
|
301 }
|
|
302 jabber_roster_update(gc->proto_data, name, groups);
|
|
303 } else {
|
|
304 jabber_presence_subscription_set(gc->proto_data, name, "unsubscribe");
|
|
305 }
|
|
306
|
|
307 if(buddies)
|
|
308 g_slist_free(buddies);
|
|
309 if(groups)
|
|
310 g_slist_free(groups);
|
|
311 }
|