Comparison

util/vcard.lua @ 6263:e208950446c8

util.vcard: Add support for converting to vcard4
author Kim Alvefur <zash@zash.se>
date Wed, 28 May 2014 21:11:02 +0200
parent 6262:e24027bafb0e
child 6265:f8945a030bc6
comparison
equal deleted inserted replaced
6262:e24027bafb0e 6263:e208950446c8
317 elseif vCard.name == "vCard" then -- A single vCard 317 elseif vCard.name == "vCard" then -- A single vCard
318 return from_xep54_vCard(vCard) 318 return from_xep54_vCard(vCard)
319 end 319 end
320 end 320 end
321 321
322 local vcard4 = { }
323
324 function vcard4:text(node, params, value)
325 self:tag(node:lower())
326 -- FIXME params
327 if type(value) == "string" then
328 self:tag("text"):text(value):up()
329 elseif vcard4[node] then
330 vcard4[node](value);
331 end
332 self:up();
333 end
334
335 function vcard4.N(value)
336 for i, k in ipairs(vCard_dtd.N.values) do
337 value:tag(k):text(value[i]):up();
338 end
339 end
340
341 local xmlns_vcard4 = "urn:ietf:params:xml:ns:vcard-4.0"
342
343 local function item_to_vcard4(item)
344 local typ = item.name:lower();
345 local t = st.stanza(typ, { xmlns = xmlns_vcard4 });
346
347 local prop_def = vCard4_dtd[typ];
348 if prop_def == "text" then
349 t:tag("text"):text(item[1]):up();
350 elseif type(prop_def) == "table" then
351 if prop_def.values then
352 for i, v in ipairs(prop_def.values) do
353 t:tag(v:lower()):text(item[i] or ""):up();
354 end
355 else
356 t:tag("unsupported",{xmlns="http://zash.se/protocol/vcardlib"})
357 end
358 else
359 t:tag("unsupported",{xmlns="http://zash.se/protocol/vcardlib"})
360 end
361 return t;
362 end
363
364 local function vcard_to_vcard4xml(vCard)
365 local t = st.stanza("vcard", { xmlns = xmlns_vcard4 });
366 for i=1,#vCard do
367 t:add_child(item_to_vcard4(vCard[i]));
368 end
369 return t;
370 end
371
372 local function vcards_to_vcard4xml(vCards)
373 if not vCards[1] or vCards[1].name then
374 return vcard_to_vcard4xml(vCards)
375 else
376 local t = st.stanza("vcards", { xmlns = xmlns_vcard4 });
377 for i=1,#vCards do
378 t:add_child(vcard_to_vcard4xml(vCards[i]));
379 end
380 return t;
381 end
382 end
383
322 -- This was adapted from http://xmpp.org/extensions/xep-0054.html#dtd 384 -- This was adapted from http://xmpp.org/extensions/xep-0054.html#dtd
323 vCard_dtd = { 385 vCard_dtd = {
324 VERSION = "text", --MUST be 3.0, so parsing is redundant 386 VERSION = "text", --MUST be 3.0, so parsing is redundant
325 FN = "text", 387 FN = "text",
326 N = { 388 N = {
443 DESC = "text", 505 DESC = "text",
444 }; 506 };
445 vCard_dtd.LOGO = vCard_dtd.PHOTO; 507 vCard_dtd.LOGO = vCard_dtd.PHOTO;
446 vCard_dtd.SOUND = vCard_dtd.PHOTO; 508 vCard_dtd.SOUND = vCard_dtd.PHOTO;
447 509
510 vCard4_dtd = {
511 source = "uri",
512 kind = "text",
513 xml = "text",
514 fn = "text",
515 n = {
516 values = {
517 "family",
518 "given",
519 "middle",
520 "prefix",
521 "suffix",
522 },
523 },
524 nickname = "text",
525 photo = "uri",
526 bday = "date-and-or-time",
527 anniversary = "date-and-or-time",
528 gender = "text",
529 adr = {
530 values = {
531 "pobox",
532 "ext",
533 "street",
534 "locality",
535 "region",
536 "code",
537 "country",
538 }
539 },
540 tel = "text",
541 email = "text",
542 impp = "uri",
543 lang = "language-tag",
544 tz = "text",
545 geo = "uri",
546 title = "text",
547 role = "text",
548 logo = "uri",
549 org = "text",
550 member = "uri",
551 related = "uri",
552 categories = "text",
553 note = "text",
554 prodid = "text",
555 rev = "timestamp",
556 sound = "uri",
557 uid = "uri",
558 clientpidmap = "number, uuid",
559 url = "uri",
560 version = "text",
561 key = "uri",
562 fburl = "uri",
563 caladruri = "uri",
564 caluri = "uri",
565 };
566
448 return { 567 return {
449 from_text = from_text; 568 from_text = from_text;
450 to_text = to_text; 569 to_text = to_text;
451 570
452 from_xep54 = from_xep54; 571 from_xep54 = from_xep54;
453 to_xep54 = to_xep54; 572 to_xep54 = to_xep54;
573
574 to_vcard4 = vcards_to_vcard4xml;
454 }; 575 };