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 restrict_room_creation = module:get_option("restrict_room_creation");
16 if restrict_room_creation then
17 if restrict_room_creation == true then
18 restrict_room_creation = "admin";
19 elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
20 restrict_room_creation = nil;
24 local muclib = module:require "muc";
25 local muc_new_room = muclib.new_room;
26 room_mt = muclib.room_mt; -- Yes, global.
27 local persistent = module:require "muc/persistent";
28 local jid_split = require "util.jid".split;
29 local jid_bare = require "util.jid".bare;
30 local st = require "util.stanza";
31 local um_is_admin = require "core.usermanager".is_admin;
32 local hosts = prosody.hosts;
36 local persistent_rooms_storage = module:open_store("persistent");
37 local persistent_rooms = persistent_rooms_storage:get() or {};
38 local room_configs = module:open_store("config");
40 -- Configurable options
41 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
43 module:depends("disco");
44 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
45 module:add_feature("http://jabber.org/protocol/muc");
46 module:depends "muc_unique"
47 module:require "muc/lock";
49 local function is_admin(jid)
50 return um_is_admin(jid, module.host);
53 do -- Monkey patch to make server admins room owners
54 local _get_affiliation = room_mt.get_affiliation;
55 function room_mt:get_affiliation(jid)
56 if is_admin(jid) then return "owner"; end
57 return _get_affiliation(self, jid);
60 local _set_affiliation = room_mt.set_affiliation;
61 function room_mt:set_affiliation(actor, jid, ...)
62 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
63 return _set_affiliation(self, actor, jid, ...);
67 local function room_save(room, forced)
68 local node = jid_split(room.jid);
69 local is_persistent = persistent.get(room);
70 persistent_rooms[room.jid] = is_persistent;
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.save = room_save;
94 module:fire_event("muc-room-created", { room = room });
98 function forget_room(jid)
102 function get_room_from_jid(room_jid)
103 return rooms[room_jid]
106 local persistent_errors = false;
107 for jid in pairs(persistent_rooms) do
108 local node = jid_split(jid);
109 local data = room_configs:get(node);
111 local room = create_room(jid);
112 room._data = data._data;
113 room._affiliations = data._affiliations;
114 else -- missing room data
115 persistent_rooms[jid] = nil;
116 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
117 persistent_errors = true;
120 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
122 module:hook("host-disco-items", function(event)
123 local reply = event.reply;
124 module:log("debug", "host-disco-items called");
125 for jid, room in pairs(rooms) do
126 if not room:get_hidden() then
127 reply:tag("item", {jid=jid, name=room:get_name()}):up();
132 module:hook("muc-room-destroyed",function(event)
133 local room = event.room
134 forget_room(room.jid)
137 module:hook("muc-occupant-left",function(event)
138 local room = event.room
139 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
140 module:fire_event("muc-room-destroyed", { room = room });
144 -- Watch presence to create rooms
145 local function attempt_room_creation(event)
146 local origin, stanza = event.origin, event.stanza;
147 local room_jid = jid_bare(stanza.attr.to);
148 if stanza.attr.type == nil and
149 get_room_from_jid(room_jid) == nil and
151 not(restrict_room_creation) or
152 is_admin(stanza.attr.from) or
154 restrict_room_creation == "local" and
155 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")
158 create_room(room_jid);
161 module:hook("presence/full", attempt_room_creation, -1)
162 module:hook("presence/bare", attempt_room_creation, -1)
163 module:hook("presence/host", attempt_room_creation, -1)
165 for event_name, method in pairs {
166 -- Normal room interactions
167 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
168 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
169 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
170 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
171 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
172 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
173 ["message/bare"] = "handle_message_to_room" ;
174 ["presence/bare"] = "handle_presence_to_room" ;
176 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
177 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
178 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
179 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
180 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
181 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
182 ["message/host"] = "handle_message_to_room" ;
183 ["presence/host"] = "handle_presence_to_room" ;
184 -- Direct to occupant (normal rooms and host room)
185 ["presence/full"] = "handle_presence_to_occupant" ;
186 ["iq/full"] = "handle_iq_to_occupant" ;
187 ["message/full"] = "handle_message_to_occupant" ;
189 module:hook(event_name, function (event)
190 local origin, stanza = event.origin, event.stanza;
191 local room = get_room_from_jid(jid_bare(stanza.attr.to))
193 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
196 return room[method](room, origin, stanza);
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_component()
220 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
221 :tag("status", { code = "332"}):up();
222 for roomjid, room in pairs(rooms) do
227 module.unload = shutdown_component;
228 module:hook_global("server-stopping", shutdown_component);
231 module:depends("adhoc")
232 local t_concat = table.concat;
233 local keys = require "util.iterators".keys;
234 local adhoc_new = module:require "adhoc".new;
235 local adhoc_initial = require "util.adhoc".new_initial_data_form;
236 local dataforms_new = require "util.dataforms".new;
238 local destroy_rooms_layout = dataforms_new {
239 title = "Destroy rooms";
240 instructions = "Select the rooms to destroy";
242 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
243 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
246 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
247 return { rooms = array.collect(keys(rooms)):sort() };
248 end, function(fields, errors)
251 for name, err in pairs(errors) do
252 errmsg[#errmsg + 1] = name .. ": " .. err;
254 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
256 for _, room in ipairs(fields.rooms) do
257 rooms[room]:destroy();
260 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
262 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
264 module:provides("adhoc", destroy_rooms_desc);