Thu, 06 Nov 2003 20:40:49 +0000
[gaim-migrate @ 8060]
jabber roster tweaks
| 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; |
| 254 | ||
| 255 | if(!js->roster_parsed) | |
| 256 | return; | |
| 257 | ||
| 7425 | 258 | if(!(who = jabber_get_bare_jid(name))) |
| 259 | return; | |
| 7014 | 260 | |
| 7425 | 261 | jb = jabber_buddy_find(js, name, FALSE); |
| 262 | ||
| 263 | if(!jb || !(jb->subscription & JABBER_SUB_TO)) { | |
| 264 | groups = g_slist_append(groups, grp->name); | |
| 265 | } | |
| 7014 | 266 | |
| 7449 | 267 | jabber_roster_update(js, who, groups); |
| 7014 | 268 | |
| 269 | if(!jb || !(jb->subscription & JABBER_SUB_TO)) | |
| 270 | jabber_presence_subscription_set(js, who, "subscribe"); | |
| 7425 | 271 | |
| 7014 | 272 | g_free(who); |
| 273 | } | |
| 274 | ||
| 275 | void jabber_roster_alias_change(GaimConnection *gc, const char *name, const char *alias) | |
| 276 | { | |
| 7449 | 277 | GaimBuddy *b = gaim_find_buddy(gc->account, name); |
| 278 | ||
| 279 | gaim_blist_alias_buddy(b, alias); | |
| 280 | ||
| 7014 | 281 | jabber_roster_update(gc->proto_data, name, NULL); |
| 282 | } | |
| 283 | ||
| 284 | void jabber_roster_group_change(GaimConnection *gc, const char *name, | |
| 285 | const char *old_group, const char *new_group) | |
| 286 | { | |
| 287 | GSList *buddies, *groups = NULL; | |
| 288 | GaimBuddy *b; | |
| 289 | GaimGroup *g; | |
| 290 | ||
| 291 | if(!old_group || !new_group || !strcmp(old_group, new_group)) | |
| 292 | return; | |
| 293 | ||
| 294 | buddies = gaim_find_buddies(gc->account, name); | |
| 295 | while(buddies) { | |
| 296 | b = buddies->data; | |
| 297 | g = gaim_find_buddys_group(b); | |
| 298 | if(!strcmp(g->name, old_group)) | |
| 299 | groups = g_slist_append(groups, (char*)new_group); /* ick */ | |
| 300 | else | |
| 301 | groups = g_slist_append(groups, g->name); | |
| 302 | buddies = g_slist_remove(buddies, b); | |
| 303 | } | |
| 304 | jabber_roster_update(gc->proto_data, name, groups); | |
| 305 | g_slist_free(groups); | |
| 306 | } | |
| 307 | ||
| 308 | void jabber_roster_group_rename(GaimConnection *gc, const char *old_group, | |
| 309 | const char *new_group, GList *members) | |
| 310 | { | |
| 311 | GList *l; | |
| 312 | if(old_group && new_group && strcmp(old_group, new_group)) { | |
| 313 | for(l = members; l; l = l->next) { | |
| 314 | jabber_roster_group_change(gc, l->data, old_group, new_group); | |
| 315 | } | |
| 316 | } | |
| 317 | } | |
| 318 | ||
| 319 | void jabber_roster_remove_buddy(GaimConnection *gc, const char *name, const char *group) { | |
| 320 | GSList *buddies = gaim_find_buddies(gc->account, name); | |
| 321 | GSList *groups = NULL; | |
| 322 | GaimGroup *g = gaim_find_group(group); | |
| 323 | GaimBuddy *b = gaim_find_buddy_in_group(gc->account, name, g); | |
| 324 | ||
| 325 | buddies = g_slist_remove(buddies, b); | |
| 326 | if(g_slist_length(buddies)) { | |
| 327 | while(buddies) { | |
| 328 | b = buddies->data; | |
| 329 | g = gaim_find_buddys_group(b); | |
| 330 | groups = g_slist_append(groups, g->name); | |
| 331 | buddies = g_slist_remove(buddies, b); | |
| 332 | } | |
| 333 | jabber_roster_update(gc->proto_data, name, groups); | |
| 334 | } else { | |
| 7171 | 335 | JabberIq *iq = jabber_iq_new_query(gc->proto_data, JABBER_IQ_SET, |
| 336 | "jabber:iq:roster"); | |
| 337 | xmlnode *query = xmlnode_get_child(iq->node, "query"); | |
| 338 | xmlnode *item = xmlnode_new_child(query, "item"); | |
| 339 | ||
| 340 | xmlnode_set_attrib(item, "jid", name); | |
| 341 | xmlnode_set_attrib(item, "subscription", "remove"); | |
| 342 | ||
| 343 | jabber_iq_send(iq); | |
| 7014 | 344 | } |
| 345 | ||
| 346 | if(buddies) | |
| 347 | g_slist_free(buddies); | |
| 348 | if(groups) | |
| 349 | g_slist_free(groups); | |
| 350 | } |