aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/muc/presence_broadcast.lib.lua
blob: 72e0d76bd480b009dc0377d2565ee39a3a955311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
-- Copyright (C) 2014 Daurnimator
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local st = require "util.stanza";

local valid_roles = { "visitor", "participant", "moderator" };
local default_broadcast = {
	visitor = true;
	participant = true;
	moderator = true;
};

local function get_presence_broadcast(room)
	return room._data.presence_broadcast or default_broadcast;
end

local function set_presence_broadcast(room, broadcast_roles)
	broadcast_roles = broadcast_roles or default_broadcast;

	local changed = false;
	local old_broadcast_roles = get_presence_broadcast(room);
	for _, role in ipairs(valid_roles) do
		if old_broadcast_roles[role] ~= broadcast_roles[role] then
			changed = true;
		end
	end

	if not changed then return false; end

	room._data.presence_broadcast = broadcast_roles;

	for _, occupant in room:each_occupant() do
		local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
		local role = occupant.role or "none";
		if broadcast_roles[role] and not old_broadcast_roles[role] then
			-- Presence broadcast is now enabled, so announce existing user
			room:publicise_occupant_status(occupant, x);
		elseif old_broadcast_roles[role] and not broadcast_roles[role] then
			-- Presence broadcast is now disabled, so mark existing user as unavailable
			room:publicise_occupant_status(occupant, x, nil, nil, nil, nil, true);
		end
	end

	return true;
end

module:hook("muc-config-form", function(event)
	local values = {};
	for role, value in pairs(get_presence_broadcast(event.room)) do
		if value then
			values[#values + 1] = role;
		end
	end

	table.insert(event.form, {
		name = "muc#roomconfig_presencebroadcast";
		type = "list-multi";
		label = "Only show participants with roles:";
		value = values;
		options = valid_roles;
	});
end, 70-7);

module:hook("muc-config-submitted/muc#roomconfig_presencebroadcast", function(event)
	local broadcast_roles = {};
	for _, role in ipairs(event.value) do
		broadcast_roles[role] = true;
	end
	if set_presence_broadcast(event.room, broadcast_roles) then
		event.status_codes["104"] = true;
	end
end);

return {
	get = get_presence_broadcast;
	set = set_presence_broadcast;
};