plugins/mod_vcard4.lua
changeset 9264 9db9e37610b7
child 9286 e977b64ebd81
equal deleted inserted replaced
9263:0fc6ffc57dc0 9264:9db9e37610b7
       
     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("urn:ietf:params:xml:ns:vcard-4.0");
       
     7 
       
     8 module:hook("iq-get/bare/urn:ietf:params:xml:ns:vcard-4.0:vcard", function (event)
       
     9 	local origin, stanza = event.origin, event.stanza;
       
    10 
       
    11 	local pep_service = mod_pep.get_pep_service(jid_split(stanza.attr.to) or origin.username);
       
    12 	local ok, id, item = pep_service:get_last_item("urn:xmpp:vcard4", stanza.attr.from);
       
    13 	if ok and item then
       
    14 		origin.send(st.reply(stanza):add_child(item.tags[1]));
       
    15 	elseif item == "item-not-found" or not id then
       
    16 		origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
       
    17 	elseif item == "forbidden" then
       
    18 		origin.send(st.error_reply(stanza, "auth", "forbidden"));
       
    19 	else
       
    20 		origin.send(st.error_reply(stanza, "modify", "undefined-condition"));
       
    21 	end
       
    22 	return true;
       
    23 end);
       
    24 
       
    25 module:hook("iq-set/self/urn:ietf:params:xml:ns:vcard-4.0:vcard", function (event)
       
    26 	local origin, stanza = event.origin, event.stanza;
       
    27 
       
    28 	local vcard4 = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" })
       
    29 		:add_child(stanza.tags[1]);
       
    30 
       
    31 	local pep_service = mod_pep.get_pep_service(origin.username);
       
    32 
       
    33 	local ok, err = pep_service:publish("urn:xmpp:vcard4", origin.full_jid, "current", vcard4);
       
    34 	if ok then
       
    35 		origin.send(st.reply(stanza));
       
    36 	elseif err == "forbidden" then
       
    37 		origin.send(st.error_reply(stanza, "auth", "forbidden"));
       
    38 	else
       
    39 		origin.send(st.error_reply(stanza, "modify", "undefined-condition", err));
       
    40 	end
       
    41 	return true;
       
    42 end);
       
    43