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