mod_muc_occupant_id/mod_muc_occupant_id.lua
author Maxime “pep” Buquet <pep@bouah.net>
Mon, 06 Jan 2020 16:43:15 +0100
changeset 3841 7440cffe30e2
parent 3839 5258f0afa8b4
child 3908 d14fc974efbc
permissions -rw-r--r--
mod_muc_occupant_id: Add TODO about MUC-PMs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
     1
3635
d6164ae6179c mod_muc_occupant_id: Update links to the XEP inbox.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3634
diff changeset
     2
-- Implementation of https://xmpp.org/extensions/inbox/occupant-id.html
3658
7b02b8de6d27 mod_muc_occupant_id: Update XEP number (XEP-0421)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3636
diff changeset
     3
-- XEP-0421: Anonymous unique occupant identifiers for MUCs
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
     4
3636
83a68f5fde1d mod_muc_occupant_id: depend on muc.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3635
diff changeset
     5
module:depends("muc");
83a68f5fde1d mod_muc_occupant_id: depend on muc.
Maxime “pep” Buquet <pep@bouah.net>
parents: 3635
diff changeset
     6
3633
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
3833
f20a5d28910f mod_muc_occupant_id: Ensure occupants have a generated id
Maxime “pep” Buquet <pep@bouah.net>
parents: 3779
diff changeset
    13
local function generate_id(occupant, room)
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    14
	local bare = occupant.bare_jid;
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    15
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    16
	if room._data.occupant_id_salt == nil then
3838
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    17
		room._data.occupant_id_salt = uuid.generate();
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    18
	end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    19
3838
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    20
	if room._data.occupant_ids == nil then
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    21
		room._data.occupant_ids = {};
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    22
	end
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    23
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    24
	if room._data.occupant_ids[bare] == nil then
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    25
		local unique_id = b64encode(hmac_sha256(bare, room._data.occupant_id_salt));
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    26
		room._data.occupant_ids[bare] = unique_id;
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    27
	end
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    28
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    29
	return room._data.occupant_ids[bare];
3833
f20a5d28910f mod_muc_occupant_id: Ensure occupants have a generated id
Maxime “pep” Buquet <pep@bouah.net>
parents: 3779
diff changeset
    30
end
f20a5d28910f mod_muc_occupant_id: Ensure occupants have a generated id
Maxime “pep” Buquet <pep@bouah.net>
parents: 3779
diff changeset
    31
3838
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    32
local function update_occupant(event)
3833
f20a5d28910f mod_muc_occupant_id: Ensure occupants have a generated id
Maxime “pep” Buquet <pep@bouah.net>
parents: 3779
diff changeset
    33
	local stanza, occupant, room = event.stanza, event.occupant, event.room;
3674
6a437d6eb69f mod_muc_occupant_id: add TODO regarding MAM handling
Maxime “pep” Buquet <pep@bouah.net>
parents: 3658
diff changeset
    34
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    35
	-- 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
    36
	stanza:remove_children("occupant-id", xmlns_occupant_id);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    37
3838
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    38
	local unique_id = generate_id(occupant, room);
3633
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    39
	stanza:tag("occupant-id", { xmlns = xmlns_occupant_id })
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    40
		:text(unique_id)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    41
		:up();
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    42
end
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    43
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    44
module:add_feature(xmlns_occupant_id);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    45
module:hook("muc-disco#info", function (event)
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    46
	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
    47
end);
cfe0907808e1 mod_muc_occupant_id: initial commit
Maxime “pep” Buquet <pep@bouah.net>
parents:
diff changeset
    48
3841
7440cffe30e2 mod_muc_occupant_id: Add TODO about MUC-PMs
Maxime “pep” Buquet <pep@bouah.net>
parents: 3839
diff changeset
    49
-- TODO: Handle MUC-PMs
3839
5258f0afa8b4 mod_muc_occupant_id: Add <occupant-id/> in presence
Maxime “pep” Buquet <pep@bouah.net>
parents: 3838
diff changeset
    50
module:hook("muc-broadcast-presence", update_occupant);
3838
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    51
module:hook("muc-occupant-pre-join", update_occupant);
2f189f022b84 mod_muc_occupant_id: Simplify handling of occupants (all into one function)
Maxime “pep” Buquet <pep@bouah.net>
parents: 3835
diff changeset
    52
module:hook("muc-occupant-groupchat", update_occupant);