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 = module:require "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";
23 local uuid_gen = require "util.uuid".generate;
24 local datamanager = require "util.datamanager";
27 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
30 local function room_route_stanza(room, stanza) core_post_stanza(component, stanza); end
31 local function room_save(room, forced)
32 local node = jid_split(room.jid);
33 persistent_rooms[room.jid] = room._data.persistent;
34 if room._data.persistent then
35 local history = room._data.history;
36 room._data.history = nil;
40 _affiliations = room._affiliations;
42 datamanager.store(node, muc_host, "config", data);
43 room._data.history = history;
45 datamanager.store(node, muc_host, "config", nil);
47 if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
50 for jid in pairs(persistent_rooms) do
51 local node = jid_split(jid);
52 local data = datamanager.load(node, muc_host, "config") or {};
53 local room = muc_new_room(jid);
54 room._data = data._data;
55 room._affiliations = data._affiliations;
56 room.route_stanza = room_route_stanza;
57 room.save = room_save;
61 local host_room = muc_new_room(muc_host);
62 host_room.route_stanza = room_route_stanza;
63 host_room.save = room_save;
65 local function get_disco_info(stanza)
66 return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
67 :tag("identity", {category='conference', type='text', name=muc_name}):up()
68 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
70 local function get_disco_items(stanza)
71 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
72 for jid, room in pairs(rooms) do
73 if not room._data.hidden then
74 reply:tag("item", {jid=jid, name=jid}):up();
77 return reply; -- TODO cache disco reply
80 local function handle_to_domain(origin, stanza)
81 local type = stanza.attr.type;
82 if type == "error" or type == "result" then return; end
83 if stanza.name == "iq" and type == "get" then
84 local xmlns = stanza.tags[1].attr.xmlns;
85 if xmlns == "http://jabber.org/protocol/disco#info" then
86 origin.send(get_disco_info(stanza));
87 elseif xmlns == "http://jabber.org/protocol/disco#items" then
88 origin.send(get_disco_items(stanza));
89 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
90 origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
92 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
95 host_room:handle_stanza(origin, stanza);
96 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
100 component = register_component(muc_host, function(origin, stanza)
101 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
103 local bare = to_node.."@"..to_host;
104 if to_host == muc_host or bare == muc_host then
105 local room = rooms[bare];
107 room = muc_new_room(bare);
108 room.route_stanza = room_route_stanza;
109 room.save = room_save;
112 room:handle_stanza(origin, stanza);
113 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
114 rooms[bare] = nil; -- discard room
116 else --[[not for us?]] end
119 -- to the main muc domain
120 handle_to_domain(origin, stanza);
122 function component.send(stanza) -- FIXME do a generic fix
123 if stanza.attr.type == "result" or stanza.attr.type == "error" then
124 core_post_stanza(component, stanza);
125 else error("component.send only supports result and error stanzas at the moment"); end
128 prosody.hosts[module:get_host()].muc = { rooms = rooms };
130 module.unload = function()
131 deregister_component(muc_host);
133 module.save = function()
134 return {rooms = rooms};
136 module.restore = function(data)
138 for jid, oldroom in pairs(data.rooms or {}) do
139 local room = muc_new_room(jid);
140 room._jid_nick = oldroom._jid_nick;
141 room._occupants = oldroom._occupants;
142 room._data = oldroom._data;
143 room._affiliations = oldroom._affiliations;
144 room.route_stanza = room_route_stanza;
145 room.save = room_save;
148 prosody.hosts[module:get_host()].muc = { rooms = rooms };