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;
|
7955
|
58 int present =0, idle=0, state=0;
|
7014
|
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 idle = ((GaimBuddy*)buddies->data)->idle;
|
|
74 state = ((GaimBuddy*)buddies->data)->uc;
|
|
75 }
|
|
76
|
|
77 while(buddies) {
|
|
78 GaimBuddy *b = buddies->data;
|
|
79 GaimGroup *g = gaim_find_buddys_group(b);
|
|
80
|
|
81 buddies = g_slist_remove(buddies, b);
|
|
82
|
|
83 if((l = g_slist_find_custom(g2, g->name, (GCompareFunc)strcmp))) {
|
7955
|
84 const char *servernick;
|
|
85
|
|
86 if((servernick = gaim_blist_node_get_string((GaimBlistNode*)b, "servernick")))
|
|
87 serv_got_alias(js->gc, jid, servernick);
|
|
88
|
7014
|
89 if(alias && (!b->alias || strcmp(b->alias, alias)))
|
|
90 gaim_blist_alias_buddy(b, alias);
|
|
91 g_free(l->data);
|
|
92 g2 = g_slist_delete_link(g2, l);
|
|
93 } else {
|
|
94 gaim_blist_remove_buddy(b);
|
|
95 }
|
|
96 }
|
|
97
|
|
98 while(g2) {
|
|
99 GaimBuddy *b = gaim_buddy_new(js->gc->account, jid, alias);
|
|
100 GaimGroup *g = gaim_find_group(g2->data);
|
|
101
|
|
102 if(!g) {
|
|
103 g = gaim_group_new(g2->data);
|
|
104 gaim_blist_add_group(g, NULL);
|
|
105 }
|
|
106
|
|
107 b->present = present;
|
|
108 b->idle = idle;
|
|
109 b->uc = state;
|
|
110
|
|
111 gaim_blist_add_buddy(b, NULL, g, NULL);
|
7955
|
112 gaim_blist_alias_buddy(b, alias);
|
7014
|
113 g_free(g2->data);
|
|
114 g2 = g_slist_delete_link(g2, g2);
|
|
115 }
|
|
116
|
|
117 g_slist_free(buddies);
|
|
118 }
|
|
119
|
|
120 void jabber_roster_parse(JabberStream *js, xmlnode *packet)
|
|
121 {
|
|
122 xmlnode *query, *item, *group;
|
|
123 const char *from = xmlnode_get_attrib(packet, "from");
|
7310
|
124
|
|
125 if(from) {
|
7445
|
126 char *from_norm;
|
7310
|
127 gboolean invalid;
|
7175
|
128
|
7445
|
129 from_norm = g_strdup(jabber_normalize(js->gc->account, from));
|
|
130
|
|
131 if(!from_norm)
|
7310
|
132 return;
|
|
133
|
7445
|
134 invalid = g_utf8_collate(from_norm,
|
|
135 jabber_normalize(js->gc->account,
|
|
136 gaim_account_get_username(js->gc->account)));
|
7175
|
137
|
7310
|
138 g_free(from_norm);
|
|
139
|
|
140 if(invalid)
|
|
141 return;
|
7175
|
142 }
|
|
143
|
7014
|
144 query = xmlnode_get_child(packet, "query");
|
|
145 if(!query)
|
|
146 return;
|
|
147
|
|
148 js->roster_parsed = TRUE;
|
|
149
|
8135
|
150 for(item = xmlnode_get_child(query, "item"); item; item = xmlnode_get_next_twin(item))
|
7014
|
151 {
|
|
152 const char *jid, *name, *subscription, *ask;
|
|
153 JabberBuddy *jb;
|
|
154
|
|
155 subscription = xmlnode_get_attrib(item, "subscription");
|
|
156 jid = xmlnode_get_attrib(item, "jid");
|
|
157 name = xmlnode_get_attrib(item, "name");
|
|
158 ask = xmlnode_get_attrib(item, "ask");
|
|
159
|
8347
|
160 if(!jid)
|
|
161 continue;
|
|
162
|
|
163 if(!(jb = jabber_buddy_find(js, jid, TRUE)))
|
|
164 continue;
|
7014
|
165
|
7244
|
166 if(!subscription)
|
|
167 jb->subscription = JABBER_SUB_NONE;
|
|
168 else if(!strcmp(subscription, "to"))
|
7014
|
169 jb->subscription = JABBER_SUB_TO;
|
|
170 else if(!strcmp(subscription, "from"))
|
|
171 jb->subscription = JABBER_SUB_FROM;
|
|
172 else if(!strcmp(subscription, "both"))
|
|
173 jb->subscription = JABBER_SUB_BOTH;
|
8194
|
174 else if(!strcmp(subscription, "remove"))
|
|
175 jb->subscription = JABBER_SUB_REMOVE;
|
7014
|
176 else
|
|
177 jb->subscription = JABBER_SUB_NONE;
|
|
178
|
|
179 if(ask && !strcmp(ask, "subscribe"))
|
|
180 jb->subscription |= JABBER_SUB_PENDING;
|
|
181 else
|
|
182 jb->subscription &= ~JABBER_SUB_PENDING;
|
|
183
|
8194
|
184 if(jb->subscription == JABBER_SUB_REMOVE) {
|
7014
|
185 remove_gaim_buddies(js, jid);
|
|
186 } else {
|
|
187 GSList *groups = NULL;
|
8135
|
188 for(group = xmlnode_get_child(item, "group"); group; group = xmlnode_get_next_twin(group)) {
|
7316
|
189 char *group_name;
|
|
190
|
|
191 if(!(group_name = xmlnode_get_data(group)))
|
|
192 group_name = g_strdup("");
|
|
193 groups = g_slist_append(groups, group_name);
|
7014
|
194 }
|
|
195 add_gaim_buddies_in_groups(js, jid, name, groups);
|
|
196 }
|
|
197 }
|
|
198 }
|
|
199
|
|
200 static void jabber_roster_update(JabberStream *js, const char *name,
|
|
201 GSList *grps)
|
|
202 {
|
|
203 GaimBuddy *b;
|
|
204 GaimGroup *g;
|
|
205 GSList *groups = NULL, *l;
|
|
206 JabberIq *iq;
|
|
207 xmlnode *query, *item, *group;
|
|
208
|
|
209 if(grps) {
|
|
210 groups = grps;
|
|
211 } else {
|
|
212 GSList *buddies = gaim_find_buddies(js->gc->account, name);
|
|
213 if(!buddies)
|
|
214 return;
|
|
215 while(buddies) {
|
|
216 b = buddies->data;
|
|
217 g = gaim_find_buddys_group(b);
|
|
218 groups = g_slist_append(groups, g->name);
|
|
219 buddies = g_slist_remove(buddies, b);
|
|
220 }
|
|
221 }
|
|
222
|
8120
|
223 if(!(b = gaim_find_buddy(js->gc->account, name)))
|
|
224 return;
|
7014
|
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
|
9285
|
247 void jabber_roster_add_buddy(GaimConnection *gc, GaimBuddy *buddy,
|
|
248 GaimGroup *group)
|
7014
|
249 {
|
|
250 JabberStream *js = gc->proto_data;
|
|
251 char *who;
|
7425
|
252 GSList *groups = NULL;
|
7014
|
253 JabberBuddy *jb;
|
7488
|
254 JabberBuddyResource *jbr;
|
8194
|
255 char *my_bare_jid;
|
7014
|
256
|
|
257 if(!js->roster_parsed)
|
|
258 return;
|
|
259
|
9285
|
260 if(!(who = jabber_get_bare_jid(buddy->name)))
|
7425
|
261 return;
|
7014
|
262
|
9285
|
263 jb = jabber_buddy_find(js, buddy->name, FALSE);
|
7425
|
264
|
|
265 if(!jb || !(jb->subscription & JABBER_SUB_TO)) {
|
9285
|
266 groups = g_slist_append(groups, group->name);
|
7425
|
267 }
|
7014
|
268
|
7449
|
269 jabber_roster_update(js, who, groups);
|
7014
|
270
|
8194
|
271 my_bare_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
|
|
272 if(!strcmp(who, my_bare_jid))
|
|
273 jabber_presence_fake_to_self(js, js->gc->away_state, js->gc->away);
|
|
274 else if(!jb || !(jb->subscription & JABBER_SUB_TO))
|
7014
|
275 jabber_presence_subscription_set(js, who, "subscribe");
|
7488
|
276 else if((jbr =jabber_buddy_find_resource(jb, NULL)))
|
|
277 serv_got_update(gc, who, 1, 0, 0, 0, jbr->state);
|
7425
|
278
|
8194
|
279 g_free(my_bare_jid);
|
7014
|
280 g_free(who);
|
|
281 }
|
|
282
|
|
283 void jabber_roster_alias_change(GaimConnection *gc, const char *name, const char *alias)
|
|
284 {
|
7449
|
285 GaimBuddy *b = gaim_find_buddy(gc->account, name);
|
7482
|
286 char *a;
|
7449
|
287
|
7482
|
288 a = g_strdup(alias);
|
|
289 gaim_blist_alias_buddy(b, a);
|
|
290 g_free(a);
|
7449
|
291
|
7014
|
292 jabber_roster_update(gc->proto_data, name, NULL);
|
|
293 }
|
|
294
|
|
295 void jabber_roster_group_change(GaimConnection *gc, const char *name,
|
|
296 const char *old_group, const char *new_group)
|
|
297 {
|
|
298 GSList *buddies, *groups = NULL;
|
|
299 GaimBuddy *b;
|
|
300 GaimGroup *g;
|
|
301
|
|
302 if(!old_group || !new_group || !strcmp(old_group, new_group))
|
|
303 return;
|
|
304
|
|
305 buddies = gaim_find_buddies(gc->account, name);
|
|
306 while(buddies) {
|
|
307 b = buddies->data;
|
|
308 g = gaim_find_buddys_group(b);
|
|
309 if(!strcmp(g->name, old_group))
|
|
310 groups = g_slist_append(groups, (char*)new_group); /* ick */
|
|
311 else
|
|
312 groups = g_slist_append(groups, g->name);
|
|
313 buddies = g_slist_remove(buddies, b);
|
|
314 }
|
|
315 jabber_roster_update(gc->proto_data, name, groups);
|
|
316 g_slist_free(groups);
|
|
317 }
|
|
318
|
9285
|
319 void jabber_roster_group_rename(GaimConnection *gc, const char *old_name,
|
|
320 GaimGroup *group, GList *moved_buddies)
|
7014
|
321 {
|
|
322 GList *l;
|
9285
|
323 for(l = moved_buddies; l; l = l->next) {
|
|
324 GaimBuddy *buddy = l->data;
|
|
325 jabber_roster_group_change(gc, buddy->name, old_name, group->name);
|
7014
|
326 }
|
|
327 }
|
|
328
|
9285
|
329 void jabber_roster_remove_buddy(GaimConnection *gc, GaimBuddy *buddy,
|
|
330 GaimGroup *group) {
|
|
331 GSList *buddies = gaim_find_buddies(gc->account, buddy->name);
|
7014
|
332 GSList *groups = NULL;
|
|
333
|
9285
|
334 buddies = g_slist_remove(buddies, buddy);
|
7014
|
335 if(g_slist_length(buddies)) {
|
9285
|
336 GaimBuddy *tmpbuddy;
|
|
337 GaimGroup *tmpgroup;
|
|
338
|
7014
|
339 while(buddies) {
|
9285
|
340 tmpbuddy = buddies->data;
|
|
341 tmpgroup = gaim_find_buddys_group(tmpbuddy);
|
|
342 groups = g_slist_append(groups, tmpgroup->name);
|
|
343 buddies = g_slist_remove(buddies, tmpbuddy);
|
7014
|
344 }
|
9285
|
345
|
|
346 jabber_roster_update(gc->proto_data, buddy->name, groups);
|
7014
|
347 } else {
|
7171
|
348 JabberIq *iq = jabber_iq_new_query(gc->proto_data, JABBER_IQ_SET,
|
|
349 "jabber:iq:roster");
|
|
350 xmlnode *query = xmlnode_get_child(iq->node, "query");
|
|
351 xmlnode *item = xmlnode_new_child(query, "item");
|
|
352
|
9285
|
353 xmlnode_set_attrib(item, "jid", buddy->name);
|
7171
|
354 xmlnode_set_attrib(item, "subscription", "remove");
|
|
355
|
|
356 jabber_iq_send(iq);
|
7014
|
357 }
|
|
358
|
|
359 if(buddies)
|
|
360 g_slist_free(buddies);
|
|
361 if(groups)
|
|
362 g_slist_free(groups);
|
|
363 }
|