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";
25 module:depends("disco");
26 module:add_identity("conference", "text", module:get_option_string("name", "Prosody Chatrooms"));
27 module:add_feature("http://jabber.org/protocol/muc");
28 module:depends "muc_unique"
29 module:require "muc/lock";
31 local function is_admin(jid)
32 return um_is_admin(jid, module.host);
35 do -- Monkey patch to make server admins room owners
36 local _get_affiliation = room_mt.get_affiliation;
37 function room_mt:get_affiliation(jid)
38 if is_admin(jid) then return "owner"; end
39 return _get_affiliation(self, jid);
42 local _set_affiliation = room_mt.set_affiliation;
43 function room_mt:set_affiliation(actor, jid, ...)
44 if is_admin(jid) then return nil, "modify", "not-acceptable"; end
45 return _set_affiliation(self, actor, jid, ...);
49 function track_room(room)
50 rooms[room.jid] = room;
53 function forget_room(jid)
57 function get_room_from_jid(room_jid)
58 return rooms[room_jid]
62 return iterators.values(rooms);
65 do -- Persistent rooms
66 local persistent = module:require "muc/persistent";
67 local persistent_rooms_storage = module:open_store("persistent");
68 local persistent_rooms = persistent_rooms_storage:get() or {};
69 local room_configs = module:open_store("config");
71 local function room_save(room, forced)
72 local node = jid_split(room.jid);
73 local is_persistent = persistent.get(room);
74 persistent_rooms[room.jid] = is_persistent;
76 local history = room._data.history;
77 room._data.history = nil;
81 _affiliations = room._affiliations;
83 room_configs:set(node, data);
84 room._data.history = history;
86 room_configs:set(node, nil);
87 if not next(room._occupants) then -- Room empty
88 rooms[room.jid] = nil;
91 if forced then persistent_rooms_storage:set(nil, persistent_rooms); end
94 -- When room is created, over-ride 'save' method
95 module:hook("muc-occupant-pre-create", function(event)
96 event.room.save = room_save;
99 -- Automatically destroy empty non-persistent rooms
100 module:hook("muc-occupant-left",function(event)
101 local room = event.room
102 if not room:has_occupant() and not persistent.get(room) then -- empty, non-persistent room
103 module:fire_event("muc-room-destroyed", { room = room });
107 local persistent_errors = false;
108 for jid in pairs(persistent_rooms) do
109 local node = jid_split(jid);
110 local data = room_configs:get(node);
112 local room = muclib.new_room(jid);
113 room._data = data._data;
114 room._affiliations = data._affiliations;
116 else -- missing room data
117 persistent_rooms[jid] = nil;
118 module:log("error", "Missing data for room '%s', removing from persistent room list", jid);
119 persistent_errors = true;
122 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end
125 module:hook("host-disco-items", function(event)
126 local reply = event.reply;
127 module:log("debug", "host-disco-items called");
128 for room in each_room() do
129 if not room:get_hidden() then
130 reply:tag("item", {jid=room.jid, name=room:get_name()}):up();
135 module:hook("muc-room-pre-create", function(event)
136 track_room(event.room);
139 module:hook("muc-room-destroyed",function(event)
140 local room = event.room
141 forget_room(room.jid)
145 local restrict_room_creation = module:get_option("restrict_room_creation");
146 if restrict_room_creation == true then
147 restrict_room_creation = "admin";
149 if restrict_room_creation then
150 local host_suffix = module.host:gsub("^[^%.]+%.", "");
151 module:hook("muc-room-pre-create", function(event)
152 local user_jid = event.stanza.attr.from;
153 if not is_admin(user_jid) and not (
154 restrict_room_creation == "local" and
155 select(2, jid_split(user_jid)) == host_suffix
157 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
164 for event_name, method in pairs {
165 -- Normal room interactions
166 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
167 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
168 ["iq-set/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
169 ["iq-get/bare/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
170 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
171 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
172 ["message/bare"] = "handle_message_to_room" ;
173 ["presence/bare"] = "handle_presence_to_room" ;
175 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ;
176 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ;
177 ["iq-set/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_set_command" ;
178 ["iq-get/host/http://jabber.org/protocol/muc#admin:query"] = "handle_admin_query_get_command" ;
179 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ;
180 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ;
181 ["message/host"] = "handle_message_to_room" ;
182 ["presence/host"] = "handle_presence_to_room" ;
183 -- Direct to occupant (normal rooms and host room)
184 ["presence/full"] = "handle_presence_to_occupant" ;
185 ["iq/full"] = "handle_iq_to_occupant" ;
186 ["message/full"] = "handle_message_to_occupant" ;
188 module:hook(event_name, function (event)
189 local origin, stanza = event.origin, event.stanza;
190 local room_jid = jid_bare(stanza.attr.to);
191 local room = get_room_from_jid(room_jid);
193 -- Watch presence to create rooms
194 if stanza.attr.type == nil and stanza.name == "presence" then
195 room = muclib.new_room(room_jid);
197 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
201 return room[method](room, origin, stanza);
205 function shutdown_component()
206 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
207 :tag("status", { code = "332"}):up();
208 for room in each_room() do
212 module:hook_global("server-stopping", shutdown_component);
214 do -- Ad-hoc commands
215 module:depends "adhoc";
216 local t_concat = table.concat;
217 local adhoc_new = module:require "adhoc".new;
218 local adhoc_initial = require "util.adhoc".new_initial_data_form;
219 local array = require "util.array";
220 local dataforms_new = require "util.dataforms".new;
222 local destroy_rooms_layout = dataforms_new {
223 title = "Destroy rooms";
224 instructions = "Select the rooms to destroy";
226 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/muc#destroy" };
227 { name = "rooms", type = "list-multi", required = true, label = "Rooms to destroy:"};
230 local destroy_rooms_handler = adhoc_initial(destroy_rooms_layout, function()
231 return { rooms = array.collect(each_room):pluck("jid"):sort(); };
232 end, function(fields, errors)
235 for name, err in pairs(errors) do
236 errmsg[#errmsg + 1] = name .. ": " .. err;
238 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
240 for _, room in ipairs(fields.rooms) do
241 get_room_from_jid(room):destroy();
243 return { status = "completed", info = "The following rooms were destroyed:\n"..t_concat(fields.rooms, "\n") };
245 local destroy_rooms_desc = adhoc_new("Destroy Rooms", "http://prosody.im/protocol/muc#destroy", destroy_rooms_handler, "admin");
247 module:provides("adhoc", destroy_rooms_desc);