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;
20 local hosts = prosody.hosts;
22 local rooms = module:shared "rooms";
24 module:depends("disco");
25 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
26 module:add_feature("http://jabber.org/protocol/muc");
27 module:depends "muc_unique"
28 module:require "muc/lock";
30 local function is_admin(jid)
31 return um_is_admin(jid, module.host);
34 do -- Monkey patch to make server admins room owners
35 local _get_affiliation = room_mt.get_affiliation;
36 function room_mt:get_affiliation(jid)
37 if is_admin(jid) then return "owner"; end
38 return _get_affiliation(self, jid);
41 local _set_affiliation = room_mt.set_affiliation;
42 function room_mt:set_affiliation(actor, jid, ...)
43 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
44 return _set_affiliation(self, actor, jid, ...);
48 function track_room(room)
49 rooms[room.jid] = room;
52 function forget_room(jid)
56 function get_room_from_jid(room_jid)
57 return rooms[room_jid]
61 return iterators.values(rooms);
64 do -- Persistent rooms
65 local persistent = module:require "muc/persistent";
66 local persistent_rooms_storage = module:open_store("persistent");
67 local persistent_rooms = persistent_rooms_storage:get() or {};
68 local room_configs = module:open_store("config");
70 local function room_save(room, forced)
71 local node = jid_split(room.jid);
72 local is_persistent = persistent.get(room);
73 persistent_rooms[room.jid] = is_persistent;
75 local history = room._data.history;
76 room._data.history = nil;
80 _affiliations = room._affiliations;
82 room_configs:set(node, data);
83 room._data.history = history;
85 room_configs:set(node, nil);
86 if not next(room._occupants) then -- Room empty
87 rooms[room.jid] = nil;
90 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
93 -- When room is created, over-ride 'save' method
94 module:hook("muc-room-pre-create", function(event)
95 event.room.save = room_save;
98 -- Automatically destroy empty non-persistent rooms
99 module:hook("muc-occupant-left",function(event)
100 local room = event.room
101 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
102 module:fire_event("muc-room-destroyed", { room = room });
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 = muclib.new_room(jid);
112 room._data = data._data;
113 room._affiliations = data._affiliations;
115 else -- missing room data
116 persistent_rooms[jid] = nil;
117 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
118 persistent_errors = true;
121 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
124 module:hook("host-disco-items", function(event)
125 local reply = event.reply;
126 module:log("debug", "host-disco-items called");
127 for room in each_room() do
128 if not room:get_hidden() then
129 reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
134 module:hook("muc-room-pre-create", function(event)
135 track_room(event.room);
138 module:hook("muc-room-destroyed",function(event)
139 local room = event.room
140 forget_room(room.jid)
144 local restrict_room_creation = module:get_option("restrict_room_creation");
145 if restrict_room_creation == true then
146 restrict_room_creation = "admin";
148 if restrict_room_creation then
149 local host_suffix = module.host:gsub("^[^%.]+%.", "");
150 module:hook("muc-room-pre-create", function(event)
151 local user_jid = event.stanza.attr.from;
152 if not is_admin(user_jid) and not (
153 restrict_room_creation == "local" and
154 select(2, jid_split(user_jid)) == host_suffix
156 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
163 for event_name, method in pairs {
164 -- Normal room interactions
165 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
166 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
167 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
168 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
169 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
170 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
171 ["message/bare"] = "handle_message_to_room" ;
172 ["presence/bare"] = "handle_presence_to_room" ;
174 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
175 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
176 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
177 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
178 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
179 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
180 ["message/host"] = "handle_message_to_room" ;
181 ["presence/host"] = "handle_presence_to_room" ;
182 -- Direct to occupant (normal rooms and host room)
183 ["presence/full"] = "handle_presence_to_occupant" ;
184 ["iq/full"] = "handle_iq_to_occupant" ;
185 ["message/full"] = "handle_message_to_occupant" ;
187 module:hook(event_name, function (event)
188 local origin, stanza = event.origin, event.stanza;
189 local room_jid = jid_bare(stanza.attr.to);
190 local room = get_room_from_jid(room_jid);
192 -- Watch presence to create rooms
193 if stanza.attr.type == nil and stanza.name == "presence" then
194 room = muclib.new_room(room_jid);
196 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
200 return room[method](room, origin, stanza);
204 function shutdown_component()
205 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
206 :tag("status", { code = "332"}):up();
207 for room in each_room() do
211 module:hook_global("server-stopping", shutdown_component);
213 do -- Ad-hoc commands
214 module:depends "adhoc";
215 local t_concat = table.concat;
216 local adhoc_new = module:require "adhoc".new;
217 local adhoc_initial = require "util.adhoc".new_initial_data_form;
218 local array = require "util.array";
219 local dataforms_new = require "util.dataforms".new;
221 local destroy_rooms_layout = dataforms_new {
222 title = "Destroy rooms";
223 instructions = "Select the rooms to destroy";
225 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
226 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
229 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
230 return { rooms = array.collect(each_room):pluck("jid"):sort(); };
231 end, function(fields, errors)
234 for name, err in pairs(errors) do
235 errmsg[#errmsg + 1] = name .. ": " .. err;
237 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
239 for _, room in ipairs(fields.rooms) do
240 get_room_from_jid(room):destroy();
242 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
244 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
246 module:provides("adhoc", destroy_rooms_desc);