3eab0cf5598aed65bd8c6a52f86be972d945b38f
[prosody.git] / plugins / muc / mod_muc.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 = module:get_option("name");
16 if type(muc_name) ~= "string" then muc_name = "Prosody Chatrooms"; end
17 local restrict_room_creation = module:get_option("restrict_room_creation");
18 if restrict_room_creation and restrict_room_creation ~= true then restrict_room_creation = nil; end
19
20 local muc_new_room = module:require "muc".new_room;
21 local register_component = require "core.componentmanager".register_component;
22 local deregister_component = require "core.componentmanager".deregister_component;
23 local jid_split = require "util.jid".split;
24 local jid_bare = require "util.jid".bare;
25 local st = require "util.stanza";
26 local uuid_gen = require "util.uuid".generate;
27 local datamanager = require "util.datamanager";
28 local um_is_admin = require "core.usermanager".is_admin;
29
30 local rooms = {};
31 local persistent_rooms = datamanager.load(nil, muc_host, "persistent") or {};
32 local component;
33
34 -- Configurable options
35 local max_history_messages = module:get_option_number("max_history_messages");
36
37 local function is_admin(jid)
38         return um_is_admin(jid, module.host);
39 end
40
41 local function room_route_stanza(room, stanza) core_post_stanza(component, stanza); end
42 local function room_save(room, forced)
43         local node = jid_split(room.jid);
44         persistent_rooms[room.jid] = room._data.persistent;
45         if room._data.persistent then
46                 local history = room._data.history;
47                 room._data.history = nil;
48                 local data = {
49                         jid = room.jid;
50                         _data = room._data;
51                         _affiliations = room._affiliations;
52                 };
53                 datamanager.store(node, muc_host, "config", data);
54                 room._data.history = history;
55         elseif forced then
56                 datamanager.store(node, muc_host, "config", nil);
57         end
58         if forced then datamanager.store(nil, muc_host, "persistent", persistent_rooms); end
59 end
60
61 for jid in pairs(persistent_rooms) do
62         local node = jid_split(jid);
63         local data = datamanager.load(node, muc_host, "config") or {};
64         local room = muc_new_room(jid, {
65                 history_length = max_history_messages;
66         });
67         room._data = data._data;
68         room._data.history_length = max_history_messages; --TODO: Need to allow per-room with a global limit
69         room._affiliations = data._affiliations;
70         room.route_stanza = room_route_stanza;
71         room.save = room_save;
72         rooms[jid] = room;
73 end
74
75 local host_room = muc_new_room(muc_host, {
76         history_length = max_history_messages;
77 });
78 host_room.route_stanza = room_route_stanza;
79 host_room.save = room_save;
80
81 local function get_disco_info(stanza)
82         return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
83                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
84                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
85 end
86 local function get_disco_items(stanza)
87         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
88         for jid, room in pairs(rooms) do
89                 if not room:is_hidden() then
90                         reply:tag("item", {jid=jid, name=room:get_name()}):up();
91                 end
92         end
93         return reply; -- TODO cache disco reply
94 end
95
96 local function handle_to_domain(origin, stanza)
97         local type = stanza.attr.type;
98         if type == "error" or type == "result" then return; end
99         if stanza.name == "iq" and type == "get" then
100                 local xmlns = stanza.tags[1].attr.xmlns;
101                 if xmlns == "http://jabber.org/protocol/disco#info" then
102                         origin.send(get_disco_info(stanza));
103                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
104                         origin.send(get_disco_items(stanza));
105                 elseif xmlns == "http://jabber.org/protocol/muc#unique" then
106                         origin.send(st.reply(stanza):tag("unique", {xmlns = xmlns}):text(uuid_gen())); -- FIXME Random UUIDs can theoretically have collisions
107                 else
108                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
109                 end
110         else
111                 host_room:handle_stanza(origin, stanza);
112                 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
113         end
114 end
115
116 component = register_component(muc_host, function(origin, stanza)
117         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
118         if to_node then
119                 local bare = to_node.."@"..to_host;
120                 if to_host == muc_host or bare == muc_host then
121                         local room = rooms[bare];
122                         if not room then
123                                 if not(restrict_room_creation) or is_admin(stanza.attr.from) then
124                                         room = muc_new_room(bare, {
125                                                 history_length = max_history_messages;
126                                         });
127                                         room.route_stanza = room_route_stanza;
128                                         room.save = room_save;
129                                         rooms[bare] = room;
130                                 end
131                         end
132                         if room then
133                                 room:handle_stanza(origin, stanza);
134                                 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room
135                                         rooms[bare] = nil; -- discard room
136                                 end
137                         else
138                                 origin.send(st.error_reply(stanza, "cancel", "not-allowed"));
139                         end
140                 else --[[not for us?]] end
141                 return;
142         end
143         -- to the main muc domain
144         handle_to_domain(origin, stanza);
145 end);
146 function component.send(stanza) -- FIXME do a generic fix
147         if stanza.attr.type == "result" or stanza.attr.type == "error" then
148                 core_post_stanza(component, stanza);
149         else error("component.send only supports result and error stanzas at the moment"); end
150 end
151
152 prosody.hosts[module:get_host()].muc = { rooms = rooms };
153
154 module.unload = function()
155         deregister_component(muc_host);
156 end
157 module.save = function()
158         return {rooms = rooms};
159 end
160 module.restore = function(data)
161         rooms = {};
162         for jid, oldroom in pairs(data.rooms or {}) do
163                 local room = muc_new_room(jid);
164                 room._jid_nick = oldroom._jid_nick;
165                 room._occupants = oldroom._occupants;
166                 room._data = oldroom._data;
167                 room._affiliations = oldroom._affiliations;
168                 room.route_stanza = room_route_stanza;
169                 room.save = room_save;
170                 rooms[jid] = room;
171         end
172         prosody.hosts[module:get_host()].muc = { rooms = rooms };
173 end