| 1 | -- Prosody IM |
| 2 | -- Copyright (C) 2008-2010 Matthew Wild |
| 3 | -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 | -- |
| 5 | -- This project is MIT/X11 licensed. Please see the |
| 6 | -- COPYING file in the source package for more information. |
| 7 | -- |
| 8 | |
| 9 | local st = require "util.stanza" |
| 10 | local jid_split = require "util.jid".split; |
| 11 | local datamanager = require "util.datamanager" |
| 12 | |
| 13 | module:add_feature("vcard-temp"); |
| 14 | |
| 15 | local function handle_vcard(event) |
| 16 | local session, stanza = event.origin, event.stanza; |
| 17 | local to = stanza.attr.to; |
| 18 | if stanza.attr.type == "get" then |
| 19 | local vCard; |
| 20 | if to then |
| 21 | local node, host = jid_split(to); |
| 22 | vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server |
| 23 | else |
| 24 | vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard |
| 25 | end |
| 26 | if vCard then |
| 27 | session.send(st.reply(stanza):add_child(vCard)); -- send vCard! |
| 28 | else |
| 29 | session.send(st.error_reply(stanza, "cancel", "item-not-found")); |
| 30 | end |
| 31 | else |
| 32 | if not to then |
| 33 | if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then |
| 34 | session.send(st.reply(stanza)); |
| 35 | else |
| 36 | -- TODO unable to write file, file may be locked, etc, what's the correct error? |
| 37 | session.send(st.error_reply(stanza, "wait", "internal-server-error")); |
| 38 | end |
| 39 | else |
| 40 | session.send(st.error_reply(stanza, "auth", "forbidden")); |
| 41 | end |
| 42 | end |
| 43 | return true; |
| 44 | end |
| 45 | |
| 46 | module:hook("iq/bare/vcard-temp:vCard", handle_vcard); |
| 47 | module:hook("iq/host/vcard-temp:vCard", handle_vcard); |
| 48 | |
| 49 | -- COMPAT: https://support.process-one.net/browse/EJAB-1045 |
| 50 | if module:get_option("vcard_compatibility") then |
| 51 | module:hook("iq/full", function(data) |
| 52 | local stanza = data.stanza; |
| 53 | local payload = stanza.tags[1]; |
| 54 | if stanza.attr.type == "get" and payload.name == "vCard" and payload.attr.xmlns == "vcard-temp" then |
| 55 | return handle_vcard(data); |
| 56 | end |
| 57 | end, 1); |
| 58 | end |