mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_muc.lua
1
2 if module:get_host_type() ~= "component" then
3         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
4 end
5
6 local muc_host = module:get_host();
7 local muc_name = "Chatrooms";
8 local history_length = 20;
9
10 local muc_new_room = require "util.muc".new_room;
11 local register_component = require "core.componentmanager".register_component;
12 local deregister_component = require "core.componentmanager".deregister_component;
13 local jid_split = require "util.jid".split;
14 local st = require "util.stanza";
15
16 local rooms = {};
17 local component;
18 local host_room = muc_new_room(muc_host);
19 host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
20
21 local function get_disco_info(stanza)
22         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
23                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
24                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
25 end
26 local function get_disco_items(stanza)
27         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
28         for jid, room in pairs(rooms) do
29                 reply:tag("item", {jid=jid, name=jid}):up();
30         end
31         return reply; -- TODO cache disco reply
32 end
33
34 local function handle_to_domain(origin, stanza)
35         local type = stanza.attr.type;
36         if type == "error" or type == "result" then return; end
37         if stanza.name == "iq" and type == "get" then
38                 local xmlns = stanza.tags[1].attr.xmlns;
39                 if xmlns == "http://jabber.org/protocol/disco#info" then
40                         origin.send(get_disco_info(stanza));
41                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
42                         origin.send(get_disco_items(stanza));
43                 else
44                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
45                 end
46         else
47                 host_room:handle_stanza(origin, stanza);
48                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
49         end
50 end
51
52 component = register_component(muc_host, function(origin, stanza)
53         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
54         if to_node then
55                 local bare = to_node.."@"..to_host;
56                 if to_host == muc_host or bare == muc_host then
57                         local room = rooms[bare];
58                         if not room then
59                                 room = muc_new_room(bare);
60                                 room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
61                                 rooms[bare] = room;
62                         end
63                         room:handle_stanza(origin, stanza);
64                 else --[[not for us?]] end
65                 return;
66         end
67         -- to the main muc domain
68         handle_to_domain(origin, stanza);
69 end);
70
71 module.unload = function()
72         deregister_component(muc_host);
73 end
74 module.save = function()
75         return {rooms = rooms};
76 end
77 module.restore = function(data)
78         rooms = data.rooms or {};
79 end