MUC: Move the locked flag into persisted data (so not to lose it on eviction)
[prosody.git] / plugins / muc / lock.lib.lua
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 lock_rooms = module:get_option_boolean("muc_room_locking", false);
13 local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
14
15 local function lock(room)
16         module:fire_event("muc-room-locked", {room = room;});
17         room._data.locked = true;
18 end
19 local function unlock(room)
20         module:fire_event("muc-room-unlocked", {room = room;});
21         room._data.locked = nil;
22 end
23 local function is_locked(room)
24         return not not room._data.locked;
25 end
26
27 if lock_rooms then
28         module:hook("muc-room-pre-create", function(event)
29                 -- Older groupchat protocol doesn't lock
30                 if not event.stanza:get_child("x", "http://jabber.org/protocol/muc") then return end
31                 -- Lock room at creation
32                 local room = event.room;
33                 lock(room);
34                 if lock_room_timeout and lock_room_timeout > 0 then
35                         module:add_timer(lock_room_timeout, function ()
36                                 if is_locked(room) then
37                                         room:destroy(); -- Not unlocked in time
38                                 end
39                         end);
40                 end
41         end, 10);
42 end
43
44 -- Don't let users into room while it is locked
45 module:hook("muc-occupant-pre-join", function(event)
46         if not event.is_new_room and is_locked(event.room) then -- Deny entry
47                 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found"));
48                 return true;
49         end
50 end, -30);
51
52 -- When config is submitted; unlock the room
53 module:hook("muc-config-submitted", function(event)
54         if is_locked(event.room) then
55                 unlock(event.room);
56         end
57 end, -1);
58
59 return {
60         lock = lock;
61         unlock = unlock;
62         is_locked = is_locked;
63 };