MUC: Separate config from live state
[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 https://prosody.im/doc/components", 0);
11 end
12
13 local muclib = module:require "muc";
14 room_mt = muclib.room_mt; -- Yes, global.
15
16 local affiliation_notify = module:require "muc/affiliation_notify"; -- luacheck: ignore 211
17
18 local name = module:require "muc/name";
19 room_mt.get_name = name.get;
20 room_mt.set_name = name.set;
21
22 local description = module:require "muc/description";
23 room_mt.get_description = description.get;
24 room_mt.set_description = description.set;
25
26 local hidden = module:require "muc/hidden";
27 room_mt.get_hidden = hidden.get;
28 room_mt.set_hidden = hidden.set;
29 function room_mt:get_public()
30         return not self:get_hidden();
31 end
32 function room_mt:set_public(public)
33         return self:set_hidden(not public);
34 end
35
36 local password = module:require "muc/password";
37 room_mt.get_password = password.get;
38 room_mt.set_password = password.set;
39
40 local members_only = module:require "muc/members_only";
41 room_mt.get_members_only = members_only.get;
42 room_mt.set_members_only = members_only.set;
43
44 local moderated = module:require "muc/moderated";
45 room_mt.get_moderated = moderated.get;
46 room_mt.set_moderated = moderated.set;
47
48 local persistent = module:require "muc/persistent";
49 room_mt.get_persistent = persistent.get;
50 room_mt.set_persistent = persistent.set;
51
52 local subject = module:require "muc/subject";
53 room_mt.get_changesubject = subject.get_changesubject;
54 room_mt.set_changesubject = subject.set_changesubject;
55 room_mt.get_subject = subject.get;
56 room_mt.set_subject = subject.set;
57 room_mt.send_subject = subject.send;
58
59 local history = module:require "muc/history";
60 room_mt.send_history = history.send;
61 room_mt.get_historylength = history.get_length;
62 room_mt.set_historylength = history.set_length;
63
64 local jid_split = require "util.jid".split;
65 local jid_bare = require "util.jid".bare;
66 local st = require "util.stanza";
67 local cache = require "util.cache";
68 local um_is_admin = require "core.usermanager".is_admin;
69
70 module:depends("disco");
71 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
72 module:add_feature("http://jabber.org/protocol/muc");
73 module:depends "muc_unique"
74 module:require "muc/lock";
75
76 local function is_admin(jid)
77         return um_is_admin(jid, module.host);
78 end
79
80 do -- Monkey patch to make server admins room owners
81         local _get_affiliation = room_mt.get_affiliation;
82         function room_mt:get_affiliation(jid)
83                 if is_admin(jid) then return "owner"; end
84                 return _get_affiliation(self, jid);
85         end
86
87         local _set_affiliation = room_mt.set_affiliation;
88         function room_mt:set_affiliation(actor, jid, affiliation, reason)
89                 if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
90                 return _set_affiliation(self, actor, jid, affiliation, reason);
91         end
92 end
93
94 local persistent_rooms_storage = module:open_store("persistent");
95 local persistent_rooms = module:open_store("persistent", "map");
96 local room_configs = module:open_store("config");
97 local room_state = module:open_store("state");
98
99 local room_items_cache = {};
100
101 local function room_save(room, forced, savestate)
102         local node = jid_split(room.jid);
103         local is_persistent = persistent.get(room);
104         room_items_cache[room.jid] = room:get_public() and room:get_name() or nil;
105         if is_persistent or savestate then
106                 persistent_rooms:set(nil, room.jid, true);
107                 local data, state = room:freeze(savestate);
108                 room_state:set(node, state);
109                 return room_configs:set(node, data);
110         elseif forced then
111                 persistent_rooms:set(nil, room.jid, nil);
112                 room_state:set(node, nil);
113                 return room_configs:set(node, nil);
114         end
115 end
116
117 local rooms = cache.new(module:get_option_number("muc_room_cache_size", 100), function (_, room)
118         module:log("debug", "%s evicted", room);
119         room_save(room, nil, true); -- Force to disk
120 end);
121
122 -- Automatically destroy empty non-persistent rooms
123 module:hook("muc-occupant-left",function(event)
124         local room = event.room
125         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
126                 module:fire_event("muc-room-destroyed", { room = room });
127         end
128 end);
129
130 function track_room(room)
131         rooms:set(room.jid, room);
132         -- When room is created, over-ride 'save' method
133         room.save = room_save;
134 end
135
136 local function restore_room(jid)
137         local node = jid_split(jid);
138         local data = room_configs:get(node);
139         local state = room_state:get(node);
140         if data then
141                 local room = muclib.restore_room(data, state);
142                 track_room(room);
143                 return room;
144         end
145 end
146
147 function forget_room(room)
148         module:log("debug", "Forgetting %s", room);
149         rooms.save = nil;
150         rooms:set(room.jid, nil);
151 end
152
153 function delete_room(room)
154         module:log("debug", "Deleting %s", room);
155         room_configs:set(jid_split(room.jid), nil);
156         persistent_rooms:set(nil, room.jid, nil);
157         room_items_cache[room.jid] = nil;
158 end
159
160 function module.unload()
161         for room in rooms:values() do
162                 room:save(nil, true);
163                 forget_room(room);
164         end
165 end
166
167 function get_room_from_jid(room_jid)
168         local room = rooms:get(room_jid);
169         if room then
170                 rooms:set(room_jid, room); -- bump to top;
171                 return room;
172         end
173         return restore_room(room_jid);
174 end
175
176 function each_room(local_only)
177         if local_only then
178                 return rooms:values();
179         end
180         return coroutine.wrap(function ()
181                 local seen = {}; -- Don't iterate over persistent rooms twice
182                 for room in rooms:values() do
183                         coroutine.yield(room);
184                         seen[room.jid] = true;
185                 end
186                 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
187                         if not seen[room_jid] then
188                                 local room = restore_room(room_jid);
189                                 if room == nil then
190                                         module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
191                                 else
192                                         coroutine.yield(room);
193                                 end
194                         end
195                 end
196         end);
197 end
198
199 module:hook("host-disco-items", function(event)
200         local reply = event.reply;
201         module:log("debug", "host-disco-items called");
202         if next(room_items_cache) ~= nil then
203                 for jid, room_name in pairs(room_items_cache) do
204                         reply:tag("item", { jid = jid, name = room_name }):up();
205                 end
206         else
207                 for room in each_room() do
208                         if not room:get_hidden() then
209                                 local jid, room_name = room.jid, room:get_name();
210                                 room_items_cache[jid] = room_name;
211                                 reply:tag("item", { jid = jid, name = room_name }):up();
212                         end
213                 end
214         end
215 end);
216
217 module:hook("muc-room-created", function(event)
218         track_room(event.room);
219 end, -1000);
220
221 module:hook("muc-room-destroyed",function(event)
222         local room = event.room;
223         forget_room(room);
224         delete_room(room);
225 end);
226
227 do
228         local restrict_room_creation = module:get_option("restrict_room_creation");
229         if restrict_room_creation == true then
230                 restrict_room_creation = "admin";
231         end
232         if restrict_room_creation then
233                 local host_suffix = module.host:gsub("^[^%.]+%.", "");
234                 module:hook("muc-room-pre-create", function(event)
235                         local origin, stanza = event.origin, event.stanza;
236                         local user_jid = stanza.attr.from;
237                         if not is_admin(user_jid) and not (
238                                 restrict_room_creation == "local" and
239                                 select(2, jid_split(user_jid)) == host_suffix
240                         ) then
241                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
242                                 return true;
243                         end
244                 end);
245         end
246 end
247
248 for event_name, method in pairs {
249         -- Normal room interactions
250         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
251         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
252         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
253         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
254         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
255         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
256         ["message/bare"] = "handle_message_to_room" ;
257         ["presence/bare"] = "handle_presence_to_room" ;
258         -- Host room
259         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
260         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
261         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
262         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
263         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
264         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
265         ["message/host"] = "handle_message_to_room" ;
266         ["presence/host"] = "handle_presence_to_room" ;
267         -- Direct to occupant (normal rooms and host room)
268         ["presence/full"] = "handle_presence_to_occupant" ;
269         ["iq/full"] = "handle_iq_to_occupant" ;
270         ["message/full"] = "handle_message_to_occupant" ;
271 } do
272         module:hook(event_name, function (event)
273                 local origin, stanza = event.origin, event.stanza;
274                 local room_jid = jid_bare(stanza.attr.to);
275                 local room = get_room_from_jid(room_jid);
276                 if room == nil then
277                         -- Watch presence to create rooms
278                         if stanza.attr.type == nil and stanza.name == "presence" then
279                                 room = muclib.new_room(room_jid);
280                                 return room:handle_first_presence(origin, stanza);
281                         elseif stanza.attr.type ~= "error" then
282                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
283                                 return true;
284                         else
285                                 return;
286                         end
287                 end
288                 return room[method](room, origin, stanza);
289         end, -2)
290 end
291
292 function shutdown_component()
293         for room in each_room(true) do
294                 room:save(nil, true);
295         end
296 end
297 module:hook_global("server-stopping", shutdown_component);
298
299 do -- Ad-hoc commands
300         module:depends "adhoc";
301         local t_concat = table.concat;
302         local adhoc_new = module:require "adhoc".new;
303         local adhoc_initial = require "util.adhoc".new_initial_data_form;
304         local array = require "util.array";
305         local dataforms_new = require "util.dataforms".new;
306
307         local destroy_rooms_layout = dataforms_new {
308                 title = "Destroy rooms";
309                 instructions = "Select the rooms to destroy";
310
311                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
312                 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
313         };
314
315         local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
316                 return { rooms = array.collect(each_room()):pluck("jid"):sort(); };
317         end, function(fields, errors)
318                 if errors then
319                         local errmsg = {};
320                         for field, err in pairs(errors) do
321                                 errmsg[#errmsg + 1] = field .. ": " .. err;
322                         end
323                         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
324                 end
325                 for _, room in ipairs(fields.rooms) do
326                         get_room_from_jid(room):destroy();
327                 end
328                 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
329         end);
330         local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
331
332         module:provides("adhoc", destroy_rooms_desc);
333 end