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");
|
7310
|
120
|
|
121 if(from) {
|
7445
|
122 char *from_norm;
|
7310
|
123 gboolean invalid;
|
7175
|
124
|
7445
|
125 from_norm = g_strdup(jabber_normalize(js->gc->account, from));
|
|
126
|
|
127 if(!from_norm)
|
7310
|
128 return;
|
|
129
|
7445
|
130 invalid = g_utf8_collate(from_norm,
|
|
131 jabber_normalize(js->gc->account,
|
|
132 gaim_account_get_username(js->gc->account)));
|
7175
|
133
|
7310
|
134 g_free(from_norm);
|
|
135
|
|
136 if(invalid)
|
|
137 return;
|
7175
|
138 }
|
|
139
|
7014
|
140 query = xmlnode_get_child(packet, "query");
|
|
141 if(!query)
|
|
142 return;
|
|
143
|
|
144 js->roster_parsed = TRUE;
|
|
145
|
|
146 for(item = query->child; item; item = item->next)
|
|
147 {
|
|
148 const char *jid, *name, *subscription, *ask;
|
|
149 JabberBuddy *jb;
|
|
150
|
|
151 if(item->type != NODE_TYPE_TAG || strcmp(item->name, "item"))
|
|
152 continue;
|
|
153
|
|
154 subscription = xmlnode_get_attrib(item, "subscription");
|
|
155 jid = xmlnode_get_attrib(item, "jid");
|
|
156 name = xmlnode_get_attrib(item, "name");
|
|
157 ask = xmlnode_get_attrib(item, "ask");
|
|
158
|
|
159 jb = jabber_buddy_find(js, jid, TRUE);
|
|
160
|
7244
|
161 if(!subscription)
|
|
162 jb->subscription = JABBER_SUB_NONE;
|
|
163 else if(!strcmp(subscription, "to"))
|
7014
|
164 jb->subscription = JABBER_SUB_TO;
|
|
165 else if(!strcmp(subscription, "from"))
|
|
166 jb->subscription = JABBER_SUB_FROM;
|
|
167 else if(!strcmp(subscription, "both"))
|
|
168 jb->subscription = JABBER_SUB_BOTH;
|
|
169 else
|
|
170 jb->subscription = JABBER_SUB_NONE;
|
|
171
|
|
172 if(ask && !strcmp(ask, "subscribe"))
|
|
173 jb->subscription |= JABBER_SUB_PENDING;
|
|
174 else
|
|
175 jb->subscription &= ~JABBER_SUB_PENDING;
|
|
176
|
|
177 if(jb->subscription == JABBER_SUB_NONE) {
|
|
178 jb = jabber_buddy_find(js, jid, FALSE);
|
|
179 if(jb)
|
|
180 jb->subscription = JABBER_SUB_NONE;
|
|
181 remove_gaim_buddies(js, jid);
|
|
182 } else {
|
|
183 GSList *groups = NULL;
|
|
184
|
|
185 for(group = item->child; group; group = group->next) {
|
7316
|
186 char *group_name;
|
7014
|
187 if(group->type != NODE_TYPE_TAG || strcmp(group->name, "group"))
|
|
188 continue;
|
7316
|
189
|
|
190 if(!(group_name = xmlnode_get_data(group)))
|
|
191 group_name = g_strdup("");
|
|
192 groups = g_slist_append(groups, group_name);
|
7014
|
193 }
|
|
194 add_gaim_buddies_in_groups(js, jid, name, groups);
|
|
195 }
|
|
196 }
|
|
197
|
|
198 gaim_blist_save();
|
|
199 }
|
|
200
|
|
201 static void jabber_roster_update(JabberStream *js, const char *name,
|
|
202 GSList *grps)
|
|
203 {
|
|
204 GaimBuddy *b;
|
|
205 GaimGroup *g;
|
|
206 GSList *groups = NULL, *l;
|
|
207 JabberIq *iq;
|
|
208 xmlnode *query, *item, *group;
|
|
209
|
|
210 if(grps) {
|
|
211 groups = grps;
|
|
212 } else {
|
|
213 GSList *buddies = gaim_find_buddies(js->gc->account, name);
|
|
214 if(!buddies)
|
|
215 return;
|
|
216 while(buddies) {
|
|
217 b = buddies->data;
|
|
218 g = gaim_find_buddys_group(b);
|
|
219 groups = g_slist_append(groups, g->name);
|
|
220 buddies = g_slist_remove(buddies, b);
|
|
221 }
|
|
222 }
|
|
223
|
|
224 b = gaim_find_buddy(js->gc->account, name);
|
|
225
|
|
226 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
|
|
227
|
|
228 query = xmlnode_get_child(iq->node, "query");
|
|
229 item = xmlnode_new_child(query, "item");
|
|
230
|
|
231 xmlnode_set_attrib(item, "jid", name);
|
|
232
|
|
233 if(b->alias)
|
|
234 xmlnode_set_attrib(item, "name", b->alias);
|
|
235
|
|
236 for(l = groups; l; l = l->next) {
|
|
237 group = xmlnode_new_child(item, "group");
|
|
238 xmlnode_insert_data(group, l->data, -1);
|
|
239 }
|
|
240
|
|
241 if(!grps)
|
|
242 g_slist_free(groups);
|
|
243
|
|
244 jabber_iq_send(iq);
|
|
245 }
|
|
246
|
|
247 void jabber_roster_add_buddy(GaimConnection *gc, const char *name,
|
|
248 GaimGroup *grp)
|
|
249 {
|
|
250 JabberStream *js = gc->proto_data;
|
|
251 char *who;
|
7425
|
252 GSList *groups = NULL;
|
7014
|
253 JabberBuddy *jb;
|
7488
|
254 JabberBuddyResource *jbr;
|
7014
|
255
|
|
256 if(!js->roster_parsed)
|
|
257 return;
|
|
258
|
7425
|
259 if(!(who = jabber_get_bare_jid(name)))
|
|
260 return;
|
7014
|
261
|
7425
|
262 jb = jabber_buddy_find(js, name, FALSE);
|
|
263
|
|
264 if(!jb || !(jb->subscription & JABBER_SUB_TO)) {
|
|
265 groups = g_slist_append(groups, grp->name);
|
|
266 }
|
7014
|
267
|
7449
|
268 jabber_roster_update(js, who, groups);
|
7014
|
269
|
|
270 if(!jb || !(jb->subscription & JABBER_SUB_TO))
|
|
271 jabber_presence_subscription_set(js, who, "subscribe");
|
7488
|
272 else if((jbr =jabber_buddy_find_resource(jb, NULL)))
|
|
273 serv_got_update(gc, who, 1, 0, 0, 0, jbr->state);
|
7425
|
274
|
7014
|
275 g_free(who);
|
|
276 }
|
|
277
|
|
278 void jabber_roster_alias_change(GaimConnection *gc, const char *name, const char *alias)
|
|
279 {
|
7449
|
280 GaimBuddy *b = gaim_find_buddy(gc->account, name);
|
7482
|
281 char *a;
|
7449
|
282
|
7482
|
283 a = g_strdup(alias);
|
|
284 gaim_blist_alias_buddy(b, a);
|
|
285 g_free(a);
|
7449
|
286
|
7014
|
287 jabber_roster_update(gc->proto_data, name, NULL);
|
|
288 }
|
|
289
|
|
290 void jabber_roster_group_change(GaimConnection *gc, const char *name,
|
|
291 const char *old_group, const char *new_group)
|
|
292 {
|
|
293 GSList *buddies, *groups = NULL;
|
|
294 GaimBuddy *b;
|
|
295 GaimGroup *g;
|
|
296
|
|
297 if(!old_group || !new_group || !strcmp(old_group, new_group))
|
|
298 return;
|
|
299
|
|
300 buddies = gaim_find_buddies(gc->account, name);
|
|
301 while(buddies) {
|
|
302 b = buddies->data;
|
|
303 g = gaim_find_buddys_group(b);
|
|
304 if(!strcmp(g->name, old_group))
|
|
305 groups = g_slist_append(groups, (char*)new_group); /* ick */
|
|
306 else
|
|
307 groups = g_slist_append(groups, g->name);
|
|
308 buddies = g_slist_remove(buddies, b);
|
|
309 }
|
|
310 jabber_roster_update(gc->proto_data, name, groups);
|
|
311 g_slist_free(groups);
|
|
312 }
|
|
313
|
|
314 void jabber_roster_group_rename(GaimConnection *gc, const char *old_group,
|
|
315 const char *new_group, GList *members)
|
|
316 {
|
|
317 GList *l;
|
|
318 if(old_group && new_group && strcmp(old_group, new_group)) {
|
|
319 for(l = members; l; l = l->next) {
|
|
320 jabber_roster_group_change(gc, l->data, old_group, new_group);
|
|
321 }
|
|
322 }
|
|
323 }
|
|
324
|
|
325 void jabber_roster_remove_buddy(GaimConnection *gc, const char *name, const char *group) {
|
|
326 GSList *buddies = gaim_find_buddies(gc->account, name);
|
|
327 GSList *groups = NULL;
|
|
328 GaimGroup *g = gaim_find_group(group);
|
|
329 GaimBuddy *b = gaim_find_buddy_in_group(gc->account, name, g);
|
|
330
|
|
331 buddies = g_slist_remove(buddies, b);
|
|
332 if(g_slist_length(buddies)) {
|
|
333 while(buddies) {
|
|
334 b = buddies->data;
|
|
335 g = gaim_find_buddys_group(b);
|
|
336 groups = g_slist_append(groups, g->name);
|
|
337 buddies = g_slist_remove(buddies, b);
|
|
338 }
|
|
339 jabber_roster_update(gc->proto_data, name, groups);
|
|
340 } else {
|
7171
|
341 JabberIq *iq = jabber_iq_new_query(gc->proto_data, JABBER_IQ_SET,
|
|
342 "jabber:iq:roster");
|
|
343 xmlnode *query = xmlnode_get_child(iq->node, "query");
|
|
344 xmlnode *item = xmlnode_new_child(query, "item");
|
|
345
|
|
346 xmlnode_set_attrib(item, "jid", name);
|
|
347 xmlnode_set_attrib(item, "subscription", "remove");
|
|
348
|
|
349 jabber_iq_send(iq);
|
7014
|
350 }
|
|
351
|
|
352 if(buddies)
|
|
353 g_slist_free(buddies);
|
|
354 if(groups)
|
|
355 g_slist_free(groups);
|
|
356 }
|