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 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);
62 function muclib.room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
63 if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
64 return _set_affiliation(self, actor, jid, affiliation, callback, reason);
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;
77 _affiliations = room._affiliations;
79 room_configs:set(node, data);
80 room._data.history = history;
82 room_configs:set(node, nil);
83 if not next(room._occupants) then -- Room empty
84 rooms[room.jid] = nil;
87 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
90 function create_room(jid)
91 local room = muc_new_room(jid);
92 room.route_stanza = room_route_stanza;
93 room.save = room_save;
97 if lock_room_timeout and lock_room_timeout > 0 then
98 module:add_timer(lock_room_timeout, function ()
100 room:destroy(); -- Not unlocked in time
105 module:fire_event("muc-room-created", { room = room });
109 local persistent_errors = false;
110 for jid in pairs(persistent_rooms) do
111 local node = jid_split(jid);
112 local data = room_configs:get(node);
114 local room = create_room(jid);
115 room._data = data._data;
116 room._affiliations = data._affiliations;
117 else -- missing room data
118 persistent_rooms[jid] = nil;
119 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
120 persistent_errors = true;
123 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
125 local host_room = muc_new_room(muc_host);
126 host_room.route_stanza = room_route_stanza;
127 host_room.save = room_save;
129 module:hook("host-disco-items", function(event)
130 local reply = event.reply;
131 module:log("debug", "host-disco-items called");
132 for jid, room in pairs(rooms) do
133 if not room:get_hidden() then
134 reply:tag("item", {jid=jid, name=room:get_name()}):up();
139 local function handle_to_domain(event)
140 local origin, stanza = event.origin, event.stanza;
141 local type = stanza.attr.type;
142 if type == "error" or type == "result" then return; end
143 if stanza.name == "iq" and type == "get" then
144 local xmlns = stanza.tags[1].attr.xmlns;
145 local node = stanza.tags[1].attr.node;
146 if xmlns == "http://jabber.org/protocol/muc#unique" then
147 origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
149 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
152 host_room:handle_stanza(origin, stanza);
153 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
158 function stanza_handler(event)
159 local origin, stanza = event.origin, event.stanza;
160 local bare = jid_bare(stanza.attr.to);
161 local room = rooms[bare];
163 if stanza.name ~= "presence" then
164 origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
167 if not(restrict_room_creation) or
168 is_admin(stanza.attr.from) or
169 (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
170 room = create_room(bare);
174 room:handle_stanza(origin, stanza);
175 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
176 module:fire_event("muc-room-destroyed", { room = room });
177 rooms[bare] = nil; -- discard room
180 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
184 module:hook("iq/bare", stanza_handler, -1);
185 module:hook("message/bare", stanza_handler, -1);
186 module:hook("presence/bare", stanza_handler, -1);
187 module:hook("iq/full", stanza_handler, -1);
188 module:hook("message/full", stanza_handler, -1);
189 module:hook("presence/full", stanza_handler, -1);
190 module:hook("iq/host", handle_to_domain, -1);
191 module:hook("message/host", handle_to_domain, -1);
192 module:hook("presence/host", handle_to_domain, -1);
194 hosts[module.host].send = function(stanza) -- FIXME do a generic fix
195 if stanza.attr.type == "result" or stanza.attr.type == "error" then
197 else error("component.send only supports result and error stanzas at the moment"); end
200 hosts[module:get_host()].muc = { rooms = rooms };
203 module.save = function()
205 return {rooms = rooms};
207 module.restore = function(data)
208 for jid, oldroom in pairs(data.rooms or {}) do
209 local room = create_room(jid);
210 room._jid_nick = oldroom._jid_nick;
211 room._occupants = oldroom._occupants;
212 room._data = oldroom._data;
213 room._affiliations = oldroom._affiliations;
215 hosts[module:get_host()].muc = { rooms = rooms };
218 function shutdown_room(room, stanza)
219 for nick, occupant in pairs(room._occupants) do
220 stanza.attr.from = nick;
221 for jid in pairs(occupant.sessions) do
222 stanza.attr.to = jid;
223 room:_route_stanza(stanza);
224 room._jid_nick[jid] = nil;
226 room._occupants[nick] = nil;
229 function shutdown_component()
231 local stanza = st.presence({type = "unavailable"})
232 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
233 :tag("item", { affiliation='none', role='none' }):up()
234 :tag("status", { code = "332"}):up();
235 for roomjid, room in pairs(rooms) do
236 shutdown_room(room, stanza);
238 shutdown_room(host_room, stanza);
241 module.unload = shutdown_component;
242 module:hook_global("server-stopping", shutdown_component);
245 module:depends("adhoc")
246 local t_concat = table.concat;
247 local keys = require "util.iterators".keys;
248 local adhoc_new = module:require "adhoc".new;
249 local adhoc_initial = require "util.adhoc".new_initial_data_form;
250 local dataforms_new = require "util.dataforms".new;
252 local destroy_rooms_layout = dataforms_new {
253 title = "Destroy rooms";
254 instructions = "Select the rooms to destroy";
256 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
257 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
260 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
261 return { rooms = array.collect(keys(rooms)):sort() };
262 end, function(fields, errors)
265 for name, err in pairs(errors) do
266 errmsg[#errmsg + 1] = name .. ": " .. err;
268 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
270 for _, room in ipairs(fields.rooms) do
271 rooms[room]:destroy();
274 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
276 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
278 module:provides("adhoc", destroy_rooms_desc);