Thu, 02 Oct 2003 01:58:26 +0000
[gaim-migrate @ 7683]
fix a big ugly memleak on jabber account signoff, fix the img display in vcards until the gtk code can be talked into understanding proper XHTML, and a few misc other things I felt like getting out of my tree.
| 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" | |
| 7076 | 23 | #include "imgstore.h" |
| 7014 | 24 | #include "multi.h" |
| 25 | #include "notify.h" | |
| 26 | #include "request.h" | |
| 27 | #include "util.h" | |
| 28 | ||
| 29 | #include "buddy.h" | |
| 30 | #include "chat.h" | |
| 31 | #include "jabber.h" | |
| 32 | #include "iq.h" | |
| 33 | #include "presence.h" | |
| 34 | #include "xmlnode.h" | |
| 35 | ||
| 36 | ||
| 7116 | 37 | void jabber_buddy_free(JabberBuddy *jb) |
| 38 | { | |
| 39 | g_return_if_fail(jb != NULL); | |
| 40 | ||
| 41 | if(jb->error_msg) | |
| 42 | g_free(jb->error_msg); | |
| 43 | while(jb->resources) | |
| 44 | jabber_buddy_resource_free(jb->resources->data); | |
| 45 | ||
| 46 | g_free(jb); | |
| 47 | } | |
| 48 | ||
| 7014 | 49 | JabberBuddy *jabber_buddy_find(JabberStream *js, const char *name, |
| 50 | gboolean create) | |
| 51 | { | |
| 52 | JabberBuddy *jb; | |
| 53 | JabberID *jid = jabber_id_new(name); | |
| 54 | char *realname; | |
| 55 | ||
| 56 | if(!jid) | |
| 57 | return NULL; | |
| 58 | ||
| 59 | if(jid->node) | |
| 60 | realname = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
| 61 | else | |
| 62 | realname = g_strdup(jid->domain); | |
| 63 | ||
| 64 | jb = g_hash_table_lookup(js->buddies, realname); | |
| 65 | ||
| 66 | if(!jb && create) { | |
| 67 | jb = g_new0(JabberBuddy, 1); | |
| 68 | g_hash_table_insert(js->buddies, g_strdup(realname), jb); | |
| 69 | } | |
| 70 | g_free(realname); | |
| 71 | ||
| 72 | jabber_id_free(jid); | |
| 73 | ||
| 74 | return jb; | |
| 75 | } | |
| 76 | ||
| 77 | ||
| 78 | JabberBuddyResource *jabber_buddy_find_resource(JabberBuddy *jb, | |
| 79 | const char *resource) | |
| 80 | { | |
| 81 | JabberBuddyResource *jbr = NULL; | |
| 82 | GList *l; | |
| 83 | ||
| 84 | if(!jb) | |
| 85 | return NULL; | |
| 86 | ||
| 87 | for(l = jb->resources; l; l = l->next) | |
| 88 | { | |
| 89 | if(!jbr && !resource) { | |
| 90 | jbr = l->data; | |
| 91 | } else if(!resource) { | |
| 92 | if(((JabberBuddyResource *)l->data)->priority >= jbr->priority) | |
| 93 | jbr = l->data; | |
| 94 | } else if(((JabberBuddyResource *)l->data)->name) { | |
| 95 | if(!strcmp(((JabberBuddyResource *)l->data)->name, resource)) { | |
| 96 | jbr = l->data; | |
| 97 | break; | |
| 98 | } | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | return jbr; | |
| 103 | } | |
| 104 | ||
| 105 | void jabber_buddy_track_resource(JabberBuddy *jb, const char *resource, | |
| 106 | int priority, int state, const char *status) | |
| 107 | { | |
| 108 | JabberBuddyResource *jbr = jabber_buddy_find_resource(jb, resource); | |
| 109 | ||
| 110 | if(!jbr) { | |
| 111 | jbr = g_new0(JabberBuddyResource, 1); | |
| 7116 | 112 | jbr->jb = jb; |
| 7014 | 113 | jbr->name = g_strdup(resource); |
| 114 | jbr->capabilities = JABBER_CAP_XHTML; | |
| 115 | jb->resources = g_list_append(jb->resources, jbr); | |
| 116 | } | |
| 117 | jbr->priority = priority; | |
| 118 | jbr->state = state; | |
| 119 | if(jbr->status) | |
| 120 | g_free(jbr->status); | |
| 121 | jbr->status = g_strdup(status); | |
| 122 | } | |
| 123 | ||
| 7116 | 124 | void jabber_buddy_resource_free(JabberBuddyResource *jbr) |
| 125 | { | |
| 126 | g_return_if_fail(jbr != NULL); | |
| 127 | ||
| 128 | jbr->jb->resources = g_list_remove(jbr->jb->resources, jbr); | |
| 129 | ||
| 130 | g_free(jbr->name); | |
| 131 | if(jbr->status) | |
| 132 | g_free(jbr->status); | |
| 133 | g_free(jbr); | |
| 134 | } | |
| 135 | ||
| 7014 | 136 | void jabber_buddy_remove_resource(JabberBuddy *jb, const char *resource) |
| 137 | { | |
| 138 | JabberBuddyResource *jbr = jabber_buddy_find_resource(jb, resource); | |
| 139 | ||
| 140 | if(!jbr) | |
| 141 | return; | |
| 142 | ||
| 7116 | 143 | jabber_buddy_resource_free(jbr); |
| 7014 | 144 | } |
| 145 | ||
| 146 | const char *jabber_buddy_get_status_msg(JabberBuddy *jb) | |
| 147 | { | |
| 148 | JabberBuddyResource *jbr; | |
| 149 | ||
| 150 | if(!jb) | |
| 151 | return NULL; | |
| 152 | ||
| 153 | jbr = jabber_buddy_find_resource(jb, NULL); | |
| 154 | ||
| 155 | if(!jbr) | |
| 156 | return NULL; | |
| 157 | ||
| 158 | return jbr->status; | |
| 159 | } | |
| 160 | ||
| 161 | /******* | |
| 162 | * This is the old vCard stuff taken from the old prpl. vCards, by definition | |
| 163 | * are a temporary thing until jabber can get its act together and come up | |
| 164 | * with a format for user information, hence the namespace of 'vcard-temp' | |
| 165 | * | |
| 166 | * Since I don't feel like putting that much work into something that's | |
| 167 | * _supposed_ to go away, i'm going to just copy the kludgy old code here, | |
| 168 | * and make it purdy when jabber comes up with a standards-track JEP to | |
| 169 | * replace vcard-temp | |
| 170 | * --Nathan | |
| 171 | *******/ | |
| 172 | ||
| 173 | /*---------------------------------------*/ | |
| 174 | /* Jabber "set info" (vCard) support */ | |
| 175 | /*---------------------------------------*/ | |
| 176 | ||
| 177 | /* | |
| 178 | * V-Card format: | |
| 179 | * | |
| 180 | * <vCard prodid='' version='' xmlns=''> | |
| 181 | * <FN></FN> | |
| 182 | * <N> | |
| 183 | * <FAMILY/> | |
| 184 | * <GIVEN/> | |
| 185 | * </N> | |
| 186 | * <NICKNAME/> | |
| 187 | * <URL/> | |
| 188 | * <ADR> | |
| 189 | * <STREET/> | |
| 190 | * <EXTADD/> | |
| 191 | * <LOCALITY/> | |
| 192 | * <REGION/> | |
| 193 | * <PCODE/> | |
| 194 | * <COUNTRY/> | |
| 195 | * </ADR> | |
| 196 | * <TEL/> | |
| 197 | * <EMAIL/> | |
| 198 | * <ORG> | |
| 199 | * <ORGNAME/> | |
| 200 | * <ORGUNIT/> | |
| 201 | * </ORG> | |
| 202 | * <TITLE/> | |
| 203 | * <ROLE/> | |
| 204 | * <DESC/> | |
| 205 | * <BDAY/> | |
| 206 | * </vCard> | |
| 207 | * | |
| 208 | * See also: | |
| 209 | * | |
| 210 | * http://docs.jabber.org/proto/html/vcard-temp.html | |
| 211 | * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd | |
| 212 | */ | |
| 213 | ||
| 214 | /* | |
| 215 | * Cross-reference user-friendly V-Card entry labels to vCard XML tags | |
| 216 | * and attributes. | |
| 217 | * | |
| 218 | * Order is (or should be) unimportant. For example: we have no way of | |
| 219 | * knowing in what order real data will arrive. | |
| 220 | * | |
| 221 | * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag | |
| 222 | * name, XML tag's parent tag "path" (relative to vCard node). | |
| 223 | * | |
| 224 | * List is terminated by a NULL label pointer. | |
| 225 | * | |
| 226 | * Entries with no label text, but with XML tag and parent tag | |
| 227 | * entries, are used by V-Card XML construction routines to | |
| 228 | * "automagically" construct the appropriate XML node tree. | |
| 229 | * | |
| 230 | * Thoughts on future direction/expansion | |
| 231 | * | |
| 232 | * This is a "simple" vCard. | |
| 233 | * | |
| 234 | * It is possible for nodes other than the "vCard" node to have | |
| 235 | * attributes. Should that prove necessary/desirable, add an | |
| 236 | * "attributes" pointer to the vcard_template struct, create the | |
| 237 | * necessary tag_attr structs, and add 'em to the vcard_dflt_data | |
| 238 | * array. | |
| 239 | * | |
| 240 | * The above changes will (obviously) require changes to the vCard | |
| 241 | * construction routines. | |
| 242 | */ | |
| 243 | ||
| 244 | struct vcard_template { | |
| 245 | char *label; /* label text pointer */ | |
| 246 | char *text; /* entry text pointer */ | |
| 247 | int visible; /* should entry field be "visible?" */ | |
| 248 | int editable; /* should entry field be editable? */ | |
| 249 | char *tag; /* tag text */ | |
| 250 | char *ptag; /* parent tag "path" text */ | |
| 251 | char *url; /* vCard display format if URL */ | |
| 252 | } vcard_template_data[] = { | |
| 253 | {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, | |
| 254 | {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, | |
| 255 | {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, | |
| 256 | {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, | |
| 257 | {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, | |
| 258 | {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, | |
| 259 | {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, | |
| 260 | {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, | |
| 261 | {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, | |
| 262 | {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, | |
| 263 | {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, | |
| 264 | {N_("Telephone"), NULL, TRUE, TRUE, "TELEPHONE", NULL, NULL}, | |
| 265 | {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, | |
| 266 | {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, | |
| 267 | {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, | |
| 268 | {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, | |
| 269 | {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, | |
| 270 | {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, | |
| 271 | {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, | |
| 272 | {"", NULL, TRUE, TRUE, "N", NULL, NULL}, | |
| 273 | {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, | |
| 274 | {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, | |
| 275 | {NULL, NULL, 0, 0, NULL, NULL, NULL} | |
| 276 | }; | |
| 277 | ||
| 278 | /* | |
| 279 | * The "vCard" tag's attibute list... | |
| 280 | */ | |
| 281 | struct tag_attr { | |
| 282 | char *attr; | |
| 283 | char *value; | |
| 284 | } vcard_tag_attr_list[] = { | |
| 285 | {"prodid", "-//HandGen//NONSGML vGen v1.0//EN"}, | |
| 286 | {"version", "2.0", }, | |
| 287 | {"xmlns", "vcard-temp", }, | |
| 288 | {NULL, NULL}, | |
| 289 | }; | |
| 290 | ||
| 291 | ||
| 292 | /* | |
| 293 | * Insert a tag node into an xmlnode tree, recursively inserting parent tag | |
| 294 | * nodes as necessary | |
| 295 | * | |
| 296 | * Returns pointer to inserted node | |
| 297 | * | |
| 298 | * Note to hackers: this code is designed to be re-entrant (it's recursive--it | |
| 299 | * calls itself), so don't put any "static"s in here! | |
| 300 | */ | |
| 301 | static xmlnode *insert_tag_to_parent_tag(xmlnode *start, const char *parent_tag, const char *new_tag) | |
| 302 | { | |
| 303 | xmlnode *x = NULL; | |
| 304 | ||
| 305 | /* | |
| 306 | * If the parent tag wasn't specified, see if we can get it | |
| 307 | * from the vCard template struct. | |
| 308 | */ | |
| 309 | if(parent_tag == NULL) { | |
| 310 | struct vcard_template *vc_tp = vcard_template_data; | |
| 311 | ||
| 312 | while(vc_tp->label != NULL) { | |
| 313 | if(strcmp(vc_tp->tag, new_tag) == 0) { | |
| 314 | parent_tag = vc_tp->ptag; | |
| 315 | break; | |
| 316 | } | |
| 317 | ++vc_tp; | |
| 318 | } | |
| 319 | } | |
| 320 | ||
| 321 | /* | |
| 322 | * If we have a parent tag... | |
| 323 | */ | |
| 324 | if(parent_tag != NULL ) { | |
| 325 | /* | |
| 326 | * Try to get the parent node for a tag | |
| 327 | */ | |
| 328 | if((x = xmlnode_get_child(start, parent_tag)) == NULL) { | |
| 329 | /* | |
| 330 | * Descend? | |
| 331 | */ | |
| 332 | char *grand_parent = g_strdup(parent_tag); | |
| 333 | char *parent; | |
| 334 | ||
| 335 | if((parent = strrchr(grand_parent, '/')) != NULL) { | |
| 336 | *(parent++) = '\0'; | |
| 337 | x = insert_tag_to_parent_tag(start, grand_parent, parent); | |
| 338 | } else { | |
| 339 | x = xmlnode_new_child(start, grand_parent); | |
| 340 | } | |
| 341 | g_free(grand_parent); | |
| 342 | } else { | |
| 343 | /* | |
| 344 | * We found *something* to be the parent node. | |
| 345 | * Note: may be the "root" node! | |
| 346 | */ | |
| 347 | xmlnode *y; | |
| 348 | if((y = xmlnode_get_child(x, new_tag)) != NULL) { | |
| 349 | return(y); | |
| 350 | } | |
| 351 | } | |
| 352 | } | |
| 353 | ||
| 354 | /* | |
| 355 | * insert the new tag into its parent node | |
| 356 | */ | |
| 357 | return(xmlnode_new_child((x == NULL? start : x), new_tag)); | |
| 358 | } | |
| 359 | ||
| 360 | /* | |
| 361 | * Send vCard info to Jabber server | |
| 362 | */ | |
| 363 | void jabber_set_info(GaimConnection *gc, const char *info) | |
| 364 | { | |
| 365 | JabberIq *iq; | |
| 366 | JabberStream *js = gc->proto_data; | |
| 367 | xmlnode *vc_node; | |
| 368 | ||
| 369 | ||
| 370 | /* | |
| 371 | * Send only if there's actually any *information* to send | |
| 372 | */ | |
| 373 | vc_node = xmlnode_from_str(info, -1); | |
| 374 | ||
| 375 | if(vc_node) { | |
| 376 | if (vc_node->name && | |
| 377 | !g_ascii_strncasecmp(vc_node->name, "vcard", 5)) { | |
| 378 | iq = jabber_iq_new(js, JABBER_IQ_SET); | |
| 379 | xmlnode_insert_child(iq->node, vc_node); | |
| 380 | jabber_iq_send(iq); | |
| 381 | } else { | |
| 382 | xmlnode_free(vc_node); | |
| 383 | } | |
| 384 | } | |
| 385 | } | |
| 386 | ||
| 387 | /* | |
| 388 | * This is the callback from the "ok clicked" for "set vCard" | |
| 389 | * | |
| 390 | * Formats GSList data into XML-encoded string and returns a pointer | |
| 391 | * to said string. | |
| 392 | * | |
| 393 | * g_free()'ing the returned string space is the responsibility of | |
| 394 | * the caller. | |
| 395 | */ | |
| 396 | static void | |
| 397 | jabber_format_info(GaimConnection *gc, GaimRequestFields *fields) | |
| 398 | { | |
| 399 | GaimAccount *account; | |
| 400 | xmlnode *vc_node; | |
| 401 | GaimRequestField *field; | |
| 402 | const char *text; | |
| 403 | char *p; | |
| 404 | const struct vcard_template *vc_tp; | |
| 405 | struct tag_attr *tag_attr; | |
| 406 | ||
| 407 | vc_node = xmlnode_new("vCard"); | |
| 408 | ||
| 409 | for(tag_attr = vcard_tag_attr_list; tag_attr->attr != NULL; ++tag_attr) | |
| 410 | xmlnode_set_attrib(vc_node, tag_attr->attr, tag_attr->value); | |
| 411 | ||
| 412 | for (vc_tp = vcard_template_data; vc_tp->label != NULL; vc_tp++) { | |
| 413 | if (*vc_tp->label == '\0') | |
| 414 | continue; | |
| 415 | ||
| 416 | field = gaim_request_fields_get_field(fields, vc_tp->tag); | |
| 417 | text = gaim_request_field_string_get_value(field); | |
| 418 | ||
| 419 | gaim_debug(GAIM_DEBUG_INFO, "jabber", | |
| 420 | "Setting %s to '%s'\n", vc_tp->tag, text); | |
| 421 | ||
| 422 | if (text != NULL && *text != '\0') { | |
| 423 | xmlnode *xp; | |
| 424 | ||
| 425 | if ((xp = insert_tag_to_parent_tag(vc_node, | |
| 426 | NULL, vc_tp->tag)) != NULL) { | |
| 427 | ||
| 428 | xmlnode_insert_data(xp, text, -1); | |
| 429 | } | |
| 430 | } | |
| 431 | } | |
| 432 | ||
| 433 | p = xmlnode_to_str(vc_node); | |
| 434 | xmlnode_free(vc_node); | |
| 435 | ||
| 436 | account = gaim_connection_get_account(gc); | |
| 437 | ||
| 438 | if (account != NULL) { | |
| 439 | gaim_account_set_user_info(account, p); | |
| 440 | ||
| 441 | if (gc != NULL) | |
| 442 | serv_set_info(gc, p); | |
| 443 | } | |
| 444 | ||
| 445 | g_free(p); | |
| 446 | } | |
| 447 | ||
| 448 | /* | |
| 449 | * This gets executed by the proto action | |
| 450 | * | |
| 451 | * Creates a new GaimRequestFields struct, gets the XML-formatted user_info | |
| 452 | * string (if any) into GSLists for the (multi-entry) edit dialog and | |
| 453 | * calls the set_vcard dialog. | |
| 454 | */ | |
| 455 | void jabber_setup_set_info(GaimConnection *gc) | |
| 456 | { | |
| 457 | GaimRequestFields *fields; | |
| 458 | GaimRequestFieldGroup *group; | |
| 459 | GaimRequestField *field; | |
| 460 | const struct vcard_template *vc_tp; | |
| 461 | char *user_info; | |
| 462 | char *cdata; | |
| 463 | xmlnode *x_vc_data = NULL; | |
| 464 | ||
| 465 | fields = gaim_request_fields_new(); | |
| 466 | group = gaim_request_field_group_new(NULL); | |
| 467 | gaim_request_fields_add_group(fields, group); | |
| 468 | ||
| 469 | /* | |
| 470 | * Get existing, XML-formatted, user info | |
| 471 | */ | |
| 472 | if((user_info = g_strdup(gaim_account_get_user_info(gc->account))) != NULL) | |
| 473 | x_vc_data = xmlnode_from_str(user_info, -1); | |
| 474 | else | |
| 475 | user_info = g_strdup(""); | |
| 476 | ||
| 477 | /* | |
| 478 | * Set up GSLists for edit with labels from "template," data from user info | |
| 479 | */ | |
| 480 | for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
| 481 | xmlnode *data_node; | |
| 482 | if((vc_tp->label)[0] == '\0') | |
| 483 | continue; | |
| 484 | if(vc_tp->ptag == NULL) { | |
| 485 | data_node = xmlnode_get_child(x_vc_data, vc_tp->tag); | |
| 486 | } else { | |
| 487 | gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
| 488 | data_node = xmlnode_get_child(x_vc_data, tag); | |
| 489 | g_free(tag); | |
| 490 | } | |
| 491 | if(data_node) | |
| 492 | cdata = xmlnode_get_data(data_node); | |
| 493 | else | |
| 494 | cdata = NULL; | |
| 495 | ||
| 496 | if(strcmp(vc_tp->tag, "DESC") == 0) { | |
| 497 | field = gaim_request_field_string_new(vc_tp->tag, | |
| 498 | _(vc_tp->label), cdata, | |
| 499 | TRUE); | |
| 500 | } else { | |
| 501 | field = gaim_request_field_string_new(vc_tp->tag, | |
| 502 | _(vc_tp->label), cdata, | |
| 503 | FALSE); | |
| 504 | } | |
| 505 | ||
| 506 | gaim_request_field_group_add_field(group, field); | |
| 507 | } | |
| 508 | ||
| 509 | if(x_vc_data != NULL) | |
| 510 | xmlnode_free(x_vc_data); | |
| 511 | ||
| 512 | g_free(user_info); | |
| 513 | ||
| 514 | gaim_request_fields(gc, _("Edit Jabber vCard"), | |
| 515 | _("Edit Jabber vCard"), | |
| 516 | _("All items below are optional. Enter only the " | |
| 517 | "information with which you feel comfortable."), | |
| 518 | fields, | |
| 519 | _("Save"), G_CALLBACK(jabber_format_info), | |
| 520 | _("Cancel"), NULL, | |
| 521 | gc); | |
| 522 | } | |
| 523 | ||
| 524 | /*---------------------------------------*/ | |
| 525 | /* End Jabber "set info" (vCard) support */ | |
| 526 | /*---------------------------------------*/ | |
| 527 | ||
| 528 | /****** | |
| 529 | * end of that ancient crap that needs to die | |
| 530 | ******/ | |
| 531 | ||
| 532 | ||
| 533 | static void jabber_vcard_parse(JabberStream *js, xmlnode *packet) | |
| 534 | { | |
| 535 | GList *resources; | |
| 536 | const char *from = xmlnode_get_attrib(packet, "from"); | |
| 537 | JabberBuddy *jb; | |
| 538 | JabberBuddyResource *jbr; | |
| 539 | GString *info_text; | |
| 540 | const char *resource_name; | |
| 541 | char *title; | |
| 542 | xmlnode *vcard; | |
| 543 | ||
| 544 | if(!from) | |
| 545 | return; | |
| 546 | ||
| 7116 | 547 | /* XXX: make this handle handle errors */ |
| 548 | ||
| 7014 | 549 | resource_name = jabber_get_resource(from); |
| 550 | ||
| 551 | jb = jabber_buddy_find(js, from, TRUE); | |
| 552 | info_text = g_string_new(""); | |
| 553 | ||
| 554 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", _("Jabber ID"), | |
| 555 | from); | |
| 556 | ||
| 557 | if(resource_name) { | |
| 558 | jbr = jabber_buddy_find_resource(jb, resource_name); | |
| 559 | if(jbr) { | |
|
7108
82655fa54acb
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
560 | char *purdy = gaim_strdup_withhtml(jbr->status); |
| 7014 | 561 | g_string_append_printf(info_text, "<b>%s:</b> %s%s%s<br/>\n", |
| 562 | _("Status"), jabber_get_state_string(jbr->state), | |
| 563 | purdy ? ": " : "", | |
| 564 | purdy ? purdy : ""); | |
| 565 | g_free(purdy); | |
| 566 | } else { | |
| 567 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 568 | _("Status"), _("Unknown")); | |
| 569 | } | |
| 570 | } else { | |
| 571 | for(resources = jb->resources; resources; resources = resources->next) { | |
| 572 | char *purdy; | |
| 573 | jbr = resources->data; | |
|
7108
82655fa54acb
[gaim-migrate @ 7673]
Christian Hammond <chipx86@chipx86.com>
parents:
7106
diff
changeset
|
574 | purdy = gaim_strdup_withhtml(jbr->status); |
| 7014 | 575 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", |
| 576 | _("Resource"), jbr->name); | |
| 577 | g_string_append_printf(info_text, "<b>%s:</b> %s%s%s<br/><br/>\n", | |
| 578 | _("Status"), jabber_get_state_string(jbr->state), | |
| 579 | purdy ? ": " : "", | |
| 580 | purdy ? purdy : ""); | |
| 581 | g_free(purdy); | |
| 582 | } | |
| 583 | } | |
| 584 | ||
| 585 | if((vcard = xmlnode_get_child(packet, "vCard"))) { | |
| 586 | xmlnode *child; | |
| 587 | for(child = vcard->child; child; child = child->next) | |
| 588 | { | |
| 589 | xmlnode *child2; | |
| 590 | char *text; | |
| 591 | ||
| 592 | if(child->type != NODE_TYPE_TAG) | |
| 593 | continue; | |
| 594 | ||
| 595 | text = xmlnode_get_data(child); | |
| 596 | if(text && !strcmp(child->name, "FN")) { | |
| 597 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 598 | _("Full Name"), text); | |
| 599 | } else if(!strcmp(child->name, "N")) { | |
| 600 | for(child2 = child->child; child2; child2 = child2->next) | |
| 601 | { | |
| 602 | char *text2; | |
| 603 | ||
| 604 | if(child2->type != NODE_TYPE_TAG) | |
| 605 | continue; | |
| 606 | ||
| 607 | text2 = xmlnode_get_data(child2); | |
| 608 | if(text2 && !strcmp(child2->name, "FAMILY")) { | |
| 609 | g_string_append_printf(info_text, | |
| 610 | "<b>%s:</b> %s<br/>\n", | |
| 611 | _("Family Name"), text2); | |
| 612 | } else if(text2 && !strcmp(child2->name, "GIVEN")) { | |
| 613 | g_string_append_printf(info_text, | |
| 614 | "<b>%s:</b> %s<br/>\n", | |
| 615 | _("Given Name"), text2); | |
| 616 | } else if(text2 && !strcmp(child2->name, "MIDDLE")) { | |
| 617 | g_string_append_printf(info_text, | |
| 618 | "<b>%s:</b> %s<br/>\n", | |
| 619 | _("Middle Name"), text2); | |
| 620 | } | |
| 621 | g_free(text2); | |
| 622 | } | |
| 623 | } else if(text && !strcmp(child->name, "NICKNAME")) { | |
| 624 | serv_got_alias(js->gc, from, text); | |
| 625 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 626 | _("Nickname"), text); | |
| 627 | } else if(text && !strcmp(child->name, "BDAY")) { | |
| 628 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 629 | _("Birthday"), text); | |
| 630 | } else if(!strcmp(child->name, "ADR")) { | |
| 631 | /* show which address it is */ | |
| 632 | if(child->child) | |
| 633 | g_string_append_printf(info_text, "<b>%s:</b><br/>\n", | |
| 634 | _("Address")); | |
| 635 | for(child2 = child->child; child2; child2 = child2->next) | |
| 636 | { | |
| 637 | char *text2; | |
| 638 | ||
| 639 | if(child2->type != NODE_TYPE_TAG) | |
| 640 | continue; | |
| 641 | ||
| 642 | text2 = xmlnode_get_data(child2); | |
| 643 | if(text2 && !strcmp(child2->name, "POBOX")) { | |
| 644 | g_string_append_printf(info_text, | |
| 645 | " <b>%s:</b> %s<br/>\n", | |
| 646 | _("P.O. Box"), text2); | |
| 647 | } else if(text2 && !strcmp(child2->name, "EXTADR")) { | |
| 648 | g_string_append_printf(info_text, | |
| 649 | " <b>%s:</b> %s<br/>\n", | |
| 650 | _("Extended Address"), text2); | |
| 651 | } else if(text2 && !strcmp(child2->name, "STREET")) { | |
| 652 | g_string_append_printf(info_text, | |
| 653 | " <b>%s:</b> %s<br/>\n", | |
| 654 | _("Street Address"), text2); | |
| 655 | } else if(text2 && !strcmp(child2->name, "LOCALITY")) { | |
| 656 | g_string_append_printf(info_text, | |
| 657 | " <b>%s:</b> %s<br/>\n", | |
| 658 | _("Locality"), text2); | |
| 659 | } else if(text2 && !strcmp(child2->name, "REGION")) { | |
| 660 | g_string_append_printf(info_text, | |
| 661 | " <b>%s:</b> %s<br/>\n", | |
| 662 | _("Region"), text2); | |
| 663 | } else if(text2 && !strcmp(child2->name, "PCODE")) { | |
| 664 | g_string_append_printf(info_text, | |
| 665 | " <b>%s:</b> %s<br/>\n", | |
| 666 | _("Postal Code"), text2); | |
| 667 | } else if(text2 && (!strcmp(child2->name, "CTRY") | |
| 668 | || !strcmp(child2->name, "COUNTRY"))) { | |
| 669 | g_string_append_printf(info_text, | |
| 670 | " <b>%s:</b> %s<br/>\n", | |
| 671 | _("Country"), text2); | |
| 672 | } | |
| 673 | g_free(text2); | |
| 674 | } | |
| 675 | } else if(!strcmp(child->name, "TEL")) { | |
| 676 | char *number; | |
| 677 | if((child2 = xmlnode_get_child(child, "NUMBER"))) { | |
| 678 | /* show what kind of number it is */ | |
| 679 | number = xmlnode_get_data(child2); | |
| 680 | if(number) { | |
| 681 | g_string_append_printf(info_text, | |
| 682 | "<b>%s:</b> %s<br/>\n", _("Telephone"), number); | |
| 683 | g_free(number); | |
| 684 | } | |
| 685 | } else if((number = xmlnode_get_data(child))) { | |
| 686 | /* lots of clients (including gaim) do this, but it's | |
| 687 | * out of spec */ | |
| 688 | g_string_append_printf(info_text, | |
| 689 | "<b>%s:</b> %s<br/>\n", _("Telephone"), number); | |
| 690 | g_free(number); | |
| 691 | } | |
| 692 | } else if(!strcmp(child->name, "EMAIL")) { | |
| 693 | char *userid; | |
| 694 | if((child2 = xmlnode_get_child(child, "USERID"))) { | |
| 695 | /* show what kind of email it is */ | |
| 696 | userid = xmlnode_get_data(child2); | |
| 697 | if(userid) { | |
| 698 | g_string_append_printf(info_text, | |
| 699 | "<b>%s:</b> <a href='mailto:%s'>%s</a><br/>\n", | |
| 700 | _("Email"), userid, userid); | |
| 701 | g_free(userid); | |
| 702 | } | |
| 703 | } else if((userid = xmlnode_get_data(child))) { | |
| 704 | /* lots of clients (including gaim) do this, but it's | |
| 705 | * out of spec */ | |
| 706 | g_string_append_printf(info_text, | |
| 707 | "<b>%s:</b> <a href='mailto:%s'>%s</a><br/>\n", | |
| 708 | _("Email"), userid, userid); | |
| 709 | g_free(userid); | |
| 710 | } | |
| 711 | } else if(!strcmp(child->name, "ORG")) { | |
| 712 | for(child2 = child->child; child2; child2 = child2->next) | |
| 713 | { | |
| 714 | char *text2; | |
| 715 | ||
| 716 | if(child2->type != NODE_TYPE_TAG) | |
| 717 | continue; | |
| 718 | ||
| 719 | text2 = xmlnode_get_data(child2); | |
| 720 | if(text2 && !strcmp(child2->name, "ORGNAME")) { | |
| 721 | g_string_append_printf(info_text, | |
| 722 | "<b>%s:</b> %s<br/>\n", | |
| 723 | _("Organization Name"), text2); | |
| 724 | } else if(text2 && !strcmp(child2->name, "ORGUNIT")) { | |
| 725 | g_string_append_printf(info_text, | |
| 726 | "<b>%s:</b> %s<br/>\n", | |
| 727 | _("Organization Unit"), text2); | |
| 728 | } | |
| 729 | g_free(text2); | |
| 730 | } | |
| 731 | } else if(text && !strcmp(child->name, "TITLE")) { | |
| 732 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 733 | _("Title"), text); | |
| 734 | } else if(text && !strcmp(child->name, "ROLE")) { | |
| 735 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 736 | _("Role"), text); | |
| 737 | } else if(text && !strcmp(child->name, "DESC")) { | |
| 738 | g_string_append_printf(info_text, "<b>%s:</b> %s<br/>\n", | |
| 739 | _("Description"), text); | |
| 7076 | 740 | } else if(!strcmp(child->name, "PHOTO") || |
| 741 | !strcmp(child->name, "LOGO")) { | |
| 742 | if((child2 = xmlnode_get_child(child, "BINVAL"))) { | |
| 743 | char *data, *text2; | |
| 744 | int size, imgid; | |
| 745 | if((text2 = xmlnode_get_data(child2))) { | |
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7076
diff
changeset
|
746 | gaim_base64_decode(text2, &data, &size); |
| 7076 | 747 | |
| 748 | imgid = gaim_imgstore_add(data, size, "logo.png"); | |
| 749 | g_string_append_printf(info_text, | |
| 7116 | 750 | "<b>%s:</b> <img id='%d'><br/>", |
| 7076 | 751 | strcmp(child->name, "PHOTO") == 0 ? |
| 752 | _("Photo") : _("Logo"), | |
| 753 | imgid); | |
| 754 | ||
| 755 | g_free(data); | |
| 756 | g_free(text2); | |
| 757 | } | |
| 758 | } | |
| 7014 | 759 | } |
| 760 | g_free(text); | |
| 761 | } | |
| 762 | } | |
| 763 | ||
| 764 | title = g_strdup_printf("User info for %s", from); | |
| 765 | ||
| 766 | gaim_notify_formatted(NULL, title, _("Jabber Profile"), | |
| 767 | NULL, info_text->str, NULL, NULL); | |
| 768 | ||
| 769 | g_free(title); | |
| 770 | g_string_free(info_text, TRUE); | |
| 771 | } | |
| 772 | ||
| 773 | void jabber_buddy_get_info(GaimConnection *gc, const char *who) | |
| 774 | { | |
| 775 | JabberStream *js = gc->proto_data; | |
| 776 | JabberIq *iq; | |
| 777 | xmlnode *vcard; | |
| 778 | ||
| 779 | iq = jabber_iq_new(js, JABBER_IQ_GET); | |
| 780 | ||
| 781 | xmlnode_set_attrib(iq->node, "to", who); | |
| 782 | vcard = xmlnode_new_child(iq->node, "vCard"); | |
| 783 | xmlnode_set_attrib(vcard, "xmlns", "vcard-temp"); | |
| 784 | ||
| 785 | jabber_iq_set_callback(iq, jabber_vcard_parse); | |
| 786 | ||
| 787 | jabber_iq_send(iq); | |
| 788 | } | |
| 789 | ||
| 790 | void jabber_buddy_get_info_chat(GaimConnection *gc, int id, | |
| 791 | const char *resource) | |
| 792 | { | |
| 793 | JabberStream *js = gc->proto_data; | |
| 794 | JabberChat *chat = jabber_chat_find_by_id(js, id); | |
| 795 | char *full_jid; | |
| 796 | ||
| 797 | if(!chat) | |
| 798 | return; | |
| 799 | ||
| 800 | full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, resource); | |
| 801 | jabber_buddy_get_info(gc, full_jid); | |
| 802 | g_free(full_jid); | |
| 803 | } | |
| 804 | ||
| 805 | static void jabber_buddy_set_invisibility(JabberStream *js, const char *who, | |
| 806 | gboolean invisible) | |
| 807 | { | |
| 808 | JabberBuddy *jb = jabber_buddy_find(js, who, TRUE); | |
| 809 | xmlnode *presence; | |
| 810 | ||
| 811 | presence = jabber_presence_create(js->gc->away_state, js->gc->away); | |
| 812 | xmlnode_set_attrib(presence, "to", who); | |
| 813 | if(invisible) { | |
| 814 | xmlnode_set_attrib(presence, "type", "invisible"); | |
| 815 | jb->invisible |= JABBER_INVIS_BUDDY; | |
| 816 | } else { | |
| 817 | jb->invisible &= ~JABBER_INVIS_BUDDY; | |
| 818 | } | |
| 819 | ||
| 820 | jabber_send(js, presence); | |
| 821 | xmlnode_free(presence); | |
| 822 | } | |
| 823 | ||
| 824 | static void jabber_buddy_make_invisible(GaimConnection *gc, const char *name) | |
| 825 | { | |
| 826 | JabberStream *js = gc->proto_data; | |
| 827 | jabber_buddy_set_invisibility(js, name, TRUE); | |
| 828 | } | |
| 829 | ||
| 830 | static void jabber_buddy_make_visible(GaimConnection *gc, const char *name) | |
| 831 | { | |
| 832 | JabberStream *js = gc->proto_data; | |
| 833 | jabber_buddy_set_invisibility(js, name, FALSE); | |
| 834 | } | |
| 835 | ||
| 836 | static void jabber_buddy_cancel_presence_notification(GaimConnection *gc, | |
| 837 | const char *name) | |
| 838 | { | |
| 839 | JabberStream *js = gc->proto_data; | |
| 840 | ||
| 841 | /* I wonder if we should prompt the user before doing this */ | |
| 842 | jabber_presence_subscription_set(js, name, "unsubscribed"); | |
| 843 | } | |
| 844 | ||
| 845 | static void jabber_buddy_rerequest_auth(GaimConnection *gc, const char *name) | |
| 846 | { | |
| 847 | JabberStream *js = gc->proto_data; | |
| 848 | ||
| 849 | jabber_presence_subscription_set(js, name, "subscribe"); | |
| 850 | } | |
| 851 | ||
| 852 | GList *jabber_buddy_menu(GaimConnection *gc, const char *name) | |
| 853 | { | |
| 854 | GList *m = NULL; | |
| 855 | struct proto_buddy_menu *pbm; | |
| 856 | JabberStream *js = gc->proto_data; | |
| 857 | JabberBuddy *jb = jabber_buddy_find(js, name, TRUE); | |
| 858 | ||
| 859 | pbm = g_new0(struct proto_buddy_menu, 1); | |
| 860 | if(jb->invisible & JABBER_INVIS_BUDDY) { | |
| 861 | pbm->label = _("Un-hide From"); | |
| 862 | pbm->callback = jabber_buddy_make_visible; | |
| 863 | } else { | |
| 864 | pbm->label = _("Temporarily Hide From"); | |
| 865 | pbm->callback = jabber_buddy_make_invisible; | |
| 866 | } | |
| 867 | pbm->gc = gc; | |
| 868 | m = g_list_append(m, pbm); | |
| 869 | ||
| 870 | if(jb->subscription & JABBER_SUB_FROM) { | |
| 871 | pbm = g_new0(struct proto_buddy_menu, 1); | |
| 872 | pbm->label = _("Cancel Presence Notification"); | |
| 873 | pbm->callback = jabber_buddy_cancel_presence_notification; | |
| 874 | pbm->gc = gc; | |
| 875 | m = g_list_append(m, pbm); | |
| 876 | } | |
| 877 | ||
| 878 | if(!(jb->subscription & JABBER_SUB_TO)) { | |
| 879 | pbm = g_new0(struct proto_buddy_menu, 1); | |
| 880 | pbm->label = _("Re-request authorization"); | |
| 881 | pbm->callback = jabber_buddy_rerequest_auth; | |
| 882 | pbm->gc = gc; | |
| 883 | m = g_list_append(m, pbm); | |
| 884 | } | |
| 885 | ||
| 886 | return m; | |
| 887 | } |