annotate mod_muc_occupant_id/mod_muc_occupant_id.lua @ 3641:58b49d883f0c

mod_conversejs: Change CSS URL I hope this is correct
author Kim Alvefur <zash@zash.se>
date Fri, 09 Aug 2019 18:41:51 +0200
parents 83a68f5fde1d
children 7b02b8de6d27
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3629
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
1
3631
d6164ae6179c mod_muc_occupant_id: Update links to the XEP inbox.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3630
diff changeset
2 -- Implementation of https://xmpp.org/extensions/inbox/occupant-id.html
3629
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
3 -- XEP-XXXX: Anonymous unique occupant identifiers for MUCs
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
4
3632
83a68f5fde1d mod_muc_occupant_id: depend on muc.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3631
diff changeset
5 module:depends("muc");
83a68f5fde1d mod_muc_occupant_id: depend on muc.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3631
diff changeset
6
3629
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
7 local uuid = require "util.uuid";
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
8 local hmac_sha256 = require "util.hashes".hmac_sha256;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
9 local b64encode = require "util.encodings".base64.encode;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
10
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
12
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
13 local function edit_occupant(event)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
14 local occupant, room = event.occupant, event.room;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
15 local bare = occupant.bare_jid;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
16
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
17 -- TODO: Move the salt on the MUC component. Setting the salt on the room
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
18 -- can be problematic when the room is destroyed. Next time it's recreated
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
19 -- the salt will be different and so will be the unique_id. Or maybe we want
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
20 -- this anyway?
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
21 if room._data.occupant_id_salt == nil then
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
22 local salt = uuid.generate();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
23 room._data.occupant_id_salt = salt;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
24 end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
25
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
26 local unique_id = b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
27
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
28 -- TODO: Store this only once per bare jid and not once per occupant?
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
29 local stanza = event.stanza;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
30 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
31 :text(unique_id)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
32 :up();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
33 end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
34
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
35 local function handle_stanza(event)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
36 local stanza, occupant = event.stanza, event.occupant;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
37
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
38 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- not required here
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
39 return;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
40 end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
41
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
42 -- strip any existing <occupant-id/> tags to avoid forgery
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
43 stanza:remove_children("occupant-id", xmlns_occupant_id);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
44
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
45 local unique_id = occupant.sessions[stanza.attr.from]
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
46 :get_child("occupant-id", xmlns_occupant_id)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
47 :get_text();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
48 stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
49 :text(unique_id)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
50 :up();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
51 end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
52
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
53 module:add_feature(xmlns_occupant_id);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
54 module:hook("muc-disco#info", function (event)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
55 event.reply:tag("feature", { var = xmlns_occupant_id }):up();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
56 end);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
57
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
58 module:hook("muc-occupant-pre-join", edit_occupant);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
59 module:hook("muc-occupant-groupchat", handle_stanza);