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.
10 if module:get_host_type() ~= "component" then
11 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
14 local muc_host = module:get_host();
15 local muc_name = module:get_option("name");
16 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
17 local restrict_room_creation = module:get_option("restrict_room_creation");
18 if restrict_room_creation then
19 if restrict_room_creation == true then
20 restrict_room_creation = "admin";
21 elseif restrict_room_creation ~= "admin" and restrict_room_creation ~= "local" then
22 restrict_room_creation = nil;
25 local muc_new_room = module:require "muc".new_room;
26 local register_component = require "core.componentmanager".register_component;
27 local deregister_component = require "core.componentmanager".deregister_component;
28 local jid_split = require "util.jid".split;
29 local jid_bare = require "util.jid".bare;
30 local st = require "util.stanza";
31 local uuid_gen = require "util.uuid".generate;
32 local datamanager = require "util.datamanager";
33 local um_is_admin = require "core.usermanager".is_admin;
37 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
40 -- Configurable options
41 local max_history_messages = module:get_option_number("max_history_messages");
43 local function is_admin(jid)
44 return um_is_admin(jid, module.host);
47 local function room_route_stanza(room, stanza) core_post_stanza(component, stanza); end
48 local function room_save(room, forced)
49 local node = jid_split(room.jid);
50 persistent_rooms[room.jid] = room._data.persistent;
51 if room._data.persistent then
52 local history = room._data.history;
53 room._data.history = nil;
57 _affiliations = room._affiliations;
59 datamanager.store(node, muc_host, "config", data);
60 room._data.history = history;
62 datamanager.store(node, muc_host, "config", nil);
64 if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
67 for jid in pairs(persistent_rooms) do
68 local node = jid_split(jid);
69 local data = datamanager.load(node, muc_host, "config") or {};
70 local room = muc_new_room(jid, {
71 history_length = max_history_messages;
73 room._data = data._data;
74 room._data.history_length = max_history_messages; --TODO: Need to allow per-room with a global limit
75 room._affiliations = data._affiliations;
76 room.route_stanza = room_route_stanza;
77 room.save = room_save;
81 local host_room = muc_new_room(muc_host, {
82 history_length = max_history_messages;
84 host_room.route_stanza = room_route_stanza;
85 host_room.save = room_save;
87 local function get_disco_info(stanza)
88 return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
89 :tag("identity", {category='conference', type='text', name=muc_name}):up()
90 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
92 local function get_disco_items(stanza)
93 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
94 for jid, room in pairs(rooms) do
95 if not room:is_hidden() then
96 reply:tag("item", {jid=jid, name=room:get_name()}):up();
99 return reply; -- TODO cache disco reply
102 local function handle_to_domain(origin, stanza)
103 local type = stanza.attr.type;
104 if type == "error" or type == "result" then return; end
105 if stanza.name == "iq" and type == "get" then
106 local xmlns = stanza.tags[1].attr.xmlns;
107 if xmlns == "http://jabber.org/protocol/disco#info" then
108 origin.send(get_disco_info(stanza));
109 elseif xmlns == "http://jabber.org/protocol/disco#items" then
110 origin.send(get_disco_items(stanza));
111 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
112 origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
114 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
117 host_room:handle_stanza(origin, stanza);
118 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
122 function stanza_handler(event)
123 local origin, stanza = event.origin, event.stanza;
124 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
126 local bare = to_node.."@"..to_host;
127 if to_host == muc_host or bare == muc_host then
128 local room = rooms[bare];
130 if not(restrict_room_creation) or
131 (restrict_room_creation == "admin" and is_admin(stanza.attr.from)) or
132 (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then
133 room = muc_new_room(bare, {
134 history_length = max_history_messages;
136 room.route_stanza = room_route_stanza;
137 room.save = room_save;
142 room:handle_stanza(origin, stanza);
143 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
144 rooms[bare] = nil; -- discard room
147 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
149 else --[[not for us?]] end
152 -- to the main muc domain
153 handle_to_domain(origin, stanza);
156 module:hook("iq/bare", stanza_handler);
157 module:hook("message/bare", stanza_handler);
158 module:hook("presence/bare", stanza_handler);
159 module:hook("iq/full", stanza_handler);
160 module:hook("message/full", stanza_handler);
161 module:hook("presence/full", stanza_handler);
162 module:hook("iq/host", stanza_handler);
163 module:hook("message/host", stanza_handler);
164 module:hook("presence/host", stanza_handler);
166 component = register_component(muc_host, function() end);
167 function component.send(stanza) -- FIXME do a generic fix
168 if stanza.attr.type == "result" or stanza.attr.type == "error" then
169 core_post_stanza(component, stanza);
170 else error("component.send only supports result and error stanzas at the moment"); end
173 prosody.hosts[module:get_host()].muc = { rooms = rooms };
175 module.unload = function()
176 deregister_component(muc_host);
178 module.save = function()
179 return {rooms = rooms};
181 module.restore = function(data)
182 for jid, oldroom in pairs(data.rooms or {}) do
183 local room = muc_new_room(jid);
184 room._jid_nick = oldroom._jid_nick;
185 room._occupants = oldroom._occupants;
186 room._data = oldroom._data;
187 room._affiliations = oldroom._affiliations;
188 room.route_stanza = room_route_stanza;
189 room.save = room_save;
192 prosody.hosts[module:get_host()].muc = { rooms = rooms };