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