Merge 0.9->0.10
[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 uuid_gen = require "util.uuid".generate;
35 local um_is_admin = require "core.usermanager".is_admin;
36 local hosts = prosody.hosts;
37
38 rooms = {};
39 local rooms = rooms;
40 local persistent_rooms_storage = module:open_store("persistent");
41 local persistent_rooms = persistent_rooms_storage:get() or {};
42 local room_configs = module:open_store("config");
43
44 -- Configurable options
45 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
46
47 module:depends("disco");
48 module:add_identity("conference", "text", muc_name);
49 module:add_feature("http://jabber.org/protocol/muc");
50
51 local function is_admin(jid)
52         return um_is_admin(jid, module.host);
53 end
54
55 local _set_affiliation = muc_new_room.room_mt.set_affiliation;
56 local _get_affiliation = muc_new_room.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 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                   (restrict_room_creation == "admin" and 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                         rooms[bare] = nil; -- discard room
176                 end
177         else
178                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
179         end
180         return true;
181 end
182 module:hook("iq/bare", stanza_handler, -1);
183 module:hook("message/bare", stanza_handler, -1);
184 module:hook("presence/bare", stanza_handler, -1);
185 module:hook("iq/full", stanza_handler, -1);
186 module:hook("message/full", stanza_handler, -1);
187 module:hook("presence/full", stanza_handler, -1);
188 module:hook("iq/host", handle_to_domain, -1);
189 module:hook("message/host", handle_to_domain, -1);
190 module:hook("presence/host", handle_to_domain, -1);
191
192 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
193         if stanza.attr.type == "result" or stanza.attr.type == "error" then
194                 module:send(stanza);
195         else error("component.send only supports result and error stanzas at the moment"); end
196 end
197
198 hosts[module:get_host()].muc = { rooms = rooms };
199
200 local saved = false;
201 module.save = function()
202         saved = true;
203         return {rooms = rooms};
204 end
205 module.restore = function(data)
206         for jid, oldroom in pairs(data.rooms or {}) do
207                 local room = create_room(jid);
208                 room._jid_nick = oldroom._jid_nick;
209                 room._occupants = oldroom._occupants;
210                 room._data = oldroom._data;
211                 room._affiliations = oldroom._affiliations;
212         end
213         hosts[module:get_host()].muc = { rooms = rooms };
214 end
215
216 function shutdown_room(room, stanza)
217         for nick, occupant in pairs(room._occupants) do
218                 stanza.attr.from = nick;
219                 for jid in pairs(occupant.sessions) do
220                         stanza.attr.to = jid;
221                         room:_route_stanza(stanza);
222                         room._jid_nick[jid] = nil;
223                 end
224                 room._occupants[nick] = nil;
225         end
226 end
227 function shutdown_component()
228         if not saved then
229                 local stanza = st.presence({type = "unavailable"})
230                         :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
231                                 :tag("item", { affiliation='none', role='none' }):up()
232                                 :tag("status", { code = "332"}):up();
233                 for roomjid, room in pairs(rooms) do
234                         shutdown_room(room, stanza);
235                 end
236                 shutdown_room(host_room, stanza);
237         end
238 end
239 module.unload = shutdown_component;
240 module:hook_global("server-stopping", shutdown_component);
241
242 -- Ad-hoc commands
243 module:depends("adhoc")
244 local t_concat = table.concat;
245 local keys = require "util.iterators".keys;
246 local adhoc_new = module:require "adhoc".new;
247 local adhoc_initial = require "util.adhoc".new_initial_data_form;
248 local dataforms_new = require "util.dataforms".new;
249
250 local destroy_rooms_layout = dataforms_new {
251         title = "Destroy rooms";
252         instructions = "Select the rooms to destroy";
253
254         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
255         { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
256 };
257
258 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
259         return { rooms = array.collect(keys(rooms)):sort() };
260 end, function(fields, errors)
261         if errors then
262                 local errmsg = {};
263                 for name, err in pairs(errors) do
264                         errmsg[#errmsg + 1] = name .. ": " .. err;
265                 end
266                 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
267         end
268         for _, room in ipairs(fields.rooms) do
269                 rooms[room]:destroy();
270                 rooms[room] = nil;
271         end
272         return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
273 end);
274 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
275
276 module:provides("adhoc", destroy_rooms_desc);