Comparison

plugins/muc/mod_muc.lua @ 1738:ee4a7151ed07

MUC: New basic mod_muc based on the new MUC library
author Waqas Hussain <waqas20@gmail.com>
date Mon, 07 Sep 2009 20:18:26 +0500
child 1741:2919f3b985fc
comparison
equal deleted inserted replaced
1737:31c3eb5797c7 1738:ee4a7151ed07
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
24 local rooms = {};
25 local component;
26 local host_room = muc_new_room(muc_host);
27 host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
28
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
33 end
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();
38 end
39 return reply; -- TODO cache disco reply
40 end
41
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));
51 else
52 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
53 end
54 else
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"));
57 end
58 end
59
60 component = register_component(muc_host, function(origin, stanza)
61 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
62 if to_node then
63 local bare = to_node.."@"..to_host;
64 if to_host == muc_host or bare == muc_host then
65 local room = rooms[bare];
66 if not room then
67 room = muc_new_room(bare);
68 room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end;
69 rooms[bare] = room;
70 end
71 room:handle_stanza(origin, stanza);
72 else --[[not for us?]] end
73 return;
74 end
75 -- to the main muc domain
76 handle_to_domain(origin, stanza);
77 end);
78
79 prosody.hosts[module:get_host()].muc = { rooms = rooms };
80
81 module.unload = function()
82 deregister_component(muc_host);
83 end
84 module.save = function()
85 return {rooms = rooms};
86 end
87 module.restore = function(data)
88 rooms = data.rooms or {};
89 prosody.hosts[module:get_host()].muc = { rooms = rooms };
90 end