comparison plugins/mod_vcard.lua @ 13656:4298e6565fec

mod_vcard: Some support for handling vcards on components
author Matthew Wild <mwild1@gmail.com>
date Thu, 06 Feb 2025 17:03:03 +0000
parents 6902bb3dd196
children 33806569d7c5
comparison
equal deleted inserted replaced
13655:6902bb3dd196 13656:4298e6565fec
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local base64 = require "prosody.util.encodings".base64; 9 local base64 = require "prosody.util.encodings".base64;
10 local jid = require "prosody.util.jid";
10 local sha1 = require "prosody.util.hashes".sha1; 11 local sha1 = require "prosody.util.hashes".sha1;
11 local st = require "prosody.util.stanza" 12 local st = require "prosody.util.stanza"
12 local jid_split = require "prosody.util.jid".split; 13 local jid_split = require "prosody.util.jid".split;
13 14
14 local vcards = module:open_store(); 15 local vcards = module:open_store();
15 16
16 module:add_feature("vcard-temp"); 17 module:add_feature("vcard-temp");
18
19 local is_component = module:get_host_type() == "component";
17 20
18 local function handle_vcard(event) 21 local function handle_vcard(event)
19 local session, stanza = event.origin, event.stanza; 22 local session, stanza = event.origin, event.stanza;
20 local to = stanza.attr.to; 23 local to = stanza.attr.to;
21 if stanza.attr.type == "get" then 24 if stanza.attr.type == "get" then
22 local vCard; 25 local vCard;
23 if to then 26 if to then
24 local node = jid_split(to); 27 local node = jid_split(to);
25 vCard = st.deserialize(vcards:get(node)); -- load vCard for user or server 28 vCard = st.deserialize(vcards:get(node)); -- load vCard for user or server
26 else 29 elseif not is_component then
27 vCard = st.deserialize(vcards:get(session.username));-- load user's own vCard 30 vCard = st.deserialize(vcards:get(session.username));-- load user's own vCard
28 end 31 end
29 if vCard then 32 if vCard then
30 session.send(st.reply(stanza):add_child(vCard)); -- send vCard! 33 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
31 else 34 else
32 session.send(st.error_reply(stanza, "cancel", "item-not-found")); 35 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
33 end 36 end
34 else -- stanza.attr.type == "set" 37 else -- stanza.attr.type == "set"
35 if not to then 38 if not to or (is_component and event.allow_vcard_modification) then
36 if vcards:set(session.username, st.preserialize(stanza.tags[1])) then 39 local node = is_component and jid.node(stanza.attr.to) or session.username;
40 if vcards:set(node, st.preserialize(stanza.tags[1])) then
37 session.send(st.reply(stanza)); 41 session.send(st.reply(stanza));
38 module:fire_event("vcard-updated", event); 42 module:fire_event("vcard-updated", event);
39 else 43 else
40 -- TODO unable to write file, file may be locked, etc, what's the correct error? 44 -- TODO unable to write file, file may be locked, etc, what's the correct error?
41 session.send(st.error_reply(stanza, "wait", "internal-server-error")); 45 session.send(st.error_reply(stanza, "wait", "internal-server-error"));