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;
27 local muclib = module:require "muc";
28 local muc_new_room = muclib.new_room;
29 local jid_split = require "util.jid".split;
30 local jid_bare = require "util.jid".bare;
31 local st = require "util.stanza";
32 local um_is_admin = require "core.usermanager".is_admin;
33 local hosts = prosody.hosts;
37 local persistent_rooms_storage = module:open_store("persistent");
38 local persistent_rooms = persistent_rooms_storage:get() or {};
39 local room_configs = module:open_store("config");
41 -- Configurable options
42 muclib.set_max_history_length(module:get_option_number("max_history_messages"));
44 module:depends("disco");
45 module:add_identity("conference", "text", muc_name);
46 module:add_feature("http://jabber.org/protocol/muc");
47 module:depends "muc_unique"
48 module:require "muc/lock";
50 local function is_admin(jid)
51 return um_is_admin(jid, module.host);
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);
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_save(room, forced)
67 local node = jid_split(room.jid);
68 persistent_rooms[room.jid] = room._data.persistent;
69 if room._data.persistent then
70 local history = room._data.history;
71 room._data.history = nil;
75 _affiliations = room._affiliations;
77 room_configs:set(node, data);
78 room._data.history = history;
80 room_configs:set(node, nil);
81 if not next(room._occupants) then -- Room empty
82 rooms[room.jid] = nil;
85 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
88 function create_room(jid)
89 local room = muc_new_room(jid);
90 room.save = room_save;
92 module:fire_event("muc-room-created", { room = room });
96 function forget_room(jid)
100 function get_room_from_jid(room_jid)
101 return rooms[room_jid]
104 local persistent_errors = false;
105 for jid in pairs(persistent_rooms) do
106 local node = jid_split(jid);
107 local data = room_configs:get(node);
109 local room = create_room(jid);
110 room._data = data._data;
111 room._affiliations = data._affiliations;
112 else -- missing room data
113 persistent_rooms[jid] = nil;
114 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
115 persistent_errors = true;
118 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
120 local host_room = muc_new_room(muc_host);
121 host_room.save = room_save;
122 rooms[muc_host] = host_room;
124 module:hook("host-disco-items", function(event)
125 local reply = event.reply;
126 module:log("debug", "host-disco-items called");
127 for jid, room in pairs(rooms) do
128 if not room:get_hidden() then
129 reply:tag("item", {jid=jid, name=room:get_name()}):up();
134 module:hook("muc-room-destroyed",function(event)
135 local room = event.room
136 forget_room(room.jid)
139 module:hook("muc-occupant-left",function(event)
140 local room = event.room
141 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
142 module:fire_event("muc-room-destroyed", { room = room });
146 -- Watch presence to create rooms
147 local function attempt_room_creation(event)
148 local origin, stanza = event.origin, event.stanza;
149 local room_jid = jid_bare(stanza.attr.to);
150 if stanza.attr.type == nil and
151 get_room_from_jid(room_jid) == nil and
153 not(restrict_room_creation) or
154 is_admin(stanza.attr.from) or
156 restrict_room_creation == "local" and
157 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")
160 create_room(room_jid);
163 module:hook("presence/full", attempt_room_creation, -1)
164 module:hook("presence/bare", attempt_room_creation, -1)
165 module:hook("presence/host", attempt_room_creation, -1)
167 for event_name, method in pairs {
168 -- Normal room interactions
169 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
170 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
171 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
172 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
173 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
174 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
175 ["message/bare"] = "handle_message_to_room" ;
176 ["presence/bare"] = "handle_presence_to_room" ;
178 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
179 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
180 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
181 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
182 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
183 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
184 ["message/host"] = "handle_message_to_room" ;
185 ["presence/host"] = "handle_presence_to_room" ;
186 -- Direct to occupant (normal rooms and host room)
187 ["presence/full"] = "handle_presence_to_occupant" ;
188 ["iq/full"] = "handle_iq_to_occupant" ;
189 ["message/full"] = "handle_message_to_occupant" ;
191 module:hook(event_name, function (event)
192 local origin, stanza = event.origin, event.stanza;
193 local room = get_room_from_jid(jid_bare(stanza.attr.to))
195 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
198 return room[method](room, origin, stanza);
202 hosts[module:get_host()].muc = { rooms = rooms };
205 module.save = function()
207 return {rooms = rooms};
209 module.restore = function(data)
210 for jid, oldroom in pairs(data.rooms or {}) do
211 local room = create_room(jid);
212 room._jid_nick = oldroom._jid_nick;
213 room._occupants = oldroom._occupants;
214 room._data = oldroom._data;
215 room._affiliations = oldroom._affiliations;
217 hosts[module:get_host()].muc = { rooms = rooms };
220 function shutdown_component()
222 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
223 :tag("status", { code = "332"}):up();
224 for roomjid, room in pairs(rooms) do
230 module.unload = shutdown_component;
231 module:hook_global("server-stopping", shutdown_component);
234 module:depends("adhoc")
235 local t_concat = table.concat;
236 local keys = require "util.iterators".keys;
237 local adhoc_new = module:require "adhoc".new;
238 local adhoc_initial = require "util.adhoc".new_initial_data_form;
239 local dataforms_new = require "util.dataforms".new;
241 local destroy_rooms_layout = dataforms_new {
242 title = "Destroy rooms";
243 instructions = "Select the rooms to destroy";
245 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
246 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
249 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
250 return { rooms = array.collect(keys(rooms)):sort() };
251 end, function(fields, errors)
254 for name, err in pairs(errors) do
255 errmsg[#errmsg + 1] = name .. ": " .. err;
257 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
259 for _, room in ipairs(fields.rooms) do
260 rooms[room]:destroy();
263 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
265 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
267 module:provides("adhoc", destroy_rooms_desc);