comparison mcabber/src/jab_iq.c @ 1003:c8b1a52b2fd6

Initial VCard retrieval support
author Mikael Berthe <mikael@lilotux.net>
date Tue, 07 Nov 2006 22:43:17 +0100
parents dd9e7eb5f8a8
children b57a01ffeed6
comparison
equal deleted inserted replaced
1002:dd9e7eb5f8a8 1003:c8b1a52b2fd6
353 xmlnode_put_attrib(iqn->xmldata, "to", fulljid); 353 xmlnode_put_attrib(iqn->xmldata, "to", fulljid);
354 iqn->callback = &iqscallback_time; 354 iqn->callback = &iqscallback_time;
355 jab_send(jc, iqn->xmldata); 355 jab_send(jc, iqn->xmldata);
356 } 356 }
357 357
358 static void handle_vcard_node(const char *barejid, xmlnode vcardnode)
359 {
360 xmlnode x;
361 const char *p;
362 char *buf;
363
364 x = xmlnode_get_firstchild(vcardnode);
365 for ( ; x; x = xmlnode_get_nextsibling(x)) {
366 const char *title, *data;
367 p = xmlnode_get_name(x);
368 data = xmlnode_get_data(x);
369 if (p && data) {
370 title = NULL;
371 if (!strcmp(p, "FN"))
372 title = "Name";
373 else if (!strcmp(p, "NICKNAME"))
374 title = "Nickname";
375 else if (!strcmp(p, "URL"))
376 title = "URL";
377 else if (!strcmp(p, "BDAY"))
378 title = "Birthday";
379 else if (!strcmp(p, "TZ"))
380 title = "Timezone";
381 else if (!strcmp(p, "TITLE"))
382 title = "Title";
383 else if (!strcmp(p, "ROLE"))
384 title = "Role";
385 else if (!strcmp(p, "DESC"))
386 title = "Comment";
387 else if (!strcmp(p, "N")) {
388 data = xmlnode_get_tag_data(x, "FAMILY");
389 if (data) {
390 buf = g_strdup_printf("Family Name: %s", data);
391 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
392 g_free(buf);
393 }
394 data = xmlnode_get_tag_data(x, "GIVEN");
395 if (data) {
396 buf = g_strdup_printf("Given Name: %s", data);
397 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
398 g_free(buf);
399 }
400 data = xmlnode_get_tag_data(x, "MIDDLE");
401 if (data) {
402 buf = g_strdup_printf("Middle Name: %s", data);
403 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
404 g_free(buf);
405 }
406 } else if (!strcmp(p, "ADR")) { // TODO
407 } else if (!strcmp(p, "TEL")) { // TODO
408 } else if (!strcmp(p, "EMAIL")) {
409 data = xmlnode_get_tag_data(x, "USERID");
410 if (data)
411 title = "Email"; // XXX
412 } else if (!strcmp(p, "ORG")) { // TODO
413 }
414
415 if (title) {
416 buf = g_strdup_printf("%s: %s", title, data);
417 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
418 g_free(buf);
419 }
420 }
421 }
422 }
423
424 static void iqscallback_vcard(eviqs *iqp, xmlnode xml_result, guint iqcontext)
425 {
426 xmlnode ansqry;
427 char *p;
428 char *bjid;
429 char *buf;
430
431 // Leave now if we cannot process xml_result
432 if (!xml_result || iqcontext) return;
433
434 // Display IQ result sender...
435 p = xmlnode_get_attrib(xml_result, "from");
436 if (!p) {
437 scr_LogPrint(LPRINT_LOGNORM, "Invalid IQ:vCard result (no sender name).");
438 return;
439 }
440 bjid = p;
441
442 buf = g_strdup_printf("Received IQ:vCard result from <%s>", bjid);
443 scr_LogPrint(LPRINT_LOGNORM, "%s", buf);
444
445 // Get the vCard node
446 ansqry = xmlnode_get_tag(xml_result, "vCard");
447 if (!ansqry) {
448 scr_LogPrint(LPRINT_LOGNORM, "Empty IQ:vCard result!");
449 return;
450 }
451
452 // bjid should really be the "bare JID", let's strip the resource
453 p = strchr(bjid, JID_RESOURCE_SEPARATOR);
454 if (p) *p = '\0';
455
456 scr_WriteIncomingMessage(bjid, buf, 0, HBB_PREFIX_INFO);
457 g_free(buf);
458
459 // Get result data...
460 handle_vcard_node(bjid, ansqry);
461 }
462
463 void request_vcard(const char *jid)
464 {
465 eviqs *iqn;
466 char *barejid;
467
468 barejid = jidtodisp(jid);
469
470 // Create a new IQ structure. We use NULL for the namespace because
471 // we'll have to use a special tag, not the usual "query" one.
472 iqn = iqs_new(JPACKET__GET, NULL, "vcard", IQS_DEFAULT_TIMEOUT);
473 xmlnode_put_attrib(iqn->xmldata, "to", barejid);
474 // Remove the useless <query/> tag, and insert a vCard one.
475 xmlnode_hide(xmlnode_get_tag(iqn->xmldata, "query"));
476 xmlnode_put_attrib(xmlnode_insert_tag(iqn->xmldata, "vCard"),
477 "xmlns", NS_VCARD);
478 iqn->callback = &iqscallback_vcard;
479 jab_send(jc, iqn->xmldata);
480
481 g_free(barejid);
482 }
483
358 void iqscallback_auth(eviqs *iqp, xmlnode xml_result) 484 void iqscallback_auth(eviqs *iqp, xmlnode xml_result)
359 { 485 {
360 if (jstate == STATE_GETAUTH) { 486 if (jstate == STATE_GETAUTH) {
361 eviqs *iqn; 487 eviqs *iqn;
362 488
389 return; 515 return;
390 } 516 }
391 517
392 if (!iqs_callback(id, xmldata, IQS_CONTEXT_RESULT)) 518 if (!iqs_callback(id, xmldata, IQS_CONTEXT_RESULT))
393 return; 519 return;
394
395 /*
396 if (!strcmp(id, "VCARDreq")) {
397 x = xmlnode_get_firstchild(xmldata);
398 if (!x) x = xmldata;
399
400 scr_LogPrint(LPRINT_LOGNORM, "Got VCARD"); // TODO
401 return;
402 } else if (!strcmp(id, "versionreq")) {
403 scr_LogPrint(LPRINT_LOGNORM, "Got version"); // TODO
404 return;
405 }
406 */
407 520
408 x = xmlnode_get_tag(xmldata, "query"); 521 x = xmlnode_get_tag(xmldata, "query");
409 if (!x) return; 522 if (!x) return;
410 523
411 ns = xmlnode_get_attrib(x, "xmlns"); 524 ns = xmlnode_get_attrib(x, "xmlns");