util.cache: Add support for creating a proxy table to a cache, that looks and acts...
[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_string("name", "Prosody Chatrooms");
17 local restrict_room_creation = module:get_option("restrict_room_creation");
18 if restrict_room_creation then
19         if restrict_room_creation == true then
20                 restrict_room_creation = "admin";
21         elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
22                 restrict_room_creation = nil;
23         end
24 end
25 local lock_rooms = module:get_option_boolean("muc_room_locking", false);
26 local lock_room_timeout = module:get_option_number("muc_room_lock_timeout", 300);
27
28 local muclib = module:require "muc";
29 local muc_new_room = muclib.new_room;
30 local jid_split = require "util.jid".split;
31 local jid_bare = require "util.jid".bare;
32 local st = require "util.stanza";
33 local uuid_gen = require "util.uuid".generate;
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
50 local function is_admin(jid)
51         return um_is_admin(jid, module.host);
52 end
53
54 room_mt = muclib.room_mt; -- Yes, global.
55 local _set_affiliation = room_mt.set_affiliation;
56 local _get_affiliation = room_mt.get_affiliation;
57 function muclib.room_mt:get_affiliation(jid)
58         if is_admin(jid) then return "owner"; end
59         return _get_affiliation(self, jid);
60 end
61 function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
62         if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
63         return _set_affiliation(self, actor, jid, affiliation, callback, reason);
64 end
65
66 local function room_route_stanza(room, stanza) module:send(stanza); end
67 local function room_save(room, forced)
68         local node = jid_split(room.jid);
69         persistent_rooms[room.jid] = room._data.persistent;
70         if room._data.persistent then
71                 local history = room._data.history;
72                 room._data.history = nil;
73                 local data = {
74                         jid = room.jid;
75                         _data = room._data;
76                         _affiliations = room._affiliations;
77                 };
78                 room_configs:set(node, data);
79                 room._data.history = history;
80         elseif forced then
81                 room_configs:set(node, nil);
82                 if not next(room._occupants) then -- Room empty
83                         rooms[room.jid] = nil;
84                 end
85         end
86         if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
87 end
88
89 function create_room(jid)
90         local room = muc_new_room(jid);
91         room.route_stanza = room_route_stanza;
92         room.save = room_save;
93         rooms[jid] = room;
94         if lock_rooms then
95                 room.locked = true;
96                 if lock_room_timeout and lock_room_timeout > 0 then
97                         module:add_timer(lock_room_timeout, function ()
98                                 if room.locked then
99                                         room:destroy(); -- Not unlocked in time
100                                 end
101                         end);
102                 end
103         end
104         module:fire_event("muc-room-created", { room = room });
105         return room;
106 end
107
108 local persistent_errors = false;
109 for jid in pairs(persistent_rooms) do
110         local node = jid_split(jid);
111         local data = room_configs:get(node);
112         if data then
113                 local room = create_room(jid);
114                 room._data = data._data;
115                 room._affiliations = data._affiliations;
116         else -- missing room data
117                 persistent_rooms[jid] = nil;
118                 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
119                 persistent_errors = true;
120         end
121 end
122 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
123
124 local host_room = muc_new_room(muc_host);
125 host_room.route_stanza = room_route_stanza;
126 host_room.save = room_save;
127
128 module:hook("host-disco-items", function(event)
129         local reply = event.reply;
130         module:log("debug", "host-disco-items called");
131         for jid, room in pairs(rooms) do
132                 if not room:get_hidden() then
133                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
134                 end
135         end
136 end);
137
138 local function handle_to_domain(event)
139         local origin, stanza = event.origin, event.stanza;
140         local type = stanza.attr.type;
141         if type == "error" or type == "result" then return; end
142         if stanza.name == "iq" and type == "get" then
143                 local xmlns = stanza.tags[1].attr.xmlns;
144                 local node = stanza.tags[1].attr.node;
145                 if xmlns == "http://jabber.org/protocol/muc#unique" then
146                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
147                 else
148                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
149                 end
150         else
151                 host_room:handle_stanza(origin, stanza);
152                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
153         end
154         return true;
155 end
156
157 function stanza_handler(event)
158         local origin, stanza = event.origin, event.stanza;
159         local bare = jid_bare(stanza.attr.to);
160         local room = rooms[bare];
161         if not room then
162                 if stanza.name ~= "presence" then
163                         origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
164                         return true;
165                 end
166                 if not(restrict_room_creation) or
167                   is_admin(stanza.attr.from) or
168                   (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
169                         room = create_room(bare);
170                 end
171         end
172         if room then
173                 room:handle_stanza(origin, stanza);
174                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
175                         module:fire_event("muc-room-destroyed", { room = room });
176                         rooms[bare] = nil; -- discard room
177                 end
178         else
179                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
180         end
181         return true;
182 end
183 module:hook("iq/bare", stanza_handler, -1);
184 module:hook("message/bare", stanza_handler, -1);
185 module:hook("presence/bare", stanza_handler, -1);
186 module:hook("iq/full", stanza_handler, -1);
187 module:hook("message/full", stanza_handler, -1);
188 module:hook("presence/full", stanza_handler, -1);
189 module:hook("iq/host", handle_to_domain, -1);
190 module:hook("message/host", handle_to_domain, -1);
191 module:hook("presence/host", handle_to_domain, -1);
192
193 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
194         if stanza.attr.type == "result" or stanza.attr.type == "error" then
195                 module:send(stanza);
196         else error("component.send only supports result and error stanzas at the moment"); end
197 end
198
199 hosts[module:get_host()].muc = { rooms = rooms };
200
201 local saved = false;
202 module.save = function()
203         saved = true;
204         return {rooms = rooms};
205 end
206 module.restore = function(data)
207         for jid, oldroom in pairs(data.rooms or {}) do
208                 local room = create_room(jid);
209                 room._jid_nick = oldroom._jid_nick;
210                 room._occupants = oldroom._occupants;
211                 room._data = oldroom._data;
212                 room._affiliations = oldroom._affiliations;
213         end
214         hosts[module:get_host()].muc = { rooms = rooms };
215 end
216
217 function shutdown_room(room, stanza)
218         for nick, occupant in pairs(room._occupants) do
219                 stanza.attr.from = nick;
220                 for jid in pairs(occupant.sessions) do
221                         stanza.attr.to = jid;
222                         room:_route_stanza(stanza);
223                         room._jid_nick[jid] = nil;
224                 end
225                 room._occupants[nick] = nil;
226         end
227 end
228 function shutdown_component()
229         if not saved then
230                 local stanza = st.presence({type = "unavailable"})
231                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
232                                 :tag("item", { affiliation='none', role='none' }):up()
233                                 :tag("status", { code = "332"}):up();
234                 for roomjid, room in pairs(rooms) do
235                         shutdown_room(room, stanza);
236                 end
237                 shutdown_room(host_room, stanza);
238         end
239 end
240 module.unload = shutdown_component;
241 module:hook_global("server-stopping", shutdown_component);
242
243 -- Ad-hoc commands
244 module:depends("adhoc")
245 local t_concat = table.concat;
246 local keys = require "util.iterators".keys;
247 local adhoc_new = module:require "adhoc".new;
248 local adhoc_initial = require "util.adhoc".new_initial_data_form;
249 local dataforms_new = require "util.dataforms".new;
250
251 local destroy_rooms_layout = dataforms_new {
252         title = "Destroy rooms";
253         instructions = "Select the rooms to destroy";
254
255         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
256         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
257 };
258
259 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
260         return { rooms = array.collect(keys(rooms)):sort() };
261 end, function(fields, errors)
262         if errors then
263                 local errmsg = {};
264                 for name, err in pairs(errors) do
265                         errmsg[#errmsg + 1] = name .. ": " .. err;
266                 end
267                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
268         end
269         for _, room in ipairs(fields.rooms) do
270                 rooms[room]:destroy();
271                 rooms[room] = nil;
272         end
273         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
274 end);
275 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
276
277 module:provides("adhoc", destroy_rooms_desc);