plugins/muc/mod_muc: Use map store for room persistence
[prosody.git] / plugins / muc / mod_muc.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 if module:get_host_type() ~= "component" then
10         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
11 end
12
13 local muclib = module:require "muc";
14 room_mt = muclib.room_mt; -- Yes, global.
15 local iterators = require "util.iterators";
16 local jid_split = require "util.jid".split;
17 local jid_bare = require "util.jid".bare;
18 local st = require "util.stanza";
19 local um_is_admin = require "core.usermanager".is_admin;
20
21 local rooms = module:shared "rooms";
22
23 module:depends("disco");
24 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
25 module:add_feature("http://jabber.org/protocol/muc");
26 module:depends "muc_unique"
27 module:require "muc/lock";
28
29 local function is_admin(jid)
30         return um_is_admin(jid, module.host);
31 end
32
33 do -- Monkey patch to make server admins room owners
34         local _get_affiliation = room_mt.get_affiliation;
35         function room_mt:get_affiliation(jid)
36                 if is_admin(jid) then return "owner"; end
37                 return _get_affiliation(self, jid);
38         end
39
40         local _set_affiliation = room_mt.set_affiliation;
41         function room_mt:set_affiliation(actor, jid, ...)
42                 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
43                 return _set_affiliation(self, actor, jid, ...);
44         end
45 end
46
47 local persistent = module:require "muc/persistent";
48 local persistent_rooms_storage = module:open_store("persistent");
49 local persistent_rooms = module:open_store("persistent", "map");
50 local room_configs = module:open_store("config");
51
52 local function room_save(room, forced)
53         local node = jid_split(room.jid);
54         local is_persistent = persistent.get(room);
55         persistent_rooms:set(room.jid, is_persistent);
56         if is_persistent then
57                 local history = room._data.history;
58                 room._data.history = nil;
59                 local data = {
60                         jid = room.jid;
61                         _data = room._data;
62                         _affiliations = room._affiliations;
63                 };
64                 room_configs:set(node, data);
65                 room._data.history = history;
66         elseif forced then
67                 room_configs:set(node, nil);
68                 if not next(room._occupants) then -- Room empty
69                         rooms[room.jid] = nil;
70                 end
71         end
72 end
73
74 -- Automatically destroy empty non-persistent rooms
75 module:hook("muc-occupant-left",function(event)
76         local room = event.room
77         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
78                 module:fire_event("muc-room-destroyed", { room = room });
79         end
80 end);
81
82 function track_room(room)
83         rooms[room.jid] = room;
84         -- When room is created, over-ride 'save' method
85         room.save = room_save;
86 end
87
88 local function restore_room(jid)
89         local node = jid_split(jid);
90         local data = room_configs:get(node);
91         if data then
92                 local room = muclib.new_room(jid);
93                 room._data = data._data;
94                 room._affiliations = data._affiliations;
95                 track_room(room);
96                 return room;
97         end
98 end
99
100 function forget_room(jid)
101         rooms[jid] = nil;
102         local node = jid_split(room.jid);
103         room_configs:set(node, nil);
104         if persistent.get(room_jid) then
105                 persistent_rooms:set(room_jid, nil);
106         end
107 end
108
109 function get_room_from_jid(room_jid)
110         local room = rooms[room_jid];
111         if room == nil then
112                 -- Check if in persistent storage
113                 if persistent_rooms:get(room_jid) then
114                         room = restore_room(room_jid);
115                         if room == nil then
116                                 module:log("error", "Missing data for room '%s', removing from persistent room list", room_jid);
117                                 persistent_rooms:set(room_jid, nil);
118                         end
119                 end
120         end
121         return room
122 end
123
124 function each_room()
125         for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
126                 if rooms[room_jid] == nil then -- Don't restore rooms that already exist
127                         local room = restore_room(room_jid);
128                         if room == nil then
129                                 module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
130                         end
131                 end
132         end
133         return iterators.values(rooms);
134 end
135
136 module:hook("host-disco-items", function(event)
137         local reply = event.reply;
138         module:log("debug", "host-disco-items called");
139         for room in each_room() do
140                 if not room:get_hidden() then
141                         reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
142                 end
143         end
144 end);
145
146 module:hook("muc-room-pre-create", function(event)
147         track_room(event.room);
148 end, -1000);
149
150 module:hook("muc-room-destroyed",function(event)
151         local room = event.room
152         forget_room(room.jid)
153 end)
154
155 do
156         local restrict_room_creation = module:get_option("restrict_room_creation");
157         if restrict_room_creation == true then
158                 restrict_room_creation = "admin";
159         end
160         if restrict_room_creation then
161                 local host_suffix = module.host:gsub("^[^%.]+%.", "");
162                 module:hook("muc-room-pre-create", function(event)
163                         local origin, stanza = event.origin, event.stanza;
164                         local user_jid = stanza.attr.from;
165                         if not is_admin(user_jid) and not (
166                                 restrict_room_creation == "local" and
167                                 select(2, jid_split(user_jid)) == host_suffix
168                         ) then
169                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
170                                 return true;
171                         end
172                 end);
173         end
174 end
175
176 for event_name, method in pairs {
177         -- Normal room interactions
178         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
179         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
180         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
181         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
182         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
183         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
184         ["message/bare"] = "handle_message_to_room" ;
185         ["presence/bare"] = "handle_presence_to_room" ;
186         -- Host room
187         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
188         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
189         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
190         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
191         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
192         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
193         ["message/host"] = "handle_message_to_room" ;
194         ["presence/host"] = "handle_presence_to_room" ;
195         -- Direct to occupant (normal rooms and host room)
196         ["presence/full"] = "handle_presence_to_occupant" ;
197         ["iq/full"] = "handle_iq_to_occupant" ;
198         ["message/full"] = "handle_message_to_occupant" ;
199 } do
200         module:hook(event_name, function (event)
201                 local origin, stanza = event.origin, event.stanza;
202                 local room_jid = jid_bare(stanza.attr.to);
203                 local room = get_room_from_jid(room_jid);
204                 if room == nil then
205                         -- Watch presence to create rooms
206                         if stanza.attr.type == nil and stanza.name == "presence" then
207                                 room = muclib.new_room(room_jid);
208                         else
209                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
210                                 return true;
211                         end
212                 end
213                 return room[method](room, origin, stanza);
214         end, -2)
215 end
216
217 function shutdown_component()
218         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
219                 :tag("status", { code = "332"}):up();
220         for room in each_room() do
221                 room:clear(x);
222         end
223 end
224 module:hook_global("server-stopping", shutdown_component);
225
226 do -- Ad-hoc commands
227         module:depends "adhoc";
228         local t_concat = table.concat;
229         local adhoc_new = module:require "adhoc".new;
230         local adhoc_initial = require "util.adhoc".new_initial_data_form;
231         local array = require "util.array";
232         local dataforms_new = require "util.dataforms".new;
233
234         local destroy_rooms_layout = dataforms_new {
235                 title = "Destroy rooms";
236                 instructions = "Select the rooms to destroy";
237
238                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
239                 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
240         };
241
242         local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
243                 return { rooms = array.collect(each_room):pluck("jid"):sort(); };
244         end, function(fields, errors)
245                 if errors then
246                         local errmsg = {};
247                         for name, err in pairs(errors) do
248                                 errmsg[#errmsg + 1] = name .. ": " .. err;
249                         end
250                         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
251                 end
252                 for _, room in ipairs(fields.rooms) do
253                         get_room_from_jid(room):destroy();
254                 end
255                 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
256         end);
257         local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
258
259         module:provides("adhoc", destroy_rooms_desc);
260 end