plugins/mod_vcard_legacy.lua
changeset 9252 506aabdb13bc
child 9253 9a8006f9e983
equal deleted inserted replaced
9251:1d6a2cc389eb 9252:506aabdb13bc
       
     1 local st = require "util.stanza"
       
     2 local jid_split = require "util.jid".split;
       
     3 
       
     4 local mod_pep = module:depends("pep");
       
     5 
       
     6 module:add_feature("vcard-temp");
       
     7 module:add_feature("urn:xmpp:pep-vcard-conversion:0");
       
     8 
       
     9 -- Simple translations
       
    10 -- <foo><text>hey</text></foo> -> <FOO>hey</FOO>
       
    11 local simple_map = {
       
    12 	nickname = "text";
       
    13 	title = "text";
       
    14 	role = "text";
       
    15 	categories = "text";
       
    16 	note = "text";
       
    17 	url = "uri";
       
    18 	bday = "date";
       
    19 }
       
    20 
       
    21 module:hook("iq-get/bare/vcard-temp:vCard", function (event)
       
    22 	local origin, stanza = event.origin, event.stanza;
       
    23 	local pep_service = mod_pep.get_pep_service(jid_split(stanza.attr.to) or origin.username);
       
    24 	local ok, id, vcard4_item = pep_service:get_last_item("urn:xmpp:vcard4", stanza.attr.from);
       
    25 
       
    26 	local vcard_temp = st.stanza("vCard", { xmlns = "vcard-temp" });
       
    27 	if ok and vcard4_item then
       
    28 		local vcard4 = vcard4_item.tags[1];
       
    29 
       
    30 		local fn = vcard4:get_child("fn");
       
    31 		vcard_temp:text_tag("FN", fn and fn:get_child_text("text"));
       
    32 
       
    33 		local v4n = vcard4:get_child("n");
       
    34 		vcard_temp:tag("N")
       
    35 			:text_tag("FAMILY", v4n and v4n:get_child_text("surname"))
       
    36 			:text_tag("GIVEN", v4n and v4n:get_child_text("given"))
       
    37 			:text_tag("MIDDLE", v4n and v4n:get_child_text("additional"))
       
    38 			:text_tag("PREFIX", v4n and v4n:get_child_text("prefix"))
       
    39 			:text_tag("SUFFIX", v4n and v4n:get_child_text("suffix"))
       
    40 			:up();
       
    41 
       
    42 		for tag in vcard4:childtags() do
       
    43 			local typ = simple_map[tag.name];
       
    44 			if typ then
       
    45 				local text = tag:get_child_text(typ);
       
    46 				if text then
       
    47 					vcard_temp:text_tag(tag.name:upper(), text);
       
    48 				end
       
    49 			end
       
    50 		end
       
    51 	end
       
    52 
       
    53 	origin.send(st.reply(stanza):add_child(vcard_temp));
       
    54 	return true;
       
    55 end);