-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmod_owner_restricted.lua
131 lines (111 loc) · 3.94 KB
/
mod_owner_restricted.lua
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
local LOGLEVEL = "debug"
local TIMEOUT = module:get_option_number("role_timeout", 120)
local is_admin = require "core.usermanager".is_admin
local is_healthcheck_room = module:require "util".is_healthcheck_room
local it = require "util.iterators"
local timer = require "util.timer"
module:log("info", "loaded")
local function _is_admin(jid)
return is_admin(jid, module.host)
end
local function terminator(room)
if is_healthcheck_room(room.jid) then
return
end
-- decrease the terminator counter
room._data.terminator_count = room._data.terminator_count - 1
-- stop this terminator if there is another active one
-- always the last terminator destroys the room
if room._data.terminator_count > 0 then
module:log(
LOGLEVEL,
"there is another active terminator. this one is stopped, %s, %s",
room.jid, room._data.meetingId
)
return
end
-- stop this terminator if there is no one in the room
-- this happens if the room was already destroyed by another mechanism
local occupant_count = it.count(room:each_occupant())
if occupant_count == 0 then
module:log(
LOGLEVEL,
"Noone in the room, terminator is stopped, %s, %s",
room.jid, room._data.meetingId
)
return
end
-- last check before destroying the room
module:log(LOGLEVEL, "last check before destroying the room")
for _, o in room:each_occupant() do
if not _is_admin(o.jid) then
if room:get_affiliation(o.jid) == "owner" then
module:log(
LOGLEVEL,
"there is an owner, terminator is stopped, %s",
room.jid
)
return
end
end
end
-- terminate the meeting
room:set_persistent(false)
room:destroy(nil, "The meeting has been terminated")
module:log(LOGLEVEL, "the meeting has been terminated")
end
local function trigger_terminator(room)
module:log(LOGLEVEL, "terminator request is received")
-- check if there is an owner in the room
-- do nothing if there is one
for _, o in room:each_occupant() do
if not _is_admin(o.jid) then
if room:get_affiliation(o.jid) == "owner" then
module:log(
LOGLEVEL,
"there is an owner, terminator is not triggered, %s",
room.jid
)
return
end
end
end
-- increase the terminator counter before triggering a new one
if not room._data.terminator_count then
room._data.terminator_count = 1
else
room._data.terminator_count = room._data.terminator_count + 1
end
-- since there is no owner in the room, destroy the room after TIMEOUT secs
timer.add_task(TIMEOUT, function()
terminator(room)
end)
module:log(LOGLEVEL, "terminator is triggered")
end
module:hook("muc-occupant-joined", function (event)
local room, occupant = event.room, event.occupant
if is_healthcheck_room(room.jid) or _is_admin(occupant.jid) then
return
end
-- do nothing if this is not the first participant (after focus)
local occupant_count = it.count(room:each_occupant())
if occupant_count > 2 then
return
end
module:log(LOGLEVEL, "the first participant joined the room, %s", room.jid)
-- the first participant joined the room, call terminator
trigger_terminator(room)
end)
module:hook("muc-occupant-left", function (event)
local room, occupant = event.room, event.occupant
if is_healthcheck_room(room.jid) or _is_admin(occupant.jid) then
return
end
-- do nothing if her role is not owner
if room:get_affiliation(occupant.jid) ~= "owner" then
return
end
module:log(LOGLEVEL, "an owner left, %s", occupant.jid)
-- an owner left the room, call terminator
trigger_terminator(room)
end)