4a8263cecf8e81b4c5ccce176581393a96c22525
[prosody.git] / plugins / muc / mod_muc.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
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);
12 end
13
14 local muc_host = module:get_host();
15 local muc_name = "Chatrooms";
16 local history_length = 20;
17
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
25 local rooms = {};
26 local component;
27 local host_room = muc_new_room(muc_host);
28 host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
29
30 local function get_disco_info(stanza)
31         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
32                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
33                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
34 end
35 local function get_disco_items(stanza)
36         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
37         for jid, room in pairs(rooms) do
38                 reply:tag("item", {jid=jid, name=jid}):up();
39         end
40         return reply; -- TODO cache disco reply
41 end
42
43 local function handle_to_domain(origin, stanza)
44         local type = stanza.attr.type;
45         if type == "error" or type == "result" then return; end
46         if stanza.name == "iq" and type == "get" then
47                 local xmlns = stanza.tags[1].attr.xmlns;
48                 if xmlns == "http://jabber.org/protocol/disco#info" then
49                         origin.send(get_disco_info(stanza));
50                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
51                         origin.send(get_disco_items(stanza));
52                 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
53                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
54                 else
55                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
56                 end
57         else
58                 host_room:handle_stanza(origin, stanza);
59                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
60         end
61 end
62
63 component = register_component(muc_host, function(origin, stanza)
64         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
65         if to_node then
66                 local bare = to_node.."@"..to_host;
67                 if to_host == muc_host or bare == muc_host then
68                         local room = rooms[bare];
69                         if not room then
70                                 room = muc_new_room(bare);
71                                 room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
72                                 rooms[bare] = room;
73                         end
74                         room:handle_stanza(origin, stanza);
75                 else --[[not for us?]] end
76                 return;
77         end
78         -- to the main muc domain
79         handle_to_domain(origin, stanza);
80 end);
81
82 prosody.hosts[module:get_host()].muc = { rooms = rooms };
83
84 module.unload = function()
85         deregister_component(muc_host);
86 end
87 module.save = function()
88         return {rooms = rooms};
89 end
90 module.restore = function(data)
91         rooms = data.rooms or {};
92         prosody.hosts[module:get_host()].muc = { rooms = rooms };
93 end