Merge 0.10->trunk
[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         if room.save then room:save(true); end
19         return true;
20 end
21
22 module:hook("muc-config-form", function(event)
23         table.insert(event.form, {
24                 name = "muc#roomconfig_persistentroom";
25                 type = "boolean";
26                 label = "Make Room Persistent?";
27                 value = get_persistent(event.room);
28         });
29 end);
30
31 module:hook("muc-config-submitted", function(event)
32         local new = event.fields["muc#roomconfig_persistentroom"];
33         if new ~= nil and set_persistent(event.room, new) then
34                 event.status_codes["104"] = true;
35         end
36 end);
37
38 module:hook("muc-disco#info", function(event)
39         event.reply:tag("feature", {var = get_persistent(event.room) and "muc_persistent" or "muc_temporary"}):up();
40 end);
41
42 module:hook("muc-room-destroyed", function(event)
43         set_persistent(event.room, false);
44 end);
45
46 return {
47         get = get_persistent;
48         set = set_persistent;
49 };