773509b5d53e536834dc17836fea09afc36bc28f
[prosody.git] / plugins / muc / persistent.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 function get_persistent(room)
11         return room._data.persistent;
12 end
13
14 local function set_persistent(room, persistent)
15         persistent = persistent and true or nil;
16         if get_persistent(room) == persistent then return false; end
17         room._data.persistent = persistent;
18         return true;
19 end
20
21 module:hook("muc-config-form", function(event)
22         table.insert(event.form, {
23                 name = "muc#roomconfig_persistentroom";
24                 type = "boolean";
25                 label = "Make Room Persistent?";
26                 value = get_persistent(event.room);
27         });
28 end);
29
30 module:hook("muc-config-submitted/muc#roomconfig_persistentroom", function(event)
31         if set_persistent(event.room, event.value) then
32                 event.status_codes["104"] = true;
33         end
34 end);
35
36 module:hook("muc-disco#info", function(event)
37         event.reply:tag("feature", {var = get_persistent(event.room) and "muc_persistent" or "muc_temporary"}):up();
38 end);
39
40 module:hook("muc-room-destroyed", function(event)
41         set_persistent(event.room, false);
42 end);
43
44 return {
45         get = get_persistent;
46         set = set_persistent;
47 };