Comparison

plugins/muc/presence_broadcast.lib.lua @ 10353:7b602e13c3b6

MUC: Add controls for whose presence is broadcast (closes #1335) Committed by Zash
author Lance Stout <lancestout@gmail.com>
date Sun, 20 Oct 2019 21:58:16 +0200
child 10516:d3e6941546a8
comparison
equal deleted inserted replaced
10352:dc1e6c2fb50a 10353:7b602e13c3b6
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local st = require "util.stanza";
11
12 local valid_roles = { "visitor", "participant", "moderator" };
13 local default_broadcast = {
14 none = true;
15 visitor = true;
16 participant = true;
17 moderator = true;
18 };
19
20 local function get_presence_broadcast(room)
21 return room._data.presence_broadcast or default_broadcast;
22 end
23
24 local function set_presence_broadcast(room, broadcast_roles)
25 broadcast_roles = broadcast_roles or default_broadcast;
26
27 -- Ensure that unavailable presence is always sent when role changes to none
28 broadcast_roles.none = true;
29
30 local changed = false;
31 local old_broadcast_roles = get_presence_broadcast(room);
32 for _, role in ipairs(valid_roles) do
33 if old_broadcast_roles[role] ~= broadcast_roles[role] then
34 changed = true;
35 end
36 end
37
38 if not changed then return false; end
39
40 room._data.presence_broadcast = broadcast_roles;
41
42 for _, occupant in room:each_occupant() do
43 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
44 local role = occupant.role or "none";
45 if broadcast_roles[role] and not old_broadcast_roles[role] then
46 -- Presence broadcast is now enabled, so announce existing user
47 room:publicise_occupant_status(occupant, x);
48 elseif old_broadcast_roles[role] and not broadcast_roles[role] then
49 -- Presence broadcast is now disabled, so mark existing user as unavailable
50 room:publicise_occupant_status(occupant, x, nil, nil, nil, nil, true);
51 end
52 end
53
54 return true;
55 end
56
57 module:hook("muc-config-form", function(event)
58 local values = {};
59 for role, value in pairs(get_presence_broadcast(event.room)) do
60 if value then
61 values[#values + 1] = role;
62 end
63 end
64
65 table.insert(event.form, {
66 name = "muc#roomconfig_presencebroadcast";
67 type = "list-multi";
68 label = "Roles for which Presence is Broadcasted";
69 value = values;
70 options = valid_roles;
71 });
72 end, 70-7);
73
74 module:hook("muc-config-submitted/muc#roomconfig_presencebroadcast", function(event)
75 local broadcast_roles = {};
76 for _, role in ipairs(event.value) do
77 broadcast_roles[role] = true;
78 end
79 if set_presence_broadcast(event.room, broadcast_roles) then
80 event.status_codes["104"] = true;
81 end
82 end);
83
84 return {
85 get = get_presence_broadcast;
86 set = set_presence_broadcast;
87 };