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