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