comparison mod_vcard_muc/mod_vcard_muc.lua @ 3043:6cc44e69443a

mod_vcard_muc: Initial commit
author Michel Le Bihan <michel@lebihan.pl>
date Thu, 24 May 2018 20:27:48 +0200
parents
children d0db28768980
comparison
equal deleted inserted replaced
3042:4f215f5227cf 3043:6cc44e69443a
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2018 Michel Le Bihan
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local st = require "util.stanza"
11 local jid_split = require "util.jid".split;
12 local base64 = require"util.encodings".base64;
13 local sha1 = require"util.hashes".sha1;
14
15 local mod_muc = module:depends"muc";
16 local get_room_from_jid = mod_muc.get_room_from_jid;
17
18 local vcards = module:open_store();
19
20 module:add_feature("vcard-temp");
21
22 local function broadcast_presence(room_jid, to)
23 local room = get_room_from_jid(room_jid);
24 local room_node = jid_split(room_jid);
25 local vcard = st.deserialize(vcards:get(room_node));
26 local photo_hash;
27
28 if vcard then
29 local photo = vcard:get_child("PHOTO");
30
31 if photo then
32 local photo_b64 = photo:get_child_text("BINVAL");
33 local photo_raw = photo_b64 and base64.decode(photo_b64);
34 photo_hash = sha1(photo_raw, true);
35 end
36 end
37
38 local presence_vcard = st.presence({to = to, from = room_jid})
39 :tag("x", { xmlns = "vcard-temp:x:update" })
40 :tag("photo"):text(photo_hash):up();
41
42 if to == nil then
43 room:broadcast_message(presence_vcard);
44 else
45 module:send(presence_vcard);
46 end
47 end
48
49 local function handle_vcard(event)
50 local session, stanza = event.origin, event.stanza;
51
52 local room_jid = stanza.attr.to;
53 local room_node = jid_split(room_jid);
54 local room = get_room_from_jid(room_jid);
55
56 local from = stanza.attr.from;
57 local from_affiliation = room:get_affiliation(from);
58
59 if stanza.attr.type == "get" then
60 local vCard;
61 vCard = st.deserialize(vcards:get(room_node));
62
63 if vCard then
64 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
65 else
66 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
67 end
68 else
69 if from_affiliation == "owner" then
70 if vcards:set(room_node, st.preserialize(stanza.tags[1])) then
71 session.send(st.reply(stanza):tag("vCard", { xmlns = "vcard-temp" }));
72 broadcast_presence(room_jid, nil)
73 else
74 -- TODO unable to write file, file may be locked, etc, what's the correct error?
75 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
76 end
77 else
78 session.send(st.error_reply(stanza, "auth", "forbidden"));
79 end
80 end
81 return true;
82 end
83
84
85 module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
86 module:hook("iq/host/vcard-temp:vCard", handle_vcard);
87
88 module:hook("muc-disco#info", function(event)
89 event.reply:tag("feature", { var = "vcard-temp" }):up();
90 broadcast_presence(event.room.jid, event.reply.attr.to);
91 end);