plugins/muc/mod_muc: Refactor to use new methods available
[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 local array = require "util.array";
10
11 if module:get_host_type() ~= "component" then
12         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
13 end
14
15 local muc_host = module:get_host();
16 local muc_name = module:get_option("name");
17 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
18 local restrict_room_creation = module:get_option("restrict_room_creation");
19 if restrict_room_creation then
20         if restrict_room_creation == true then
21                 restrict_room_creation = "admin";
22         elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
23                 restrict_room_creation = nil;
24         end
25 end
26 local lock_rooms = module:get_option_boolean("muc_room_locking", false);
27 local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
28
29 local muclib = module:require "muc";
30 local muc_new_room = muclib.new_room;
31 local jid_split = require "util.jid".split;
32 local jid_bare = require "util.jid".bare;
33 local st = require "util.stanza";
34 local um_is_admin = require "core.usermanager".is_admin;
35 local hosts = prosody.hosts;
36
37 rooms = {};
38 local rooms = rooms;
39 local persistent_rooms_storage = module:open_store("persistent");
40 local persistent_rooms = persistent_rooms_storage:get() or {};
41 local room_configs = module:open_store("config");
42
43 -- Configurable options
44 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
45
46 module:depends("disco");
47 module:add_identity("conference", "text", muc_name);
48 module:add_feature("http://jabber.org/protocol/muc");
49 module:depends "muc_unique"
50
51 local function is_admin(jid)
52         return um_is_admin(jid, module.host);
53 end
54
55 room_mt = muclib.room_mt; -- Yes, global.
56 local _set_affiliation = room_mt.set_affiliation;
57 local _get_affiliation = room_mt.get_affiliation;
58 function muclib.room_mt:get_affiliation(jid)
59         if is_admin(jid) then return "owner"; end
60         return _get_affiliation(self, jid);
61 end
62 function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
63         if is_admin(jid) then return nil, "modify", "not-acceptable"; end
64         return _set_affiliation(self, actor, jid, affiliation, callback, reason);
65 end
66
67 local function room_route_stanza(room, stanza) module:send(stanza); end
68 local function room_save(room, forced)
69         local node = jid_split(room.jid);
70         persistent_rooms[room.jid] = room._data.persistent;
71         if room._data.persistent then
72                 local history = room._data.history;
73                 room._data.history = nil;
74                 local data = {
75                         jid = room.jid;
76                         _data = room._data;
77                         _affiliations = room._affiliations;
78                 };
79                 room_configs:set(node, data);
80                 room._data.history = history;
81         elseif forced then
82                 room_configs:set(node, nil);
83                 if not next(room._occupants) then -- Room empty
84                         rooms[room.jid] = nil;
85                 end
86         end
87         if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
88 end
89
90 function create_room(jid)
91         local room = muc_new_room(jid);
92         room.route_stanza = room_route_stanza;
93         room.save = room_save;
94         rooms[jid] = room;
95         if lock_rooms then
96                 room.locked = true;
97                 if lock_room_timeout and lock_room_timeout > 0 then
98                         module:add_timer(lock_room_timeout, function ()
99                                 if room.locked then
100                                         room:destroy(); -- Not unlocked in time
101                                 end
102                         end);
103                 end
104         end
105         module:fire_event("muc-room-created", { room = room });
106         return room;
107 end
108
109 function forget_room(jid)
110         rooms[jid] = nil;
111 end
112
113 function get_room_from_jid(room_jid)
114         return rooms[room_jid]
115 end
116
117 local persistent_errors = false;
118 for jid in pairs(persistent_rooms) do
119         local node = jid_split(jid);
120         local data = room_configs:get(node);
121         if data then
122                 local room = create_room(jid);
123                 room._data = data._data;
124                 room._affiliations = data._affiliations;
125         else -- missing room data
126                 persistent_rooms[jid] = nil;
127                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
128                 persistent_errors = true;
129         end
130 end
131 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
132
133 local host_room = muc_new_room(muc_host);
134 host_room.route_stanza = room_route_stanza;
135 host_room.save = room_save;
136 rooms[muc_host] = host_room;
137
138 module:hook("host-disco-items", function(event)
139         local reply = event.reply;
140         module:log("debug", "host-disco-items called");
141         for jid, room in pairs(rooms) do
142                 if not room:get_hidden() then
143                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
144                 end
145         end
146 end);
147
148 module:hook("muc-room-destroyed",function(event)
149         local room = event.room
150         forget_room(room.jid)
151 end)
152
153 module:hook("muc-occupant-left",function(event)
154         local room = event.room
155         if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
156                 module:fire_event("muc-room-destroyed", { room = room });
157         end
158 end);
159
160 -- Watch presence to create rooms
161 local function attempt_room_creation(event)
162         local origin, stanza = event.origin, event.stanza;
163         local room_jid = jid_bare(stanza.attr.to);
164         if stanza.attr.type == nil and
165                 get_room_from_jid(room_jid) == nil and
166                 (
167                         not(restrict_room_creation) or
168                         is_admin(stanza.attr.from) or
169                         (
170                                 restrict_room_creation == "local" and
171                                 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")
172                         )
173                 ) then
174                 create_room(room_jid);
175         end
176 end
177 module:hook("presence/full", attempt_room_creation, -1)
178 module:hook("presence/bare", attempt_room_creation, -1)
179 module:hook("presence/host", attempt_room_creation, -1)
180
181 for event_name, method in pairs {
182         -- Normal room interactions
183         ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
184         ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
185         ["iq-set/bare/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_set_command" ;
186         ["iq-get/bare/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_get_command" ;
187         ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
188         ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
189         ["message/bare"] = "handle_message_to_room" ;
190         ["presence/bare"] = "handle_presence_to_room" ;
191         -- Host room
192         ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
193         ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
194         ["iq-set/host/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_set_command" ;
195         ["iq-get/host/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_get_command" ;
196         ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
197         ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
198         ["message/host"] = "handle_message_to_room" ;
199         ["presence/host"] = "handle_presence_to_room" ;
200         -- Direct to occupant (normal rooms and host room)
201         ["presence/full"] = "handle_presence_to_occupant" ;
202         ["iq/full"] = "handle_iq_to_occupant" ;
203         ["message/full"] = "handle_message_to_occupant" ;
204 } do
205         module:hook(event_name, function (event)
206                 local origin, stanza = event.origin, event.stanza;
207                 local room = get_room_from_jid(jid_bare(stanza.attr.to))
208                 if room == nil then
209                         origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
210                         return true;
211                 end
212                 return room[method](room, origin, stanza);
213         end, -2)
214 end
215
216
217 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
218         if stanza.attr.type == "result" or stanza.attr.type == "error" then
219                 module:send(stanza);
220         else error("component.send only supports result and error stanzas at the moment"); end
221 end
222
223 hosts[module:get_host()].muc = { rooms = rooms };
224
225 local saved = false;
226 module.save = function()
227         saved = true;
228         return {rooms = rooms};
229 end
230 module.restore = function(data)
231         for jid, oldroom in pairs(data.rooms or {}) do
232                 local room = create_room(jid);
233                 room._jid_nick = oldroom._jid_nick;
234                 room._occupants = oldroom._occupants;
235                 room._data = oldroom._data;
236                 room._affiliations = oldroom._affiliations;
237         end
238         hosts[module:get_host()].muc = { rooms = rooms };
239 end
240
241 function shutdown_room(room, stanza)
242         for nick, occupant in pairs(room._occupants) do
243                 stanza.attr.from = nick;
244                 for jid in pairs(occupant.sessions) do
245                         stanza.attr.to = jid;
246                         room:_route_stanza(stanza);
247                         room._jid_nick[jid] = nil;
248                 end
249                 room._occupants[nick] = nil;
250         end
251 end
252 function shutdown_component()
253         if not saved then
254                 local stanza = st.presence({type = "unavailable"})
255                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
256                                 :tag("item", { affiliation='none', role='none' }):up()
257                                 :tag("status", { code = "332"}):up();
258                 for roomjid, room in pairs(rooms) do
259                         shutdown_room(room, stanza);
260                 end
261                 shutdown_room(host_room, stanza);
262         end
263 end
264 module.unload = shutdown_component;
265 module:hook_global("server-stopping", shutdown_component);
266
267 -- Ad-hoc commands
268 module:depends("adhoc")
269 local t_concat = table.concat;
270 local keys = require "util.iterators".keys;
271 local adhoc_new = module:require "adhoc".new;
272 local adhoc_initial = require "util.adhoc".new_initial_data_form;
273 local dataforms_new = require "util.dataforms".new;
274
275 local destroy_rooms_layout = dataforms_new {
276         title = "Destroy rooms";
277         instructions = "Select the rooms to destroy";
278
279         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
280         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
281 };
282
283 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
284         return { rooms = array.collect(keys(rooms)):sort() };
285 end, function(fields, errors)
286         if errors then
287                 local errmsg = {};
288                 for name, err in pairs(errors) do
289                         errmsg[#errmsg + 1] = name .. ": " .. err;
290                 end
291                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
292         end
293         for _, room in ipairs(fields.rooms) do
294                 rooms[room]:destroy();
295                 rooms[room] = nil;
296         end
297         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
298 end);
299 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
300
301 module:provides("adhoc", destroy_rooms_desc);