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