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 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(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(jid)
102 local node = jid_split(room.jid);
103 room_configs:set(node, nil);
104 if persistent.get(room_jid) then
105 persistent_rooms:set(room_jid, nil);
109 function get_room_from_jid(room_jid)
110 local room = rooms[room_jid];
112 -- Check if in persistent storage
113 if persistent_rooms:get(room_jid) then
114 room = restore_room(room_jid);
116 module:log("error", "Missing data for room '%s', removing from persistent room list", room_jid);
117 persistent_rooms:set(room_jid, nil);
125 for room_jid in pairs(persistent_rooms_storage:get(nil) or {}) do
126 if rooms[room_jid] == nil then -- Don't restore rooms that already exist
127 local room = restore_room(room_jid);
129 module:log("error", "Missing data for room '%s', omitting from iteration", room_jid);
133 return iterators.values(rooms);
136 module:hook("host-disco-items", function(event)
137 local reply = event.reply;
138 module:log("debug", "host-disco-items called");
139 for room in each_room() do
140 if not room:get_hidden() then
141 reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
146 module:hook("muc-room-pre-create", function(event)
147 track_room(event.room);
150 module:hook("muc-room-destroyed",function(event)
151 local room = event.room
152 forget_room(room.jid)
156 local restrict_room_creation = module:get_option("restrict_room_creation");
157 if restrict_room_creation == true then
158 restrict_room_creation = "admin";
160 if restrict_room_creation then
161 local host_suffix = module.host:gsub("^[^%.]+%.", "");
162 module:hook("muc-room-pre-create", function(event)
163 local origin, stanza = event.origin, event.stanza;
164 local user_jid = stanza.attr.from;
165 if not is_admin(user_jid) and not (
166 restrict_room_creation == "local" and
167 select(2, jid_split(user_jid)) == host_suffix
169 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
176 for event_name, method in pairs {
177 -- Normal room interactions
178 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
179 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
180 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
181 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
182 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
183 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
184 ["message/bare"] = "handle_message_to_room" ;
185 ["presence/bare"] = "handle_presence_to_room" ;
187 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
188 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
189 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
190 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
191 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
192 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
193 ["message/host"] = "handle_message_to_room" ;
194 ["presence/host"] = "handle_presence_to_room" ;
195 -- Direct to occupant (normal rooms and host room)
196 ["presence/full"] = "handle_presence_to_occupant" ;
197 ["iq/full"] = "handle_iq_to_occupant" ;
198 ["message/full"] = "handle_message_to_occupant" ;
200 module:hook(event_name, function (event)
201 local origin, stanza = event.origin, event.stanza;
202 local room_jid = jid_bare(stanza.attr.to);
203 local room = get_room_from_jid(room_jid);
205 -- Watch presence to create rooms
206 if stanza.attr.type == nil and stanza.name == "presence" then
207 room = muclib.new_room(room_jid);
209 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
213 return room[method](room, origin, stanza);
217 function shutdown_component()
218 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
219 :tag("status", { code = "332"}):up();
220 for room in each_room() do
224 module:hook_global("server-stopping", shutdown_component);
226 do -- Ad-hoc commands
227 module:depends "adhoc";
228 local t_concat = table.concat;
229 local adhoc_new = module:require "adhoc".new;
230 local adhoc_initial = require "util.adhoc".new_initial_data_form;
231 local array = require "util.array";
232 local dataforms_new = require "util.dataforms".new;
234 local destroy_rooms_layout = dataforms_new {
235 title = "Destroy rooms";
236 instructions = "Select the rooms to destroy";
238 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
239 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
242 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
243 return { rooms = array.collect(each_room):pluck("jid"):sort(); };
244 end, function(fields, errors)
247 for name, err in pairs(errors) do
248 errmsg[#errmsg + 1] = name .. ": " .. err;
250 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
252 for _, room in ipairs(fields.rooms) do
253 get_room_from_jid(room):destroy();
255 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
257 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
259 module:provides("adhoc", destroy_rooms_desc);