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