comparison mcabber/src/jab_iq.c @ 1006:f61131cd86a0

Clean up vcard code
author Mikael Berthe <mikael@lilotux.net>
date Fri, 10 Nov 2006 22:00:14 +0100
parents e5c10cc29660
children bbf53cd43fbb
comparison
equal deleted inserted replaced
1005:e5c10cc29660 1006:f61131cd86a0
34 #include "hbuf.h" 34 #include "hbuf.h"
35 35
36 36
37 static GSList *iqs_list; 37 static GSList *iqs_list;
38 38
39 // Enum for vCard attributes
40 enum vcard_attr {
41 vcard_home = 1<<0,
42 vcard_work = 1<<1,
43 vcard_postal = 1<<2,
44 vcard_voice = 1<<3,
45 vcard_fax = 1<<4,
46 vcard_cell = 1<<5,
47 vcard_inet = 1<<6,
48 vcard_pref = 1<<7,
49 };
39 50
40 // iqs_new(type, namespace, prefix, timeout) 51 // iqs_new(type, namespace, prefix, timeout)
41 // Create a query (GET, SET) IQ structure. This function should not be used 52 // Create a query (GET, SET) IQ structure. This function should not be used
42 // for RESULT packets. 53 // for RESULT packets.
43 eviqs *iqs_new(guint8 type, const char *ns, const char *prefix, time_t timeout) 54 eviqs *iqs_new(guint8 type, const char *ns, const char *prefix, time_t timeout)
353 xmlnode_put_attrib(iqn->xmldata, "to", fulljid); 364 xmlnode_put_attrib(iqn->xmldata, "to", fulljid);
354 iqn->callback = &iqscallback_time; 365 iqn->callback = &iqscallback_time;
355 jab_send(jc, iqn->xmldata); 366 jab_send(jc, iqn->xmldata);
356 } 367 }
357 368
369 static void display_vcard_item(const char *bjid, const char *label,
370 enum vcard_attr vcard_attrib, const char *text)
371 {
372 char *buf;
373
374 if (!text || !bjid || !label)
375 return;
376
377 buf = g_strdup_printf("%s: %s%s%s%s%s%s%s%s%s%s", label,
378 (vcard_attrib & vcard_home ? "[home]" : ""),
379 (vcard_attrib & vcard_work ? "[work]" : ""),
380 (vcard_attrib & vcard_postal ? "[postal]" : ""),
381 (vcard_attrib & vcard_voice ? "[voice]" : ""),
382 (vcard_attrib & vcard_fax ? "[fax]" : ""),
383 (vcard_attrib & vcard_cell ? "[cell]" : ""),
384 (vcard_attrib & vcard_inet ? "[inet]" : ""),
385 (vcard_attrib & vcard_pref ? "[pref]" : ""),
386 (vcard_attrib ? " " : ""),
387 text);
388 scr_WriteIncomingMessage(bjid, buf, 0, HBB_PREFIX_NONE);
389 g_free(buf);
390 }
391
358 static void handle_vcard_node(const char *barejid, xmlnode vcardnode) 392 static void handle_vcard_node(const char *barejid, xmlnode vcardnode)
359 { 393 {
360 xmlnode x; 394 xmlnode x;
361 const char *p; 395 const char *p;
362 char *buf;
363 396
364 x = xmlnode_get_firstchild(vcardnode); 397 x = xmlnode_get_firstchild(vcardnode);
365 for ( ; x; x = xmlnode_get_nextsibling(x)) { 398 for ( ; x; x = xmlnode_get_nextsibling(x)) {
366 const char *title, *data; 399 const char *data;
400 enum vcard_attr vcard_attrib = 0;
401
367 p = xmlnode_get_name(x); 402 p = xmlnode_get_name(x);
368 data = xmlnode_get_data(x); 403 data = xmlnode_get_data(x);
369 if (p && data) { 404 if (!p || !data)
370 title = NULL; 405 continue;
371 if (!strcmp(p, "FN")) 406
372 title = "Name"; 407 if (!strcmp(p, "FN"))
373 else if (!strcmp(p, "NICKNAME")) 408 display_vcard_item(barejid, "Name", vcard_attrib, data);
374 title = "Nickname"; 409 else if (!strcmp(p, "NICKNAME"))
375 else if (!strcmp(p, "URL")) 410 display_vcard_item(barejid, "Nickname", vcard_attrib, data);
376 title = "URL"; 411 else if (!strcmp(p, "URL"))
377 else if (!strcmp(p, "BDAY")) 412 display_vcard_item(barejid, "URL", vcard_attrib, data);
378 title = "Birthday"; 413 else if (!strcmp(p, "BDAY"))
379 else if (!strcmp(p, "TZ")) 414 display_vcard_item(barejid, "Birthday", vcard_attrib, data);
380 title = "Timezone"; 415 else if (!strcmp(p, "TZ"))
381 else if (!strcmp(p, "TITLE")) 416 display_vcard_item(barejid, "Timezone", vcard_attrib, data);
382 title = "Title"; 417 else if (!strcmp(p, "TITLE"))
383 else if (!strcmp(p, "ROLE")) 418 display_vcard_item(barejid, "Title", vcard_attrib, data);
384 title = "Role"; 419 else if (!strcmp(p, "ROLE"))
385 else if (!strcmp(p, "DESC")) 420 display_vcard_item(barejid, "Role", vcard_attrib, data);
386 title = "Comment"; 421 else if (!strcmp(p, "DESC"))
387 else if (!strcmp(p, "N")) { 422 display_vcard_item(barejid, "Comment", vcard_attrib, data);
388 data = xmlnode_get_tag_data(x, "FAMILY"); 423 else if (!strcmp(p, "N")) {
389 if (data) { 424 data = xmlnode_get_tag_data(x, "FAMILY");
390 buf = g_strdup_printf("Family Name: %s", data); 425 display_vcard_item(barejid, "Family Name", vcard_attrib, data);
391 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE); 426 data = xmlnode_get_tag_data(x, "GIVEN");
392 g_free(buf); 427 display_vcard_item(barejid, "Given Name", vcard_attrib, data);
393 } 428 data = xmlnode_get_tag_data(x, "MIDDLE");
394 data = xmlnode_get_tag_data(x, "GIVEN"); 429 display_vcard_item(barejid, "Middle Name", vcard_attrib, data);
395 if (data) { 430 } else if (!strcmp(p, "ORG")) {
396 buf = g_strdup_printf("Given Name: %s", data); 431 data = xmlnode_get_tag_data(x, "ORGNAME");
397 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE); 432 display_vcard_item(barejid, "Organisation name", vcard_attrib, data);
398 g_free(buf); 433 data = xmlnode_get_tag_data(x, "ORGUNIT");
399 } 434 display_vcard_item(barejid, "Organisation unit", vcard_attrib, data);
400 data = xmlnode_get_tag_data(x, "MIDDLE"); 435 } else {
401 if (data) { 436 // The HOME, WORK and PREF attributes are common to the remaining fields
402 buf = g_strdup_printf("Middle Name: %s", data); 437 // (ADR, TEL & EMAIL)
403 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE); 438 if (xmlnode_get_tag(x, "HOME"))
404 g_free(buf); 439 vcard_attrib |= vcard_home;
405 } 440 if (xmlnode_get_tag(x, "WORK"))
406 } else if (!strcmp(p, "ADR")) { 441 vcard_attrib |= vcard_work;
407 enum { 442 if (xmlnode_get_tag(x, "PREF"))
408 adr_home = 1<<0, 443 vcard_attrib |= vcard_pref;
409 adr_work = 1<<1, 444 if (!strcmp(p, "ADR")) { // Address
410 adr_postal = 1<<2,
411 adr_pref = 1<<3,
412 } adr_attrib = 0;
413 if (xmlnode_get_tag(x, "HOME"))
414 adr_attrib |= adr_home;
415 if (xmlnode_get_tag(x, "WORK"))
416 adr_attrib |= adr_work;
417 if (xmlnode_get_tag(x, "POSTAL")) 445 if (xmlnode_get_tag(x, "POSTAL"))
418 adr_attrib |= adr_postal; 446 vcard_attrib |= vcard_postal;
419 if (xmlnode_get_tag(x, "PREF")) 447 data = xmlnode_get_tag_data(x, "EXTADD");
420 adr_attrib |= adr_pref; 448 display_vcard_item(barejid, "Addr (ext)", vcard_attrib, data);
421 data = xmlnode_get_tag_data(x, "EXTADD"); // Ext. Address 449 data = xmlnode_get_tag_data(x, "STREET");
422 if (data) { 450 display_vcard_item(barejid, "Street", vcard_attrib, data);
423 buf = g_strdup_printf("Addr (ext): %s%s%s%s %s", 451 data = xmlnode_get_tag_data(x, "LOCALITY");
424 (adr_attrib & adr_home ? "[home]" : ""), 452 display_vcard_item(barejid, "Locality", vcard_attrib, data);
425 (adr_attrib & adr_work ? "[work]" : ""), 453 data = xmlnode_get_tag_data(x, "REGION");
426 (adr_attrib & adr_postal ? "[voice]" : ""), 454 display_vcard_item(barejid, "Region", vcard_attrib, data);
427 (adr_attrib & adr_pref ? "[pref]" : ""), 455 data = xmlnode_get_tag_data(x, "PCODE");
428 data); 456 display_vcard_item(barejid, "Postal code", vcard_attrib, data);
429 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE); 457 data = xmlnode_get_tag_data(x, "CTRY");
430 g_free(buf); 458 display_vcard_item(barejid, "Country", vcard_attrib, data);
431 } 459 } else if (!strcmp(p, "TEL")) { // Telephone
432 data = xmlnode_get_tag_data(x, "STREET"); // Street
433 if (data) {
434 buf = g_strdup_printf("Street: %s%s%s%s %s",
435 (adr_attrib & adr_home ? "[home]" : ""),
436 (adr_attrib & adr_work ? "[work]" : ""),
437 (adr_attrib & adr_postal ? "[voice]" : ""),
438 (adr_attrib & adr_pref ? "[pref]" : ""),
439 data);
440 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
441 g_free(buf);
442 }
443 data = xmlnode_get_tag_data(x, "LOCALITY"); // Locality
444 if (data) {
445 buf = g_strdup_printf("Locality: %s%s%s%s %s",
446 (adr_attrib & adr_home ? "[home]" : ""),
447 (adr_attrib & adr_work ? "[work]" : ""),
448 (adr_attrib & adr_postal ? "[voice]" : ""),
449 (adr_attrib & adr_pref ? "[pref]" : ""),
450 data);
451 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
452 g_free(buf);
453 }
454 data = xmlnode_get_tag_data(x, "REGION"); // Region
455 if (data) {
456 buf = g_strdup_printf("Region: %s%s%s%s %s",
457 (adr_attrib & adr_home ? "[home]" : ""),
458 (adr_attrib & adr_work ? "[work]" : ""),
459 (adr_attrib & adr_postal ? "[voice]" : ""),
460 (adr_attrib & adr_pref ? "[pref]" : ""),
461 data);
462 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
463 g_free(buf);
464 }
465 data = xmlnode_get_tag_data(x, "PCODE"); // Postal Code
466 if (data) {
467 buf = g_strdup_printf("Postal code: %s%s%s%s %s",
468 (adr_attrib & adr_home ? "[home]" : ""),
469 (adr_attrib & adr_work ? "[work]" : ""),
470 (adr_attrib & adr_postal ? "[voice]" : ""),
471 (adr_attrib & adr_pref ? "[pref]" : ""),
472 data);
473 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
474 g_free(buf);
475 }
476 data = xmlnode_get_tag_data(x, "CTRY"); // Country
477 if (data) {
478 buf = g_strdup_printf("Country: %s%s%s%s %s",
479 (adr_attrib & adr_home ? "[home]" : ""),
480 (adr_attrib & adr_work ? "[work]" : ""),
481 (adr_attrib & adr_postal ? "[voice]" : ""),
482 (adr_attrib & adr_pref ? "[pref]" : ""),
483 data);
484 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
485 g_free(buf);
486 }
487 } else if (!strcmp(p, "TEL")) {
488 enum {
489 tel_home = 1<<0,
490 tel_work = 1<<1,
491 tel_voice = 1<<2,
492 tel_fax = 1<<3,
493 tel_cell = 1<<4,
494 tel_pref = 1<<5
495 } tel_attrib = 0;
496 data = xmlnode_get_tag_data(x, "NUMBER"); 460 data = xmlnode_get_tag_data(x, "NUMBER");
497 if (data) { 461 if (data) {
498 if (xmlnode_get_tag(x, "HOME"))
499 tel_attrib |= tel_home;
500 if (xmlnode_get_tag(x, "WORK"))
501 tel_attrib |= tel_work;
502 if (xmlnode_get_tag(x, "VOICE")) 462 if (xmlnode_get_tag(x, "VOICE"))
503 tel_attrib |= tel_voice; 463 vcard_attrib |= vcard_voice;
504 if (xmlnode_get_tag(x, "FAX")) 464 if (xmlnode_get_tag(x, "FAX"))
505 tel_attrib |= tel_fax; 465 vcard_attrib |= vcard_fax;
506 if (xmlnode_get_tag(x, "CELL")) 466 if (xmlnode_get_tag(x, "CELL"))
507 tel_attrib |= tel_cell; 467 vcard_attrib |= vcard_cell;
508 if (xmlnode_get_tag(x, "PREF")) 468 display_vcard_item(barejid, "Phone", vcard_attrib, data);
509 tel_attrib |= tel_pref;
510 buf = g_strdup_printf("Phone: %s%s%s%s%s%s %s",
511 (tel_attrib & tel_home ? "[home]" : ""),
512 (tel_attrib & tel_work ? "[work]" : ""),
513 (tel_attrib & tel_voice ? "[voice]" : ""),
514 (tel_attrib & tel_fax ? "[fax]" : ""),
515 (tel_attrib & tel_cell ? "[cell]" : ""),
516 (tel_attrib & tel_pref ? "[pref]" : ""),
517 data);
518 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
519 g_free(buf);
520 } 469 }
521 } else if (!strcmp(p, "EMAIL")) { 470 } else if (!strcmp(p, "EMAIL")) { // Email
522 enum { 471 if (xmlnode_get_tag(x, "INTERNET"))
523 email_home = 1<<0, 472 vcard_attrib |= vcard_inet;
524 email_work = 1<<1,
525 email_inet = 1<<2,
526 email_pref = 1<<3
527 } email_attrib = 0;
528 data = xmlnode_get_tag_data(x, "USERID"); 473 data = xmlnode_get_tag_data(x, "USERID");
529 if (data) { 474 display_vcard_item(barejid, "Email", vcard_attrib, data);
530 if (xmlnode_get_tag(x, "HOME"))
531 email_attrib |= email_home;
532 if (xmlnode_get_tag(x, "WORK"))
533 email_attrib |= email_work;
534 if (xmlnode_get_tag(x, "INTERNET"))
535 email_attrib |= email_inet;
536 if (xmlnode_get_tag(x, "PREF"))
537 email_attrib |= email_pref;
538 buf = g_strdup_printf("Email: %s%s%s%s %s",
539 (email_attrib & email_home ? "[home]" : ""),
540 (email_attrib & email_work ? "[work]" : ""),
541 (email_attrib & email_inet ? "[inet]" : ""),
542 (email_attrib & email_pref ? "[pref]" : ""),
543 data);
544 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
545 g_free(buf);
546 }
547 } else if (!strcmp(p, "ORG")) {
548 data = xmlnode_get_tag_data(x, "ORGNAME");
549 if (data) {
550 buf = g_strdup_printf("Organisation name: %s", data);
551 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
552 g_free(buf);
553 }
554 data = xmlnode_get_tag_data(x, "ORGUNIT");
555 if (data) {
556 buf = g_strdup_printf("Organisation unit: %s", data);
557 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
558 g_free(buf);
559 }
560 }
561
562 if (title) {
563 buf = g_strdup_printf("%s: %s", title, data);
564 scr_WriteIncomingMessage(barejid, buf, 0, HBB_PREFIX_NONE);
565 g_free(buf);
566 } 475 }
567 } 476 }
568 } 477 }
569 } 478 }
570 479