MUC: Switch to util.cache for storing rooms, store rooms to disk on eviction
[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
98 local function room_save(room, forced)
99         local node = jid_split(room.jid);
100         local is_persistent = persistent.get(room);
101         if is_persistent or forced then
102                 persistent_rooms:set(nil, room.jid, true);
103                 local data = room:freeze(forced);
104                 return room_configs:set(node, data);
105         else
106                 persistent_rooms:set(nil, room.jid, nil);
107                 return room_configs:set(node, nil);
108         end
109 end
110
111 local rooms = cache.new(module:get_option_number("muc_room_cache_size", 100), function (_, room)
112         module:log("debug", "%s evicted", room);
113         room_save(room, true); -- Force to disk
114 end);
115
116 -- Automatically destroy empty non-persistent rooms
117 module:hook("muc-occupant-left",function(event)
118         local room = event.room
119         if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
120                 module:fire_event("muc-room-destroyed", { room = room });
121         end
122 end);
123
124 function track_room(room)
125         rooms:set(room.jid, room);
126         -- When room is created, over-ride 'save' method
127         room.save = room_save;
128 end
129
130 local function restore_room(jid)
131         local node = jid_split(jid);
132         local data = room_configs:get(node);
133         if data then
134                 local room = muclib.restore_room(data);
135                 track_room(room);
136                 return room;
137         end
138 end
139
140 function forget_room(room)
141         module:log("debug", "Forgetting %s", room);
142         rooms.save = nil;
143         rooms:set(room.jid, nil);
144 end
145
146 function delete_room(room)
147         module:log("debug", "Deleting %s", room);
148         room_configs:set(jid_split(room.jid), nil);
149         persistent_rooms:set(nil, room.jid, nil);
150 end
151
152 function module.unload()
153         for room in rooms:values() do
154                 room:save(true);
155                 forget_room(room);
156         end
157 end
158
159 function get_room_from_jid(room_jid)
160         local room = rooms:get(room_jid);
161         if room then
162                 rooms:set(room_jid, room); -- bump to top;
163                 return room;
164         end
165         return restore_room(room_jid);
166 end
167
168 function each_room(local_only)
169         if local_only then
170                 return rooms:values();
171         end
172         return coroutine.wrap(function ()
173                 local seen = {}; -- Don't iterate over persistent rooms twice
174                 for room in rooms:values() do
175                         coroutine.yield(room);
176                         seen[room.jid] = true;
177                 end
178                 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
179                         if seen[room_jid] then
180                                 local room = restore_room(room_jid);
181                                 if room == nil then
182                                         module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
183                                 else
184                                         coroutine.yield(room);
185                                 end
186                         end
187                 end
188         end);
189 end
190
191 module:hook("host-disco-items", function(event)
192         local reply = event.reply;
193         module:log("debug", "host-disco-items called");
194         for room in each_room() do
195                 if not room:get_hidden() then
196                         reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
197                 end
198         end
199 end);
200
201 module:hook("muc-room-pre-create", function(event)
202         track_room(event.room);
203 end, -1000);
204
205 module:hook("muc-room-destroyed",function(event)
206         local room = event.room;
207         forget_room(room);
208         delete_room(room);
209 end);
210
211 do
212         local restrict_room_creation = module:get_option("restrict_room_creation");
213         if restrict_room_creation == true then
214                 restrict_room_creation = "admin";
215         end
216         if restrict_room_creation then
217                 local host_suffix = module.host:gsub("^[^%.]+%.", "");
218                 module:hook("muc-room-pre-create", function(event)
219                         local origin, stanza = event.origin, event.stanza;
220                         local user_jid = stanza.attr.from;
221                         if not is_admin(user_jid) and not (
222                                 restrict_room_creation == "local" and
223                                 select(2, jid_split(user_jid)) == host_suffix
224                         ) then
225                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
226                                 return true;
227                         end
228                 end);
229         end
230 end
231
232 for event_name, method in pairs {
233         -- Normal room interactions
234         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
235         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
236         ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
237         ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
238         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
239         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
240         ["message/bare"] = "handle_message_to_room" ;
241         ["presence/bare"] = "handle_presence_to_room" ;
242         -- Host room
243         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
244         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
245         ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
246         ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
247         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
248         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
249         ["message/host"] = "handle_message_to_room" ;
250         ["presence/host"] = "handle_presence_to_room" ;
251         -- Direct to occupant (normal rooms and host room)
252         ["presence/full"] = "handle_presence_to_occupant" ;
253         ["iq/full"] = "handle_iq_to_occupant" ;
254         ["message/full"] = "handle_message_to_occupant" ;
255 } do
256         module:hook(event_name, function (event)
257                 local origin, stanza = event.origin, event.stanza;
258                 local room_jid = jid_bare(stanza.attr.to);
259                 local room = get_room_from_jid(room_jid);
260                 if room == nil then
261                         -- Watch presence to create rooms
262                         if stanza.attr.type == nil and stanza.name == "presence" then
263                                 room = muclib.new_room(room_jid);
264                         elseif stanza.attr.type ~= "error" then
265                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
266                                 return true;
267                         else
268                                 return;
269                         end
270                 end
271                 return room[method](room, origin, stanza);
272         end, -2)
273 end
274
275 function shutdown_component()
276         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
277                 :tag("status", { code = "332"}):up();
278         for room in each_room(true) do
279                 room:clear(x);
280         end
281 end
282 module:hook_global("server-stopping", shutdown_component);
283
284 do -- Ad-hoc commands
285         module:depends "adhoc";
286         local t_concat = table.concat;
287         local adhoc_new = module:require "adhoc".new;
288         local adhoc_initial = require "util.adhoc".new_initial_data_form;
289         local array = require "util.array";
290         local dataforms_new = require "util.dataforms".new;
291
292         local destroy_rooms_layout = dataforms_new {
293                 title = "Destroy rooms";
294                 instructions = "Select the rooms to destroy";
295
296                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
297                 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
298         };
299
300         local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
301                 return { rooms = array.collect(each_room()):pluck("jid"):sort(); };
302         end, function(fields, errors)
303                 if errors then
304                         local errmsg = {};
305                         for field, err in pairs(errors) do
306                                 errmsg[#errmsg + 1] = field .. ": " .. err;
307                         end
308                         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
309                 end
310                 for _, room in ipairs(fields.rooms) do
311                         get_room_from_jid(room):destroy();
312                 end
313                 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
314         end);
315         local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
316
317         module:provides("adhoc", destroy_rooms_desc);
318 end