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 if module:get_host_type() ~= "component" then
10 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
13 local muclib = module:require "muc";
14 room_mt = muclib.room_mt; -- Yes, global.
15 local iterators = require "util.iterators";
16 local jid_split = require "util.jid".split;
17 local jid_bare = require "util.jid".bare;
18 local st = require "util.stanza";
19 local um_is_admin = require "core.usermanager".is_admin;
21 local rooms = module:shared "rooms";
23 module:depends("disco");
24 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
25 module:add_feature("http://jabber.org/protocol/muc");
26 module:depends "muc_unique"
27 module:require "muc/lock";
29 local function is_admin(jid)
30 return um_is_admin(jid, module.host);
33 do -- Monkey patch to make server admins room owners
34 local _get_affiliation = room_mt.get_affiliation;
35 function room_mt:get_affiliation(jid)
36 if is_admin(jid) then return "owner"; end
37 return _get_affiliation(self, jid);
40 local _set_affiliation = room_mt.set_affiliation;
41 function room_mt:set_affiliation(actor, jid, ...)
42 if affiliation ~= "owner" and is_admin(jid) then return nil, "modify", "not-acceptable"; end
43 return _set_affiliation(self, actor, jid, ...);
47 local persistent = module:require "muc/persistent";
48 local persistent_rooms_storage = module:open_store("persistent");
49 local persistent_rooms = module:open_store("persistent", "map");
50 local room_configs = module:open_store("config");
52 local function room_save(room, forced)
53 local node = jid_split(room.jid);
54 local is_persistent = persistent.get(room);
55 persistent_rooms:set(nil, room.jid, is_persistent);
57 local history = room._data.history;
58 room._data.history = nil;
62 _affiliations = room._affiliations;
64 room_configs:set(node, data);
65 room._data.history = history;
67 room_configs:set(node, nil);
68 if not next(room._occupants) then -- Room empty
69 rooms[room.jid] = nil;
74 -- Automatically destroy empty non-persistent rooms
75 module:hook("muc-occupant-left",function(event)
76 local room = event.room
77 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
78 module:fire_event("muc-room-destroyed", { room = room });
82 function track_room(room)
83 rooms[room.jid] = room;
84 -- When room is created, over-ride 'save' method
85 room.save = room_save;
88 local function restore_room(jid)
89 local node = jid_split(jid);
90 local data = room_configs:get(node);
92 local room = muclib.new_room(jid);
93 room._data = data._data;
94 room._affiliations = data._affiliations;
100 function forget_room(room)
101 local room_jid = room.jid;
102 local node = jid_split(room.jid);
103 rooms[room_jid] = nil;
104 room_configs:set(node, nil);
105 if persistent.get(room) then
106 persistent_rooms:set(nil, room_jid, nil);
110 function get_room_from_jid(room_jid)
111 local room = rooms[room_jid];
113 -- Check if in persistent storage
114 if persistent_rooms:get(nil, room_jid) then
115 room = restore_room(room_jid);
117 module:log("error", "Missing data for room '%s', removing from persistent room list", room_jid);
118 persistent_rooms:set(nil, room_jid, nil);
125 function each_room(local_only)
126 if not local_only then
127 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
128 if rooms[room_jid] == nil then -- Don't restore rooms that already exist
129 local room = restore_room(room_jid);
131 module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
136 return iterators.values(rooms);
139 module:hook("host-disco-items", function(event)
140 local reply = event.reply;
141 module:log("debug", "host-disco-items called");
142 for room in each_room() do
143 if not room:get_hidden() then
144 reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
149 module:hook("muc-room-pre-create", function(event)
150 track_room(event.room);
153 module:hook("muc-room-destroyed",function(event)
154 return forget_room(event.room);
158 local restrict_room_creation = module:get_option("restrict_room_creation");
159 if restrict_room_creation == true then
160 restrict_room_creation = "admin";
162 if restrict_room_creation then
163 local host_suffix = module.host:gsub("^[^%.]+%.", "");
164 module:hook("muc-room-pre-create", function(event)
165 local origin, stanza = event.origin, event.stanza;
166 local user_jid = stanza.attr.from;
167 if not is_admin(user_jid) and not (
168 restrict_room_creation == "local" and
169 select(2, jid_split(user_jid)) == host_suffix
171 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
178 for event_name, method in pairs {
179 -- Normal room interactions
180 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
181 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
182 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
183 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
184 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
185 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
186 ["message/bare"] = "handle_message_to_room" ;
187 ["presence/bare"] = "handle_presence_to_room" ;
189 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
190 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
191 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
192 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
193 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
194 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
195 ["message/host"] = "handle_message_to_room" ;
196 ["presence/host"] = "handle_presence_to_room" ;
197 -- Direct to occupant (normal rooms and host room)
198 ["presence/full"] = "handle_presence_to_occupant" ;
199 ["iq/full"] = "handle_iq_to_occupant" ;
200 ["message/full"] = "handle_message_to_occupant" ;
202 module:hook(event_name, function (event)
203 local origin, stanza = event.origin, event.stanza;
204 local room_jid = jid_bare(stanza.attr.to);
205 local room = get_room_from_jid(room_jid);
207 -- Watch presence to create rooms
208 if stanza.attr.type == nil and stanza.name == "presence" then
209 room = muclib.new_room(room_jid);
211 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
215 return room[method](room, origin, stanza);
219 function shutdown_component()
220 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
221 :tag("status", { code = "332"}):up();
222 for room in each_room(true) do
226 module:hook_global("server-stopping", shutdown_component);
228 do -- Ad-hoc commands
229 module:depends "adhoc";
230 local t_concat = table.concat;
231 local adhoc_new = module:require "adhoc".new;
232 local adhoc_initial = require "util.adhoc".new_initial_data_form;
233 local array = require "util.array";
234 local dataforms_new = require "util.dataforms".new;
236 local destroy_rooms_layout = dataforms_new {
237 title = "Destroy rooms";
238 instructions = "Select the rooms to destroy";
240 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
241 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
244 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
245 return { rooms = array.collect(each_room()):pluck("jid"):sort(); };
246 end, function(fields, errors)
249 for name, err in pairs(errors) do
250 errmsg[#errmsg + 1] = name .. ": " .. err;
252 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
254 for _, room in ipairs(fields.rooms) do
255 get_room_from_jid(room):destroy();
257 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
259 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
261 module:provides("adhoc", destroy_rooms_desc);