MUC: Added a send() method to the component. Fixes issues with local mod_vcard.
[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 local datamanager = require "util.datamanager";
25
26 local rooms = {};
27 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
28 local component;
29
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;
39                 local data = {
40                         jid = room.jid;
41                         _data = room._data;
42                         _affiliations = room._affiliations;
43                 };
44                 datamanager.store(node, muc_host, "config", data);
45                 room._data.history = history;
46         elseif forced then
47                 module:log("debug", "3");
48                 datamanager.store(node, muc_host, "config", nil);
49         end
50         module:log("debug", "4");
51         if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
52 end
53
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;
62         rooms[jid] = room;
63 end
64
65 local host_room = muc_new_room(muc_host);
66 host_room.route_stanza = room_route_stanza;
67 host_room.save = room_save;
68
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
73 end
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();
79                 end
80         end
81         return reply; -- TODO cache disco reply
82 end
83
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
95                 else
96                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
97                 end
98         else
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"));
101         end
102 end
103
104 component = register_component(muc_host, function(origin, stanza)
105         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
106         if to_node then
107                 local bare = to_node.."@"..to_host;
108                 if to_host == muc_host or bare == muc_host then
109                         local room = rooms[bare];
110                         if not room then
111                                 room = muc_new_room(bare);
112                                 room.route_stanza = room_route_stanza;
113                                 room.save = room_save;
114                                 rooms[bare] = room;
115                         end
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
119                         end
120                 else --[[not for us?]] end
121                 return;
122         end
123         -- to the main muc domain
124         handle_to_domain(origin, stanza);
125 end);
126 function component.send(stanza) -- FIXME do a generic fix
127         if stanza.attr.type == "result" or stanza.attr.type == "error" then
128                 core_post_stanza(component, stanza);
129         else error("component.send only supports result and error stanzas at the moment"); end
130 end
131
132 prosody.hosts[module:get_host()].muc = { rooms = rooms };
133
134 module.unload = function()
135         deregister_component(muc_host);
136 end
137 module.save = function()
138         return {rooms = rooms};
139 end
140 module.restore = function(data)
141         rooms = {};
142         for jid, oldroom in pairs(data.rooms or {}) do
143                 local room = muc_new_room(jid);
144                 room._jid_nick = oldroom._jid_nick;
145                 room._occupants = oldroom._occupants;
146                 room._data = oldroom._data;
147                 room._affiliations = oldroom._affiliations;
148                 room.route_stanza = room_route_stanza;
149                 room.save = room_save;
150                 rooms[jid] = room;
151         end
152         prosody.hosts[module:get_host()].muc = { rooms = rooms };
153 end