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 gaim_blist_save();
|
|
200 }
|
|
201
|
|
202 static void jabber_roster_update(JabberStream *js, const char *name,
|
|
203 GSList *grps)
|
|
204 {
|
|
205 GaimBuddy *b;
|
|
206 GaimGroup *g;
|
|
207 GSList *groups = NULL, *l;
|
|
208 JabberIq *iq;
|
|
209 xmlnode *query, *item, *group;
|
|
210
|
|
211 if(grps) {
|
|
212 groups = grps;
|
|
213 } else {
|
|
214 GSList *buddies = gaim_find_buddies(js->gc->account, name);
|
|
215 if(!buddies)
|
|
216 return;
|
|
217 while(buddies) {
|
|
218 b = buddies->data;
|
|
219 g = gaim_find_buddys_group(b);
|
|
220 groups = g_slist_append(groups, g->name);
|
|
221 buddies = g_slist_remove(buddies, b);
|
|
222 }
|
|
223 }
|
|
224
|
8120
|
225 if(!(b = gaim_find_buddy(js->gc->account, name)))
|
|
226 return;
|
7014
|
227
|
|
228 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
|
|
229
|
|
230 query = xmlnode_get_child(iq->node, "query");
|
|
231 item = xmlnode_new_child(query, "item");
|
|
232
|
|
233 xmlnode_set_attrib(item, "jid", name);
|
|
234
|
|
235 if(b->alias)
|
|
236 xmlnode_set_attrib(item, "name", b->alias);
|
|
237
|
|
238 for(l = groups; l; l = l->next) {
|
|
239 group = xmlnode_new_child(item, "group");
|
|
240 xmlnode_insert_data(group, l->data, -1);
|
|
241 }
|
|
242
|
|
243 if(!grps)
|
|
244 g_slist_free(groups);
|
|
245
|
|
246 jabber_iq_send(iq);
|
|
247 }
|
|
248
|
|
249 void jabber_roster_add_buddy(GaimConnection *gc, const char *name,
|
|
250 GaimGroup *grp)
|
|
251 {
|
|
252 JabberStream *js = gc->proto_data;
|
|
253 char *who;
|
7425
|
254 GSList *groups = NULL;
|
7014
|
255 JabberBuddy *jb;
|
7488
|
256 JabberBuddyResource *jbr;
|
8194
|
257 char *my_bare_jid;
|
7014
|
258
|
|
259 if(!js->roster_parsed)
|
|
260 return;
|
|
261
|
7425
|
262 if(!(who = jabber_get_bare_jid(name)))
|
|
263 return;
|
7014
|
264
|
7425
|
265 jb = jabber_buddy_find(js, name, FALSE);
|
|
266
|
|
267 if(!jb || !(jb->subscription & JABBER_SUB_TO)) {
|
|
268 groups = g_slist_append(groups, grp->name);
|
|
269 }
|
7014
|
270
|
7449
|
271 jabber_roster_update(js, who, groups);
|
7014
|
272
|
8194
|
273 my_bare_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain);
|
|
274 if(!strcmp(who, my_bare_jid))
|
|
275 jabber_presence_fake_to_self(js, js->gc->away_state, js->gc->away);
|
|
276 else if(!jb || !(jb->subscription & JABBER_SUB_TO))
|
7014
|
277 jabber_presence_subscription_set(js, who, "subscribe");
|
7488
|
278 else if((jbr =jabber_buddy_find_resource(jb, NULL)))
|
|
279 serv_got_update(gc, who, 1, 0, 0, 0, jbr->state);
|
7425
|
280
|
8194
|
281 g_free(my_bare_jid);
|
7014
|
282 g_free(who);
|
|
283 }
|
|
284
|
|
285 void jabber_roster_alias_change(GaimConnection *gc, const char *name, const char *alias)
|
|
286 {
|
7449
|
287 GaimBuddy *b = gaim_find_buddy(gc->account, name);
|
7482
|
288 char *a;
|
7449
|
289
|
7482
|
290 a = g_strdup(alias);
|
|
291 gaim_blist_alias_buddy(b, a);
|
|
292 g_free(a);
|
7449
|
293
|
7014
|
294 jabber_roster_update(gc->proto_data, name, NULL);
|
|
295 }
|
|
296
|
|
297 void jabber_roster_group_change(GaimConnection *gc, const char *name,
|
|
298 const char *old_group, const char *new_group)
|
|
299 {
|
|
300 GSList *buddies, *groups = NULL;
|
|
301 GaimBuddy *b;
|
|
302 GaimGroup *g;
|
|
303
|
|
304 if(!old_group || !new_group || !strcmp(old_group, new_group))
|
|
305 return;
|
|
306
|
|
307 buddies = gaim_find_buddies(gc->account, name);
|
|
308 while(buddies) {
|
|
309 b = buddies->data;
|
|
310 g = gaim_find_buddys_group(b);
|
|
311 if(!strcmp(g->name, old_group))
|
|
312 groups = g_slist_append(groups, (char*)new_group); /* ick */
|
|
313 else
|
|
314 groups = g_slist_append(groups, g->name);
|
|
315 buddies = g_slist_remove(buddies, b);
|
|
316 }
|
|
317 jabber_roster_update(gc->proto_data, name, groups);
|
|
318 g_slist_free(groups);
|
|
319 }
|
|
320
|
|
321 void jabber_roster_group_rename(GaimConnection *gc, const char *old_group,
|
|
322 const char *new_group, GList *members)
|
|
323 {
|
|
324 GList *l;
|
|
325 if(old_group && new_group && strcmp(old_group, new_group)) {
|
|
326 for(l = members; l; l = l->next) {
|
|
327 jabber_roster_group_change(gc, l->data, old_group, new_group);
|
|
328 }
|
|
329 }
|
|
330 }
|
|
331
|
|
332 void jabber_roster_remove_buddy(GaimConnection *gc, const char *name, const char *group) {
|
|
333 GSList *buddies = gaim_find_buddies(gc->account, name);
|
|
334 GSList *groups = NULL;
|
|
335 GaimGroup *g = gaim_find_group(group);
|
|
336 GaimBuddy *b = gaim_find_buddy_in_group(gc->account, name, g);
|
|
337
|
|
338 buddies = g_slist_remove(buddies, b);
|
|
339 if(g_slist_length(buddies)) {
|
|
340 while(buddies) {
|
|
341 b = buddies->data;
|
|
342 g = gaim_find_buddys_group(b);
|
|
343 groups = g_slist_append(groups, g->name);
|
|
344 buddies = g_slist_remove(buddies, b);
|
|
345 }
|
|
346 jabber_roster_update(gc->proto_data, name, groups);
|
|
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
|
|
353 xmlnode_set_attrib(item, "jid", name);
|
|
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 }
|