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 module:log("debug", "1, %s, %s", room.jid, tostring(room._data.persistent));
35 if room._data.persistent then
36 module:log("debug", "2");
37 local history = room._data.history;
38 room._data.history = nil;
42 _affiliations = room._affiliations;
44 datamanager.store(node, muc_host, "config", data);
45 room._data.history = history;
47 module:log("debug", "3");
48 datamanager.store(node, muc_host, "config", nil);
50 module:log("debug", "4");
51 if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
54 for jid in pairs(persistent_rooms) do
55 local node = jid_split(jid);
56 local data = datamanager.load(node, muc_host, "config") or {};
57 local room = muc_new_room(jid);
58 room._data = data._data;
59 room._affiliations = data._affiliations;
60 room.route_stanza = room_route_stanza;
61 room.save = room_save;
65 local host_room = muc_new_room(muc_host);
66 host_room.route_stanza = room_route_stanza;
67 host_room.save = room_save;
69 local function get_disco_info(stanza)
70 return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
71 :tag("identity", {category='conference', type='text', name=muc_name}):up()
72 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
74 local function get_disco_items(stanza)
75 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
76 for jid, room in pairs(rooms) do
77 if not room._data.hidden then
78 reply:tag("item", {jid=jid, name=jid}):up();
81 return reply; -- TODO cache disco reply
84 local function handle_to_domain(origin, stanza)
85 local type = stanza.attr.type;
86 if type == "error" or type == "result" then return; end
87 if stanza.name == "iq" and type == "get" then
88 local xmlns = stanza.tags[1].attr.xmlns;
89 if xmlns == "http://jabber.org/protocol/disco#info" then
90 origin.send(get_disco_info(stanza));
91 elseif xmlns == "http://jabber.org/protocol/disco#items" then
92 origin.send(get_disco_items(stanza));
93 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
94 origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
96 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
99 host_room:handle_stanza(origin, stanza);
100 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
104 component = register_component(muc_host, function(origin, stanza)
105 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
107 local bare = to_node.."@"..to_host;
108 if to_host == muc_host or bare == muc_host then
109 local room = rooms[bare];
111 room = muc_new_room(bare);
112 room.route_stanza = room_route_stanza;
113 room.save = room_save;
116 room:handle_stanza(origin, stanza);
117 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
118 rooms[bare] = nil; -- discard room
120 else --[[not for us?]] end
123 -- to the main muc domain
124 handle_to_domain(origin, stanza);
127 prosody.hosts[module:get_host()].muc = { rooms = rooms };
129 module.unload = function()
130 deregister_component(muc_host);
132 module.save = function()
133 return {rooms = rooms};
135 module.restore = function(data)
137 for jid, oldroom in pairs(data.rooms or {}) do
138 local room = muc_new_room(jid);
139 room._jid_nick = oldroom._jid_nick;
140 room._occupants = oldroom._occupants;
141 room._data = oldroom._data;
142 room._affiliations = oldroom._affiliations;
143 room.route_stanza = room_route_stanza;
144 room.save = room_save;
147 prosody.hosts[module:get_host()].muc = { rooms = rooms };