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