2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 = "Chatrooms";
16 local history_length = 20;
18 local muc_new_room = require "util.muc".new_room;
19 local register_component = require "core.componentmanager".register_component;
20 local deregister_component = require "core.componentmanager".deregister_component;
21 local jid_split = require "util.jid".split;
22 local st = require "util.stanza";
26 local host_room = muc_new_room(muc_host);
27 host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
29 local function get_disco_info(stanza)
30 return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
31 :tag("identity", {category='conference', type='text', name=muc_name}):up()
32 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
34 local function get_disco_items(stanza)
35 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
36 for jid, room in pairs(rooms) do
37 reply:tag("item", {jid=jid, name=jid}):up();
39 return reply; -- TODO cache disco reply
42 local function handle_to_domain(origin, stanza)
43 local type = stanza.attr.type;
44 if type == "error" or type == "result" then return; end
45 if stanza.name == "iq" and type == "get" then
46 local xmlns = stanza.tags[1].attr.xmlns;
47 if xmlns == "http://jabber.org/protocol/disco#info" then
48 origin.send(get_disco_info(stanza));
49 elseif xmlns == "http://jabber.org/protocol/disco#items" then
50 origin.send(get_disco_items(stanza));
52 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
55 host_room:handle_stanza(origin, stanza);
56 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
60 component = register_component(muc_host, function(origin, stanza)
61 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
63 local bare = to_node.."@"..to_host;
64 if to_host == muc_host or bare == muc_host then
65 local room = rooms[bare];
67 room = muc_new_room(bare);
68 room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
71 room:handle_stanza(origin, stanza);
72 else --[[not for us?]] end
75 -- to the main muc domain
76 handle_to_domain(origin, stanza);
79 prosody.hosts[module:get_host()].muc = { rooms = rooms };
81 module.unload = function()
82 deregister_component(muc_host);
84 module.save = function()
85 return {rooms = rooms};
87 module.restore = function(data)
88 rooms = data.rooms or {};
89 prosody.hosts[module:get_host()].muc = { rooms = rooms };