2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local array = require "util.array";
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);
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;
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);
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;
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");
44 -- Configurable options
45 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
47 module:depends("disco");
48 module:add_identity("conference", "text", muc_name);
49 module:add_feature("http://jabber.org/protocol/muc");
51 local function is_admin(jid)
52 return um_is_admin(jid, module.host);
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);
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);
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;
76 _affiliations = room._affiliations;
78 room_configs:set(node, data);
79 room._data.history = history;
81 room_configs:set(node, nil);
82 if not next(room._occupants) then -- Room empty
83 rooms[room.jid] = nil;
86 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
89 function create_room(jid)
90 local room = muc_new_room(jid);
91 room.route_stanza = room_route_stanza;
92 room.save = room_save;
96 if lock_room_timeout and lock_room_timeout > 0 then
97 module:add_timer(lock_room_timeout, function ()
99 room:destroy(); -- Not unlocked in time
104 module:fire_event("muc-room-created", { room = room });
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);
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;
122 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
124 local host_room = muc_new_room(muc_host);
125 host_room.route_stanza = room_route_stanza;
126 host_room.save = room_save;
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();
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
148 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
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"));
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];
162 if stanza.name ~= "presence" then
163 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
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);
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
178 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
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);
192 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
193 if stanza.attr.type == "result" or stanza.attr.type == "error" then
195 else error("component.send only supports result and error stanzas at the moment"); end
198 hosts[module:get_host()].muc = { rooms = rooms };
201 module.save = function()
203 return {rooms = rooms};
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;
213 hosts[module:get_host()].muc = { rooms = rooms };
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;
224 room._occupants[nick] = nil;
227 function shutdown_component()
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);
236 shutdown_room(host_room, stanza);
239 module.unload = shutdown_component;
240 module:hook_global("server-stopping", shutdown_component);
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;
250 local destroy_rooms_layout = dataforms_new {
251 title = "Destroy rooms";
252 instructions = "Select the rooms to destroy";
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:"};
258 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
259 return { rooms = array.collect(keys(rooms)):sort() };
260 end, function(fields, errors)
263 for name, err in pairs(errors) do
264 errmsg[#errmsg + 1] = name .. ": " .. err;
266 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
268 for _, room in ipairs(fields.rooms) do
269 rooms[room]:destroy();
272 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
274 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
276 module:provides("adhoc", destroy_rooms_desc);