annotate mod_vcard_muc/mod_vcard_muc.lua @ 3206:d57635562216

mod_s2s_auth_posh: Beginnings of a README
author Kim Alvefur <zash@zash.se>
date Mon, 19 Feb 2018 09:24:44 +0100
parents f16b021e8a61
children d7b589dec591
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
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
17 local vcards = module:open_store();
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
18
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
19 module:add_feature("vcard-temp");
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
20
3077
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
21 local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
22 function (jid)
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
23 local rooms = rawget(mod_muc, "rooms");
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
24 return rooms[jid];
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
25 end
f16b021e8a61 mod_vcard_muc: Get room from rooms table when get_room_from_jid API is not supported
Michel Le Bihan <michel@lebihan.pl>
parents: 3046
diff changeset
26
3043
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
27 local function broadcast_presence(room_jid, to)
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
28 local room = get_room_from_jid(room_jid);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
29 local room_node = jid_split(room_jid);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
30 local vcard = st.deserialize(vcards:get(room_node));
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
31 local photo_hash;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
32
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
33 if vcard then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
34 local photo = vcard:get_child("PHOTO");
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
35
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
36 if photo then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
37 local photo_b64 = photo:get_child_text("BINVAL");
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
38 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
39 photo_hash = sha1(photo_raw, true);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
40 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
41 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
42
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
43 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
44 :tag("x", { xmlns = "vcard-temp:x:update" })
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
45 :tag("photo"):text(photo_hash):up();
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
46
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
47 if to == nil then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
48 room:broadcast_message(presence_vcard);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
49 else
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
50 module:send(presence_vcard);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
51 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
52 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
53
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
54 local function handle_vcard(event)
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
55 local session, stanza = event.origin, event.stanza;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
56
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
57 local room_jid = stanza.attr.to;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
58 local room_node = jid_split(room_jid);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
59 local room = get_room_from_jid(room_jid);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
60
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
61 local from = stanza.attr.from;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
62 local from_affiliation = room:get_affiliation(from);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
63
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
64 if stanza.attr.type == "get" then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
65 local vCard;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
66 vCard = st.deserialize(vcards:get(room_node));
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
67
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
68 if vCard then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
69 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
70 else
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
71 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
72 end
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 if from_affiliation == "owner" then
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
75 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
76 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
77 broadcast_presence(room_jid, nil)
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
78 else
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
79 -- 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
80 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
81 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
82 else
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
83 session.send(st.error_reply(stanza, "auth", "forbidden"));
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
84 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
85 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
86 return true;
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
87 end
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
88
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
89
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
90 module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
91 module:hook("iq/host/vcard-temp:vCard", handle_vcard);
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
92
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
93 module:hook("muc-disco#info", function(event)
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
94 event.reply:tag("feature", { var = "vcard-temp" }):up();
6cc44e69443a mod_vcard_muc: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
95 end);
3046
d0db28768980 mod_vcard_muc: Send presence on muc-occupant-session-new event
Michel Le Bihan <michel@lebihan.pl>
parents: 3043
diff changeset
96
d0db28768980 mod_vcard_muc: Send presence on muc-occupant-session-new event
Michel Le Bihan <michel@lebihan.pl>
parents: 3043
diff changeset
97 module:hook("muc-occupant-session-new", function(event)
d0db28768980 mod_vcard_muc: Send presence on muc-occupant-session-new event
Michel Le Bihan <michel@lebihan.pl>
parents: 3043
diff changeset
98 broadcast_presence(event.room.jid, event.jid);
d0db28768980 mod_vcard_muc: Send presence on muc-occupant-session-new event
Michel Le Bihan <michel@lebihan.pl>
parents: 3043
diff changeset
99 end)